blob: 5761327428d30ee1d87a24b21511d15947cea99a [file] [log] [blame]
Sapan Bhatia88181f22014-11-19 15:25:38 -05001#!/usr/bin/python
2import jinja2
3import tempfile
4import os
5import json
Sapan810240c2014-12-02 23:50:37 -05006import pdb
7import string
8import random
Sapan Bhatia88181f22014-11-19 15:25:38 -05009
10try:
11 step_dir = Config().observer_steps_dir
Sapan810240c2014-12-02 23:50:37 -050012 sys_dir = Config().observer_sys_dir
Sapan Bhatia88181f22014-11-19 15:25:38 -050013except:
14 step_dir = '/opt/planetstack/observer/steps'
Sapan810240c2014-12-02 23:50:37 -050015 sys_dir = '/opt/opencloud'
Sapan Bhatia88181f22014-11-19 15:25:38 -050016
17os_template_loader = jinja2.FileSystemLoader( searchpath=step_dir)
18os_template_env = jinja2.Environment(loader=os_template_loader)
19
20def parse_output(msg):
21 lines = msg.splitlines()
22 results = []
23 print msg
24
25 for l in lines:
Sapan810240c2014-12-02 23:50:37 -050026 magic_str = 'ok: [127.0.0.1] => '
27 magic_str2 = 'changed: [127.0.0.1] => '
Sapan Bhatia88181f22014-11-19 15:25:38 -050028 if (l.startswith(magic_str)):
Sapan810240c2014-12-02 23:50:37 -050029 w = len(magic_str)
30 str = l[w:]
31 d = json.loads(str)
32 results.append(d)
33 elif (l.startswith(magic_str2)):
34 w = len(magic_str2)
35 str = l[w:]
36 d = json.loads(str)
37 results.append(d)
Sapan Bhatia88181f22014-11-19 15:25:38 -050038
39
40 return results
Sapan810240c2014-12-02 23:50:37 -050041
42def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
43 return ''.join(random.choice(chars) for _ in range(size))
44
45def run_template(name, opts,path=''):
Sapan Bhatia88181f22014-11-19 15:25:38 -050046 template = os_template_env.get_template(name)
47 buffer = template.render(opts)
Sapan810240c2014-12-02 23:50:37 -050048
Sapan810240c2014-12-02 23:50:37 -050049
50 #f = open('/tmp/obsans','w')
51 try:
52 objname = opts['ansible_tag']
53 except:
54 objname= id_generator()
55 os.system('mkdir -p %s'%'/'.join([sys_dir,path]))
56 fqp = '/'.join([sys_dir,path,objname])
57
58
59 f = open(fqp,'w')
Sapan Bhatia88181f22014-11-19 15:25:38 -050060 f.write(buffer)
61 f.flush()
Sapan810240c2014-12-02 23:50:37 -050062
Sapan810240c2014-12-02 23:50:37 -050063 run = os.popen('/opt/planetstack/observer/run_ansible '+fqp)
Sapan Bhatia88181f22014-11-19 15:25:38 -050064 msg = run.read()
65 status = run.close()
66
67 try:
Sapan810240c2014-12-02 23:50:37 -050068 ok_results = parse_output(msg)
Sapan Bhatia88181f22014-11-19 15:25:38 -050069 except ValueError,e:
Sapan Bhatiab2cef882014-12-21 02:33:13 -050070 all_fatal = re.findall(r'^msg: (.*)',msg,re.MULTILINE)
71 all_fatal2 = re.findall(r'^ERROR: (.*)',msg,re.MULTILINE)
72
73
74 all_fatal.extend(all_fatal2)
75 try:
76 error = ' // '.join(all_fatal)
77 except:
78 pass
79 raise Exception(error)
80
Sapan Bhatia88181f22014-11-19 15:25:38 -050081 return ok_results
82
83def main():
Sapan810240c2014-12-02 23:50:37 -050084 run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/",
85 "name" : "Sapan Bhatia",
86 "email": "gwsapan@gmail.com",
87 "password": "foobar",
88 "admin_user":"admin",
89 "admin_password":"6a789bf69dd647e2",
90 "admin_tenant":"admin",
91 "tenant":"demo",
92 "roles":['user','admin'] })