summaryrefslogtreecommitdiffstats
path: root/compass-deck/deployment/installers/config_manager.py
blob: 597c3a6c79a3167a11f92590c4424afc2618ac9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# Copyright 2014 Huawei Technologies Co. Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = "baigk baiguoku@huawei.com)"

from collections import defaultdict
from copy import deepcopy
import json
import logging
import netaddr

from compass.deployment.utils import constants as const

ip_generator_map = {}


def get_ip_addr(ip_ranges):
    def _get_ip_addr():
        for ip_range in ip_ranges:
            for ip in netaddr.iter_iprange(*ip_range):
                yield str(ip)

    s = json.dumps(ip_ranges)
    if s not in ip_generator_map:
        ip_generator_map[s] = _get_ip_addr()
        return ip_generator_map[s]
    else:
        return ip_generator_map[s]


class AdapterInfo(object):
    def __init__(self, adapter_info):
        self.adapter_info = adapter_info
        self.name = self.adapter_info.get(const.NAME)
        self.dist_system_name = self.name
        self.health_check_cmd = self.adapter_info.get(const.HEALTH_CHECK_CMD)

        self.os_installer = self.adapter_info.setdefault(
            const.OS_INSTALLER, {}
        )
        self.os_installer.setdefault(const.INSTALLER_SETTINGS, {})

        self.package_installer = self.adapter_info.setdefault(
            const.PK_INSTALLER, {}
        )
        self.package_installer.setdefault(const.INSTALLER_SETTINGS, {})

        self.metadata = self.adapter_info.setdefault(const.METADATA, {})
        self.os_metadata = self.metadata.setdefault(const.OS_CONFIG, {})
        self.package_metadata = self.metadata.setdefault(const.PK_CONFIG, {})

        self.flavors = dict([(f[const.FLAVOR_NAME], f)
                            for f in self.adapter_info.get(const.FLAVOR, [])])

    @property
    def flavor_list(self):
        return self.flavors.values()

    def get_flavor(self, flavor_name):
        return self.flavors.get(flavor_name)


class ClusterInfo(object):
    def __init__(self, cluster_info):
        self.cluster_info = cluster_info
        self.id = self.cluster_info.get(const.ID)
        self.name = self.cluster_info.get(const.NAME)
        self.os_version = self.cluster_info.get(const.OS_VERSION)
        self.flavor = self.cluster_info.setdefault(
            const.FLAVOR, {}
        )
        self.os_config = self.cluster_info.setdefault(
            const.OS_CONFIG, {}
        )
        self.package_config = self.cluster_info.setdefault(
            const.PK_CONFIG, {}
        )
        self.deployed_os_config = self.cluster_info.setdefault(
            const.DEPLOYED_OS_CONFIG, {}
        )
        self.deployed_package_config = self.cluster_info.setdefault(
            const.DEPLOYED_PK_CONFIG, {}
        )
        self.network_mapping = self.package_config.setdefault(
            const.NETWORK_MAPPING, {}
        )

        os_config_general = self.os_config.setdefault(
            const.OS_CONFIG_GENERAL, {}
        )
        self.domain = os_config_general.setdefault(const.DOMAIN, None)
        self.hosts = []

    def add_host(self, host):
        self.hosts.append(host)

    @property
    def roles_mapping(self):
        deploy_config = self.deployed_package_config
        return deploy_config.setdefault(
            const.ROLES_MAPPING, self._get_cluster_roles_mapping()
        )

    def _get_cluster_roles_mapping(self):
        """The ouput format will be as below, for example:

        {
            "controller": [{
                "hostname": "xxx",
                "management": {
                    "interface": "eth0",
                    "ip": "192.168.1.10",
                    "netmask": "255.255.255.0",
                    "subnet": "192.168.1.0/24",
                    "is_mgmt": True,
                    "is_promiscuous": False
                },
                ...
            }],
                ...
        }
        """
        mapping = defaultdict(list)
        for host in self.hosts:
            for role, value in host.roles_mapping.iteritems():
                mapping[role].append(value)

        return dict(mapping)

    def _get_cluster_patched_roles_mapping(self):
        mapping = defaultdict(list)
        for host in self.hosts:
            for role, value in host.patched_roles_mapping.iteritems():
                mapping[role].append(value)

        return dict(mapping)

    @property
    def base_info(self):
        return {
            const.ID: self.id,
            const.NAME: self.name,
            const.OS_VERSION: self.os_version
        }


class HostInfo(object):
    def __init__(self, host_info, cluster_info):
        self.host_info = host_info
        self.cluster_info = cluster_info
        self.id = self.host_info.get(const.ID)
        self.name = self.host_info.get(const.NAME)
        self.mac = self.host_info.get(const.MAC_ADDR)
        self.hostname = self.host_info.get(const.HOSTNAME)
        self.networks = self.host_info.setdefault(const.NETWORKS, {})
        self.os_config = self.host_info.setdefault(const.OS_CONFIG, {})

        self.package_config = self.host_info.setdefault(const.PK_CONFIG, {})
        self.roles = self.host_info.setdefault(const.ROLES, [])
        self.patched_roles = self.host_info.setdefault(const.PATCHED_ROLES, [])
        self.ipmi = deepcopy(self.host_info.setdefault(const.IPMI, {}))
        self.reinstall_os_flag = self.host_info.get(const.REINSTALL_OS_FLAG)
        self.deployed_os_config = self.host_info.setdefault(
            const.DEPLOYED_OS_CONFIG, {}
        )
        self.deployed_package_config = self.host_info.setdefault(
            const.DEPLOYED_PK_CONFIG, {}
        )

        os_general_config = self.os_config.setdefault(
            const.OS_CONFIG_GENERAL, {}
        )
        domain = os_general_config.setdefault(const.DOMAIN, None)
        if domain is None:
            self.domain = self.cluster_info.domain
        else:
            self.domain = domain

        if const.DNS in host_info:
            self.dns = host_info[const.DNS]
        else:
            self.dns = '.'.join((self.hostname, self.domain))

        if const.NETWORK_MAPPING not in self.package_config:
            self.network_mapping = self.cluster_info.network_mapping
        else:
            self.network_mapping = self.package_config[const.NETWORK_MAPPING]

        if const.ROLES_MAPPING not in self.deployed_package_config:
            self.roles_mapping = self._get_host_roles_mapping()
            self.deployed_package_config[
                const.ROLES_MAPPING
            ] = self.roles_mapping
        else:
            self.roles_mapping = \
                self.deployed_package_config[const.ROLES_MAPPING]

        self.patched_roles_mapping = self._get_host_patched_roles_mapping()

        self.cluster_info.add_host(self)

    def valid_interface(self, interface):
        if interface not in self.networks:
            raise RuntimeError("interface %s is invalid" % interface)

    def get_interface(self, interface):
        self.valid_interface(interface)
        return self.networks[interface]

    def get_interface_ip(self, interface):
        return self.get_interface(interface).get(const.IP_ADDR)

    def get_interface_netmask(self, interface):
        return self.get_interface(interface).get(const.NETMASK)

    def get_interface_subnet(self, interface):
        return self.get_interface(interface).get(const.SUBNET)

    def is_interface_promiscuous(self, interface):
        return self.get_interface(interface).get(const.PROMISCUOUS_FLAG)

    def is_interface_mgmt(self, interface):
        return self.get_interface(interface).get(const.MGMT_NIC_FLAG)

    def _get_host_roles_mapping(self):
        if not self.network_mapping:
            return {}

        net_info = {const.HOSTNAME: self.hostname}
        for k, v in self.network_mapping.items():
            try:
                net_info[k] = self.networks[v[const.NIC]]
                net_info[k][const.NIC] = v[const.NIC]
            except Exception:
                pass

        mapping = {}
        for role in self.roles:
            role = role.replace("-", "_")
            mapping[role] = net_info

        return mapping

    def _get_host_patched_roles_mapping(self):
        if not self.network_mapping:
            return {}

        net_info = {const.HOSTNAME: self.hostname}
        for k, v in self.network_mapping.items():
            try:
                net_info[k] = self.networks[v[const.NIC]]
                net_info[k][const.NIC] = v[const.NIC]
            except Exception:
                pass

        mapping = {}
        for role in self.patched_roles:
            role = role['name'].replace("-", "_")
            mapping[role] = net_info

        return mapping

    @property
    def baseinfo(self):
        return {
            const.REINSTALL_OS_FLAG: self.reinstall_os_flag,
            const.MAC_ADDR: self.mac,
            const.NAME: self.name,
            const.HOSTNAME: self.hostname,
            const.DNS: self.dns,
            const.NETWORKS: deepcopy(self.networks)
        }


class BaseConfigManager(object):
    def __init__(self, adapter_info={}, cluster_info={}, hosts_info={}):
        assert(adapter_info and isinstance(adapter_info, dict))
        assert(cluster_info and isinstance(cluster_info, dict))
        assert(hosts_info and isinstance(hosts_info, dict))

        self.adapter_info = AdapterInfo(adapter_info)
        self.cluster_info = ClusterInfo(cluster_info)
        self.hosts_info = dict([(k, HostInfo(v, self.cluster_info))
                               for k, v in hosts_info.iteritems()])

    def get_adapter_name(self):
        return self.adapter_info.name

    def get_dist_system_name(self):
        return self.adapter_info.dist_system_name

    def get_adapter_health_check_cmd(self):
        return self.adapter_info.health_check_cmd

    def get_os_installer_settings(self):
        return self.adapter_info.os_installer[const.INSTALLER_SETTINGS]

    def get_pk_installer_settings(self):
        return self.adapter_info.package_installer[const.INSTALLER_SETTINGS]

    def get_os_config_metadata(self):
        return self.adapter_info.metadata[const.OS_CONFIG]

    def get_pk_config_meatadata(self):
        return self.adapter_info.metadata[const.PK_CONFIG]

    def get_adapter_all_flavors(self):
        return self.adapter_info.flavor_list

    def get_adapter_flavor(self, flavor_name):
        return self.adapter_info.get_flavor(flavor_name)

    def get_cluster_id(self):
        return self.cluster_info.id

    def get_clustername(self):
        return self.cluster_info.name

    def get_os_version(self):
        return self.cluster_info.os_version

    def get_cluster_os_config(self):
        return self.cluster_info.os_config

    def get_cluster_baseinfo(self):
        return self.cluster_info.base_info

    def get_cluster_flavor_name(self):
        return self.cluster_info.flavor.get(const.FLAVOR_NAME)

    def get_cluster_flavor_roles(self):
        return self.cluster_info.flavor.get(const.ROLES, [])

    def get_cluster_flavor_template(self):
        return self.cluster_info.flavor.get(const.TMPL)

    def get_cluster_package_config(self):
        return self.cluster_info.package_config

    def get_cluster_network_mapping(self):
        mapping = self.cluster_info.network_mapping
        logging.info("Network mapping in the config is '%s'!", mapping)
        return mapping

    def get_cluster_deployed_os_config(self):
        return self.cluster_info.deployed_os_config

    def get_cluster_deployed_package_config(self):
        return self.cluster_info.deployed_package_config

    def get_cluster_roles_mapping(self):
        return self.cluster_info.roles_mapping

    def get_cluster_patched_roles_mapping(self):
        return self.cluster_info._get_cluster_patched_roles_mapping()

    def validate_host(self, host_id):
        if host_id not in self.hosts_info:
            raise RuntimeError("host_id %s is invalid" % host_id)

    def get_host_id_list(self):
        return self.hosts_info.keys()

    def get_hosts_id_list_for_os_installation(self):
        """Get info of hosts which need to install/reinstall OS."""
        return [
            id for id, info in self.hosts_info.items()
            if info.reinstall_os_flag
        ]

    def get_server_credentials(self):
        cluster_os_config = self.get_cluster_os_config()
        if not cluster_os_config:
            logging.info("cluster os_config is None!")
            return ()

        username = cluster_os_config[const.SERVER_CREDS][const.USERNAME]
        password = cluster_os_config[const.SERVER_CREDS][const.PASSWORD]
        return (username, password)

    def _get_host_info(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id]

    def get_host_baseinfo(self, host_id):
        self.validate_host(host_id)
        host_info = self.hosts_info[host_id]
        return host_info.baseinfo

    def get_host_fullname(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].name

    def get_host_dns(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].dns

    def get_host_mac_address(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].mac

    def get_hostname(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].hostname

    def get_host_networks(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].networks

    def get_host_interfaces(self, host_id):
        # get interface names
        return self.get_host_networks(host_id).keys()

    def get_host_interface_ip(self, host_id, interface):
        self.validate_host(host_id)
        return self.hosts_info[host_id].get_interface_ip(interface)

    def get_host_interface_netmask(self, host_id, interface):
        self.validate_host(host_id)
        return self.hosts_info[host_id].get_interface_netmask(interface)

    def get_host_interface_subnet(self, host_id, interface):
        self.validate_host(host_id)
        return self.hosts_info[host_id].get_interface_subnet(interface)

    def is_interface_promiscuous(self, host_id, interface):
        self.validate_host(host_id)
        return self.hosts_info[host_id].is_interface_promiscuous(interface)

    def is_interface_mgmt(self, host_id, interface):
        self.validate_host(host_id)
        return self.hosts_info[host_id].is_interface_mgmt(interface)

    def get_host_os_config(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].os_config

    def get_host_domain(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].domain

    def get_host_network_mapping(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].network_mapping

    def get_host_package_config(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].package_config

    def get_host_deployed_os_config(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].deployed_os_config

    def get_host_deployed_package_config(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].deployed_package_config

    def get_host_roles(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].roles

    def get_all_hosts_roles(self, hosts_id_list=None):
        roles = []
        for host_id, host_info in self.hosts_info.iteritems():
            roles.extend(host_info.roles)

        return list(set(roles))

    def get_hosts_ip_settings(self, ip_settings, sys_intf_mappings):
        logging.info(
            "get_hosts_ip_settings:ip_settings=%s, sys_intf_mappings=%s" %
            (ip_settings, sys_intf_mappings)
        )

        intf_alias = {}
        for m in sys_intf_mappings:
            if "vlan_tag" in m:
                intf_alias[m["name"]] = m["name"]
            else:
                intf_alias[m["name"]] = m["interface"]

        mappings = {}
        hosts_id_list = self.get_host_id_list()
        for host_id in hosts_id_list:
            hostname = self.get_hostname(host_id)
            mappings[hostname] = []
            for ip_info in ip_settings:
                logging.info("ip_info=%s" % ip_info)
                new_ip_info = deepcopy(ip_info)
                del new_ip_info["ip_ranges"]

                ip_ranges = ip_info["ip_ranges"]
                new_ip_info["netmask"] = netaddr.IPNetwork(
                    ip_info["cidr"]
                ).netmask.bin.count("1")
                new_ip_info["ip"] = get_ip_addr(ip_ranges).next()
                new_ip_info["alias"] = intf_alias[ip_info["name"]]
                mappings[hostname].append(new_ip_info)

        return {"ip_settings": mappings}

    def get_host_roles_mapping(self, host_id):
        self.validate_host(host_id)
        return self.hosts_info[host_id].roles_mapping

    def get_host_ipmi_info(self, host_id):
        self.validate_host(host_id)
        if self.hosts_info[host_id].ipmi:
            return (
                self.hosts_info[host_id].ipmi[const.IP_ADDR],
                self.hosts_info[host_id].ipmi
                [const.IPMI_CREDS][const.USERNAME],
                self.hosts_info[host_id].ipmi
                [const.IPMI_CREDS][const.USERNAME])
        else:
            return (None, None, None)