-
Notifications
You must be signed in to change notification settings - Fork 3
/
Container_app.py
161 lines (144 loc) · 5.21 KB
/
Container_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
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
from pymongo import MongoClient
from bson.objectid import ObjectId
from flask import Flask,render_template,jsonify,json,request
from fabric.api import *
import subprocess
from subprocess import PIPE, Popen
import webbrowser
application = Flask(__name__)
client = MongoClient('localhost:27017')
db = client.MachineData
print ("db value:",db)
print ("Database name",client.database_names())
@application.route("/addMachine",methods=['POST'])
def addMachine():
print("In add machine")
try:
json_data = request.json['info']
print(json_data)
cont_name = json_data['cont_name']
cont_ip = json_data['cont_ip']
userName = json_data['username']
password = json_data['password']
portNumber = json_data['port']
#print(deviceName)
db.Machines.insert_one({
'cont_name':cont_name,'cont_ip':cont_ip,'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():
print("In showmachine para")
return render_template('list.html')
@application.route('/getMachine',methods=['POST'])
def getMachine():
print ("In getMachine para")
try:
machineId = request.json['id']
#print "machineID is",machineID
machine = db.Machines.find_one({'_id':ObjectId(machineId)})
machineDetail = {
'cont_name':machine['cont_name'],
'cont_ip':machine['cont_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():
print ("In machinelist para:")
try:
machines = db.Machines.find()
print ("machines:",machines)
machineList = []
for machine in machines:
print ("Machine",machine)
machineItem = {
'cont_name':machine['cont_name'],
'cont_ip':machine['cont_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():
# print("In execute para")
# try:
# machineInfo = request.json['info']
# print (machineInfo)
# cont_ip = machineInfo['cont_ip']
# username = machineInfo['username']
# password = machineInfo['password']
# command = machineInfo['command']
# print (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))
def execute():
print("In execute para")
try:
machineInfo = request.json['info']
#cont_name = machineInfo['cont_name']
#cmnd = machineInfo['command']
#env.host_string = username + '@' + ip
#env.password = password
#output = webbrowser.open(/home/ubuntu/oscap_docker/img_report.html[, new=0[, autoraise=True]])
output = subprocess.Popen("lynx /home/ubuntu/oscap_docker/img_report.html",shell=True,executable="/bin/bash")
#print (output)
resp = ''
#with settings(warn_only=True):
#if isRoot:
# resp = sudo(command)
#else:
#resp = run(command)
#return jsonify(status='OK',message=)
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')