Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | import jinja2 |
| 3 | import tempfile |
| 4 | import os |
| 5 | import json |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 6 | import pdb |
| 7 | import string |
| 8 | import random |
Sapan Bhatia | 58da661 | 2015-01-23 16:15:37 +0000 | [diff] [blame] | 9 | import re |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 10 | |
Scott Baker | 7069c16 | 2015-02-04 16:59:45 -0800 | [diff] [blame] | 11 | # XXX hardcoded path |
| 12 | # is there any reason why we aren't importing planetstack.config ? |
Scott Baker | 06116da | 2015-02-04 21:50:08 -0800 | [diff] [blame] | 13 | XOS_DIR="/opt/xos" |
Scott Baker | 7069c16 | 2015-02-04 16:59:45 -0800 | [diff] [blame] | 14 | |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 15 | try: |
| 16 | step_dir = Config().observer_steps_dir |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 17 | sys_dir = Config().observer_sys_dir |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 18 | except: |
Scott Baker | 7069c16 | 2015-02-04 16:59:45 -0800 | [diff] [blame] | 19 | step_dir = XOS_DIR + '/observer/steps' |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 20 | sys_dir = '/opt/opencloud' |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 21 | |
| 22 | os_template_loader = jinja2.FileSystemLoader( searchpath=step_dir) |
| 23 | os_template_env = jinja2.Environment(loader=os_template_loader) |
| 24 | |
| 25 | def parse_output(msg): |
| 26 | lines = msg.splitlines() |
| 27 | results = [] |
| 28 | print msg |
| 29 | |
| 30 | for l in lines: |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 31 | magic_str = 'ok: [127.0.0.1] => ' |
| 32 | magic_str2 = 'changed: [127.0.0.1] => ' |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 33 | if (l.startswith(magic_str)): |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 34 | 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 Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 43 | |
| 44 | |
| 45 | return results |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 46 | |
| 47 | def id_generator(size=6, chars=string.ascii_uppercase + string.digits): |
| 48 | return ''.join(random.choice(chars) for _ in range(size)) |
| 49 | |
Sapan Bhatia | 58da661 | 2015-01-23 16:15:37 +0000 | [diff] [blame] | 50 | def shellquote(s): |
| 51 | return "'" + s.replace("'", "'\\''") + "'" |
| 52 | |
| 53 | def run_template(name, opts,path='', expected_num=None): |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 54 | template = os_template_env.get_template(name) |
| 55 | buffer = template.render(opts) |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 56 | |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 57 | try: |
| 58 | objname = opts['ansible_tag'] |
| 59 | except: |
| 60 | objname= id_generator() |
Sapan Bhatia | 58da661 | 2015-01-23 16:15:37 +0000 | [diff] [blame] | 61 | |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 62 | os.system('mkdir -p %s'%'/'.join([sys_dir,path])) |
| 63 | fqp = '/'.join([sys_dir,path,objname]) |
| 64 | |
| 65 | |
| 66 | f = open(fqp,'w') |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 67 | f.write(buffer) |
| 68 | f.flush() |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 69 | |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 70 | run = os.popen(XOS_DIR + '/observer/run_ansible %s'%shellquote(fqp)) |
Sapan Bhatia | 02fd756 | 2015-01-23 16:16:27 +0000 | [diff] [blame] | 71 | #run = os.popen('ansible-playbook -v %s'%shellquote(fqp)) |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 72 | msg = run.read() |
| 73 | status = run.close() |
| 74 | |
| 75 | try: |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 76 | ok_results = parse_output(msg) |
Sapan Bhatia | 58da661 | 2015-01-23 16:15:37 +0000 | [diff] [blame] | 77 | if (len(ok_results) != expected_num): |
| 78 | raise ValueError('Unexpected num') |
Sapan Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 79 | except ValueError,e: |
Sapan Bhatia | b2cef88 | 2014-12-21 02:33:13 -0500 | [diff] [blame] | 80 | 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 Bhatia | 88181f2 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 91 | return ok_results |
| 92 | |
| 93 | def main(): |
Sapan | 810240c | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 94 | 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'] }) |