blob: 0459cb961420efb59f1f0af02a3f6d774f7c821a [file] [log] [blame]
S.Çağlar Onura95895d2015-02-09 13:34:11 -05001#!/usr/bin/env python
Sapan Bhatiad2c59152014-11-19 15:25:38 -05002import 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
Scott Bakerd9e01232015-02-04 16:59:45 -080011# XXX hardcoded path
12# is there any reason why we aren't importing planetstack.config ?
Scott Baker0c550f72015-02-04 21:50:08 -080013XOS_DIR="/opt/xos"
Scott Bakerd9e01232015-02-04 16:59:45 -080014
Sapan Bhatiad2c59152014-11-19 15:25:38 -050015try:
16 step_dir = Config().observer_steps_dir
Sapan01cf3312014-12-02 23:50:37 -050017 sys_dir = Config().observer_sys_dir
Sapan Bhatiad2c59152014-11-19 15:25:38 -050018except:
Scott Bakerd9e01232015-02-04 16:59:45 -080019 step_dir = XOS_DIR + '/observer/steps'
Sapan01cf3312014-12-02 23:50:37 -050020 sys_dir = '/opt/opencloud'
Sapan Bhatiad2c59152014-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:
Sapan01cf3312014-12-02 23:50:37 -050031 magic_str = 'ok: [127.0.0.1] => '
32 magic_str2 = 'changed: [127.0.0.1] => '
Sapan Bhatiad2c59152014-11-19 15:25:38 -050033 if (l.startswith(magic_str)):
Sapan01cf3312014-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 Bhatiad2c59152014-11-19 15:25:38 -050043
44
45 return results
Sapan01cf3312014-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 Bhatia76fd1912015-01-23 16:15:37 +000050def shellquote(s):
51 return "'" + s.replace("'", "'\\''") + "'"
52
53def run_template(name, opts,path='', expected_num=None):
Sapan Bhatiad2c59152014-11-19 15:25:38 -050054 template = os_template_env.get_template(name)
55 buffer = template.render(opts)
Sapan01cf3312014-12-02 23:50:37 -050056
Sapan01cf3312014-12-02 23:50:37 -050057 try:
58 objname = opts['ansible_tag']
59 except:
60 objname= id_generator()
Sapan Bhatia76fd1912015-01-23 16:15:37 +000061
Sapan01cf3312014-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 Bhatiad2c59152014-11-19 15:25:38 -050067 f.write(buffer)
68 f.flush()
Sapan01cf3312014-12-02 23:50:37 -050069
Scott Baker596397c2015-02-04 22:47:45 -080070 run = os.popen(XOS_DIR + '/observer/run_ansible %s'%shellquote(fqp))
Sapan Bhatia6ac96b12015-01-23 16:16:27 +000071 #run = os.popen('ansible-playbook -v %s'%shellquote(fqp))
Sapan Bhatiad2c59152014-11-19 15:25:38 -050072 msg = run.read()
73 status = run.close()
74
75 try:
Sapan01cf3312014-12-02 23:50:37 -050076 ok_results = parse_output(msg)
Sapan Bhatia76fd1912015-01-23 16:15:37 +000077 if (len(ok_results) != expected_num):
78 raise ValueError('Unexpected num')
Sapan Bhatiad2c59152014-11-19 15:25:38 -050079 except ValueError,e:
Sapan Bhatia6255f822014-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 Bhatiad2c59152014-11-19 15:25:38 -050091 return ok_results
92
93def main():
Sapan01cf3312014-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'] })