Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Write vassals no webserver reload #2

Merged
merged 4 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <number> /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)

```
<VirtualHost *:80>

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/

</VirtualHost>
```
11 changes: 11 additions & 0 deletions app.skel
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions config.ini.example
Original file line number Diff line number Diff line change
@@ -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 = ./sites/*/*.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

42 changes: 18 additions & 24 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datetime
from base64 import b64encode, urlsafe_b64encode
import random
from pathlib import Path

app = Flask(__name__)
# Load .env settings
Expand All @@ -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
Expand Down Expand Up @@ -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 = <website-hostname>" 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_token])

return login_url
Expand Down