blob: 0459cb961420efb59f1f0af02a3f6d774f7c821a [file] [log] [blame]
S.Çağlar Onur3e92b4d2015-02-09 13:34:11 -05001#!/usr/bin/env python
Sapan Bhatia88181f22014-11-19 15:25:38 -05002import jinja2
3import tempfile
4import os
5import json
Sapan810240c2014-12-02 23:50:37 -05006import pdb
7import string
8import random
Sapan Bhatia58da6612015-01-23 16:15:37 +00009import re
Sapan Bhatia88181f22014-11-19 15:25:38 -050010
Scott Baker7069c162015-02-04 16:59:45 -080011# XXX hardcoded path
12# is there any reason why we aren't importing planetstack.config ?
Scott Baker06116da2015-02-04 21:50:08 -080013XOS_DIR="/opt/xos"
Scott Baker7069c162015-02-04 16:59:45 -080014
Sapan Bhatia88181f22014-11-19 15:25:38 -050015try:
16 step_dir = Config().observer_steps_dir
Sapan810240c2014-12-02 23:50:37 -050017 sys_dir = Config().observer_sys_dir
Sapan Bhatia88181f22014-11-19 15:25:38 -050018except:
Scott Baker7069c162015-02-04 16:59:45 -080019 step_dir = XOS_DIR + '/observer/steps'
Sapan810240c2014-12-02 23:50:37 -050020 sys_dir = '/opt/opencloud'
Sapan Bhatia88181f22014-11-19 15:25:38 -050021
22os_template_loader = jinja2.FileSystemLoader( searchpath=step_dir)
23os_template_env = jinja2.Environment(loader=os_template_loader)
24
25def parse_output(msg):
26 lines = msg.splitlines()
27 results = []
28 print msg
29
30 for l in lines:
Sapan810240c2014-12-02 23:50:37 -050031 magic_str = 'ok: [127.0.0.1] => '
32 magic_str2 = 'changed: [127.0.0.1] => '
Sapan Bhatia88181f22014-11-19 15:25:38 -050033 if (l.startswith(magic_str)):
Sapan810240c2014-12-02 23:50:37 -050034 w = len(magic_str)
35 str = l[w:]
36 d = json.loads(str)
37 results.append(d)
38 elif (l.startswith(magic_str2)):
39 w = len(magic_str2)
40 str = l[w:]
41 d = json.loads(str)
42 results.append(d)
Sapan Bhatia88181f22014-11-19 15:25:38 -050043
44
45 return results
Sapan810240c2014-12-02 23:50:37 -050046
47def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
48 return ''.join(random.choice(chars) for _ in range(size))
49
Sapan Bhatia58da6612015-01-23 16:15:37 +000050def shellquote(s):
51 return "'" + s.replace("'", "'\\''") + "'"
52
53def run_template(name, opts,path='', expected_num=None):
Sapan Bhatia88181f22014-11-19 15:25:38 -050054 template = os_template_env.get_template(name)
55 buffer = template.render(opts)
Sapan810240c2014-12-02 23:50:37 -050056
Sapan810240c2014-12-02 23:50:37 -050057 try:
58 objname = opts['ansible_tag']
59 except:
60 objname= id_generator()
Sapan Bhatia58da6612015-01-23 16:15:37 +000061
Sapan810240c2014-12-02 23:50:37 -050062 os.system('mkdir -p %s'%'/'.join([sys_dir,path]))
63 fqp = '/'.join([sys_dir,path,objname])
64
65
66 f = open(fqp,'w')
Sapan Bhatia88181f22014-11-19 15:25:38 -050067 f.write(buffer)
68 f.flush()
Sapan810240c2014-12-02 23:50:37 -050069
Scott Bakera8b263b2015-02-04 22:47:45 -080070 run = os.popen(XOS_DIR + '/observer/run_ansible %s'%shellquote(fqp))
Sapan Bhatia02fd7562015-01-23 16:16:27 +000071 #run = os.popen('ansible-playbook -v %s'%shellquote(fqp))
Sapan Bhatia88181f22014-11-19 15:25:38 -050072 msg = run.read()
73 status = run.close()
74
75 try:
Sapan810240c2014-12-02 23:50:37 -050076 ok_results = parse_output(msg)
Sapan Bhatia58da6612015-01-23 16:15:37 +000077 if (len(ok_results) != expected_num):
78 raise ValueError('Unexpected num')
Sapan Bhatia88181f22014-11-19 15:25:38 -050079 except ValueError,e:
Sapan Bhatiab2cef882014-12-21 02:33:13 -050080 all_fatal = re.findall(r'^msg: (.*)',msg,re.MULTILINE)
81 all_fatal2 = re.findall(r'^ERROR: (.*)',msg,re.MULTILINE)
82
83
84 all_fatal.extend(all_fatal2)
85 try:
86 error = ' // '.join(all_fatal)
87 except:
88 pass
89 raise Exception(error)
90
Sapan Bhatia88181f22014-11-19 15:25:38 -050091 return ok_results
92
93def main():
Sapan810240c2014-12-02 23:50:37 -050094 run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/",
95 "name" : "Sapan Bhatia",
96 "email": "gwsapan@gmail.com",
97 "password": "foobar",
98 "admin_user":"admin",
99 "admin_password":"6a789bf69dd647e2",
100 "admin_tenant":"admin",
101 "tenant":"demo",
102 "roles":['user','admin'] })