943c25255192d48894f5f87065c34a4d808e51bd
[apex.git] / apex / builders / undercloud_builder.py
1 ##############################################################################
2 # Copyright (c) 2017 Tim Rozet (trozet@redhat.com) and others.
3 #
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 ##############################################################################
9
10 # Used to modify undercloud qcow2 image
11 import logging
12 import json
13 import os
14 import subprocess
15
16 from apex.common import constants as con
17 from apex.common import utils
18 from apex.virtual import utils as virt_utils
19
20
21 def add_upstream_packages(image):
22     """
23     Adds required base upstream packages to Undercloud for deployment
24     :param image:
25     :return: None
26     """
27     virt_ops = list()
28     pkgs = [
29         'epel-release',
30         'openstack-utils',
31         'ceph-common',
32         'python2-networking-sfc',
33         'openstack-ironic-inspector',
34         'subunit-filters',
35         'docker-distribution',
36         'openstack-tripleo-validations',
37         'libguestfs-tools',
38         'ceph-ansible',
39         'python-tripleoclient',
40         'openstack-tripleo-heat-templates'
41     ]
42     # Remove incompatible python-docker version
43     virt_ops.append({con.VIRT_RUN_CMD: "yum remove -y python-docker-py"})
44
45     for pkg in pkgs:
46         virt_ops.append({con.VIRT_INSTALL: pkg})
47     virt_utils.virt_customize(virt_ops, image)
48
49
50 def inject_calipso_installer(tmp_dir, image):
51     """
52     Downloads calipso installer script from artifacts.opnfv.org
53     and puts it under /root/ for further installation process.
54     :return:
55     """
56     calipso_file = os.path.basename(con.CALIPSO_INSTALLER_URL)
57     calipso_url = con.CALIPSO_INSTALLER_URL.replace(calipso_file, '')
58     utils.fetch_upstream_and_unpack(tmp_dir, calipso_url, [calipso_file])
59
60     virt_ops = [
61         {con.VIRT_UPLOAD: "{}/{}:/root/".format(tmp_dir, calipso_file)}]
62     virt_utils.virt_customize(virt_ops, image)
63     logging.info("Calipso injected into {}".format(image))
64
65 # TODO(trozet): add unit testing for calipso injector
66 # TODO(trozet): add rest of build for undercloud here as well
67
68
69 def update_repos(image, branch):
70     virt_ops = [
71         {con.VIRT_RUN_CMD: "rm -f /etc/yum.repos.d/delorean*"},
72         {con.VIRT_RUN_CMD: "yum-config-manager --add-repo "
73                            "https://trunk.rdoproject.org/centos7/{}"
74                            "/delorean.repo".format(con.RDO_TAG)},
75         {con.VIRT_RUN_CMD: "yum clean all"},
76         {con.VIRT_INSTALL: "python2-tripleo-repos"},
77         {con.VIRT_RUN_CMD: "tripleo-repos -b {} {} ceph".format(branch,
78                                                                 con.RDO_TAG)}
79     ]
80     virt_utils.virt_customize(virt_ops, image)
81
82
83 def expand_disk(image, desired_size=50):
84     """
85     Expands a disk image to desired_size in GigaBytes
86     :param image: image to resize
87     :param desired_size: desired size in GB
88     :return: None
89     """
90     # there is a lib called vminspect which has some dependencies and is
91     # not yet available in pip. Consider switching to this lib later.
92     try:
93         img_out = json.loads(subprocess.check_output(
94             ['qemu-img', 'info', '--output=json', image],
95             stderr=subprocess.STDOUT).decode())
96         disk_gb_size = int(img_out['virtual-size'] / 1000000000)
97         if disk_gb_size < desired_size:
98             logging.info("Expanding disk image: {}. Current size: {} is less"
99                          "than require size: {}".format(image, disk_gb_size,
100                                                         desired_size))
101             diff_size = desired_size - disk_gb_size
102             subprocess.check_call(['qemu-img', 'resize', image,
103                                    "+{}G".format(diff_size)],
104                                   stderr=subprocess.STDOUT)
105
106     except (subprocess.CalledProcessError, json.JSONDecodeError, KeyError) \
107             as e:
108         logging.warning("Unable to resize disk, disk may not be large "
109                         "enough: {}".format(e))