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