Skip to content

Commit 438a510

Browse files
Merge pull request #2 from Subscribie/write-vassals-no-webserver-reload
Write vassals no webserver reload
2 parents 2af198d + 90b6561 commit 438a510

File tree

4 files changed

+111
-24
lines changed

4 files changed

+111
-24
lines changed

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Builder module
2+
3+
The builder module is responsible for building new subscribie sites.
4+
5+
- When a new site is created (via /start-building) , all the
6+
data to build that site (in yaml) is sent to the builder module which builds
7+
a new subscribie site
8+
- Each site is defined in a yaml file, and a clone of the Subscribie repo
9+
- Each site runs as a uwsgi 'vassal' which allows new sites to come online
10+
without having to restart the web server
11+
12+
13+
### UWSGI notes
14+
How to run:
15+
16+
uwsgi --ini config.ini # add -d to demonize
17+
18+
Ensure that dir /tmp/sockets/ exists (for the vassal sites .ini
19+
files)
20+
21+
Then chmod <number> /tmp/sock1 (todo fix this using chmod uwsgi flag)
22+
23+
24+
## Example Nginx Config
25+
26+
```
27+
# mysite_nginx.conf
28+
#
29+
30+
# configuration of the server
31+
server {
32+
# the port your site will be served on
33+
listen 80;
34+
# the domain name it will serve for
35+
server_name *.app1 example.com ~^.*.example.com app2 site1.local site2.local; # substitute your machine's IP address or FQDN
36+
root /home/chris/Documents/python/uwsgi/vassals/;
37+
charset utf-8;
38+
39+
client_max_body_size 75M;
40+
41+
# max upload size
42+
43+
location / {
44+
#include /etc/nginx/uwsgi_params;
45+
uwsgi_pass unix:///tmp/sock1;
46+
}
47+
}
48+
```
49+
## Apache config example
50+
51+
(Using ip rather than sockets)
52+
53+
```
54+
<VirtualHost *:80>
55+
56+
ServerAdmin webmaster@localhost
57+
DocumentRoot /var/www/html
58+
59+
ErrorLog ${APACHE_LOG_DIR}/error.log
60+
CustomLog ${APACHE_LOG_DIR}/access.log combined
61+
62+
ServerName example.com
63+
ServerAlias *.example.com
64+
ProxyPass / uwsgi://127.0.0.1:8001/
65+
66+
</VirtualHost>
67+
```

app.skel

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[uwsgi]
2+
strict = true
3+
# %d absolute path of the directory containing the configuration file
4+
chdir = %d/subscribie
5+
virtualenv = %d/subscribie/venv
6+
wsgi-file = %d/subscribie/subscribie.wsgi
7+
master = true
8+
vacuum = true
9+
# %n the filename without extension
10+
socket = /tmp/sockets/%n.sock
11+
#subscribe-to = /tmp/sock2:three.example.com

config.ini.example

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[uwsgi]
2+
# Example uwsgi emperor config
3+
# run with: uwsgi --ini config.ini
4+
strict = true
5+
protocol = uwsgi
6+
master = true
7+
pidfile = uwsgi.pid
8+
emperor = ./sites/*/*.ini
9+
#fastrouter = /tmp/sock1
10+
fastrouter = 127.0.0.1:8001
11+
chown-socket = www-data:www-data
12+
chmod = 777
13+
fastrouter-subscription-server = /tmp/sock2
14+
vacuum = true
15+

main.py

+18-24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import datetime
1010
from base64 import b64encode, urlsafe_b64encode
1111
import random
12+
from pathlib import Path
1213

1314
app = Flask(__name__)
1415
# Load .env settings
@@ -33,6 +34,9 @@ def deploy():
3334
# Create directory for site
3435
try:
3536
dstDir = app.config['SITES_DIRECTORY'] + webaddress + '/'
37+
if Path(dstDir).exists():
38+
print("Site {} already exists. Exiting...".format(webaddress))
39+
exit()
3640
os.mkdir(dstDir)
3741
file.save(os.path.join(dstDir, filename + '.yaml'))
3842
# Rename to jamla.yaml
@@ -127,32 +131,22 @@ def deploy():
127131
for icon in request.files.getlist('icons'):
128132
iconFilename = secure_filename(icon.filename)
129133
icon.save(os.path.join(static_folder, iconFilename))
130-
# Append new site to apache config
131-
vhost = " ".join(["Use VHost", webaddress, app.config['APACHE_USER'], dstDir])
132-
ssl = " ".join(["Use SSL", webaddress, '443', app.config['APACHE_USER'], dstDir])
133-
#Verify Vhost isn't already present
134-
try:
135-
fp = open(app.config['APACHE_CONF_FILE'], "a+")
136-
for line in fp:
137-
if webaddress in line:
138-
fp.close()
139-
raise
140134

141-
fp = open(app.config['APACHE_CONF_FILE'], "a+")
142-
fp.write(vhost + "\n")
143-
fp.write(ssl + "\n")
144-
fp.close()
145-
except:
146-
print ("Skipping as " + webaddress + "already exists.")
147-
pass
135+
# Begin uwsgi vassal config creation
136+
# Open application skeleton (app.skel) file and append
137+
# "subscribe-to = <website-hostname>" config entry for the new
138+
# sites webaddress so that uwsgi's fastrouter can route the hostname.
139+
curDir = os.path.dirname(os.path.realpath(__file__))
140+
with open(curDir + '/' + 'app.skel') as f:
141+
contents = f.read()
142+
# Append uwsgi's subscribe-to line with hostname of new site:
143+
contents += "\nsubscribe-to = /tmp/sock2:" + webaddress + "\n"
144+
# Writeout app.ini config to file. uwsgi watches for .ini files
145+
# uwsgi will automatically detect this .ini file and start
146+
# routing requests to the site
147+
with open(dstDir + '/' + 'app.ini', 'w') as f:
148+
f.write(contents)
148149

149-
try:
150-
# Reload apache with new vhost
151-
subprocess.call("sudo /etc/init.d/apache2 graceful", shell=True)
152-
except Exception as e:
153-
print ("Problem reloading apache:")
154-
print (e)
155-
pass
156150
login_url = ''.join(['https://', webaddress, '/#/', login_token])
157151

158152
return login_url

0 commit comments

Comments
 (0)