blob: 8570c17292b65a8ae1c8cf42720dd696642fbe82 [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 Bhatiad2c59152014-11-19 15:25:38 -05009
10try:
11 step_dir = Config().observer_steps_dir
Sapan01cf3312014-12-02 23:50:37 -050012 sys_dir = Config().observer_sys_dir
Sapan Bhatiad2c59152014-11-19 15:25:38 -050013except:
14 step_dir = '/opt/planetstack/observer/steps'
Sapan01cf3312014-12-02 23:50:37 -050015 sys_dir = '/opt/opencloud'
Sapan Bhatiad2c59152014-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:
Sapan01cf3312014-12-02 23:50:37 -050026 magic_str = 'ok: [127.0.0.1] => '
27 magic_str2 = 'changed: [127.0.0.1] => '
Sapan Bhatiad2c59152014-11-19 15:25:38 -050028 if (l.startswith(magic_str)):
Sapan01cf3312014-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 Bhatiad2c59152014-11-19 15:25:38 -050038
39
40 return results
Sapan01cf3312014-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 Bhatiad2c59152014-11-19 15:25:38 -050046 template = os_template_env.get_template(name)
47 buffer = template.render(opts)
Sapan01cf3312014-12-02 23:50:37 -050048
49 import pdb
50
51 #f = open('/tmp/obsans','w')
52 try:
53 objname = opts['ansible_tag']
54 except:
55 objname= id_generator()
56 os.system('mkdir -p %s'%'/'.join([sys_dir,path]))
57 fqp = '/'.join([sys_dir,path,objname])
58
59
60 f = open(fqp,'w')
Sapan Bhatiad2c59152014-11-19 15:25:38 -050061 f.write(buffer)
62 f.flush()
Sapan01cf3312014-12-02 23:50:37 -050063
Sapan4f075cc2014-12-03 01:04:25 -050064 #os.system('cp %s %s-backup'%(fqp,fqp))
Sapan01cf3312014-12-02 23:50:37 -050065 run = os.popen('/opt/planetstack/observer/run_ansible '+fqp)
Sapan Bhatiad2c59152014-11-19 15:25:38 -050066 msg = run.read()
67 status = run.close()
68
69 try:
Sapan01cf3312014-12-02 23:50:37 -050070 ok_results = parse_output(msg)
Sapan Bhatiad2c59152014-11-19 15:25:38 -050071 except ValueError,e:
Sapan01cf3312014-12-02 23:50:37 -050072 print str(e)
73 raise e
Sapan Bhatiad2c59152014-11-19 15:25:38 -050074 return ok_results
75
76def main():
Sapan01cf3312014-12-02 23:50:37 -050077 run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/",
78 "name" : "Sapan Bhatia",
79 "email": "gwsapan@gmail.com",
80 "password": "foobar",
81 "admin_user":"admin",
82 "admin_password":"6a789bf69dd647e2",
83 "admin_tenant":"admin",
84 "tenant":"demo",
85 "roles":['user','admin'] })