-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayersbox-studio.py
106 lines (97 loc) · 2.76 KB
/
layersbox-studio.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
from flask import Flask, url_for, request
from threading import Thread
import flask
import re
import subprocess
import os
app = Flask(__name__)
@app.route('/containers/', methods=['GET'])
def layersbox_container_status():
proc = subprocess.Popen(['python', 'layersbox', 'status'],stdout=subprocess.PIPE)
result = []
proc.stdout.readline()
proc.stdout.readline()
while True:
line = proc.stdout.readline()
if line != '':
data = {}
#the real code does filtering here
rarr = re.sub(' +',' ',line.rstrip()).split(" ")
if(len(rarr)<3):
break
data["component"] = rarr[0]
data["cmd"] = rarr[1]
data["status"] = rarr[2]
if(len(rarr)>3):
data["ports"] = rarr[3]
result.append(data)
else:
break
return flask.jsonify(*result)
@app.route('/components/', methods=['GET'])
def layersbox_component_status():
proc = subprocess.Popen(['docker', 'ps'],stdout=subprocess.PIPE)
result = []
proc.stdout.readline()
while True:
line = proc.stdout.readline()
if line != '':
data = {}
#the real code does filtering here
rarr = re.sub(' +',' ',line.rstrip()).split(" ")
if(len(rarr)<3):
break
data["id"] = rarr[0]
data["component"] = rarr[1]
data["cmd"] = rarr[2]
data["created"] = rarr[3]
data["status"] = rarr[4]
if(len(rarr)<7):
data["name"] = rarr[5]
else:
data["ports"] = rarr[5]
data["name"] = rarr[6]
result.append(data)
else:
break
return flask.jsonify(*result)
@app.route('/components/', methods=['POST'])
def layersbox_install():
component=request.data
print(component)
thread = Thread(target = installComponent, args = (component, ))
thread.start()
return "Installing..."
def installComponent(arg):
print("Installing " + arg)
component = arg.split("#")
c = component[0].split('/')[-1]
if not os.path.exists("logs/"+c):
os.makedirs("logs/"+c)
with open("logs/"+c+"/install.txt", "w+") as outfile:
proc = subprocess.Popen(['python', 'layersbox', 'install', arg ],stdout=outfile,stderr=outfile)
print("Check logs for the install process.")
@app.route('/components/', methods=['DELETE'])
def layersbox_uninstall():
component=request.data
proc = subprocess.Popen(['python', 'layersbox', 'uninstall', component ],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, error = proc.communicate()
if proc.returncode != 0:
return error
result = ""
while True:
line = proc.stdout.readline()
if line != '':
#the real code does filtering here
result = result+line.rstrip()+ "<br>"
else:
break
return result
@app.route('/components/<component>')
def page(component):
f = open("logs/"+component+"/install.txt", 'r+')
result = {}
result['log'] = f.read().split("\n")
return flask.jsonify(**result)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8081)