blob: 186ecabf01bca7ae54c58410fd6a038292e15ab0 [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 Bhatia5155c5a2015-04-15 13:31:12 -040010from xos.config import Config, XOS_DIR
Scott Bakerd9e01232015-02-04 16:59:45 -080011
Sapan Bhatiad2c59152014-11-19 15:25:38 -050012try:
13 step_dir = Config().observer_steps_dir
Sapan01cf3312014-12-02 23:50:37 -050014 sys_dir = Config().observer_sys_dir
Sapan Bhatiad2c59152014-11-19 15:25:38 -050015except:
Scott Bakerd9e01232015-02-04 16:59:45 -080016 step_dir = XOS_DIR + '/observer/steps'
Sapan01cf3312014-12-02 23:50:37 -050017 sys_dir = '/opt/opencloud'
Sapan Bhatiad2c59152014-11-19 15:25:38 -050018
19os_template_loader = jinja2.FileSystemLoader( searchpath=step_dir)
20os_template_env = jinja2.Environment(loader=os_template_loader)
21
22def parse_output(msg):
23 lines = msg.splitlines()
24 results = []
25 print msg
26
27 for l in lines:
Sapan01cf3312014-12-02 23:50:37 -050028 magic_str = 'ok: [127.0.0.1] => '
29 magic_str2 = 'changed: [127.0.0.1] => '
Sapan Bhatiad2c59152014-11-19 15:25:38 -050030 if (l.startswith(magic_str)):
Sapan01cf3312014-12-02 23:50:37 -050031 w = len(magic_str)
32 str = l[w:]
33 d = json.loads(str)
34 results.append(d)
35 elif (l.startswith(magic_str2)):
36 w = len(magic_str2)
37 str = l[w:]
38 d = json.loads(str)
39 results.append(d)
Sapan Bhatiad2c59152014-11-19 15:25:38 -050040
41
42 return results
Sapan01cf3312014-12-02 23:50:37 -050043
44def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
45 return ''.join(random.choice(chars) for _ in range(size))
46
Sapan Bhatia76fd1912015-01-23 16:15:37 +000047def shellquote(s):
48 return "'" + s.replace("'", "'\\''") + "'"
49
50def run_template(name, opts,path='', expected_num=None):
Sapan Bhatiad2c59152014-11-19 15:25:38 -050051 template = os_template_env.get_template(name)
52 buffer = template.render(opts)
Sapan01cf3312014-12-02 23:50:37 -050053
Sapan01cf3312014-12-02 23:50:37 -050054 try:
55 objname = opts['ansible_tag']
56 except:
57 objname= id_generator()
Sapan Bhatia76fd1912015-01-23 16:15:37 +000058
Sapan01cf3312014-12-02 23:50:37 -050059 os.system('mkdir -p %s'%'/'.join([sys_dir,path]))
60 fqp = '/'.join([sys_dir,path,objname])
61
Sapan Bhatiac269d062015-03-13 18:43:46 -040062
Sapan01cf3312014-12-02 23:50:37 -050063 f = open(fqp,'w')
Sapan Bhatiad2c59152014-11-19 15:25:38 -050064 f.write(buffer)
65 f.flush()
Sapan01cf3312014-12-02 23:50:37 -050066
Sapan Bhatiaca4939b2015-03-13 18:51:02 -040067
68 if (Config().observer_steps):
69 run = os.popen(XOS_DIR + '/observer/run_ansible %s'%shellquote(fqp))
70 msg = run.read()
71 status = run.close()
Sapan Bhatiad2c59152014-11-19 15:25:38 -050072
Sapan Bhatiaca4939b2015-03-13 18:51:02 -040073
74 else:
75 msg = open(fqp+'.out').read()
76
Sapan Bhatiac269d062015-03-13 18:43:46 -040077 try:
78 ok_results = parse_output(msg)
Sapan Bhatiaca4939b2015-03-13 18:51:02 -040079 if (len(ok_results) != expected_num):
80 raise ValueError('Unexpected num')
Sapan Bhatiac269d062015-03-13 18:43:46 -040081 except ValueError,e:
82 all_fatal = re.findall(r'^msg: (.*)',msg,re.MULTILINE)
83 all_fatal2 = re.findall(r'^ERROR: (.*)',msg,re.MULTILINE)
Sapan Bhatiaca4939b2015-03-13 18:51:02 -040084
85
86 all_fatal.extend(all_fatal2)
Sapan Bhatia6255f822014-12-21 02:33:13 -050087 try:
Sapan Bhatiac269d062015-03-13 18:43:46 -040088 error = ' // '.join(all_fatal)
89 except:
90 pass
91 raise Exception(error)
Sapan Bhatia6255f822014-12-21 02:33:13 -050092
Sapan Bhatiad2c59152014-11-19 15:25:38 -050093 return ok_results
94
95def main():
Sapan01cf3312014-12-02 23:50:37 -050096 run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/",
97 "name" : "Sapan Bhatia",
98 "email": "gwsapan@gmail.com",
99 "password": "foobar",
100 "admin_user":"admin",
101 "admin_password":"6a789bf69dd647e2",
102 "admin_tenant":"admin",
103 "tenant":"demo",
104 "roles":['user','admin'] })