forked from thehyperadvisor/cldstk-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcldstk-deploy.py
More file actions
424 lines (367 loc) · 19.4 KB
/
cldstk-deploy.py
File metadata and controls
424 lines (367 loc) · 19.4 KB
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
#!/usr/bin/python
from subprocess import call,Popen, PIPE
import sys, os, commands
cloud_repl_password = 'password'
mysql_root_password = 'PaSSw0rd1234'
savedHome = os.getcwd()
userHome = os.path.expanduser('~')
eth = 'eth0'
eth_ip = commands.getoutput("ip address show dev " + eth).split()
eth_ip = eth_ip[eth_ip.index('inet') + 1].split('/')[0]
def setUp():
call(["rpm","-Uvh", "http://mirror.pnl.gov/epel/6/x86_64/epel-release-6-8.noarch.rpm"], shell=False)
call(["yum","install", "wget", "-y"], shell=False)
call(["yum","install", "python-setuptools", "-y"], shell=False)
call(["yum","install", "ansible", "-y"], shell=False)
call(["yum","install", "nodejs", "-y"], shell=False)
call(["yum","install", "npm", "-y"], shell=False)
call(["npm","update"], shell=False)
call(["npm","install", "forever", "-g"], shell=False)
call(["service","iptables", "stop"], shell=False)
call(["chkconfig","iptables", "off"], shell=False)
if os.path.isfile(userHome + "/.ssh/known_hosts") == False:
call(["mkdir ~/.ssh"], shell=True)
call(["touch ~/.ssh/known_hosts"], shell=True)
else: pass
call(["ssh-keyscan -H '127.0.0.1' >> ~/.ssh/known_hosts"], shell=True)
def getRPMS(repo):
# Download rpm packages from remote repository
if repo == '4.3':
os.chdir(savedHome + '/public/cloudstack.apt-get.eu/rhel/')
call(["rm","-rf", "4.3"], shell=False)
call(["wget","https://www.dropbox.com/sh/7fa1j6ymap1wrgu/BXfoDzUNWy/cloudstack-4.3.0-rpms.tar.gz"], shell=False)
call(["tar","-zxvf", "cloudstack-4.3.0-rpms.tar.gz"], shell=False)
call(["rm -f cloudstack-4.3.0-rpms.tar.*"], shell=True)
if repo == '4.2':
os.chdir(savedHome + '/public/cloudstack.apt-get.eu/rhel/')
call(["rm","-rf", "4.2"], shell=False)
call(["wget","https://www.dropbox.com/sh/7fa1j6ymap1wrgu/KC6chASB5H/cloudstack-4.2.1-rpms.tar.gz"], shell=False)
call(["tar","-zxvf", "cloudstack-4.2.1-rpms.tar.gz"], shell=False)
call(["rm -f cloudstack-4.2.1-rpms.tar.*"], shell=True)
os.chdir(savedHome)
def getSystemtemplate(repo):
# Download rpm packages from remote repository
if repo == '4.3':
os.chdir(savedHome + '/public/templates/4.3/')
call(["wget","http://download.cloud.com/templates/4.3/systemvm64template-2014-01-14-master-kvm.qcow2.bz2"], shell=False)
#call(["wget","http://download.cloud.com/templates/4.3/systemvm64template-2014-01-14-master-xen.vhd.bz2"], shell=False)
#call(["wget","http://download.cloud.com/templates/4.3/systemvm64template-2014-01-14-master-vmware.ova"], shell=False)
os.chdir(savedHome)
if repo == '4.2':
os.chdir(savedHome + '/public/templates/4.2/')
os.chdir('public/template/4.2/')
call(["wget","http://download.cloud.com/templates/4.2/systemvmtemplate-2013-06-12-master-kvm.qcow2.bz2"], shell=False)
os.chdir(savedHome)
def startnode():
# Check if webserver is already running. If so, kill and restart new process
chkprog = Popen(['node_modules/forever/bin/forever', 'list'], stdout=PIPE)
stdout, stderr = chkprog.communicate()
a = stdout.split('\n')
if len(a) > 2:
for l in a:
try:
id = l.split(' ')[4][1:-1]
Popen(['node_modules/forever/bin/forever', 'stop', '%s' % id], stderr=PIPE)
except: pass
print('Starting webserver........')
print ""
prog = Popen(['node_modules/forever/bin/forever', 'start', 'app.js'], stderr=PIPE)
errdata = prog.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
if len(errdata) == 0:
pass
print('')
print('Started webserver........')
print('Browse to: http://%s:3000' % eth_ip)
print('')
def stopnode():
# Check if webserver is already running. If so, kill and restart new process
chkprog = Popen(['node_modules/forever/bin/forever', 'list'], stdout=PIPE)
stdout, stderr = chkprog.communicate()
a = stdout.split('\n')
if len(a) > 2:
for l in a:
try:
id = l.split(' ')[4][1:-1]
Popen(['node_modules/forever/bin/forever', 'stop', '%s' % id], stderr=PIPE)
except: pass
print('Stopped webserver........')
else:
print('No webservers found........')
print('')
def startall():
# ansible-playbook -i ./ansible/hosts ./ansible/run-all.yml -k
start_now = Popen(['ansible-playbook', '-i', './ansible/hosts', './ansible/play-books/run-all.yml', '-k'], stderr=PIPE)
errdata = start_now.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
def startall_in_one():
# ansible-playbook -i ./ansible/hosts ./ansible/run-all.yml -k
start_now = Popen(['ansible-playbook', '-i', './ansible/hosts', './ansible/play-books/all-in-one.yml', '-k'], stderr=PIPE)
errdata = start_now.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
def runfileupdates():
# ansible-playbook -i ./ansible/hosts ./ansible/run-all.yml -k
start_now = Popen(['ansible-playbook', '-i', './ansible/hosts', './ansible/play-books/cldstk-files-update.yml', '-k'], stderr=PIPE)
errdata = start_now.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
def kvm_agent_Install():
# ansible-playbook -i ./ansible/hosts ./ansible/run-all.yml -k
start_now = Popen(['ansible-playbook', '-i', './ansible/hosts', './ansible/play-books/cldstk-agent_deploy.yml', '-k'], stderr=PIPE)
errdata = start_now.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
def management_Install():
# ansible-playbook -i ./ansible/hosts ./ansible/run-all.yml -k
start_now = Popen(['ansible-playbook', '-i', './ansible/hosts', './ansible/play-books/cldstk-mgmt_deploy.yml', '-k'], stderr=PIPE)
errdata = start_now.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
def db_Install():
# ansible-playbook -i ./ansible/hosts ./ansible/run-all.yml -k
start_now = Popen(['ansible-playbook', '-i', './ansible/hosts', './ansible/play-books/mysql-server-install.yml', '-k'], stderr=PIPE)
errdata = start_now.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
def db_replication_Install():
# ansible-playbook -i ./ansible/hosts ./ansible/run-all.yml -k
start_now = Popen(['ansible-playbook', '-i', './ansible/hosts', './ansible/play-books/mysql-replication-setup.yml', '-k'], stderr=PIPE)
errdata = start_now.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
def systemtemplate_Install():
# ansible-playbook -i ./ansible/hosts ./ansible/run-all.yml -k
start_now = Popen(['ansible-playbook', '-i', './ansible/hosts', './ansible/play-books/cldstk-preseed-kvm-systmpl.yml', '-k'], stderr=PIPE)
errdata = start_now.communicate()[1]
if len(errdata) != 0:
print errdata.strip("\n")
def main():
print ""
print "Cloudstack Deployment: Answer the questions below...."
print ""
installmysql = ''
while installmysql.lower() != 'y' and installmysql.lower() != 'n':
installmysql = raw_input('Install Primary Database Server?[Y/n]: ')
if installmysql.lower() == 'y':
db_master = raw_input('Db Server[dns/ip]: ')
else:
db_master = ''
if installmysql.lower() == 'n':
db_primary = raw_input('Whats the current DB Server?[dns/ip]: ')
else:
db_primary = ''
installmysqlreplica = ''
while installmysqlreplica.lower() != 'y' and installmysqlreplica.lower() != 'n':
installmysqlreplica = raw_input('Configure Database Replica?[Y/n]: ')
if installmysqlreplica.lower() == 'y':
db_slave = raw_input('DB Replica Server[dns/ip]: ')
else:
db_slave = ''
installmgmtsrv = ''
while installmgmtsrv.lower() != 'y' and installmgmtsrv.lower() != 'n':
installmgmtsrv = raw_input('Install Primary Management Server?[Y/n]: ')
if installmgmtsrv.lower() == 'y':
cldstk_mgmt = raw_input('Server[dns/ip]: ')
else:
cldstk_mgmt = ''
installwebsrv = ''
while installwebsrv.lower() != 'y' and installwebsrv.lower() != 'n':
installwebsrv = raw_input('Install additional Management servers?[Y/n]: ')
if installwebsrv.lower() == 'y':
cldstk_web = raw_input('Comma separated list: ')
else:
cldstk_web = ''
installkvmhost = ''
while installkvmhost.lower() != 'y' and installkvmhost.lower() != 'n':
installkvmhost = raw_input('Install KVM Hosts?[Y/n]: ')
if installkvmhost.lower() == 'y':
cldstk_kvmhost = raw_input('Comma separated list: ')
else:
cldstk_kvmhost = ''
mgmtrestart = ''
if installmysqlreplica.lower() == 'y' and installmgmtsrv.lower() == 'n' and installwebsrv.lower() == 'n':
while mgmtrestart.lower() == '':
print('Need to restart services on management servers. List them below.')
mgmtrestart = raw_input('Comma separated list: ')
else: pass
preseedtemplates = ''
while preseedtemplates.lower() != 'y' and preseedtemplates.lower() != 'n':
preseedtemplates = raw_input('Install System Templates?[Y/n]: ')
if preseedtemplates.lower() == 'y':
nfs_server = raw_input('NFS Server[dns/ip]: ')
else:
nfs_server = ''
if preseedtemplates.lower() == 'y':
nfs_path = raw_input('NFS Path[/nfsdirpath]: ')
else:
nfs_path = ''
changerepotype = ''
while changerepotype.lower() != 'y' and changerepotype.lower() != 'n':
changerepotype = raw_input('Change install type to "Internet"?[Y/n]: ')
if changerepotype.lower() == 'y':
repo_type = 'Internet'
else:
repo_type = 'Local'
changerepoversion = ''
while changerepoversion.lower() != 'y' and changerepoversion.lower() != 'n':
changerepoversion = raw_input('Change install version to "4.2"?[Y/n]: ')
if changerepoversion.lower() == 'y':
repo_version = '4.2'
else:
repo_version = '4.3'
ssh_addrsakeys = ''
systemlist = []
while ssh_addrsakeys.lower() != 'y' and ssh_addrsakeys.lower() != 'n':
ssh_addrsakeys = raw_input('Add ssh rsa keys to ~/.ssh/known_hosts?[Y/n]: ')
if ssh_addrsakeys.lower() == 'y':
addrsakeys = 'True'
else:
addrsakeys = 'False'
system_template = ''
if repo_version == '4.3' and repo_type == 'Local':
system_template = 'http://%s:3000/acs/templates/4.3/systemvm64template-2014-01-14-master-kvm.qcow2.bz2' % eth_ip
if repo_version == '4.2' and repo_type == 'Local':
system_template = 'http://%s:3000/acs/templates/4.2/systemvmtemplate-2013-06-12-master-kvm.qcow2.bz2' % eth_ip
if repo_version == '4.3' and repo_type == 'Internet':
system_template = 'ttp://download.cloud.com/templates/4.3/systemvm64template-2014-01-14-master-kvm.qcow2.bz2' % eth_ip
if repo_version == '4.2' and repo_type == 'Internet':
system_template = 'http://download.cloud.com/templates/4.2/systemvmtemplate-2013-06-12-master-kvm.qcow2.bz2' % eth_ip
# Write the /etc/ansible/hosts file
if len(db_master) != 0 or len(db_primary) !=0:
hostsfile = open('./ansible/hosts','w')
hostsfile.write('[localhost]\n127.0.0.1\n\n')
hostsfile.write('[db_master]\n')
if len(db_master) != 0:
hostsfile.write('%s\n' % db_master)
systemlist.append('%s' % db_master)
else:
hostsfile.write('%s\n' % db_primary)
systemlist.append('%s' % db_primary)
hostsfile.write('\n[db_slave]\n')
if len(db_slave) != 0:
hostsfile.write('%s\n' % db_slave)
systemlist.append('%s' % db_slave)
hostsfile.write('\n[mysql_servers]\n')
if len(db_master) != 0:
hostsfile.write('%s\n' % db_master)
systemlist.append('%s' % db_master)
if len(db_slave) != 0:
hostsfile.write('%s\n' % db_slave)
systemlist.append('%s' % db_slave)
hostsfile.write('\n[cldstk_mgmt]\n')
if len(cldstk_mgmt) != 0:
cldstk_mgmt = cldstk_mgmt.split(',')[0]
hostsfile.write('%s\n' % cldstk_mgmt)
systemlist.append('%s' % cldstk_mgmt)
hostsfile.write('\n[cldstk_web]\n')
if len(cldstk_web) != 0:
cldstk_web = cldstk_web.split(',')
for w in cldstk_web:
hostsfile.write('%s\n' % w.strip())
systemlist.append('%s' % w.strip())
hostsfile.write('\n[cldstk_kvm]\n')
if len(cldstk_kvmhost) != 0:
cldstk_kvmhost = cldstk_kvmhost.split(',')
for k in cldstk_kvmhost:
hostsfile.write('%s\n' % k.strip())
systemlist.append('%s' % k.strip())
hostsfile.write('\n[mgmt_restart]\n')
if len(mgmtrestart) != 0:
mgmt_restart = mgmtrestart.split(',')
for w in mgmt_restart:
hostsfile.write('%s\n' % w.strip())
systemlist.append('%s' % w.strip())
hostsfile.close()
print('ansible hosts file successfully writing to disk.....')
# Write the vars_file.yml file
if len(db_master) != 0:
vars_file = open('./ansible/vars_file.yml','w')
vars_file.write('mgmt_primary: %s\n' % cldstk_mgmt)
vars_file.write('master: %s\n' % db_master)
vars_file.write('slave: %s\n' % db_slave)
vars_file.write('cloud_repl_password: %s\n' % cloud_repl_password)
vars_file.write('mysql_root_password: %s\n' % mysql_root_password)
vars_file.write('nfs_server: %s\n' % nfs_server)
vars_file.write('nfs_path: %s\n' % nfs_path)
vars_file.write('repotype: %s\n' % repo_type)
vars_file.write('repoversion: %s\n' % repo_version)
vars_file.write('systemtemplate: %s\n' % system_template)
vars_file.close()
else:
vars_file = open('./ansible/vars_file.yml','w')
vars_file.write('mgmt_primary: %s\n' % cldstk_mgmt)
vars_file.write('master: %s\n' % db_primary)
vars_file.write('slave: %s\n' % db_slave)
vars_file.write('cloud_repl_password: %s\n' % cloud_repl_password)
vars_file.write('mysql_root_password: %s\n' % mysql_root_password)
vars_file.write('nfs_server: %s\n' % nfs_server)
vars_file.write('nfs_path: %s\n' % nfs_path)
vars_file.write('repotype: %s\n' % repo_type)
vars_file.write('repoversion: %s\n' % repo_version)
vars_file.write('systemtemplate: %s\n' % system_template)
vars_file.close()
print('vars_file successfully writing to disk.....')
# Add systems ssh rsa keys to ~/.ssh/known_hosts file
if addrsakeys == 'True':
uniquesys = []
for system in systemlist:
if system not in uniquesys:
uniquesys.append(system)
call(["ssh-keyscan -H '%s' >> ~/.ssh/known_hosts" % system], shell=True)
else: pass
else: pass
startinstallation = raw_input('Start installation now?[Y/n]: ')
if startinstallation.lower() == 'y':
startall()
else:
print('Exiting program......')
sys.exit()
else:
print('You must provide the Master Database Server...')
main()
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-s':
startnode()
sys.exit()
if len(sys.argv) == 2 and sys.argv[1] == '-S':
stopnode()
sys.exit()
if len(sys.argv) == 3 and sys.argv[1] == 'get':
if sys.argv[2].split('=')[0] == 'rpmversion' and len(sys.argv[2].split('=')[1]) != 0:
getRPMS('%s' % sys.argv[2].split('=')[1])
elif sys.argv[2].split('=')[0] == 'systemtemplate' and len(sys.argv[2].split('=')[1]) != 0:
getSystemtemplate('%s' % sys.argv[2].split('=')[1])
else:
print('Wrong Syntax.....')
if len(sys.argv) == 3 and sys.argv[1] == 'setup':
if sys.argv[2] == 'all':
setUp()
else:
print('Wrong Syntax.....')
if len(sys.argv) == 3 and sys.argv[1] == 'install':
startnode()
if sys.argv[2] == 'all':
startall()
elif sys.argv[2] == 'all-in-one':
startall_in_one()
elif sys.argv[2] == 'kvm-agent':
runfileupdates()
kvm_agent_Install()
elif sys.argv[2] == 'management':
runfileupdates()
management_Install()
elif sys.argv[2] == 'db':
runfileupdates()
db_Install()
elif sys.argv[2] == 'db-replication':
runfileupdates()
db_replication_Install()
elif sys.argv[2] == 'systemtemplate':
systemtemplate_Install()
sys.exit()
if len(sys.argv) == 1:
startnode()
main()