blob: dfe4ed780fac29b7a60779ee0b733255503a3610 [file] [log] [blame]
#! /usr/bin/python2.7
import argparse
import os
import re
import subprocess
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-d', dest='directory', help='Directory location to store ONAP docker images')
parser.add_argument('-l', dest='docker_images_list', help='List of ONAP docker images')
parser.add_argument('-s', dest='source_registry', default=os.path.join("registry.nordix.org", "onap", "online-install"),
help='Source registry')
parser.add_argument('-t', dest='target_registry', default="nexus3.onap.org:10001", help='Source registry')
args = parser.parse_args()
def get_image_list_variables(image_list):
variable_pattern = re.compile("^# @.*=(.*)$")
nordix_promotion_id = None
onap_integration_repo_sha = None
onap_oom_repo_sha = None
onap_offline_installer_repo_sha = None
with open(image_list) as f:
for line in f.readlines():
if line.startswith("# @NORDIX_PROMOTION_ID"):
matches = re.match(variable_pattern, line)
nordix_promotion_id = matches.group(0)
elif line.startswith("# @ONAP_INTEGRATION_REPO_SHA"):
matches = re.match(variable_pattern, line)
onap_integration_repo_sha = matches.group(0)
elif line.startswith("# @ONAP_OOM_REPO_SHA"):
matches = re.match(variable_pattern, line)
onap_oom_repo_sha = matches.group(0)
elif line.startswith("# @ONAP_OFFLINE_INSTALLER_REPO_SHA"):
matches = re.match(variable_pattern, line)
onap_offline_installer_repo_sha = matches.group(0)
print("NORDIX_PROMOTION_ID: {nordix_promo}\nONAP_INTEGRATION_REPO_SHA: {onap_int_repo}".format(
nordix_promo=nordix_promotion_id, onap_int_repo=onap_integration_repo_sha))
print("ONAP_OOM_REPO_SHA: {onap_oom_repo}\nONAP_OFFLINE_INSTALLER_REPO_SHA: {onap_offline_inst_repo}".format(
onap_oom_repo=onap_oom_repo_sha, onap_offline_inst_repo=onap_offline_installer_repo_sha))
return nordix_promotion_id, onap_integration_repo_sha, onap_oom_repo_sha, onap_offline_installer_repo_sha
def get_image_tokens(image_list):
tokens = []
with open(image_list) as f:
for line in f.readlines():
if not line.startswith("#"):
pattern = re.compile("^(.*):([0-9.]*)\\s#(.*)$")
matches = re.match(pattern, line)
tokens.append(matches.groups())
return tokens
def docker_save_images(tokens, directory, source, target):
if not os.path.exists(directory):
os.makedirs(directory)
for token in tokens:
docker_pull(token)
docker_tag(token, source, target)
docker_save(token, directory, source, target)
docker_remove_image(token)
def docker_save(token, directory, source_reg, target_reg):
image, version, sha = token
print("Saving docker image: {image} (Version: {version})".format(image=image, version=version))
image_target = "{dir}{sep}{image}_{tag}.tar".format(dir=directory, sep=os.path.sep, image=image.replace(
source_reg, target_reg).replace(os.path.sep, "_").replace(":", "_"), tag=version)
if not os.path.exists(image_target):
try:
subprocess.check_output(['docker', 'save', '{image}:{tag}'.format(image=image, tag=version), '-o',
image_target])
except subprocess.CalledProcessError as e:
print("Error saving docker file ({file}): {error}".format(file=image_target, error=e.message))
def docker_tag(token, source_reg, target_reg):
image, version, sha = token
target_image = image.replace(source_reg, target_reg)
print("Tagging docker image...\n\t/Source: {image} (SHA: {sha}, Version: {version}\n\tTarget: {target})".format(
image=image, sha=sha, version=version, target=target_image))
try:
subprocess.check_output(['docker', 'tag', '{image}@{tag}'.format(image=image, tag=sha),
'{image}:{tag}'.format(image=target_image, tag=version)])
except subprocess.CalledProcessError as e:
print("Error tagging docker image ({image}): {error}".format(image=image, error=e.message))
def docker_remove_image(token):
image, version, sha = token
print("Removing docker image: {image}:{version})".format(image=image, version=version))
try:
subprocess.check_output(['docker', 'rmi', '{image}:{tag}'.format(image=image, tag=version)])
except subprocess.CalledProcessError as e:
print("Error tagging docker image ({image}): {error}".format(image=image, error=e.message))
def docker_pull(token):
image, version, sha = token
print("Pulling docker image: {image} (Version: {version})".format(image=image, version=version))
try:
subprocess.check_output(['docker', 'pull', '{image}:{tag}'.format(image=image, tag=version)])
except subprocess.CalledProcessError as e:
print("Error pulling docker image ({image}): {error}".format(image=image, error=e.message))
nordix_promotion_id, onap_integration_repo_sha, onap_oom_repo_sha, onap_offline_installer_repo_sha = \
get_image_list_variables(args.docker_images_list)
image_tokens = get_image_tokens(args.docker_images_list)
docker_save_images(image_tokens, args.directory, args.source_registry, args.target_registry)