blob: 41dc6f0cabceb5936b4a3b15a6659c9883b39387 [file] [log] [blame]
Sapan Bhatiad2c59152014-11-19 15:25:38 -05001#!/usr/bin/python
2import jinja2
3import tempfile
4import os
5import json
Sapan01cf3312014-12-02 23:50:37 -05006import pdb
7import string
8import random
Sapan Bhatia76fd1912015-01-23 16:15:37 +00009import re
Sapan Bhatiad2c59152014-11-19 15:25:38 -050010
11try:
12 step_dir = Config().observer_steps_dir
Sapan01cf3312014-12-02 23:50:37 -050013 sys_dir = Config().observer_sys_dir
Sapan Bhatiad2c59152014-11-19 15:25:38 -050014except:
15 step_dir = '/opt/planetstack/observer/steps'
Sapan01cf3312014-12-02 23:50:37 -050016 sys_dir = '/opt/opencloud'
Sapan Bhatiad2c59152014-11-19 15:25:38 -050017
18os_template_loader = jinja2.FileSystemLoader( searchpath=step_dir)
19os_template_env = jinja2.Environment(loader=os_template_loader)
20
21def parse_output(msg):
22 lines = msg.splitlines()
23 results = []
24 print msg
25
26 for l in lines:
Sapan01cf3312014-12-02 23:50:37 -050027 magic_str = 'ok: [127.0.0.1] => '
28 magic_str2 = 'changed: [127.0.0.1] => '
Sapan Bhatiad2c59152014-11-19 15:25:38 -050029 if (l.startswith(magic_str)):
Sapan01cf3312014-12-02 23:50:37 -050030 w = len(magic_str)
31 str = l[w:]
32 d = json.loads(str)
33 results.append(d)
34 elif (l.startswith(magic_str2)):
35 w = len(magic_str2)
36 str = l[w:]
37 d = json.loads(str)
38 results.append(d)
Sapan Bhatiad2c59152014-11-19 15:25:38 -050039
40
41 return results
Sapan01cf3312014-12-02 23:50:37 -050042
43def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
44 return ''.join(random.choice(chars) for _ in range(size))
45
Sapan Bhatia76fd1912015-01-23 16:15:37 +000046def shellquote(s):
47 return "'" + s.replace("'", "'\\''") + "'"
48
49def run_template(name, opts,path='', expected_num=None):
Sapan Bhatiad2c59152014-11-19 15:25:38 -050050 template = os_template_env.get_template(name)
51 buffer = template.render(opts)
Sapan01cf3312014-12-02 23:50:37 -050052
Sapan01cf3312014-12-02 23:50:37 -050053 try:
54 objname = opts['ansible_tag']
55 except:
56 objname= id_generator()
Sapan Bhatia76fd1912015-01-23 16:15:37 +000057
Sapan01cf3312014-12-02 23:50:37 -050058 os.system('mkdir -p %s'%'/'.join([sys_dir,path]))
59 fqp = '/'.join([sys_dir,path,objname])
60
61
62 f = open(fqp,'w')
Sapan Bhatiad2c59152014-11-19 15:25:38 -050063 f.write(buffer)
64 f.flush()
Sapan01cf3312014-12-02 23:50:37 -050065
Sapan Bhatia6ac96b12015-01-23 16:16:27 +000066 run = os.popen('/opt/planetstack/observer/run_ansible %s'%shellquote(fqp))
67 #run = os.popen('ansible-playbook -v %s'%shellquote(fqp))
Sapan Bhatiad2c59152014-11-19 15:25:38 -050068 msg = run.read()
69 status = run.close()
70
71 try:
Sapan01cf3312014-12-02 23:50:37 -050072 ok_results = parse_output(msg)
Sapan Bhatia76fd1912015-01-23 16:15:37 +000073 if (len(ok_results) != expected_num):
74 raise ValueError('Unexpected num')
Sapan Bhatiad2c59152014-11-19 15:25:38 -050075 except ValueError,e:
Sapan Bhatia6255f822014-12-21 02:33:13 -050076 all_fatal = re.findall(r'^msg: (.*)',msg,re.MULTILINE)
77 all_fatal2 = re.findall(r'^ERROR: (.*)',msg,re.MULTILINE)
78
79
80 all_fatal.extend(all_fatal2)
81 try:
82 error = ' // '.join(all_fatal)
83 except:
84 pass
85 raise Exception(error)
86
Sapan Bhatiad2c59152014-11-19 15:25:38 -050087 return ok_results
88
89def main():
Sapan01cf3312014-12-02 23:50:37 -050090 run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/",
91 "name" : "Sapan Bhatia",
92 "email": "gwsapan@gmail.com",
93 "password": "foobar",
94 "admin_user":"admin",
95 "admin_password":"6a789bf69dd647e2",
96 "admin_tenant":"admin",
97 "tenant":"demo",
98 "roles":['user','admin'] })