aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Fincham <michael@hotplate.co.nz>2019-02-19 21:44:52 +1300
committerGitHub <noreply@github.com>2019-02-19 21:44:52 +1300
commita1996a01e853d6d42f258d3226d492f3e5b1ec87 (patch)
tree7f42f32b765392af947ea6043ad283c5e71300b9
parentbe9536229a30cc02fc928d476742727c060cf076 (diff)
downloadwads-master.tar.gz
wads-master.tar.bz2
wads-master.zip
Create buildHEADmaster
-rw-r--r--build103
1 files changed, 103 insertions, 0 deletions
diff --git a/build b/build
new file mode 100644
index 0000000..dbc4568
--- /dev/null
+++ b/build
@@ -0,0 +1,103 @@
+#!/usr/bin/python3
+
+from pathlib import Path
+import configparser
+import subprocess
+
+from jinja2 import Template
+
+supervisor_template = Template("""# This file is managed by WADS. Generated on {{ generated }}
+[program:{{ app }}]
+command=/srv/wads/{{ app }}/{{ command }}
+directory=/srv/wads/{{ app }}
+user=wads-{{ app }}
+group=wads-{{ app }}
+autostart=true
+autorestart=true
+redirect_stderr=true
+environment=WADS_PORT={{ port }}{% if environment %},{{ environment }}{% endif %}
+""")
+
+apache_template = Template("""# This file is managed by WADS. Generated on {{ generated }}
+<IfModule mod_ssl.c>
+<VirtualHost *:443>
+ServerName {{ hostnames[0] }}
+{% for alias in hostnames[1:] %}
+ServerAlias {{ alias }}
+{% endfor %}
+
+ServerAdmin {{ admin }}
+
+DocumentRoot /var/empty
+
+ErrorLog /var/log/wads/{{ hostnames[0] }}_error.log
+CustomLog /var/log/{{ hostnames[0] }}_access.log combined
+
+ProxyPreserveHost On
+
+{% for proxy in proxies %}
+ProxyPassMatch ^/{{ proxy.uri }}/(.*)$ http://127.0.0.1:{{ proxy.port }}/$1
+ProxyPassReverse "/{{ proxy.uri }}/" http://127.0.0.1:{{ proxy.port }}/
+{% endfor %}
+
+Include /etc/letsencrypt/options-ssl-apache.conf
+
+SSLCertificateFile {{ cert }}
+SSLCertificateKeyFile {{ key }}
+</VirtualHost>
+</IfModule>
+<VirtualHost *:80>
+ServerName {{ hostnames[0] }}
+{% for alias in hostnames[1:] %}
+ServerAlias {{ alias }}
+{% endfor %}
+
+ServerAdmin {{ admin }}
+
+DocumentRoot /var/empty
+
+ErrorLog /var/log/wads/{{ hostnames[0] }}_error.log
+CustomLog /var/log/{{ hostnames[0] }}_access.log combined
+
+RewriteEngine on
+RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
+</VirtualHost>
+""")
+
+if __name__ == "__main__":
+ wads_config = configparser.ConfigParser()
+ wads_config['ports'] = {}
+ wads_config.read('/opt/hotplate/wads/state.ini')
+
+ sites = {}
+
+ for app_config_path in Path('/srv/wads').glob('*/wads.ini'):
+ app = configparser.ConfigParser()
+ app.read((str(app_config_path), ))
+ app_name = app_config_path.parts[-2]
+
+ # find or register a port
+ if app_name not in wads_config['ports']:
+ port = 4000
+ while port < 4999:
+ if port not in wads_config['ports'].values():
+ break
+ wads_config['ports'][app_name] = str(port)
+ else:
+ port = wads_config['ports'][app_name]
+
+ # create user
+ subprocess.run("useradd -r wads-%s -d /opt/hotplate/wads/homes/%s 2>/dev/null" % (app_name, app_name), shell=True)
+
+ # create supervisor job
+ supervisor_config = supervisor_template.render(app=app_name, command=app['app']['command'], port=port, environment=app['app'].get('environment', None))
+ with open('/etc/supervisor/conf.d/wads-%s' % app_name, 'w') as configfile:
+ configfile.write(supervisor_config + "\n")
+
+ # create apache config
+ apache_config = supervisor_template.render(app=app_name, command=app['app']['command'], port=port, environment=app['app'].get('environment', None))
+ with open('/etc/supervisor/conf.d/wads-%s' % app_name, 'w') as configfile:
+ configfile.write(supervisor_config + "\n")
+
+ with open('/opt/hotplate/wads/state.ini', 'w') as configfile:
+ wads_config.write(configfile)