-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathapp.py
122 lines (105 loc) · 3.89 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
from pymongo import MongoClient
from bson.objectid import ObjectId
from flask import Flask,render_template,jsonify,json,request
from fabric.api import *
application = Flask(__name__)
client = MongoClient('localhost:27017')
db = client.MachineData
@application.route("/addMachine",methods=['POST'])
def addMachine():
try:
json_data = request.json['info']
deviceName = json_data['device']
ipAddress = json_data['ip']
userName = json_data['username']
password = json_data['password']
portNumber = json_data['port']
db.Machines.insert_one({
'device':deviceName,'ip':ipAddress,'username':userName,'password':password,'port':portNumber
})
return jsonify(status='OK',message='inserted successfully')
except Exception,e:
return jsonify(status='ERROR',message=str(e))
@application.route('/')
def showMachineList():
return render_template('list.html')
@application.route('/getMachine',methods=['POST'])
def getMachine():
try:
machineId = request.json['id']
machine = db.Machines.find_one({'_id':ObjectId(machineId)})
machineDetail = {
'device':machine['device'],
'ip':machine['ip'],
'username':machine['username'],
'password':machine['password'],
'port':machine['port'],
'id':str(machine['_id'])
}
return json.dumps(machineDetail)
except Exception, e:
return str(e)
@application.route('/updateMachine',methods=['POST'])
def updateMachine():
try:
machineInfo = request.json['info']
machineId = machineInfo['id']
device = machineInfo['device']
ip = machineInfo['ip']
username = machineInfo['username']
password = machineInfo['password']
port = machineInfo['port']
db.Machines.update_one({'_id':ObjectId(machineId)},{'$set':{'device':device,'ip':ip,'username':username,'password':password,'port':port}})
return jsonify(status='OK',message='updated successfully')
except Exception, e:
return jsonify(status='ERROR',message=str(e))
@application.route("/getMachineList",methods=['POST'])
def getMachineList():
try:
machines = db.Machines.find()
machineList = []
for machine in machines:
print machine
machineItem = {
'device':machine['device'],
'ip':machine['ip'],
'username':machine['username'],
'password':machine['password'],
'port':machine['port'],
'id': str(machine['_id'])
}
machineList.append(machineItem)
except Exception,e:
return str(e)
return json.dumps(machineList)
@application.route("/execute",methods=['POST'])
def execute():
try:
machineInfo = request.json['info']
ip = machineInfo['ip']
username = machineInfo['username']
password = machineInfo['password']
command = machineInfo['command']
isRoot = machineInfo['isRoot']
env.host_string = username + '@' + ip
env.password = password
resp = ''
with settings(warn_only=True):
if isRoot:
resp = sudo(command)
else:
resp = run(command)
return jsonify(status='OK',message=resp)
except Exception, e:
print 'Error is ' + str(e)
return jsonify(status='ERROR',message=str(e))
@application.route("/deleteMachine",methods=['POST'])
def deleteMachine():
try:
machineId = request.json['id']
db.Machines.remove({'_id':ObjectId(machineId)})
return jsonify(status='OK',message='deletion successful')
except Exception, e:
return jsonify(status='ERROR',message=str(e))
if __name__ == "__main__":
application.run(host='0.0.0.0')