-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.py
executable file
·86 lines (70 loc) · 2.7 KB
/
start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
from yaml import load,dump
import urllib2
import select
import os
import sys
from subprocess import PIPE, Popen
class NginxRewriter(object):
def __init__(self):
self.home = os.path.expanduser("~")
self.confd = '/etc/nginx/conf.d/'
self.webroot = '/var/www/nginx-rewriter/'
config_url = os.getenv('CONFIG_URL',"https://raw.githubusercontent.com/sellerlabs/nginx-rewriter/master/rewrite-rules.example.yaml")
response = urllib2.urlopen(config_url)
self.config = load(response)
self.email_addr = self.config['email']
self.target_domains = self.config['target_domains']
self.gen_config()
def gen_cert(self, redir_domains):
print redir_domains
command = Popen(["/usr/bin/certbot","certonly","--staging","--dry-run","--email",self.email_addr,"-n","--agree-tos","-t","--standalone","--standalone-supported-challenges","http-01","-d",redir_domains])
command.wait()
def gen_config(self):
print "loading domains"
for target_domain in self.target_domains:
redir_domains = self.target_domains[target_domain]['redir_domains']
redir_domains_spaces = " ".join(redir_domains)
redir_domains_csv = ",".join(redir_domains)
p = Popen(["mkdir",self.webroot+target_domain])
p.wait()
print self.webroot+target_domain
self.gen_cert(redir_domains_csv)
c = open(self.confd + target_domain + ".conf", 'w')
print "Nginx config %s" % target_domain
server_conf = """
server {
listen 80;
server_name %s;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name %s;
return 301 https://%s$request_uri;
ssl_certificate /etc/letsencrypt/live/%s/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/%s/privkey.pem;
}
""" % (redir_domains_spaces, redir_domains_spaces, target_domain, redir_domains[0], redir_domains[0])
print server_conf
c.write(server_conf)
c.close
self.start_nginx()
def start_nginx(self):
print "testing config..."
sys.stdout.flush()
p = Popen(["/usr/sbin/nginx","-t"], stderr=PIPE, stdout=PIPE)
p.wait()
output = p.communicate()[1]
print output
if "test failed" in output:
# Exit program with status code "1" if tests failed.
sys.exit(1)
print "starting nginx in the foreground"
sys.stdout.flush()
p = Popen(["/usr/sbin/nginx","-g", "daemon off;"], stderr=sys.stderr, stdout=sys.stdout)
p.wait()
if __name__ == "__main__":
NginxRewriter()