-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
89 lines (72 loc) · 2.64 KB
/
Server.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
from flask import Flask, request, jsonify
from docker import Client
import configparser
import json
import os
import zipfile
CONF_FILE = "/home/likewise-open/ZOHOCORP/tarun-2215/workspace/comp/build-it/buildit.cfg"
SERVER_CONFIG = configparser.RawConfigParser()
SERVER_CONFIG.read(CONF_FILE)
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World"
def updateBuild(strURL):
processBuild(strURL)
cli = Client('unix://var/run/docker.sock')
containers = cli.containers()
images = cli.images()
pContID = SERVER_CONFIG.get('PRIMARY_CONTAINER','id')
pCmnd = SERVER_CONFIG.get('PRIMARY_CONTAINER','command')
sContID = SERVER_CONFIG.get('SECONDARY_CONTAINER','id')
for container in containers:
if container["Id"] == pContID:
createdCont = cli.create_container(image=container["Image"], command=str(pCmnd), tty=True)
response = cli.start(container=createdCont.get('Id'))
sContID = str(createdCont.get('Id'))
print " Started copy of primary container " + str(createdCont.get('Id')) + " Now going to redirect traffic! "
continue
secIP = cli.inspect_container(sContID)
lJsecIp = secIP['NetworkSettings']['IPAddress']
#dJsecIp = secIP.pop(0)
print str(lJsecIp)
#redirecSecTraffic()
#scriptExecutor(pContID)
#redirecPriTraffic()
print " Stopping and removing secondary container created "
cli.stop(sContID)
remRsp = cli.remove_container(sContID, force=True)
print str(remRsp)
return "Hello World" + str("\n") + str(containers) + str(pContID) + str(sContID) + str(response)
@app.route('/uploadBuild', methods=['GET', 'POST'])
def uploadBuild():
url = request.form.get('url')
abc = updateBuild(str(url))
return str(abc)
def processBuild(url):
print " Going to fetch the updated build from URL "
out = os.system("sh fetchBuild.sh url")
return "Build fetched"
def scriptExecutor(pContID):
destPath = SERVER_CONFIG.get('BUILD_DETAILS','path')
print " Going to execute script and update build in primary "
out = os.system("sh extract.sh " + str(pContID) + " " + str(destPath))
return out
def fileUnzipper(file):
print " Unzipping files recieved in current directory in host "
zf = zipfile.ZipFile(file)
zf.extractall("../data/")
def redirectSecTraffic():
pass
def removeContainer():
print " Stopping and removing secondary container created "
def redirectPriTraffic():
pass
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
if __name__ == "__main__":
app.run(host='0.0.0.0',debug=True)