1 ##############################################################################
2 # Copyright (c) 2017 Tim Rozet (trozet@redhat.com) and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
18 from apex.builders import undercloud_builder as uc_builder
19 from apex.virtual import utils as virt_utils
20 from apex.virtual import configure_vm as vm_lib
21 from apex.common import constants
22 from apex.common import utils
25 class ApexUndercloudException(Exception):
31 This class represents an Apex Undercloud VM
33 def __init__(self, image_path, template_path,
34 root_pw=None, external_network=False,
35 image_name='undercloud.qcow2',
36 os_version=constants.DEFAULT_OS_VERSION):
38 self.os_version = os_version
39 self.root_pw = root_pw
40 self.external_net = external_network
41 self.volume = os.path.join(constants.LIBVIRT_VOLUME_PATH,
43 self.image_path = image_path
44 self.image_name = image_name
45 self.template_path = template_path
47 if Undercloud._get_vm():
48 logging.error("Undercloud VM already exists. Please clean "
50 raise ApexUndercloudException("Undercloud VM already exists!")
55 conn = libvirt.open('qemu:///system')
57 vm = conn.lookupByName('undercloud')
59 except libvirt.libvirtError:
60 logging.debug("No undercloud VM exists")
65 networks.append('external')
66 console = 'ttyAMA0' if platform.machine() == 'aarch64' else 'ttyS0'
67 root = 'vda' if platform.machine() == 'aarch64' else 'sda'
69 self.vm = vm_lib.create_vm(name='undercloud',
71 baremetal_interfaces=networks,
72 direct_boot='overcloud-full',
73 kernel_args=['console={}'.format(console),
74 'root=/dev/{}'.format(root)],
76 template_dir=self.template_path,
83 ip_out = vm.interfaceAddresses(
84 libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE, 0)
86 for (name, val) in ip_out.items():
87 for ipaddr in val['addrs']:
88 if ipaddr['type'] == libvirt.VIR_IP_ADDR_TYPE_IPV4:
92 ip = self._get_ip(self.vm)
99 vm = Undercloud._get_vm()
100 return Undercloud._get_ip(vm)
107 if self.vm.isActive():
108 logging.info("Undercloud already started")
110 logging.info("Starting undercloud")
112 # give 10 seconds to come up
117 logging.info("Undercloud started. IP Address: {}".format(
120 logging.debug("Did not find undercloud IP in {} "
121 "attempts...".format(x))
124 logging.error("Cannot find IP for Undercloud")
125 raise ApexUndercloudException(
126 "Unable to find IP for undercloud. Check if VM booted "
129 def detect_nat(self, net_settings):
130 if self.external_net:
131 net = net_settings['networks'][constants.EXTERNAL_NETWORK][0]
133 net = net_settings['networks'][constants.ADMIN_NETWORK]
134 if net['gateway'] == net['installer_vm']['ip']:
139 def configure(self, net_settings, deploy_settings,
140 playbook, apex_temp_dir, virtual_oc=False):
142 Configures undercloud VM
143 :param net_settings: Network settings for deployment
144 :param deploy_settings: Deployment settings for deployment
145 :param playbook: playbook to use to configure undercloud
146 :param apex_temp_dir: temporary apex directory to hold configs/logs
147 :param virtual_oc: Boolean to determine if overcloud is virt
151 logging.info("Configuring Undercloud...")
153 ansible_vars = Undercloud.generate_config(net_settings,
155 ansible_vars['apex_temp_dir'] = apex_temp_dir
157 ansible_vars['nat'] = self.detect_nat(net_settings)
159 utils.run_ansible(ansible_vars, playbook, host=self.ip,
161 except subprocess.CalledProcessError:
163 "Failed to install undercloud..."
164 "please check log: {}".format(os.path.join(
165 apex_temp_dir, 'apex-undercloud-install.log')))
166 raise ApexUndercloudException('Failed to install undercloud')
167 logging.info("Undercloud installed!")
169 def setup_volumes(self):
170 for img_file in ('overcloud-full.vmlinuz', 'overcloud-full.initrd',
172 src_img = os.path.join(self.image_path, img_file)
173 if img_file == self.image_name:
174 dest_img = os.path.join(constants.LIBVIRT_VOLUME_PATH,
177 dest_img = os.path.join(constants.LIBVIRT_VOLUME_PATH,
179 if not os.path.isfile(src_img):
180 raise ApexUndercloudException(
181 "Required source file does not exist:{}".format(src_img))
182 if os.path.exists(dest_img):
184 shutil.copyfile(src_img, dest_img)
185 if img_file == self.image_name and platform.machine() != 'aarch64':
186 uc_builder.expand_disk(dest_img)
187 self.expand_root_fs()
189 shutil.chown(dest_img, user='qemu', group='qemu')
190 os.chmod(dest_img, 0o0744)
192 def expand_root_fs(self):
193 # there is a lib called vminspect which has some dependencies and is
194 # not yet available in pip. Consider switching to this lib later.
195 logging.debug("Expanding root filesystem on /dev/sda partition")
196 virt_ops = [{constants.VIRT_RUN_CMD: 'xfs_growfs /dev/sda'}]
197 virt_utils.virt_customize(virt_ops, self.volume)
199 def inject_auth(self):
201 # virt-customize keys/pws
203 pw_op = "password:{}".format(self.root_pw)
204 virt_ops.append({constants.VIRT_PW: pw_op})
206 virt_ops.append({constants.VIRT_RUN_CMD:
207 'mkdir -p /root/.ssh'})
208 virt_ops.append({constants.VIRT_UPLOAD:
209 '/root/.ssh/id_rsa.pub:/root/.ssh/authorized_keys'})
211 'chmod 600 /root/.ssh/authorized_keys',
212 'restorecon -R -v /root/.ssh',
213 'id -u stack || useradd -m stack',
214 'mkdir -p /home/stack/.ssh',
215 'chown stack:stack /home/stack/.ssh',
216 'cp /root/.ssh/authorized_keys /home/stack/.ssh/',
217 'chown stack:stack /home/stack/.ssh/authorized_keys',
218 'chmod 600 /home/stack/.ssh/authorized_keys',
219 'echo "stack ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers',
220 'touch /etc/cloud/cloud-init.disabled'
223 virt_ops.append({constants.VIRT_RUN_CMD: cmd})
224 virt_utils.virt_customize(virt_ops, self.volume)
227 def generate_config(ns, ds):
229 Generates a dictionary of settings for configuring undercloud
230 :param ns: network settings to derive undercloud settings
231 :param ds: deploy settings to derive undercloud settings
232 :return: dictionary of settings
235 ns_admin = ns['networks']['admin']
236 intro_range = ns['apex']['networks']['admin']['introspection_range']
238 # Check if this is an ARM deployment
239 config['aarch64'] = platform.machine() == 'aarch64'
240 # Configuration for undercloud.conf
241 config['undercloud_config'] = [
243 "undercloud_update_packages false",
244 "undercloud_debug false",
245 "inspection_extras false",
246 "ipxe_enabled {}".format(
247 str(ds['global_params'].get('ipxe', True) and
248 not config['aarch64'])),
249 "undercloud_hostname undercloud.{}".format(ns['dns-domain']),
250 "local_ip {}/{}".format(str(ns_admin['installer_vm']['ip']),
251 str(ns_admin['cidr']).split('/')[1]),
252 "generate_service_certificate false",
253 "undercloud_ntp_servers {}".format(str(ns['ntp'][0])),
254 "container_images_file "
255 "/home/stack/containers-prepare-parameter.yaml"
258 config['undercloud_network_config'] = [
259 "gateway {}".format(str(ns_admin['installer_vm']['ip'])),
260 "cidr {}".format(str(ns_admin['cidr'])),
261 "dhcp_start {}".format(str(ns_admin['dhcp_range'][0])),
262 "dhcp_end {}".format(str(ns_admin['dhcp_range'][1])),
263 "inspection_iprange {}".format(','.join(intro_range)),
266 config['ironic_config'] = [
267 "disk_utils iscsi_verify_attempts 30",
268 "disk_partitioner check_device_max_retries 40"
271 config['nova_config'] = [
272 "dns_domain {}".format(ns['dns-domain']),
273 "dhcp_domain {}".format(ns['dns-domain'])
276 config['neutron_config'] = [
277 "dns_domain {}".format(ns['dns-domain']),
279 # FIXME(trozet): possible bug here with not using external network
280 ns_external = ns['networks']['external'][0]
281 config['external_network'] = {
282 "vlan": ns_external['installer_vm']['vlan'],
283 "ip": ns_external['installer_vm']['ip'],
284 "prefix": str(ns_external['cidr']).split('/')[1],
285 "enabled": ns_external['enabled']
287 # We will NAT external network if it is enabled. If external network
288 # is IPv6, we will NAT admin network in case we need IPv4 connectivity
289 # for things like DNS server.
290 if 'external' in ns.enabled_network_list and \
291 ns_external['cidr'].version == 4:
292 nat_cidr = ns_external['cidr']
294 nat_cidr = ns['networks']['admin']['cidr']
295 config['nat_cidr'] = str(nat_cidr)
296 if nat_cidr.version == 6:
297 config['nat_network_ipv6'] = True
299 config['nat_network_ipv6'] = False
300 config['http_proxy'] = ns.get('http_proxy', '')
301 config['https_proxy'] = ns.get('https_proxy', '')
305 def _update_delorean_repo(self):
306 if utils.internet_connectivity():
307 logging.info('Updating delorean repo on Undercloud')
309 "https://trunk.rdoproject.org/centos7-{}"
310 "/current-tripleo/delorean.repo".format(self.os_version))
311 cmd = ("curl -L -f -o "
312 "/etc/yum.repos.d/deloran.repo {}".format(delorean_repo))
314 virt_utils.virt_customize([{constants.VIRT_RUN_CMD: cmd}],
317 logging.warning("Failed to download and update delorean repo "