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 | |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 49 | |
| 50 | #f = open('/tmp/obsans','w') |
| 51 | try: |
| 52 | objname = opts['ansible_tag'] |
| 53 | except: |
| 54 | objname= id_generator() |
| 55 | os.system('mkdir -p %s'%'/'.join([sys_dir,path])) |
| 56 | fqp = '/'.join([sys_dir,path,objname]) |
| 57 | |
| 58 | |
| 59 | f = open(fqp,'w') |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 60 | f.write(buffer) |
| 61 | f.flush() |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 62 | |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 63 | run = os.popen('/opt/planetstack/observer/run_ansible '+fqp) |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 64 | msg = run.read() |
| 65 | status = run.close() |
| 66 | |
| 67 | try: |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 68 | ok_results = parse_output(msg) |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 69 | except ValueError,e: |
Sapan Bhatia | 6255f82 | 2014-12-21 02:33:13 -0500 | [diff] [blame] | 70 | all_fatal = re.findall(r'^msg: (.*)',msg,re.MULTILINE) |
| 71 | all_fatal2 = re.findall(r'^ERROR: (.*)',msg,re.MULTILINE) |
| 72 | |
| 73 | |
| 74 | all_fatal.extend(all_fatal2) |
| 75 | try: |
| 76 | error = ' // '.join(all_fatal) |
| 77 | except: |
| 78 | pass |
| 79 | raise Exception(error) |
| 80 | |
Sapan Bhatia | d2c5915 | 2014-11-19 15:25:38 -0500 | [diff] [blame] | 81 | return ok_results |
| 82 | |
| 83 | def main(): |
Sapan | 01cf331 | 2014-12-02 23:50:37 -0500 | [diff] [blame] | 84 | run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/", |
| 85 | "name" : "Sapan Bhatia", |
| 86 | "email": "gwsapan@gmail.com", |
| 87 | "password": "foobar", |
| 88 | "admin_user":"admin", |
| 89 | "admin_password":"6a789bf69dd647e2", |
| 90 | "admin_tenant":"admin", |
| 91 | "tenant":"demo", |
| 92 | "roles":['user','admin'] }) |