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 ##############################################################################
10 # Used to modify overcloud qcow2 image
16 import apex.builders.common_builder
17 from apex.common import constants as con
18 from apex.common import utils as utils
19 from apex.common.exceptions import ApexBuildException
20 from apex.virtual import utils as virt_utils
23 def inject_opendaylight(odl_version, image, tmp_dir, uc_ip,
24 os_version, docker_tag=None):
25 assert odl_version in con.VALID_ODL_VERSIONS
27 if odl_version == 'master':
28 odl_pkg_version = con.VALID_ODL_VERSIONS[-2]
31 odl_pkg_version = odl_version
32 branch = "stable/{}".format(odl_version)
33 odl_url = "https://nexus.opendaylight.org/content/repositories" \
34 "/opendaylight-{}-epel-7-x86_64-devel/".format(odl_pkg_version)
35 repo_name = "opendaylight-{}".format(odl_pkg_version)
36 apex.builders.common_builder.add_repo(odl_url, repo_name, image, tmp_dir)
37 # download puppet-opendaylight
38 archive = apex.builders.common_builder.create_git_archive(
39 repo_url=con.PUPPET_ODL_URL, repo_name='puppet-opendaylight',
40 tmp_dir=tmp_dir, branch=branch, prefix='opendaylight/')
41 # install ODL, puppet-odl
43 {con.VIRT_UPLOAD: "{}:/etc/puppet/modules/".format(archive)},
44 {con.VIRT_RUN_CMD: 'rm -rf /etc/puppet/modules/opendaylight'},
45 {con.VIRT_RUN_CMD: "cd /etc/puppet/modules/ && tar xvf "
46 "puppet-opendaylight.tar"},
47 {con.VIRT_INSTALL: "java-1.8.0-openjdk"}
51 "RUN yum remove opendaylight -y",
52 "RUN echo $'[opendaylight]\\n\\",
53 "baseurl={}\\n\\".format(odl_url),
55 "enabled=1' > /etc/yum.repos.d/opendaylight.repo",
56 "RUN yum -y install opendaylight"
58 src_img_uri = "{}:8787/tripleo{}/centos-binary-{}:" \
59 "{}".format(uc_ip, os_version, 'opendaylight',
61 build_dockerfile('opendaylight', tmp_dir, docker_cmds, src_img_uri)
63 virt_ops.append({con.VIRT_INSTALL: 'opendaylight'})
64 virt_utils.virt_customize(virt_ops, image)
65 logging.info("OpenDaylight injected into {}".format(image))
68 def inject_quagga(image, tmp_dir):
70 Downloads quagga tarball from artifacts.opnfv.org
71 and install it on the overcloud image on the fly.
76 utils.fetch_upstream_and_unpack(tmp_dir,
77 os.path.split(con.QUAGGA_URL)[0] + "/",
78 [os.path.basename(con.QUAGGA_URL)])
81 {con.VIRT_UPLOAD: "{}/quagga-4.tar.gz:/root/".format(tmp_dir)},
82 {con.VIRT_RUN_CMD: "cd /root/ && tar xzf quagga-4.tar.gz"},
83 {con.VIRT_RUN_CMD: "cd /root/quagga;packages=$(ls |grep -vE 'debug"
84 "info|devel|contrib');yum -y install $packages"}
86 virt_utils.virt_customize(virt_ops, image)
87 logging.info("Quagga injected into {}".format(image))
90 def build_dockerfile(service, tmp_dir, docker_cmds, src_image_uri):
92 Builds docker file per service and stores it in a
93 tmp_dir/containers/<service> directory. If the Dockerfile already exists,
94 simply append the docker cmds to it.
95 :param service: name of sub-directory to store Dockerfile in
96 :param tmp_dir: Temporary directory to store the container's dockerfile in
97 :param docker_cmds: List of commands to insert into the dockerfile
98 :param src_image_uri: Docker URI format for where the source image exists
101 logging.debug("Building Dockerfile for {} with docker_cmds: {}".format(
102 service, docker_cmds))
103 c_dir = os.path.join(tmp_dir, 'containers')
104 service_dir = os.path.join(c_dir, service)
105 if not os.path.isdir(service_dir):
106 os.makedirs(service_dir, exist_ok=True)
107 from_cmd = "FROM {}\n".format(src_image_uri)
108 service_file = os.path.join(service_dir, 'Dockerfile')
109 assert isinstance(docker_cmds, list)
110 if os.path.isfile(service_file):
114 with open(service_file, "a+") as fh:
117 fh.write('\n'.join(docker_cmds))
120 def archive_docker_patches(tmp_dir):
122 Archives Overcloud docker patches into a tar file for upload to Undercloud
123 :param tmp_dir: temporary directory where containers folder is stored
126 container_path = os.path.join(tmp_dir, 'containers')
127 if not os.path.isdir(container_path):
128 raise ApexBuildException("Docker directory for patches not found: "
129 "{}".format(container_path))
130 archive_file = os.path.join(tmp_dir, 'docker_patches.tar.gz')
131 with tarfile.open(archive_file, "w:gz") as tar:
132 tar.add(container_path, arcname=os.path.basename(container_path))