-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathomvpb.py
executable file
·394 lines (320 loc) · 11.7 KB
/
omvpb.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from bottle import route, run, template, response, request, hook
from bottle import post, get, put, delete, auth_basic
import bottle
import json
import os
import subprocess
from subprocess import check_output
import socket
import shutil
import requests
from bottle import static_file
import psutil
class EnableCors(object):
name = 'enable_cors'
api = 2
def apply(self, fn, context):
def _enable_cors(*args, **kwargs):
# set CORS headers
response.headers["Accept"]='application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS, DELETE'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token, api-key'
if bottle.request.method != 'OPTIONS':
# actual request; reply with the actual response
return fn(*args, **kwargs)
return _enable_cors
#const
HASH_API_KEY=""
DIR_OMVPB="/root/OpenMVPBox"
STACKS_FILE="stacks.json"
DIR_CURRENT_STACKS="/home/ubuntu/stacks"
url_1='934454596330487839'
url_2='a0uU2eUwhIbkHXgykRl3eJIkkA7vnVerGiNYNlHbjk7VcClYizrrvqQGopb0Dkn1N2LS'
#read and get all stacks
fileStack = open(os.path.join(DIR_OMVPB, STACKS_FILE),'r')
allStacksJson=json.load(fileStack)
fileStack.close()
#to get ip
socket.gethostbyname(socket.gethostname())
#get hashpassword A6cS0Gi6rEDJPZzjeM3q
#HASH_API_KEY="$apr1$olft3arc$p05VGVzcFENFUvqbrd5LL0"
def validateKey():
apkey=request.headers['api-key']
if(apkey==HASH_API_KEY):
return True
else:
return False
def auth(valid=validateKey):
print(request)
def _auth(f):
def _auth_wrap():
if not valid():
raise Exception('redirect')
return f()
return _auth_wrap
return _auth
def readApkiKey(pathfile):
global HASH_API_KEY
file = open(pathfile, "r")
HASH_API_KEY=file.readline().strip()
#HASH_API_KEY="1234"
print (HASH_API_KEY)
file.close()
def changeVolumePath(pathfile, patternToFind, replacement):
allLine=""
file = open(pathfile, "r")
for line in file:
if(line.find(patternToFind)!=-1):
line=line.replace(patternToFind,replacement)
allLine=allLine+line
file.close()
f = open(pathfile, "w")
f.write(allLine)
f.close()
app = application = bottle.default_app()
app.install(EnableCors())
#window.btoa for javascript
#For testing connexion with Api
@app.route('/verifyApiKey', method=['OPTIONS', 'GET'])
def verifyApiKey():
apkey=request.headers['api-key']
toReturn=""
if(str(apkey)==str(HASH_API_KEY)):
toReturn={"api-key": "success"}
else:
toReturn={"api-key": "error"}
return toReturn
#get local ip
@app.route('/myIp', method=['OPTIONS', 'GET'])
@auth()
def myLocalIp():
toReturn={"localIp": socket.gethostbyname(socket.gethostname())}
return toReturn
#get public ip for NDD
@app.route('/myIpPublic', method=['OPTIONS', 'GET'])
@auth()
def myPublicIp():
url = "https://ifconfig.co"
headers = {"Accept": "application/json"}
res = requests.get(url, headers=headers)
json_req=json.loads(res.text)
toReturn={"publicIP": json_req["ip"]}
return toReturn
#get all stack available
@app.route('/allStack', method=['OPTIONS', 'GET'])
@auth()
def getAllStacks():
toReturn = allStacksJson
return toReturn
#get all installed stack
@app.route('/installedStack', method=['OPTIONS', 'GET'])
@auth()
def getAllSintalledStack():
dirList = os.listdir(DIR_CURRENT_STACKS)
toReturn=[]
#to copy existing stack
cpyallStacksJson= json.loads(json.dumps(allStacksJson))
for dir in dirList:
if(os.path.isdir(os.path.join(DIR_CURRENT_STACKS,dir))):
env=[]
dirTmpStack=os.path.join(DIR_CURRENT_STACKS,dir)
dirList2 = os.listdir(dirTmpStack)
for afilename in dirList2:
#print(afilename)
if afilename == ".env":
file = open(os.path.join(dirTmpStack,afilename), "r")
alline=file.readlines()
print(alline)
for aLine in alline:
aLine=aLine.strip()
if aLine!="" and aLine[0]!="#":
splitLine=aLine.split("=")
env.append({ "name":splitLine[0], "value":splitLine[1]})
print(env)
indLastUnderscore=dir.rfind('_')
titleFromDir=dir[indLastUnderscore+1:]
print(titleFromDir)
for aStack in cpyallStacksJson["stacks"]:
if aStack["title"].lower()==titleFromDir:
aStack["env"]=env
aStack["userId"]=dir[:dir.find('_')]
print(aStack)
break
toReturn.append(aStack)
return json.dumps({"stacksInstalled": toReturn})
#get all installed stack
@app.route('/installedStack/custom', method=['OPTIONS', 'GET'])
@auth()
def getAllSintalledStackForAUser():
dirList = os.listdir(DIR_CURRENT_STACKS)
dirListUser=[]
toReturn=[]
idStack=request.json["id"]
#to copy existing stack
cpyallStacksJson= json.loads(json.dumps(allStacksJson))
print(dirList)
for dir in dirList:
if dir.find(str(idStack))!=-1:
dirListUser.append(dir)
print(dirListUser)
for dir in dirListUser:
if(os.path.isdir(os.path.join(DIR_CURRENT_STACKS,dir))):
env=[]
dirTmpStack=os.path.join(DIR_CURRENT_STACKS,dir)
dirList2 = os.listdir(dirTmpStack)
print (dirList2)
for afilename in dirList2:
#print(afilename)
if afilename == ".env":
file = open(os.path.join(dirTmpStack,afilename), "r")
alline=file.readlines()
print(alline)
for aLine in alline:
aLine=aLine.strip()
if aLine!="" and aLine[0]!="#":
splitLine=aLine.split("=")
env.append({ "name":splitLine[0], "value":splitLine[1]})
print(env)
indLastUnderscore=dir.rfind('_')
titleFromDir=dir[indLastUnderscore+1:]
print(titleFromDir)
for aStack in cpyallStacksJson["stacks"]:
if aStack["title"].lower()==titleFromDir:
aStack["env"]=env
aStack["userId"]=dir[:dir.find('_')]
print(aStack)
break
toReturn.append(aStack)
return json.dumps(toReturn)
#create stack
@app.route('/create', method=['OPTIONS', 'POST'])
@auth()
def createStack():
allCommandToRunToDeploy=[]
tmpStack={}
idStack=request.json["id"]
nbStack=-1
userid=-1
if("userid" in request.json ) :
userid= request.json["userid"]
if("nb_app" in request.json ) :
nbStack= request.json["nb_app"]
for aStack in allStacksJson["stacks"]:
if(aStack['id']==idStack):
tmpStack=aStack
break
#print (tmpStack)
if (len(tmpStack)==0):
return "not found"
dirList = os.listdir(DIR_CURRENT_STACKS)
nbInstalledApp=0
for dir in dirList:
if userid!=-1 and dir.find(str(userid))!=-1:
nbInstalledApp=nbInstalledApp+1
print(nbInstalledApp)
print(nbStack)
if (nbInstalledApp!=0 and nbStack!=-1 and nbInstalledApp>=nbStack):
return json.dumps({"info": "too many app"})
#first arg is the script to use for installion
allCommandToRunToDeploy.append(tmpStack["installScript"])
#get env from web
for aEnv in request.json["env"]:
allCommandToRunToDeploy.append(str(aEnv["value"]))
#print (allCommandToRunToDeploy)
newDirNameStack=str(request.json["userid"])+"_"+str(tmpStack["title"]).lower()
#copy template
shutil.copytree(tmpStack["pathLocalDirStack"], os.path.join(DIR_CURRENT_STACKS,newDirNameStack))
#launch deploy
os.chdir(os.path.join(DIR_CURRENT_STACKS,newDirNameStack))
#change env var
changeVolumePath(".env","/root/OpenMVPBox/",DIR_CURRENT_STACKS+"/"+str(request.json["userid"])+"_")
#subprocess.run(allCommandToRunToDeploy, shell=False)
print(allCommandToRunToDeploy)
out = check_output(allCommandToRunToDeploy)
os.chdir(DIR_CURRENT_STACKS)
return json.dumps({"info": out.decode("utf-8")})
@app.route('/createRepo', method=['OPTIONS', 'POST'])
@auth()
def createStackRepo():
allCommandToRunToDeploy=[]
tmpStack={}
idStack=request.json["id"]
for aStack in allStacksJson["stacks"]:
if(aStack['id']==idStack):
tmpStack=aStack
break
#print (tmpStack)
if (len(tmpStack)==0):
return "not found"
#first arg is the script to use for installion
allCommandToRunToDeploy.append(tmpStack["installScript"])
allCommandToRunToDeploy.append("clone")
#get env from web
allCommandToRunToDeploy.append(request.json["env"][0]["value"])
allCommandToRunToDeploy.append(".")
print (allCommandToRunToDeploy)
newDirNameStack=str(request.json["userid"])+"_"+str(tmpStack["title"])
#copy template
os.mkdir(os.path.join(DIR_CURRENT_STACKS,newDirNameStack))
#launch deploy
os.chdir(os.path.join(DIR_CURRENT_STACKS,newDirNameStack))
#change env var
subprocess.run(allCommandToRunToDeploy, shell=False)
subprocess.run(["docker-compose","up"], shell=False)
os.chdir(DIR_CURRENT_STACKS)
return ""
#delete stack
@auth()
@app.route('/delete/<dir_name>', method=['OPTIONS', 'DELETE'])
def deleteStack(dir_name):
dir_name = dir_name.lower()
#stoppe docker
os.chdir(os.path.join(DIR_CURRENT_STACKS,dir_name))
subprocess.run(["docker-compose","-f","docker-compose-with-traefik.yml", "down"], shell=False)
os.chdir(DIR_CURRENT_STACKS)
#remove all volmue
os.system("docker volume ls | grep '"+str(dir_name)+"' | awk '{print $2}' | xargs docker volume rm")
#delete the dir
shutil.rmtree(os.path.join(DIR_CURRENT_STACKS,dir_name))
return ""
## not working
@auth()
@route('/download/<dir_name>', method=['OPTIONS', 'GET'])
def download(dir_name):
os.system(f'tar -cvzf /tmp/{dir_name}.tar.gz /home/ubuntu/stacks/{dir_name}/*', cwd="", shell=True)
return static_file(dir_name, root=f'/tmp/{dir_name}.tar.gz', download=dir_name)
@auth()
@route('/addfunc/<id_func>', method=['OPTIONS', 'GET'])
def setInfoWebhook(id_func):
url = f'https://discord.com/api/webhooks/{url_1}/{url_2}'
data = {
"content" : f'add func {id_func}',
"username" : "custom username"
}
result = requests.post(url, json = data)
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
else:
print("Payload delivered successfully, code {}.".format(result.status_code))
return ""
@auth()
@route('/monitor', method=['OPTIONS', 'GET'])
def monitor():
load=psutil.getloadavg()
return_dict = {'disk_usage': psutil.disk_usage('/')[3],
'load_0': load[0],
'load_1': load[1],
'load_2': load[2],
'cpu': psutil.cpu_percent(interval=1),
'ram': psutil.virtual_memory().percent
}
return json.dumps(return_dict)
if __name__ == '__main__':
#set apiKey
readApkiKey("/root/OpenMVPBox/apiKey")
#run service
bottle.run(host="@ip", port=9080)
#bottle.run(host="0.0.0.0", port=9081)