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 |
| 6 | |
| 7 | try: |
| 8 | step_dir = Config().observer_steps_dir |
| 9 | except: |
| 10 | step_dir = '/opt/planetstack/observer/steps' |
| 11 | |
| 12 | os_template_loader = jinja2.FileSystemLoader( searchpath=step_dir) |
| 13 | os_template_env = jinja2.Environment(loader=os_template_loader) |
| 14 | |
| 15 | def parse_output(msg): |
| 16 | lines = msg.splitlines() |
| 17 | results = [] |
| 18 | print msg |
| 19 | |
| 20 | for l in lines: |
| 21 | magic_str = 'ok: [127.0.0.1] => ' |
| 22 | magic_str2 = 'changed: [127.0.0.1] => ' |
| 23 | if (l.startswith(magic_str)): |
| 24 | w = len(magic_str) |
| 25 | str = l[w:] |
| 26 | d = json.loads(str) |
| 27 | results.append(d) |
| 28 | elif (l.startswith(magic_str2)): |
| 29 | w = len(magic_str2) |
| 30 | str = l[w:] |
| 31 | d = json.loads(str) |
| 32 | results.append(d) |
| 33 | |
| 34 | |
| 35 | return results |
| 36 | |
| 37 | def run_template(name, opts): |
| 38 | template = os_template_env.get_template(name) |
| 39 | buffer = template.render(opts) |
| 40 | |
| 41 | f = tempfile.NamedTemporaryFile(mode='w') |
| 42 | f.write(buffer) |
| 43 | f.flush() |
| 44 | |
| 45 | run = os.popen('/opt/planetstack/observer/run_ansible '+f.name) |
| 46 | msg = run.read() |
| 47 | status = run.close() |
| 48 | |
| 49 | try: |
| 50 | ok_results = parse_output(msg) |
| 51 | except ValueError,e: |
| 52 | print str(e) |
| 53 | raise e |
| 54 | return ok_results |
| 55 | |
| 56 | def main(): |
| 57 | run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/", |
| 58 | "name" : "Sapan Bhatia", |
| 59 | "email": "gwsapan@gmail.com", |
| 60 | "password": "foobar", |
| 61 | "admin_user":"admin", |
| 62 | "admin_password":"6a789bf69dd647e2", |
| 63 | "admin_tenant":"admin", |
| 64 | "tenant":"demo", |
| 65 | "roles":['user','admin'] }) |