blob: 6e9c85d2906e36380e8051b2bd9fcb84eac27356 [file] [log] [blame]
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +02001#!/usr/bin/env python
2
3from __future__ import print_function
4from __future__ import unicode_literals
5
6import os
7import sys
8import copy
9import ConfigParser
10
11import json
12
13from itertools import chain
14from itertools import ifilter
15from collections import namedtuple
16from collections import OrderedDict
17from heatclient import client as heatclient
18from keystoneauth1.identity import V2Password
19from keystoneauth1.session import Session as KeystoneSession
20
21SshHost = 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
32CONFIG_TMPL = OrderedDict([
33 ('hardware', {
Dennis Dmitriev552454c2019-03-21 23:15:51 +020034 'env_manager': 'heat',
Dennis Dmitrievf5f2e602017-11-03 15:36:19 +020035 'current_snapshot': None
36 }),
37 ('underlay', {
38 'ssh': None,
39 'roles': None,
40 }),
41 ('salt', {
42 'salt_master_host': None
43 })
44])
45
46
47def 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
62def 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
79def 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
92def 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
104def 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
112def 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
130if __name__ == '__main__':
131 main()