Coverage for gws-app/gws/server/manager.py: 59%

123 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 22:59 +0200

1"""Configuration manager for embedded servers. 

2 

3This object creates configuration files for embedded servers and the server startup script. 

4 

5The configuration is template-based, there are following template subjects defined: 

6 

7- ``server.rsyslog_config`` - for the embedded ``rsyslogd`` daemon 

8- ``server.uwsgi_config`` - for backend uWSGI servers (the ``uwsgi`` argument contains the specific backend name) 

9- ``server.nginx_config`` - for the frontend NGINX proxy 

10 

11Each template receives a :obj:`TemplateArgs` object as arguments. 

12 

13By default, the Manager uses text-only templates from the ``templates`` directory. 

14""" 

15 

16from typing import cast 

17 

18import os 

19import re 

20 

21import gws 

22import gws.base.web 

23import gws.config 

24import gws.config.util 

25import gws.gis.mpx.config 

26import gws.lib.osx 

27 

28from . import core 

29 

30 

31class TemplateArgs(gws.TemplateArgs): 

32 """Arguments for configuration templates.""" 

33 

34 root: gws.Root 

35 """Root object.""" 

36 inContainer: bool 

37 """True if we're running in a container.""" 

38 userName: str 

39 """User name.""" 

40 groupName: str 

41 """Group name.""" 

42 gwsEnv: dict 

43 """A dict of GWS environment variables.""" 

44 mapproxyPid: str 

45 """Mapproxy pid path.""" 

46 mapproxySocket: str 

47 """Mapproxy socket path.""" 

48 nginxPid: str 

49 """nginx pid path.""" 

50 serverDir: str 

51 """Absolute path to app/server directory.""" 

52 spoolPid: str 

53 """Spooler pid path.""" 

54 spoolSocket: str 

55 """Spooler socket path.""" 

56 uwsgi: str 

57 """uWSGI backend name.""" 

58 webPid: str 

59 """Web server pid path.""" 

60 webSocket: str 

61 """Web server socket path.""" 

62 

63 

64_DEFAULT_BASE_TIMEOUT = 60 

65_DEFAULT_SPOOL_TIMEOUT = 300 

66 

67_SERVER_DIR = gws.u.dirname(__file__) 

68 

69_DEFAULT_TEMPLATES = [ 

70 gws.Config(type='text', subject='server.rsyslog_config', path=f'{_SERVER_DIR}/templates/rsyslog_config.cx.txt'), 

71 gws.Config(type='text', subject='server.nginx_config', path=f'{_SERVER_DIR}/templates/nginx_config.cx.txt'), 

72 gws.Config(type='text', subject='server.uwsgi_config', path=f'{_SERVER_DIR}/templates/uwsgi_config.cx.txt'), 

73] 

74 

75 

76class Object(gws.ServerManager): 

77 def configure(self): 

78 self.config = self._add_defaults(self.config, 'gws.server.core.Config') 

79 self.config.log = self._add_defaults(self.config.log, 'gws.server.core.LogConfig') 

80 self.config.mapproxy = self._add_defaults(self.config.mapproxy, 'gws.server.core.MapproxyConfig') 

81 self.config.monitor = self._add_defaults(self.config.monitor, 'gws.server.core.MonitorConfig') 

82 self.config.qgis = self._add_defaults(self.config.qgis, 'gws.server.core.QgisConfig') 

83 self.config.spool = self._add_defaults(self.config.spool, 'gws.server.core.SpoolConfig') 

84 self.config.web = self._add_defaults(self.config.web, 'gws.server.core.WebConfig') 

85 

86 # deprecated 'enabled' keys 

87 if self.config.mapproxy.enabled is False: 

88 self.config.withMapproxy = False 

89 if self.config.spool.enabled is False: 

90 self.config.withSpool = False 

91 if self.config.web.enabled is False: 

92 self.config.withWeb = False 

93 if self.config.monitor.enabled is False: 

94 self.config.withMonitor = False 

95 

96 self.configure_environment() 

97 self.configure_templates() 

98 

99 def _add_defaults(self, value, type_name): 

100 return gws.u.merge( 

101 self.root.specs.read({}, type_name), 

102 value 

103 ) 

104 

105 def configure_environment(self): 

106 """Overwrite config values from the environment.""" 

107 

108 cfg = cast(core.Config, self.config) 

109 p = gws.env.GWS_LOG_LEVEL 

110 if p: 

111 cfg.log.level = p 

112 p = gws.env.GWS_WEB_WORKERS 

113 if p: 

114 cfg.web.workers = int(p) 

115 p = gws.env.GWS_SPOOL_WORKERS 

116 if p: 

117 cfg.spool.workers = int(p) 

118 

119 def configure_templates(self): 

120 gws.config.util.configure_templates_for(self, extra=_DEFAULT_TEMPLATES) 

121 

122 def create_server_configs(self, target_dir, script_path, pid_paths): 

123 ui = gws.lib.osx.user_info(gws.c.UID, gws.c.GID) 

124 args = TemplateArgs( 

125 root=self.root, 

126 inContainer=gws.c.env.GWS_IN_CONTAINER, 

127 userName=ui['pw_name'], 

128 groupName=ui['gr_name'], 

129 gwsEnv={k: v for k, v in sorted(os.environ.items()) if k.startswith('GWS_')}, 

130 mapproxyPid=pid_paths['mapproxy'], 

131 mapproxySocket=f'{gws.c.TMP_DIR}/mapproxy.uwsgi.sock', 

132 nginxPid=pid_paths['nginx'], 

133 serverDir=_SERVER_DIR, 

134 spoolPid=pid_paths['spool'], 

135 spoolSocket=f'{gws.c.TMP_DIR}/spool.uwsgi.sock', 

136 webPid=pid_paths['web'], 

137 webSocket=f'{gws.c.TMP_DIR}/web.uwsgi.sock', 

138 ) 

139 

140 commands = [] 

141 

142 commands.append(f"export HOME={ui['pw_dir']}") 

143 

144 if args.inContainer: 

145 path = self._create_config('server.rsyslog_config', f'{target_dir}/syslog.conf', args) 

146 commands.append(f'rsyslogd -i {gws.c.PIDS_DIR}/rsyslogd.pid -f {path}') 

147 

148 if self.cfg('withWeb'): 

149 args.uwsgi = 'web' 

150 path = self._create_config('server.uwsgi_config', f'{target_dir}/uwsgi_web.ini', args) 

151 commands.append(f'uwsgi --ini {path}') 

152 

153 if self.cfg('withMapproxy') and gws.u.is_file(gws.gis.mpx.config.CONFIG_PATH): 

154 args.uwsgi = 'mapproxy' 

155 path = self._create_config('server.uwsgi_config', f'{target_dir}/uwsgi_mapproxy.ini', args) 

156 commands.append(f'uwsgi --ini {path}') 

157 

158 if self.cfg('withSpool'): 

159 args.uwsgi = 'spool' 

160 path = self._create_config('server.uwsgi_config', f'{target_dir}/uwsgi_spool.ini', args) 

161 commands.append(f'uwsgi --ini {path}') 

162 

163 path = self._create_config('server.nginx_config', f'{target_dir}/nginx.conf', args, True) 

164 commands.append(f'exec nginx -c {path}') 

165 

166 gws.u.write_file(script_path, '\n'.join(commands) + '\n') 

167 

168 def _create_config(self, subject: str, path: str, args: TemplateArgs, is_nginx=False) -> str: 

169 tpl = self.root.app.templateMgr.find_template(subject, where=[self]) 

170 res = tpl.render(gws.TemplateRenderInput(args=args)) 

171 

172 text = str(res.content) 

173 if is_nginx: 

174 text = re.sub(r'\s*{\s*', ' {\n', text) 

175 text = re.sub(r'\s*}\s*', '\n}\n', text) 

176 

177 lines = [] 

178 indent = 0 

179 

180 for line in text.split('\n'): 

181 line = re.sub(r'\s+', ' ', line.strip()) 

182 if not line: 

183 continue 

184 if is_nginx: 

185 if line == '}': 

186 indent -= 1 

187 lines.append((' ' * (indent * 4)) + line) 

188 if '{' in line: 

189 indent += 1 

190 else: 

191 lines.append(line) 

192 

193 text = '\n'.join(lines) + '\n' 

194 gws.u.write_file(path, text) 

195 return path