-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
208 lines (185 loc) · 7.03 KB
/
app.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""API for a docker containers application server
This project uses subprocess as it restart apache when updating the conf file...
It could use docker-py but subprocess or dbus_next would be necessary to restart apache.
maybe using docker in docker ?
"""
import random
import subprocess
import datetime
import re
from flask import Flask
from flask import render_template
from flask import request
import redis
from conf import CNAME, LOCAL_NAME, SU_PASS, DOCKER_IMAGE
from conf import APACHE_CONF, CERT_DIR, CRT_FILE, KEY_FILE, MQTT_HOST
# pylint: disable=R0914
app = Flask(__name__)
RCO = redis.Redis(host="localhost", port=6379, db=0)
def exec_shell_command(cmd):
"""execute a shell command"""
try:
result_success = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError as err:
return f'An error {err} occurred'
return result_success
def get_free_port():
"""get a random port to create a socket
on offre en théorie 1011 ports :-)"""
if RCO.scard("ports") == 1011:
return False
while True:
port_nb = random.randint(8080, 9090)
if not RCO.sismember("ports", port_nb):
RCO.sadd("ports", port_nb)
return str(port_nb)
def maj_apache_conf(new_conf):
"""met à jour le fichier apache de configuration
puis relance apache
"""
with open("apache.conf", "w", encoding="utf-8") as f_p:
for line in new_conf:
f_p.write(line)
cmd = [f'echo {SU_PASS} | sudo -S mv apache.conf {APACHE_CONF}']
exec_shell_command(cmd)
cmd = [f'echo {SU_PASS} | sudo -S systemctl reload apache2']
exec_shell_command(cmd)
def read_apache_conf():
"""retourne le fichier apache de configuration
sous la forme d'un tableau
"""
lines = []
with open(APACHE_CONF, "r", encoding="utf-8") as f_p:
lines = f_p.readlines()
return lines
@app.route("/")
def home():
"""home page"""
lang = request.accept_languages.best_match(['fr', 'en'])
return render_template('home.html', lang=lang)
@app.route("/start")
def start():
"""start a new container, configure the reverse proxy and set a new cookie"""
proto = request.scheme
container_port = 443 if proto == "https" else 80
host_port = get_free_port()
cmd = f'echo {SU_PASS} | sudo -S docker run -d --restart always'
cmd = f'{cmd} -p {host_port}:{container_port}'
if proto == "https":
cmd = f'{cmd} -v {CERT_DIR}:/cert'
cmd = f'{cmd} -e KEY_FILE=/cert/{KEY_FILE}'
cmd = f'{cmd} -e CRT_FILE=/cert/{CRT_FILE}'
cmd = f'{cmd} -e REVERSE_PROXY=1'
cmd = f'{cmd} -e USE_HOSTNAME_FOR_MQTT_TOPIC_CLIENTID=1'
cmd = f'{cmd} -e MQTT_HOST={MQTT_HOST}'
cmd = f'{cmd} {DOCKER_IMAGE}'
long_token = exec_shell_command([cmd])
token = long_token[:12].decode()
app_url = f'{proto}://{CNAME}/{token}/'
lines = read_apache_conf()
end_balise = -1
for i, line in enumerate(lines):
if '</VirtualHost>' in line:
end_balise = i
break
tab = " "
lines.insert(end_balise, f'{tab}ProxyPass /{token} {proto}://{LOCAL_NAME}:{host_port}\n')
lines.insert(end_balise, f'{tab}ProxyPassReverse /{token} {proto}://{LOCAL_NAME}:{host_port}\n')
proxy_check_cn_configured = False
proxy_check_cn_directive = f'{tab}SSLProxyCheckPeerCN off\n'
proxy_engine_configured = False
proxy_engine_directive = f'{tab}SSLProxyEngine on\n'
for i, line in enumerate(lines):
if "SSLProxyCheckPeerCN" in line:
proxy_check_cn_configured = True
if "off" not in line:
lines[i] = proxy_check_cn_directive
if "SSLProxyEngine" in line:
proxy_engine_configured = True
if "on" not in line:
lines[i] = proxy_engine_directive
if not proxy_check_cn_configured:
lines.insert(end_balise, proxy_check_cn_directive)
if not proxy_engine_configured:
lines.insert(end_balise, proxy_engine_directive)
maj_apache_conf(lines)
json_datas = {}
json_datas["token"] = token
json_datas["port"] = host_port
json_datas["app_url"] = app_url
resp = app.make_response(json_datas)
cookie_life_duration = datetime.datetime.now() + datetime.timedelta(days=30)
resp.set_cookie('emoncms_token', token, samesite='Lax', secure=True,
expires=cookie_life_duration)
resp.set_cookie('port', host_port, samesite='Lax', secure=True,
expires=cookie_life_duration)
resp.set_cookie('app_url', app_url, samesite='Lax', secure=True,
expires=cookie_life_duration)
return resp
@app.route("/list")
def list_running_containers():
"""list running containers"""
nb_used_ports = RCO.scard("ports")
cmd = [f'echo {SU_PASS} | sudo -S docker container ls']
containers = exec_shell_command(cmd)
containers = containers.decode().replace("\n", "<br>")
content = f'Il y a {nb_used_ports} port(s) utilisé(s)'
content = f'{content}<br>{containers}'
return content
@app.route("/check/<token>")
def check(token):
"""given a token, check if container is running"""
cmd = [f'echo {SU_PASS} | sudo -S docker ps | grep {token}']
return exec_shell_command(cmd)
@app.route("/clear")
def clear():
"""delete all running containers"""
all_ports = RCO.smembers("ports")
lines = read_apache_conf()
suppress_list = []
tokens = []
pattern = "ProxyPass /([0-9a-z]{12})"
motif = re.compile(pattern)
for i, line in enumerate(lines):
for port in all_ports:
if str(port.decode()) in line:
suppress_list.append(i)
if "ProxyPass /" in line:
result = re.search(motif, line)
tokens.append(result.group(1))
RCO.delete("ports")
# un peu trop brutal
# cmd = [f'echo {SU_PASS} | sudo -S docker ps -aq | xargs docker stop | xargs docker rm']
# exec_shell_command(cmd)
token_string = ""
for token in tokens:
token_string = f'{token_string} {token}'
cmd = [f'echo {SU_PASS} | sudo -S docker container stop{token_string}']
exec_shell_command(cmd)
cmd = [f'echo {SU_PASS} | sudo -S docker container rm{token_string}']
exec_shell_command(cmd)
new_conf = []
for i, line in enumerate(lines):
if i not in suppress_list:
new_conf.append(line)
maj_apache_conf(new_conf)
return 'done'
@app.route("/delete/<port>/<token>")
def delete(port, token):
"""given a port and a token, delete corresponding container"""
RCO.srem("ports", port)
cmd = [f'echo {SU_PASS} | sudo -S docker container stop {token}']
exec_shell_command(cmd)
cmd = [f'echo {SU_PASS} | sudo -S docker container rm {token}']
exec_shell_command(cmd)
lines = read_apache_conf()
suppress_list = []
for i, line in enumerate(lines):
if port in line and token in line:
suppress_list.append(i)
new_conf = []
for i, line in enumerate(lines):
if i not in suppress_list:
new_conf.append(line)
maj_apache_conf(new_conf)
return 'done'