+
+
+def build_dockerfile(service, tmp_dir, docker_cmds, src_image_uri):
+ """
+ Builds docker file per service and stores it in a
+ tmp_dir/containers/<service> directory. If the Dockerfile already exists,
+ simply append the docker cmds to it.
+ :param service: name of sub-directory to store Dockerfile in
+ :param tmp_dir: Temporary directory to store the container's dockerfile in
+ :param docker_cmds: List of commands to insert into the dockerfile
+ :param src_image_uri: Docker URI format for where the source image exists
+ :return: None
+ """
+ logging.debug("Building Dockerfile for {} with docker_cmds: {}".format(
+ service, docker_cmds))
+ c_dir = os.path.join(tmp_dir, 'containers')
+ service_dir = os.path.join(c_dir, service)
+ if not os.path.isdir(service_dir):
+ os.makedirs(service_dir, exist_ok=True)
+ from_cmd = "FROM {}\n".format(src_image_uri)
+ service_file = os.path.join(service_dir, 'Dockerfile')
+ assert isinstance(docker_cmds, list)
+ if os.path.isfile(service_file):
+ append_cmds = True
+ else:
+ append_cmds = False
+ with open(service_file, "a+") as fh:
+ if not append_cmds:
+ fh.write(from_cmd)
+ fh.write('\n'.join(docker_cmds))
+
+
+def archive_docker_patches(tmp_dir):
+ """
+ Archives Overcloud docker patches into a tar file for upload to Undercloud
+ :param tmp_dir: temporary directory where containers folder is stored
+ :return: None
+ """
+ container_path = os.path.join(tmp_dir, 'containers')
+ if not os.path.isdir(container_path):
+ raise ApexBuildException("Docker directory for patches not found: "
+ "{}".format(container_path))
+ archive_file = os.path.join(tmp_dir, 'docker_patches.tar.gz')
+ with tarfile.open(archive_file, "w:gz") as tar:
+ tar.add(container_path, arcname=os.path.basename(container_path))