-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
174 lines (156 loc) · 4.83 KB
/
run.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
from flask import Flask
from flask import render_template
from flask import jsonify
from flask import session
from flask import request
from flask import Response
from flask import make_response
from application.socket import lxd_socketio
from application.api import LxdApi
from application.api import SshApi
from application.api.servers import Servers
#from flask_socketio import SocketIO
from config import Config
import time
import random
import pdb
import json
app = Flask(__name__)
app.debug = True #TODO REMOVE FOR LIVE
app.config["SECRET_KEY"] = "secret!" #TODO ACTUAL SECRET KEY
socketio = lxd_socketio()
lxd_api = LxdApi()
global config
thread = None
servers = None
@app.route("/")
def main():
# containers = {}
# for s in config:
# containers[s] = lxd_api.get_container_info(s)
#
# #ssh = SshApi(config)
# #server_info = {}
# #for s in config:
# # server_info[s] = ssh.get_server_info(s)
#
# print '[[['
# print config
# print containers
# print ']]]'
# vals = {
# # "servers": config,
# # "containers": containers,
# # "server_info": server_info,
# }
# return render_template("index.html", **vals)
return render_template("index.html")
@app.route("/get_info/")
def get_info_r():
containers = {}
for s in config:
containers[s] = lxd_api.get_container_info(s)
vals = {
"servers": servers.get_display_info(),
"containers": containers,
# "server_info": server_info,
}
return jsonify(vals)
@app.route("/container_cmd/", methods=["POST"])
def container_cmd_handler():
if request.method == "POST":
req_data = request.get_json()
server = req_data.get("server")
container = req_data.get("container")
method = req_data.get("method")
snap = req_data.get("snap")
tar_name = '' or req_data.get("tar_name")
if req_data.get("type") == "container":
lxd_api.exec_container_cmd(server, container, method, tar_name)
# get update container info after method
c_info = get_container_info(server, container)
return make_response(jsonify(c_info))
elif req_data.get("type") == "snapshot":
lxd_api.exec_snapshot_cmd(server, snap, container, method)
c_info = get_container_info(server, container)
return make_response(jsonify(c_info))
def get_container_info(server, container):
field_translations = {
"name": "name",
}
for c in lxd_api.get_container_info(server):
if c["name"] == container:
return c
def update_thread():
# global servers
"""Example of how to send server generated events to clients."""
count = 0
while True:
socketio.sleep(5)
count += 1
mes = {}
if servers:
for s in servers.get():
mes.update({s : servers.get(s).get_info()})
mes = json.dumps(mes)
socketio.emit('my_response',
mes,
namespace='/update')
@socketio.on("connected", namespace="/update")
def got_event(msg):
global thread
if thread is None:
thread = socketio.start_background_task(target=update_thread)
socketio.emit('my_response', {'data': 'Connected', 'count': 0})
@app.after_request
def add_header(response):
#TODO REMOVE FOR LIVE
response.headers["Cache-Control"] = "public, max-age=0"
return response
""" setup / run app """
def _get_configuration():
f = file("server_cfg.cfg")
cfg = Config(f)
config = {}
for server in cfg.servers:
config[server.name] = {
"servername": server.name,
"username": server.username,
"password": server.password,
"endpoint": server.endpoint,
"cert": (server.certfile,server.keyfile),
"verify": server.verify,
"keyfile": server.keyfile,
"certfile": server.certfile,
"rgba_color": "rgba(%s,%s,%s,1)" % (
random.randrange(255),
random.randrange(255),
random.randrange(255)
),
"excluded_containers": tuple(server.excluded_containers),
}
return config
def config_app():
print "1"
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
print "2"
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
print "3"
global config
config = _get_configuration()
print "4"
socketio.init_app(app)
print "5"
socketio.set_lxd_config(config)
print "6"
lxd_api.set_config(config)
# New
global servers
print "7"
servers = Servers(config)
print "8"
#servers.execute_ssh_command()
if __name__ == "__main__":
config_app()
socketio.run(app, host='0.0.0.0')