Sapan Bhatia | d2c5915 | 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 | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 6 | import pdb |
| 7 | import string |
| 8 | import random |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 9 | |
| 10 | try: |
| 11 | step_dir = Config().observer_steps_dir |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 12 | sys_dir = Config().observer_sys_dir |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 13 | except: |
| 14 | step_dir = '/opt/planetstack/observer/steps' |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 15 | sys_dir = '/opt/opencloud' |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 16 | |
| 17 | os_template_loader = jinja2.FileSystemLoader( searchpath=step_dir) |
| 18 | os_template_env = jinja2.Environment(loader=os_template_loader) |
| 19 | |
| 20 | def parse_output(msg): |
| 21 | lines = msg.splitlines() |
| 22 | results = [] |
| 23 | print msg |
| 24 | |
| 25 | for l in lines: |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 26 | magic_str = 'ok: [127.0.0.1] => ' |
| 27 | magic_str2 = 'changed: [127.0.0.1] => ' |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 28 | if (l.startswith(magic_str)): |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 29 | w = len(magic_str) |
| 30 | str = l[w:] |
| 31 | d = json.loads(str) |
| 32 | results.append(d) |
| 33 | elif (l.startswith(magic_str2)): |
| 34 | w = len(magic_str2) |
| 35 | str = l[w:] |
| 36 | d = json.loads(str) |
| 37 | results.append(d) |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 38 | |
| 39 | |
| 40 | return results |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 41 | |
| 42 | def id_generator(size=6, chars=string.ascii_uppercase + string.digits): |
| 43 | return ''.join(random.choice(chars) for _ in range(size)) |
| 44 | |
| 45 | def run_template(name, opts,path=''): |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 46 | template = os_template_env.get_template(name) |
| 47 | buffer = template.render(opts) |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 48 | |
| 49 | import pdb |
| 50 | |
| 51 | #f = open('/tmp/obsans','w') |
| 52 | try: |
| 53 | objname = opts['ansible_tag'] |
| 54 | except: |
| 55 | objname= id_generator() |
| 56 | os.system('mkdir -p %s'%'/'.join([sys_dir,path])) |
| 57 | fqp = '/'.join([sys_dir,path,objname]) |
| 58 | |
| 59 | |
| 60 | f = open(fqp,'w') |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 61 | f.write(buffer) |
| 62 | f.flush() |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 63 | |
Sapan | 4f075cc | 2014-12-03 01:04:25 -0500 | [diff] [blame] | 64 | #os.system('cp %s %s-backup'%(fqp,fqp)) |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 65 | run = os.popen('/opt/planetstack/observer/run_ansible '+fqp) |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 66 | msg = run.read() |
| 67 | status = run.close() |
| 68 | |
| 69 | try: |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 70 | ok_results = parse_output(msg) |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 71 | except ValueError,e: |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 72 | print str(e) |
| 73 | raise e |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 74 | return ok_results |
| 75 | |
| 76 | def main(): |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 77 | run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/", |
| 78 | "name" : "Sapan Bhatia", |
| 79 | "email": "gwsapan@gmail.com", |
| 80 | "password": "foobar", |
| 81 | "admin_user":"admin", |
| 82 | "admin_password":"6a789bf69dd647e2", |
| 83 | "admin_tenant":"admin", |
| 84 | "tenant":"demo", |
| 85 | "roles":['user','admin'] }) |