add ccsdk-sli-northbound
[infra/cicd.git] / jjb / onap-offline / build_jobs / scripts / docker_sha_downloader.py
1 import argparse
2 import os
3 import re
4 import subprocess
5
6 parser = argparse.ArgumentParser()
7
8 parser.add_argument('-d', action='store', dest='directory', help='Directory location to store ONAP docker images')
9 parser.add_argument('-l', action='store', dest='docker_images_list', help='List of ONAP docker images')
10 args = parser.parse_args()
11
12
13 def get_image_tokens(image_list):
14     tokens = []
15     with open(image_list) as f:
16         for line in f.readlines():
17             pattern = re.compile("^(.*):([0-9.]*)\\s#(.*)$")
18             matches = re.match(pattern, line)
19             tokens.append(matches.groups())
20
21     return tokens
22
23
24 def docker_save_images(tokens, directory):
25     if not os.path.exists(directory):
26         os.makedirs(directory)
27
28     for token in tokens:
29         docker_pull(token)
30         docker_tag(token)
31         docker_save(token, directory)
32         docker_remove_image(token)
33
34
35 def docker_save(token, directory):
36     image, version, sha = token
37
38     print("Saving docker image: {image} (Version: {version})".format(image=image, version=version))
39
40     src_str = os.path.join("registry.nordix.org", "onap", "online-install")
41     repl_str = "nexus.onap.org:10001"
42     image_target = "{dir}{sep}{image}_{tag}.tar".format(dir=directory, sep=os.path.sep, image=image.replace(
43         src_str, repl_str).replace(os.path.sep, "_").replace(":", "_"), tag=version)
44
45     if not os.path.exists(image_target):
46         try:
47             subprocess.check_output(['docker', 'save', '{image}:{tag}'.format(image=image, tag=version), '-o',
48                                      image_target])
49         except subprocess.CalledProcessError as e:
50             print("Error saving docker file ({file}): {error}".format(file=image_target, error=e.message))
51
52
53 def docker_tag(token):
54     image, version, sha = token
55
56     target_image = image.replace("registry.nordix.org/onap/online-install", "nexus.onap.org:10001")
57
58     print("Tagging docker image...\n\t/Source: {image} (SHA: {sha}, Version: {version}\n\tTarget: {target})".format(
59         image=image, sha=sha, version=version, target=target_image))
60     try:
61         subprocess.check_output(['docker', 'tag', '{image}@{tag}'.format(image=image, tag=sha),
62                                  '{image}:{tag}'.format(image=target_image, tag=version)])
63     except subprocess.CalledProcessError as e:
64         print("Error tagging docker image ({image}): {error}".format(image=image, error=e.message))
65
66
67 def docker_remove_image(token):
68     image, version, sha = token
69
70     print("Removing docker image: {image}:{version})".format(image=image, version=version))
71     try:
72         subprocess.check_output(['docker', 'rmi', '{image}:{tag}'.format(image=image, tag=version)])
73     except subprocess.CalledProcessError as e:
74         print("Error tagging docker image ({image}): {error}".format(image=image, error=e.message))
75
76
77 def docker_pull(token):
78     image, version, sha = token
79
80     print("Pulling docker image: {image} (Version: {version})".format(image=image, version=version))
81     try:
82         subprocess.check_output(['docker', 'pull', '{image}:{tag}'.format(image=image, tag=version)])
83     except subprocess.CalledProcessError as e:
84         print("Error pulling docker image ({image}): {error}".format(image=image, error=e.message))
85
86
87 image_tokens = get_image_tokens(args.docker_images_list)
88 docker_save_images(image_tokens, args.directory)