3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from jinja2 import Environment
18 from cmframework.apis import cmansibleinventoryconfig
19 from cmdatahandlers.api import utils
23 [ {%- if 'mgmt_mac' in all_vars['hosts'][host] %}
24 {%- for mac_members in all_vars['hosts'][host]['mgmt_mac'] %}
26 "mac": "{{ mac_members }}"
28 {%- if not loop.last %},{%- endif %}
32 "mac": "{{ all_vars['hw_inventory_details'][host]['mgmt_mac'] }}"
39 class zbaremetalnodeinventory(cmansibleinventoryconfig.CMAnsibleInventoryConfigPlugin):
40 def __init__(self, confman, inventory, ownhost):
41 super(zbaremetalnodeinventory, self).__init__(confman, inventory, ownhost)
43 def handle_bootstrapping(self):
46 def handle_provisioning(self):
49 def handle_postconfig(self):
52 def handle_setup(self):
56 def _check_host_single_nic(host_network_profile_value, host_interface_net_mapping):
57 if 'provider_network_interfaces' in host_network_profile_value:
58 host_provider_network_interfaces = host_network_profile_value['provider_network_interfaces']
59 if len(host_interface_net_mapping) == 1 and len(host_provider_network_interfaces) == 1:
60 if host_interface_net_mapping.keys()[0] == host_provider_network_interfaces.keys()[0]:
65 def _generate_linux_bonding_options(options):
66 mode_mapping = {'active-backup': 'active-backup', 'lacp': '802.3ad'}
67 default_options = {'active-backup': 'miimon=100',
68 'lacp': 'lacp_rate=fast miimon=100'}
69 for i in options.split():
70 key, value = i.split('=')
72 if default_options[value]:
73 return 'mode=' + mode_mapping[value] + ' ' + default_options[value]
74 return 'mode=' + mode_mapping[value]
77 def _generate_ovs_bonding_options(options):
78 mode_mapping = {'active-backup': 'active-backup', 'lacp': 'balance-slb',
79 'lacp-layer34': 'balance-tcp'}
80 default_options = {'active-backup': '',
81 'lacp': 'lacp=active other_config:lacp-time=fast other_config:bond-detect-mode=carrier',
82 'lacp-layer34': 'lacp=active other_config:lacp-time=fast other_config:bond-detect-mode=carrier'}
83 for i in options.split():
84 key, value = i.split('=')
86 if default_options[value]:
87 return 'bond_mode=' + mode_mapping[value] + ' ' + default_options[value]
88 return 'bond_mode=' + mode_mapping[value]
91 def _add_static_routes(routes):
94 routes_list.append({"ip_netmask": route["to"], "next_hop": route["via"]})
98 usersconf = self.confman.get_users_config_handler()
99 admin_user = usersconf.get_admin_user()
100 self.add_global_var("home_dir", "/home/" + admin_user)
101 all_vars = self.inventory['all']['vars']
102 host_locals = self.inventory['_meta']['hostvars']
103 nfs_server_ip = host_locals[all_vars['installation_controller']]['networking']['infra_external']['ip']
105 for host, hostvars in host_locals.iteritems():
106 host_hdd_mapping = hostvars['by_path_disks']
107 host_networking = hostvars['networking']
108 host_network_profiles_list = all_vars['hosts'][host]['network_profiles']
109 host_network_profile_value = all_vars['network_profiles'][host_network_profiles_list[0]]
110 host_interface_net_mapping = host_network_profile_value['interface_net_mapping']
112 infra_bond = {'in_use': False}
113 host_bonding_interfaces = host_network_profile_value.get('bonding_interfaces', {})
114 default_mtu = all_vars['networking'].get('mtu', 1500)
117 if 'sriov_provider_networks' in host_network_profile_value:
118 sriov_nets = host_network_profile_value['sriov_provider_networks']
119 prov_infos = host_networking.get('provider_networks', {})
120 for net_name, sriov_info in sriov_nets.iteritems():
121 if prov_infos.get(net_name):
122 prov_info = prov_infos[net_name]
123 sriov_mtu = prov_info.get('mtu', default_mtu)
124 for iface in sriov_info['interfaces']:
125 sriov_mtus[iface] = sriov_mtu
128 if 'mtu' in all_vars['networking']['infra_internal']:
129 mtu = all_vars['networking']['infra_internal']['mtu']
131 phys_iface_mtu = 1500
132 if 'vlan' in host_networking['infra_internal']:
133 for iface, infras in host_interface_net_mapping.iteritems():
134 if 'infra_internal' in infras:
136 tmp_mtu = default_mtu
137 if 'mtu' in all_vars['networking'][infra]:
138 tmp_mtu = all_vars['networking'][infra]['mtu']
139 if infra == 'cloud_tenant':
140 tmp_mtu = tmp_mtu + 50
141 if tmp_mtu > phys_iface_mtu:
142 phys_iface_mtu = tmp_mtu
144 if host_bonding_interfaces.get(iface):
145 for slave in host_bonding_interfaces[iface]:
146 if slave in sriov_mtus and sriov_mtus[slave] > phys_iface_mtu:
147 phys_iface_mtu = sriov_mtus[slave]
148 elif iface in sriov_mtus and sriov_mtus[iface] > phys_iface_mtu:
149 phys_iface_mtu = sriov_mtus[iface]
153 "capabilities": "boot_option:local",
154 "cpu_arch": "x86_64",
161 "provisioning_server": nfs_server_ip,
162 "virtmedia_deploy_iso": "file:///opt/images/ironic-deploy.iso",
165 if utils.is_virtualized():
166 driver = "ssh_virtmedia"
167 properties["root_device"] = {"by_path": host_hdd_mapping['os']}
168 power["ssh_address"] = all_vars['hosts'][host]['hwmgmt']['address']
169 power["ssh_username"] = all_vars['hosts'][host]['hwmgmt']['user']
170 power["ipmi_port"] = all_vars['hosts'][host]['vbmc_port']
171 power["ipmi_username"] = "admin"
172 power["ipmi_password"] = "password"
173 power["ssh_key_contents"] = "{{ lookup('file', '/etc/userconfig/id_rsa') }}"
174 power["ipmi_address"] = host_locals[all_vars['installation_controller']]['networking']['infra_internal']['ip']
176 driver = "ipmi_virtmedia"
177 power["ipmi_address"] = all_vars['hosts'][host]['hwmgmt']['address']
178 power["ipmi_password"] = all_vars['hosts'][host]['hwmgmt']['password']
179 power["ipmi_username"] = all_vars['hosts'][host]['hwmgmt']['user']
180 power["product_family"] = all_vars['hw_inventory_details'][host]['product_family']
181 power["vendor"] = all_vars['hw_inventory_details'][host]['vendor']
183 if host_hdd_mapping['os'] != "/dev/sda":
184 properties["root_device"] = {"by_path": host_hdd_mapping['os']}
186 properties["root_device"] = {"name": host_hdd_mapping['os']}
188 nics_text = Environment().from_string(nics_json_txt).render(all_vars=all_vars, host=host)
189 nics_inventory = json.loads(nics_text)
192 driver_info["power"] = power
193 #####################################################
195 if 'interface' in host_networking['infra_internal']:
196 if not self._check_host_single_nic(host_network_profile_value, host_interface_net_mapping):
197 if 'bonding_interfaces' in host_network_profile_value:
198 for net_key, net_value in host_interface_net_mapping.iteritems():
200 if "bond" in net_key and "infra_internal" in net_value:
202 for member in host_bonding_interfaces[net_key]:
204 if 'bond' in host_networking['infra_internal']['interface']:
205 member_element["mtu"] = mtu
207 member_element["mtu"] = phys_iface_mtu
208 member_element["name"] = member
209 member_element["type"] = "interface"
210 member_element["use_dhcp"] = False
211 members.append(member_element)
214 "type": "linux_bond",
217 bond_contents["name"] = net_key
218 bond_contents["members"] = members
220 if 'linux_bonding_options' in host_network_profile_value:
221 bond_contents["bonding_options"] = self._generate_linux_bonding_options(host_network_profile_value['linux_bonding_options'])
222 if 'bond' in host_networking['infra_internal']['interface']:
223 bond_contents["addresses"] = [{"ip_netmask": "%s/%s" % (host_networking['infra_internal']['ip'], host_networking['infra_internal']['mask'])}]
224 bond_contents["mtu"] = mtu
225 if 'routes' in host_networking['infra_internal']:
226 routes = host_networking['infra_internal']['routes']
227 bond_contents["routes"] = self._add_static_routes(routes)
229 bond_contents["mtu"] = phys_iface_mtu
231 infra_bond.update({'in_use': True})
233 network_config.append(bond_contents)
234 if 'vlan' in host_networking['infra_internal']:
239 vlan_contents["addresses"] = [{"ip_netmask": "%s/%s" % (host_networking['infra_internal']['ip'], host_networking['infra_internal']['mask'])}]
240 vlan_contents["vlan_id"] = host_networking['infra_internal']['vlan']
241 for net_key, net_value in host_interface_net_mapping.iteritems():
242 if "infra_internal" in net_value:
243 vlan_contents["device"] = net_key
244 vlan_contents["mtu"] = mtu
245 if 'routes' in host_networking['infra_internal']:
246 routes = host_networking['infra_internal']['routes']
247 vlan_contents["routes"] = []
249 vlan_contents["routes"].append({"ip_netmask": route["to"], "next_hop": route["via"]})
250 if not infra_bond["in_use"]:
251 vlan_phy_contents = {
254 "mtu": phys_iface_mtu
256 for net_key, net_value in host_interface_net_mapping.iteritems():
257 if "infra_internal" in net_value:
258 vlan_phy_contents["name"] = net_key
259 network_config.append(vlan_phy_contents)
261 network_config.append(vlan_contents)
263 elif not infra_bond["in_use"]:
265 "name": host_networking['infra_internal']['interface'],
270 phy_contents["addresses"] = [{"ip_netmask": "%s/%s" % (host_networking['infra_internal']['ip'], host_networking['infra_internal']['mask'])}]
271 if 'routes' in host_networking['infra_internal']:
272 routes = host_networking['infra_internal']['routes']
273 phy_contents["routes"] = self._add_static_routes(routes)
275 network_config.append(phy_contents)
277 # --> single_nic_setup <-- #
279 single_nic_contents = {
281 "type": "ovs_bridge",
284 member_elements = {"mtu": phys_iface_mtu, "use_dhcp": False}
285 iface = host_interface_net_mapping.keys()[0]
287 for bond_iface, bond_value in host_bonding_interfaces.iteritems():
288 if bond_iface == iface:
289 if 'ovs_bonding_options' in host_network_profile_value:
290 member_elements["ovs_options"] = self._generate_ovs_bonding_options(host_network_profile_value['ovs_bonding_options'])
291 member_elements["name"] = iface
292 member_elements["type"] = "ovs_bond"
293 member_elements["members"] = []
294 for member in bond_value:
298 "mtu": phys_iface_mtu,
301 member_elements["members"].append(ovs_bond_member)
302 single_nic_contents["members"].append(member_elements)
304 member_elements["name"] = iface
305 member_elements["type"] = "interface"
306 single_nic_contents["members"].append(member_elements)
309 infra = host_networking['infra_internal']
310 infra_elements["use_dhcp"] = False
311 infra_elements["type"] = "vlan"
312 infra_elements["vlan_id"] = infra['vlan']
313 infra_elements["mtu"] = mtu
314 infra_elements["addresses"] = [{"ip_netmask": "%s/%s" % (infra['ip'], infra['mask'])}]
315 if 'routes' in infra:
316 routes = infra['routes']
317 infra_elements["routes"] = self._add_static_routes(routes)
319 single_nic_contents["members"].append(infra_elements)
320 network_config.append(single_nic_contents)
321 #####################################################
322 driver_info["power"]["os_net_config"] = {"network_config": network_config}
324 ironic_node_details = {
327 "network_interface": "noop",
328 "nics": nics_inventory,
329 "properties": properties,
330 "driver_info": driver_info
332 self.add_host_var(host, 'ironic_node_details', ironic_node_details)