Dennis Dmitriev | f5f2e60 | 2017-11-03 15:36:19 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from __future__ import print_function |
| 4 | from __future__ import unicode_literals |
| 5 | |
| 6 | import os |
| 7 | import sys |
| 8 | import copy |
| 9 | import ConfigParser |
| 10 | |
| 11 | import json |
| 12 | |
| 13 | from itertools import chain |
| 14 | from itertools import ifilter |
| 15 | from collections import namedtuple |
| 16 | from collections import OrderedDict |
| 17 | from heatclient import client as heatclient |
| 18 | from keystoneauth1.identity import V2Password |
| 19 | from keystoneauth1.session import Session as KeystoneSession |
| 20 | |
| 21 | SshHost = namedtuple("SshHost", |
| 22 | "roles node_name host login password keys") |
| 23 | |
| 24 | # host_tmpl = { |
| 25 | # "roles": ["salt_master"], |
| 26 | # "node_name": "cfg01.mk22-lab-dvr.local", |
| 27 | # "host": "172.16.10.100", |
| 28 | # "address_pool": "admin-pool01", |
| 29 | # "login": "root", |
| 30 | # "password": "r00tme"} |
| 31 | |
| 32 | CONFIG_TMPL = OrderedDict([ |
| 33 | ('hardware', { |
Dennis Dmitriev | 552454c | 2019-03-21 23:15:51 +0200 | [diff] [blame] | 34 | 'env_manager': 'heat', |
Dennis Dmitriev | f5f2e60 | 2017-11-03 15:36:19 +0200 | [diff] [blame] | 35 | 'current_snapshot': None |
| 36 | }), |
| 37 | ('underlay', { |
| 38 | 'ssh': None, |
| 39 | 'roles': None, |
| 40 | }), |
| 41 | ('salt', { |
| 42 | 'salt_master_host': None |
| 43 | }) |
| 44 | ]) |
| 45 | |
| 46 | |
| 47 | def fill_hosts(hosts, ssh_key=None): |
| 48 | ret = [] |
| 49 | for h in hosts: |
| 50 | |
| 51 | ret.append( |
| 52 | SshHost( |
| 53 | roles=h['roles'], |
| 54 | node_name=h['hostname'], |
| 55 | host=h['public_ip'], |
| 56 | login='ubuntu', |
| 57 | password='ubuntu', |
| 58 | keys=[ssh_key] if ssh_key else [])) |
| 59 | return ret |
| 60 | |
| 61 | |
| 62 | def get_heat(): |
| 63 | keystone_auth = V2Password( |
| 64 | auth_url=os.environ['OS_AUTH_URL'], |
| 65 | username=os.environ['OS_USERNAME'], |
| 66 | password=os.environ['OS_PASSWORD'], |
| 67 | tenant_name=os.environ['OS_TENANT_NAME']) |
| 68 | session = KeystoneSession(auth=keystone_auth, verify=False) |
| 69 | endpoint_url = session.get_endpoint( |
| 70 | service_type='orchestration', |
| 71 | endpoint_type='publicURL') |
| 72 | heat = heatclient.Client( |
| 73 | version='1', |
| 74 | endpoint=endpoint_url, |
| 75 | token=session.get_token()) |
| 76 | return heat |
| 77 | |
| 78 | |
| 79 | def fill_config(hosts, env_name, last_snapshot): |
| 80 | ini = copy.deepcopy(CONFIG_TMPL) |
| 81 | |
| 82 | ini['hardware']['current_snapshot'] = last_snapshot |
| 83 | ini['underlay']['ssh'] = json.dumps([h.__dict__ for h in hosts]) |
| 84 | ini['underlay']['roles'] = json.dumps( |
| 85 | list(set(chain(*[h.roles for h in hosts])))) |
| 86 | ini['salt']['salt_master_host'] = next(h.host for h in hosts |
| 87 | if 'salt-master' in h.roles) |
| 88 | |
| 89 | return ini |
| 90 | |
| 91 | |
| 92 | def save_ini_config(ini, filename): |
| 93 | config = ConfigParser.ConfigParser() |
| 94 | |
| 95 | for s in ini: |
| 96 | config.add_section(s) |
| 97 | for k, v in ini[s].items(): |
| 98 | config.set(s, k, v) |
| 99 | |
| 100 | with open(filename, 'w') as f: |
| 101 | config.write(f) |
| 102 | |
| 103 | |
| 104 | def print_help(): |
| 105 | text = """ |
| 106 | Usage: {command} HEAT_STACK_NAME HEAT_SNAPHOT_NAME |
| 107 | """.format(command=sys.argv[0]) |
| 108 | print(text) |
| 109 | sys.exit(1) |
| 110 | |
| 111 | |
| 112 | def main(): |
| 113 | if len(sys.argv) < 3: |
| 114 | print_help() |
| 115 | |
| 116 | heat = get_heat() |
| 117 | env_name = sys.argv[1] |
| 118 | snapshot = sys.argv[2] |
| 119 | ssh_key = next(iter(sys.argv[3:]), None) |
| 120 | stack = heat.stacks.get(env_name) |
| 121 | hosts = next(ifilter( |
| 122 | lambda v: v['output_key'] == 'hosts', stack.outputs))['output_value'] |
| 123 | hosts = list(chain(*hosts)) |
| 124 | hosts = fill_hosts(hosts, ssh_key=ssh_key) |
| 125 | ini = fill_config(hosts, env_name, snapshot) |
| 126 | save_ini_config(ini, "{name}_{snapshot}.ini".format(name=env_name, |
| 127 | snapshot=snapshot)) |
| 128 | |
| 129 | |
| 130 | if __name__ == '__main__': |
| 131 | main() |