-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
233 lines (174 loc) · 5.94 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import sys
import os
import shutil
import boto
import urllib
from boto.s3.key import Key
from xml.etree.ElementTree import fromstring, tostring, Element
import settings
from lib import get_bucket, get_versioninfo
def install_packages():
packages = [
'mc',
'rsync',
'python-setuptools',
'python-dev',
'build-essential',
'python-virtualenv',
'libmysqlclient-dev',
'libjpeg62-dev',
'apache2',
'libapache2-mod-wsgi',
'nginx'
]
os.system('apt-get update')
os.system('apt-get -y upgrade')
os.system('apt-get install -y ' + ' '.join(packages))
def compare_applications():
""" If true, we stopped the servers because of an change, so they need
to be started again at the end """
group = settings.GROUP
local = open(settings.APPS_LOCALINFO).read()
remote = remote = get_versioninfo()
""" Collect app list for local and remove """
localApps = get_app_list(local)
remoteApps = get_app_list(remote, group)
""" First, check for apps to remove """
for app, localVersion in localApps.items():
found = False
for remoteApp, version in remoteApps.items():
if app == remoteApp:
found = True
if not found:
uninstall(app)
""" Now check for new apps """
for app, version in remoteApps.items():
found = False
for localApp, localVersion in localApps.items():
if app == localApp:
found = True
if not found:
install(app, version)
""" Now check for apps to upgrade """
for app, version in remoteApps.items():
diff = False
for localApp, localVersion in localApps.items():
if app == localApp and version != localVersion:
diff = True
if diff:
upgrade(app, version)
handle_webserver()
def get_app_list(filecontent, group = False):
list = {}
if filecontent.strip() == '':
filecontent = '<info></info>'
dom = fromstring(filecontent)
for node in dom:
if group and node.attrib['name'] != group:
continue;
for app in node:
list[app.attrib['name']] = app.attrib['version']
return list
GLOBAL_start_webserver = False
def stop_webserver():
return # TODO
global GLOBAL_start_webserver
if GLOBAL_start_webserver:
return
GLOBAL_start_webserver = True
os.system('/etc/init.d/nginx stop')
os.system('/etc/init.d/apache2 stop')
def handle_webserver():
return # TODO
global GLOBAL_start_webserver
if GLOBAL_start_webserver:
os.system('/etc/init.d/apache2 start')
os.system('/etc/init.d/nginx start')
def upgrade(app, version):
print "UPGRADE " + app, version
uninstall(app)
install(app, version)
def uninstall(app):
print "UNINSTALL " + app
stop_webserver()
local_dir = os.path.join(settings.APPS_LOCALDIR, app)
os.system('cd ' + local_dir + ' && /bin/bash mwpackage/uninstall.sh')
try:
os.removedirs(local_dir)
except:
pass
write_localinfo_remove(app)
def install(app, version):
print "INSTALL " + app, version
stop_webserver()
local_dir = os.path.join(settings.APPS_LOCALDIR, app)
try:
os.makedirs(local_dir)
except:
pass
localfile = os.path.join(local_dir, app + '.tar.bz2')
""" Download file form S3 """
bucket = get_bucket()
k = Key(bucket)
k.key = 'applications/' + app + '_' + version + '.tar.bz2'
k.get_contents_to_file(open(localfile, 'w'))
""" extract file """
os.system('cd ' + local_dir + ' && tar -jxvf %s' % app + '.tar.bz2')
os.unlink(localfile)
""" Run install scrpt """
os.system('cd ' + local_dir + ' && /bin/bash mwpackage/install.sh')
""" write new version to config """
write_localinfo_new(app, version)
def write_localinfo_remove(app):
local = open(settings.APPS_LOCALINFO).read()
if local.strip() == '':
local = '<info><local></local></info>'
dom = fromstring(local)
""" remove and old entry, if exists """
l = dom.find("local/")
for application in l:
if application.attrib['name'] == app:
l.remove(application)
open(settings.APPS_LOCALINFO, 'w').write(tostring(dom))
def write_localinfo_new(app, version):
local = open(settings.APPS_LOCALINFO).read()
if local.strip() == '':
local = '<info><local></local></info>'
dom = fromstring(local)
""" remove and old entry, if exists """
l = dom.find("local/")
for application in l:
if application.attrib['name'] == app:
l.remove(application)
appelement = Element('application')
appelement.attrib['name'] = app
appelement.attrib['version'] = version
l.append(appelement)
open(settings.APPS_LOCALINFO, 'w').write(tostring(dom))
def get_hostname():
""" Get the hostname. WARNING: THIS IS SOME CODE OPTIMIZED FOR AMAZON
INSTANCES!! """
url = 'http://%s/%s/%s/' % ('169.254.169.254', '1.0', 'meta-data/instance-id')
value = urllib.urlopen(url).read()
if "404 - Not Found" in value:
return 'unknown'
return value
def upload_log(app, sys, filename):
bucket = get_bucket()
get_bucket(settings.AWS_LOGS_ACCESS_KEY_ID, settings.AWS_LOGS_SECRET_ACCESS_KEY,
settings.AWS_LOGS_BUCKET_NAME)
basename = os.path.basename(filename)
hostname = get_hostname()
k = Key(bucket)
k.key = 'milkywhite/' + '_'.join(hostname, app, sys, basename)
k.set_contents_from_filename(filename)
print "Upload finished"
# ------------------------------------------------------------------------------
if __name__ == "__main__":
if len(sys.argv) == 1 or sys.argv[1] == '':
install_packages()
compare_applications()
elif sys.argv[1] == 'appsonly':
compare_applications()
elif sys.argv[1] == 'uploadlog':
upload_log(sys.argv[2], sys.argv[3], sys.argv[4])