Plugins for configuration manager
[ta/cm-plugins.git] / userconfighandlers / installhandler / installhandler.py
1 #! /usr/bin/python
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import netifaces as ni
17 from cmframework.apis import cmuserconfig
18 from cmframework.apis import cmerror
19 from cmdatahandlers.api import configerror
20 from cmdatahandlers.api import utils
21 """
22 This plugin is used to define the installation node in the system
23 """
24 class installhandler(cmuserconfig.CMUserConfigPlugin):
25     def __init__(self):
26         super(installhandler,self).__init__()
27
28     def handle(self, confman):
29         try:
30             hostsconf = confman.get_hosts_config_handler()
31             hostname = 'controller-1'
32             if not utils.is_virtualized():
33                 ownip = utils.get_own_hwmgmt_ip()
34                 hostname = hostsconf.get_host_having_hwmgmt_address(ownip)
35             else:
36                 mgmt_addr = {}
37
38                 for host in hostsconf.get_hosts():
39                     try:
40                         mgmt_addr[host] = hostsconf.get_mgmt_mac(host)[0]
41                     except IndexError:
42                         pass
43                 for interface in ni.interfaces():
44                     a = ni.ifaddresses(interface)
45                     mac_list = []
46                     for mac in a[ni.AF_LINK]:
47                         mac_list.append(mac.get('addr', None))
48                     for host, mgmt_mac in mgmt_addr.iteritems():
49                         if mgmt_mac in mac_list:
50                             hostsconf.set_installation_host(host)
51                             return
52
53             hostsconf.set_installation_host(hostname)
54         except configerror.ConfigError as exp:
55             raise cmerror.CMError(str(exp))