/
deploy-ubuntu.py
437 lines (371 loc) · 12.9 KB
/
deploy-ubuntu.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#! /usr/bin/env python
#-----------------------------------------#
# Written by Kelcey Damage, 2013
#+
import os, subprocess
from paramiko import SSHClient, AutoAddPolicy
import time
import sys
sys.path.append("/Volumes/git/axol")
from axol_common.distributed import axol_roledefs
#folder_to_be_archived = '/home/mysql_backup_v2.py'
roledefs = axol_roledefs.generate_base_roles('external')
class DeployTool(object):
"""docstring for ClassName"""
def __init__(self, remote_user, remote_password, remote_server):
self.remote_user = remote_user
self.remote_password = remote_password
self.remote_server = remote_server
self.client = SSHClient()
self.client.set_missing_host_key_policy(AutoAddPolicy())
self.client.load_system_host_keys()
self.client.connect(
hostname=self.remote_server,
username=self.remote_user,
password=self.remote_password
)
def restart_node(self):
print 'RESTARTING AXOL ################'
self._ssh('sudo /opt/AXOL_Management/AXOL/axol_node/axol_start')
def install_analytics_node(self, infile):
self.create_directories()
print 'INSTALLING PACKAGES ################'
self._ssh('sudo apt-get install python-dev python-pip python-numpy \
python-scipy python-matplotlib ipython ipython-notebook python-pandas \
python-sympy python-nose -y')
self._ssh('sudo pip install scikit-learn')
self._ssh('sudo pip install bokeh')
self.deploy(infile)
def update_analytics_node(self, infile):
self.deploy(infile)
def update_axol_node(self, infile):
self.deploy(infile)
self.install_config_files()
def install_axol_node(self, infile):
self.create_directories()
self.install_packages()
#self.setup_rabbitmq()
self.install_python_packages()
self.update_apt()
self.create_user()
self.deploy(infile)
self.install_config_files()
def create_directories(self):
print 'CREATING DIRECTORIES ###############'
try:
self._ssh('sudo touch /home/test')
self._ssh('sudo mkdir /opt/AXOL_Management')
self._ssh('sudo mkdir /opt/AXOL_Management/AXOL')
except Exception, e:
print e
def install_packages(self):
print 'INSTALLING PACKAGES ################'
try:
self._ssh('sudo apt-get update')
self._ssh('echo "deb http://www.rabbitmq.com/debian/ testing main" >> /etc/apt/sources.list')
self._ssh('sudo apt-get install apache2 -y')
self._ssh('sudo apt-get install dos2unix -y')
self._ssh('sudo apt-get install apt-show-versions -y')
self._ssh('sudo apt-get install htop -y')
self._ssh('sudo a2enmod ssl')
self._ssh('sudo aptitude install libapache2-mod-wsgi -y')
self._ssh('sudo apt-get install python-dev -y')
except Exception, e:
print e
print 'INSTALLING PACKAGES RMQ #############'
try:
self._ssh('sudo aptitude install rabbitmq-server -y')
except Exception, e:
print e
def setup_rabbitmq(self):
print 'CONFIGURING RABBITMQ ###############'
try:
self._ssh('sudo apt-get update')
self._ssh('echo "deb http://www.rabbitmq.com/debian/ testing main" >> /etc/apt/sources.list')
self._ssh('sudo aptitude install rabbitmq-server -y')
self._ssh('touch /var/lib/rabbitmq/.erlang.cookie')
self._ssh('chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie')
self._ssh('sudo echo "XUNTWTQPUMITKLDZYPAY" > /var/lib/rabbitmq/.erlang.cookie')
self._ssh('service rabbitmq-server start')
self._ssh('sudo rabbitmqctl add_user celery jgh8&hbJVk56')
self._ssh('sudo rabbitmqctl add_vhost celery')
self._ssh('sudo rabbitmqctl set_permissions -p celery celery ".*" ".*" ".*"')
except Exception, e:
print e
def install_python_packages(self):
print 'INSTALLING PYTHON PACKAGES #########'
try:
#self._ssh('sudo apt-get install python-pip -y')
self._ssh('sudo pip install elasticsearch')
self._ssh('sudo pip install celery==3.1.16')
self._ssh('sudo pip install lmdb')
self._ssh('pip install cassandra-driver')
#self._ssh('sudo pip install librabbitmq')
#self._ssh('sudo pip install requests')
#self._ssh('sudo pip install redis')
except Exception, e:
print e
def update_apt(self):
print 'COMPLETING PACKAGE INSTALLATION ####'
self._ssh('sudo apt-get update')
def create_user(self):
print 'CREATING USERS #####################'
self._ssh('sudo useradd axol -G www-data')
def install_config_files(self):
print 'INSTALLING CONFIG FILES ############'
try:
self._ssh('service apache2 stop')
self._ssh('sudo rm -f /etc/apache2/sites-available/000-default.conf')
self._ssh('sudo rm -f /etc/apache2/sites-enabled/000-default.conf')
self._ssh('sudo rm -f /etc/apache2/sites-enabled/000-default')
time.sleep(1)
self._ssh('sudo cp /opt/AXOL_Management/AXOL/axol_node/server_config/default /etc/apache2/sites-available/default')
self._ssh('sudo ln -s /etc/apache2/sites-available/default /etc/apache2/sites-enabled/000-default.conf')
self._ssh('sudo cp /opt/AXOL_Management/AXOL/axol_node/server_config/celeryd.sh /etc/init.d/celeryd')
print 'INSTALLING /ETC/DEFAULT'
self._ssh('sudo cp /opt/AXOL_Management/AXOL/axol_node/server_config/etc_default/celeryd.sh /etc/default/celeryd')
#self._ssh('sudo dos2unix /etc/default/celeryd')
self._ssh('sudo chown axol:axol /var/log/celery; sudo chown axol:axol /var/run/celery')
self._ssh('sudo ln -s /opt/AXOL_Management/AXOL/axol_node/axol_start.sh /home/axol_start')
#self._ssh('sudo chmod 777 /opt/AXOL_Management/AXOL/axol_node/tmp/*')
except Exception, e:
print e
def install_axol_scheduler(self, infile):
print 'CREATING DIRECTORIES ###############'
self.create_directories()
print 'INSTALLING PACKAGES ################'
self._ssh('sudo apt-get update')
self._ssh('sudo apt-get install python-dev -y')
self._ssh('sudo apt-get install python-pip -y')
self._ssh('sudo apt-get install apache2 -y')
self._ssh('sudo a2enmod ssl')
self._ssh('sudo aptitude install libapache2-mod-wsgi -y')
print 'INSTALLING PYTHON PACKAGES #########'
self._ssh('sudo pip install boto fabric flask flower')
print 'COMPLETING INSTALLATION ############'
self._ssh('sudo apt-get update')
self.deploy(infile)
self.install_axol_scheduler_config()
def install_axol_scheduler_config(self):
print 'INSTALLING CONFIG FILES ############'
self._ssh('service apache2 stop')
self._ssh('sudo rm -f /etc/apache2/sites-available/000-default.conf')
self._ssh('sudo rm -f /etc/apache2/sites-enabled/000-default.conf')
self._ssh('sudo cp /opt/AXOL_Management/AXOL/axol_scheduler/server_config/default /etc/apache2/sites-available/default')
self._ssh('sudo ln -s /etc/apache2/sites-available/default /etc/apache2/sites-enabled/000-default.conf')
self._ssh('service apache2 start')
def deploy(self, infile):
command = 'tar -zcvf pfmon.tar %s' % infile
subprocess.Popen(
command,
shell=True
)
print 'Starting Deploy ####################'
time.sleep(2)
print 'SFTP To Remote #####################'
self._sftp(
'pfmon.tar',
'/home/%s/pfmon.tar' % self.remote_user
)
print 'SSH To Remote ######################'
print 'Copy TAR ###########################'
self._ssh('sudo cp /home/%s/pfmon.tar /opt/AXOL_Management/AXOL/pfmon.tar' % self.remote_user)
self._ssh('cd /opt/AXOL_Management/AXOL/; sudo tar -zxvf pfmon.tar')
self._ssh('sudo rm -f /opt/AXOL_Management/AXOL/pfmon.tar')
print 'Completed Deploy ###################'
def build_agent_package(self, infile):
print 'Building Archive ###################'
command = 'tar -zcvf agent.tar %s' % infile
subprocess.Popen(
command,
shell=True
)
time.sleep(0.5)
def deploy_agent(self, infile, name):
print 'SFTP To Remote #####################'
self._sftp(
'agent.tar',
'/home/%s/agent.tar' % self.remote_user
)
#sudo rm -f agent.tar; \
print 'Create Base Folder #################'
self._ssh('sudo mkdir -p /opt/AXOL_Management/AXOL')
print 'Copy Archive To Base Folder ########'
self._ssh('sudo cp /home/%s/agent.tar /opt/AXOL_Management/AXOL/agent.tar' % self.remote_user)
print 'Unpack Archive #####################'
self._ssh('cd /opt/AXOL_Management/AXOL; sudo tar -zxvf agent.tar')
print 'Deploy Init Script #################'
self._ssh('sudo cp /opt/AXOL_Management/AXOL/%s/%s_init.sh /etc/init.d/%s' % (name,name,name))
print 'Set Agent Permissions ##############'
self._ssh('sudo chmod 755 /etc/init.d/%s' % name)
print 'Rename Executable ##################'
self._ssh('sudo mv /opt/AXOL_Management/AXOL/%s/%s.py /opt/AXOL_Management/AXOL/%s/%s' % (name,name,name,name))
print 'Updating RC ########################'
self._ssh('sudo update-rc.d %s defaults' % name)
self._ssh('sudo ln -s /etc/init.d/%s /sbin/%s' % (name,name))
print 'Agent Installed ####################'
def restart_agent(self, name):
try:
self._ssh('sudo service %s stop' % name)
time.sleep(1)
self._ssh('sudo service %s start' % name)
print 'Agent Rebooted #####################'
except Exception, e:
print e
def deploy_research(self, infile):
print 'Building Archive ###################'
command = 'tar -zcvf research.tar %s' % infile
subprocess.Popen(
command,
shell=True
)
time.sleep(0.5)
print 'SFTP To Remote #####################'
self._sftp(
'research.tar',
'/home/%s/research.tar' % self.remote_user
)
#sudo rm -f agent.tar; \
print 'Create Base Folder #################'
self._ssh('sudo mkdir /opt/AXOL_Management')
self._ssh('sudo mkdir /opt/AXOL_Management/AXOL')
print 'Copy Archive To Base Folder ########'
self._ssh('sudo cp /home/%s/research.tar /opt/AXOL_Management/AXOL/research.tar' % self.remote_user)
print 'Unpack Archive #####################'
self._ssh('cd /opt/AXOL_Management/AXOL; sudo tar -zxvf research.tar')
print 'Setting Permissions ################'
self._ssh('sudo chmod 777 /opt/AXOL_Management/AXOL/research/agent\ research\ code/socket_test_client.py')
print 'Remove Archive #####################'
self._ssh('sudo rm -f /opt/AXOL_Management/AXOL/research.tar')
def test_deploy(self, infile):
print 'SFTP To Remote #####################'
self._sftp(
'pfmon.tar',
'/home/%s/pfmon.tar' % self.remote_user
)
def _ssh(self, command):
stdin, stdout, stderr = self.client.exec_command(command)
ssh_output = stdout.read()
ssh_error = stderr.read()
return (ssh_output, ssh_error)
def _sftp(self, infile, outfile):
sftp = self.client.open_sftp()
sftp.put(
infile,
outfile
)
remote_server = '<remote-ip>'
remote_user = 'deploy-user'
remote_password = 'deploy-pass'
DT = DeployTool(
remote_user,
remote_password,
remote_server
)
folder_to_be_archived = 'axol_node'
servers = ['<remote-ip>']
for server in servers:
print 'Deploying Node To: %s' % server
DT = DeployTool(
remote_user,
remote_password,
server
)
#DT.install_axol_node(folder_to_be_archived)
DT.update_axol_node(folder_to_be_archived)
DT.deploy('axol_common')
DT.restart_node()
#DT.install_config_files()
#DT.setup_rabbitmq()
print 'FINISHED NODE DEPLOY'
#data['response']['value'][item][item_2]
#output = DT.deploy(folder_to_be_archived)
#output = DT.test_deploy(folder_to_be_archived)
# analytics
remote_server = '<remote-ip>'
folder_to_be_archived = 'axol_analytics'
DT = DeployTool(
remote_user,
remote_password,
remote_server
)
#DT.install_analytics_node(folder_to_be_archived)
DT.update_analytics_node(folder_to_be_archived)
DT.deploy('axol_common')
'''
# research
remote_server = '<remote-ip>'
folder_to_be_archived = 'axol_scheduler'
DT = DeployTool(
remote_user,
remote_password,
remote_server
)
#DT.install_axol_scheduler(folder_to_be_archived)
DT.deploy('axol_common')
DT.deploy(folder_to_be_archived)
'''
'''
remote_server = '<remote-ip>'
folder_to_be_archived = 'axol_api_tests'
DT = DeployTool(
remote_user,
remote_password,
remote_server
)
#DT.install_axol_scheduler(folder_to_be_archived)
DT.deploy('axol_common')
DT.deploy(folder_to_be_archived)
'''
'''
#DT.install_axol_scheduler_config()
#output = DT.deploy(folder_to_be_archived)
print 'FINISHED SCHEDULER DEPLOY'
'''
'''
remote_servers = [
'<remote-ip>',
'<remote-ip>'
]
'''
folder_to_be_archived = 'axol_agent'
DT.build_agent_package(folder_to_be_archived)
servers = ['<remote-ip>']
print 'Starting'
for server in servers:
print 'Deploying Agent To: %s' % server
DT = DeployTool(
remote_user,
remote_password,
server
)
#DT.deploy_agent(folder_to_be_archived,'axol_test_processor')
DT.deploy_agent(folder_to_be_archived, 'axol_agent')
DT.deploy('axol_common')
#DT.restart_agent('axol_agent')
'''
folder_to_be_archived = 'cassandra_agent'
DT.build_agent_package(folder_to_be_archived)
servers = ['<remote-ip>']
for server in servers:
print 'Deploying Agent To: %s' % server
DT = DeployTool(
remote_user,
remote_password,
server
)
#DT.deploy_agent(folder_to_be_archived,'axol_test_processor')
DT.deploy_agent(folder_to_be_archived, 'cassandra_agent')
#DT.restart_agent('axol_agent')
#print output
'''
'''
#remote_server = '<remote-ip>'
folder_to_be_archived = 'research'
DT = DeployTool(
remote_user,
remote_password,
remote_server
)
'''
#DT.deploy_research(folder_to_be_archived)