summaryrefslogtreecommitdiffstats
path: root/apex/builders/undercloud_builder.py
blob: 47d2568dac1acc187bcd6baf7e69ba4502c481de (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
##############################################################################
# Copyright (c) 2017 Tim Rozet (trozet@redhat.com) and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

# Used to modify undercloud qcow2 image
import logging
import json
import os
import subprocess

from apex.common import constants as con
from apex.common import utils
from apex.virtual import utils as virt_utils


def add_upstream_packages(image):
    """
    Adds required base upstream packages to Undercloud for deployment
    :param image:
    :return: None
    """
    virt_ops = list()
    pkgs = [
        'epel-release',
        'openstack-utils',
        'python2-networking-sfc',
        'openstack-ironic-inspector',
        'subunit-filters',
        'docker-distribution',
        'openstack-tripleo-validations',
        'libguestfs-tools',
        'python-tripleoclient',
        'openstack-tripleo-heat-templates'
    ]
    # Remove incompatible python-docker version
    virt_ops.append({con.VIRT_RUN_CMD: "yum remove -y python-docker-py"})

    for pkg in pkgs:
        virt_ops.append({con.VIRT_INSTALL: pkg})
    virt_utils.virt_customize(virt_ops, image)


def inject_calipso_installer(tmp_dir, image):
    """
    Downloads calipso installer script from artifacts.opnfv.org
    and puts it under /root/ for further installation process.
    :return:
    """
    calipso_file = os.path.basename(con.CALIPSO_INSTALLER_URL)
    calipso_url = con.CALIPSO_INSTALLER_URL.replace(calipso_file, '')
    utils.fetch_upstream_and_unpack(tmp_dir, calipso_url, [calipso_file])

    virt_ops = [
        {con.VIRT_UPLOAD: "{}/{}:/root/".format(tmp_dir, calipso_file)}]
    virt_utils.virt_customize(virt_ops, image)
    logging.info("Calipso injected into {}".format(image))

# TODO(trozet): add unit testing for calipso injector
# TODO(trozet): add rest of build for undercloud here as well


def update_repos(image, branch):
    virt_ops = [
        {con.VIRT_RUN_CMD: "rm -f /etc/yum.repos.d/delorean*"},
        {con.VIRT_RUN_CMD: "yum-config-manager --add-repo "
                           "https://trunk.rdoproject.org/centos7/{}"
                           "/delorean.repo".format(con.RDO_TAG)},
        {con.VIRT_RUN_CMD: "yum clean all"},
        {con.VIRT_INSTALL: "python2-tripleo-repos"},
        {con.VIRT_RUN_CMD: "tripleo-repos -b {} {} ceph".format(branch,
                                                                con.RDO_TAG)}
    ]
    virt_utils.virt_customize(virt_ops, image)


def expand_disk(image, desired_size=50):
    """
    Expands a disk image to desired_size in GigaBytes
    :param image: image to resize
    :param desired_size: desired size in GB
    :return: None
    """
    # there is a lib called vminspect which has some dependencies and is
    # not yet available in pip. Consider switching to this lib later.
    try:
        img_out = json.loads(subprocess.check_output(
            ['qemu-img', 'info', '--output=json', image],
            stderr=subprocess.STDOUT).decode())
        disk_gb_size = int(img_out['virtual-size'] / 1000000000)
        if disk_gb_size < desired_size:
            logging.info("Expanding disk image: {}. Current size: {} is less"
                         "than require size: {}".format(image, disk_gb_size,
                                                        desired_size))
            diff_size = desired_size - disk_gb_size
            subprocess.check_call(['qemu-img', 'resize', image,
                                   "+{}G".format(diff_size)],
                                  stderr=subprocess.STDOUT)

    except (subprocess.CalledProcessError, json.JSONDecodeError, KeyError) \
            as e:
        logging.warning("Unable to resize disk, disk may not be large "
                        "enough: {}".format(e))