From 5e2656ee5039ffa195834f2a4b5dd4b7d16cf768 Mon Sep 17 00:00:00 2001 From: chrisjsimpson Date: Sat, 15 Feb 2020 19:07:22 +0000 Subject: [PATCH 1/4] write uwsgi vassals requiring no webserver restart/reload --- app.skel | 11 +++++++++++ main.py | 42 ++++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 app.skel diff --git a/app.skel b/app.skel new file mode 100644 index 0000000..9329e8d --- /dev/null +++ b/app.skel @@ -0,0 +1,11 @@ +[uwsgi] +strict = true +# %d absolute path of the directory containing the configuration file +chdir = %d/subscribie +virtualenv = %d/subscribie/venv +wsgi-file = %d/subscribie/subscribie.wsgi +master = true +vacuum = true +# %n the filename without extension +socket = /tmp/sockets/%n.sock +#subscribe-to = /tmp/sock2:three.example.com diff --git a/main.py b/main.py index 33b2bca..d5f37b5 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ import datetime from base64 import b64encode, urlsafe_b64encode import random +from pathlib import Path app = Flask(__name__) # Load .env settings @@ -33,6 +34,9 @@ def deploy(): # Create directory for site try: dstDir = app.config['SITES_DIRECTORY'] + webaddress + '/' + if Path(dstDir).exists(): + print("Site {} already exists. Exiting...".format(webaddress)) + exit() os.mkdir(dstDir) file.save(os.path.join(dstDir, filename + '.yaml')) # Rename to jamla.yaml @@ -127,32 +131,22 @@ def deploy(): for icon in request.files.getlist('icons'): iconFilename = secure_filename(icon.filename) icon.save(os.path.join(static_folder, iconFilename)) - # Append new site to apache config - vhost = " ".join(["Use VHost", webaddress, app.config['APACHE_USER'], dstDir]) - ssl = " ".join(["Use SSL", webaddress, '443', app.config['APACHE_USER'], dstDir]) - #Verify Vhost isn't already present - try: - fp = open(app.config['APACHE_CONF_FILE'], "a+") - for line in fp: - if webaddress in line: - fp.close() - raise - fp = open(app.config['APACHE_CONF_FILE'], "a+") - fp.write(vhost + "\n") - fp.write(ssl + "\n") - fp.close() - except: - print ("Skipping as " + webaddress + "already exists.") - pass + # Begin uwsgi vassal config creation + # Open application skeleton (app.skel) file and append + # "subscribe-to = " config entry for the new + # sites webaddress so that uwsgi's fastrouter can route the hostname. + curDir = os.path.dirname(os.path.realpath(__file__)) + with open(curDir + '/' + 'app.skel') as f: + contents = f.read() + # Append uwsgi's subscribe-to line with hostname of new site: + contents += "\nsubscribe-to = /tmp/sock2:" + webaddress + "\n" + # Writeout app.ini config to file. uwsgi watches for .ini files + # uwsgi will automatically detect this .ini file and start + # routing requests to the site + with open(dstDir + '/' + 'app.ini', 'w') as f: + f.write(contents) - try: - # Reload apache with new vhost - subprocess.call("sudo /etc/init.d/apache2 graceful", shell=True) - except Exception as e: - print ("Problem reloading apache:") - print (e) - pass login_url = ''.join(['https://', webaddress, '/login/', login_token]) return login_url From 84447ff278e351c7dfcfca892c8b90a79eddc0ed Mon Sep 17 00:00:00 2001 From: chrisjsimpson Date: Sat, 15 Feb 2020 19:30:59 +0000 Subject: [PATCH 2/4] added config.ini.example --- config.ini.example | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 config.ini.example diff --git a/config.ini.example b/config.ini.example new file mode 100644 index 0000000..8377093 --- /dev/null +++ b/config.ini.example @@ -0,0 +1,15 @@ +[uwsgi] +# Example uwsgi emperor config +# run with: uwsgi --ini config.ini +strict = true +protocol = uwsgi +master = true +pidfile = uwsgi.pid +emperor = ./vassals/*/*.ini +#fastrouter = /tmp/sock1 +fastrouter = 127.0.0.1:8001 +chown-socket = www-data:www-data +chmod = 777 +fastrouter-subscription-server = /tmp/sock2 +vacuum = true + From e50d0d5323c8af30f8ebfd35440cf7517e189b93 Mon Sep 17 00:00:00 2001 From: chrisjsimpson Date: Sat, 15 Feb 2020 19:38:54 +0000 Subject: [PATCH 3/4] added readme --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..58724e2 --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +# Builder module + +The builder module is responsible for building new subscribie sites. + +- When a new site is created (via /start-building) , all the + data to build that site (in yaml) is sent to the builder module which builds + a new subscribie site +- Each site is defined in a yaml file, and a clone of the Subscribie repo +- Each site runs as a uwsgi 'vassal' which allows new sites to come online + without having to restart the web server + + +### UWSGI notes +How to run: + +uwsgi --ini config.ini # add -d to demonize + +Ensure that dir /tmp/sockets/ exists (for the vassal sites .ini + files) + +Then chmod /tmp/sock1 (todo fix this using chmod uwsgi flag) + + +## Example Nginx Config + +``` +# mysite_nginx.conf +# + +# configuration of the server +server { + # the port your site will be served on + listen 80; + # the domain name it will serve for + server_name *.app1 example.com ~^.*.example.com app2 site1.local site2.local; # substitute your machine's IP address or FQDN + root /home/chris/Documents/python/uwsgi/vassals/; + charset utf-8; + + client_max_body_size 75M; + + # max upload size + + location / { + #include /etc/nginx/uwsgi_params; + uwsgi_pass unix:///tmp/sock1; + } +} +``` +## Apache config example + +(Using ip rather than sockets) + +``` + + + ServerAdmin webmaster@localhost + DocumentRoot /var/www/html + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + + ServerName example.com + ServerAlias *.example.com + ProxyPass / uwsgi://127.0.0.1:8001/ + + +``` From 90b6561eff8b547d8993442ac01ba48fa5b73a11 Mon Sep 17 00:00:00 2001 From: chrisjsimpson Date: Sat, 15 Feb 2020 20:20:05 +0000 Subject: [PATCH 4/4] default uwsgi emperor to ./sites/*/*.ini directory --- config.ini.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.ini.example b/config.ini.example index 8377093..1773abc 100644 --- a/config.ini.example +++ b/config.ini.example @@ -5,7 +5,7 @@ strict = true protocol = uwsgi master = true pidfile = uwsgi.pid -emperor = ./vassals/*/*.ini +emperor = ./sites/*/*.ini #fastrouter = /tmp/sock1 fastrouter = 127.0.0.1:8001 chown-socket = www-data:www-data