forked from DistributedSky/drone-employee-connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresources.py
105 lines (89 loc) · 3.87 KB
/
resources.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
from flask_restful import Resource, Api, reqparse
from platform import machine, processor, system
from os import system as system_call
from netifaces import interfaces
from docker import from_env
from application import app
import psutil, time, json, wifi
API_PREFIX = '/api/v1'
class Containers(Resource):
def __init__(self):
self.parser = reqparse.RequestParser()
self.parser.add_argument('image')
self.parser.add_argument('params')
def get(self):
names = map(lambda x: x.name, from_env().containers.list(all=True))
return {'containers': list(names)}
def post(self):
args = self.parser.parse_args()
params = json.loads(args['params'])
env = map(lambda key: key.upper().strip()+'='+params[key].strip(), params)
network = params['name']+'-net'
if 'master' in params:
network = 'container:'+params['master']
else:
from_env().networks.create(network, driver='bridge')
container = from_env().containers.run('droneemployee/'+args['image']+':armhf',
name=params['name'],
network_mode=network,
environment=list(env),
privileged=True,
detach=True)
if 'wlan' in params:
with open('/etc/dronelinks.csv', 'a') as links:
links.write('{0},{1},{2},{3}\n'.format(params['name'],params['wlan'],params['ssid'],params['password']))
wifi.spawn(params['name'],params['wlan'],params['ssid'],params['password'])
return {'containers': {
container.name: {
'status': container.status,
'image': container.attrs['Config']['Image']
}}}
class Container(Resource):
def __init__(self):
self.parser = reqparse.RequestParser()
self.parser.add_argument('cmd')
def get(self, name):
c = from_env().containers.get(name)
return {'containers': {
name: {
'status': c.status,
'image': c.attrs['Config']['Image']
}}}
def delete(self, name):
try:
c = from_env().containers.get(name)
c.remove(force=True)
if name+'-net' in from_env().networks.list():
n = from_env().networks.get(name+'-net')
n.remove()
except:
return {'success': False}
return {'success': True}
def post(self, name):
args = self.parser.parse_args()
c = from_env().containers.get(name)
if args['cmd'] == 'logs':
return {'containers': {name: { 'logs': '{0}'.format(c.logs()) }}}
elif args['cmd'] == 'restart':
return {'containers': {name: { 'restart': '{0}'.format(c.restart()) }}}
else:
return {'success': False}
class Hardware(Resource):
def get(self):
ping_param = "-n 1" if system().lower()=="windows" else "-c 1"
hasInternet = system_call("ping " + ping_param + " github.com") == 0
wlans = filter(lambda x: x.startswith('wlan'), interfaces())
return {'hardware':
{ 'system': system()
, 'arch': machine()
, 'time': time.time()
, 'internet': hasInternet
, 'wlans': list(wlans)
, 'usage': {
'cpu': psutil.cpu_percent(interval=1, percpu=True),
'mem': psutil.virtual_memory().percent
}}}
api = Api(app)
api.add_resource(Containers, API_PREFIX+'/containers')
api.add_resource(Container, API_PREFIX+'/containers/<name>')
api.add_resource(Hardware, API_PREFIX+'/hardware')