922d752627944c081ba5d45863a4922598f01925
[pti/rtp.git] /
1 From ecdbdc0db662ffb5abb6eca9c84d5307fabad0f6 Mon Sep 17 00:00:00 2001
2 From: Jackie Huang <jackie.huang@windriver.com>
3 Date: Wed, 19 Jan 2022 04:49:59 -0500
4 Subject: [PATCH 2/2] download_images: add support to load image from offline
5  file
6
7 Add support to load image from offline file, the steps to use
8 this feature:
9 1. Get and place all offline image files in a dir:
10    e.g. /home/sysadmin/docker_images
11
12 2. Add the following line in local.conf:
13    offline_img_dir: /home/sysadmin/docker_images
14
15 3. run bootstrap playbook as usual.
16
17 Signed-off-by: Jackie Huang <jackie.huang@windriver.com>
18 ---
19  .../files/download_images.py                  | 28 +++++++++++++++----
20  .../common/push-docker-images/tasks/main.yml  |  2 ++
21  2 files changed, 25 insertions(+), 5 deletions(-)
22
23 diff --git a/playbookconfig/src/playbooks/roles/common/push-docker-images/files/download_images.py b/playbookconfig/src/playbooks/roles/common/push-docker-images/files/download_images.py
24 index e4239188..8e47a28b 100644
25 --- a/playbookconfig/src/playbooks/roles/common/push-docker-images/files/download_images.py
26 +++ b/playbookconfig/src/playbooks/roles/common/push-docker-images/files/download_images.py
27 @@ -43,6 +43,8 @@ registries = json.loads(os.environ['REGISTRIES'])
28  add_docker_prefix = False
29  crictl_image_list = []
30  
31 +offline_img_suffix = ['.tar.gz', '.tar.bz2', '.tar']
32 +offline_img_dir = os.environ.get('OFFLINE_IMG_DIR', '')
33  
34  def get_local_registry_auth():
35      password = keyring.get_password("sysinv", "services")
36 @@ -50,6 +52,16 @@ def get_local_registry_auth():
37          raise Exception("Local registry password not found.")
38      return dict(username="sysinv", password=str(password))
39  
40 +def get_offline_img_file(img):
41 +    if not offline_img_dir:
42 +        return False
43 +
44 +    for suffix in offline_img_suffix:
45 +        img_file_name = img.replace("/", "_").replace(":", "_") + suffix
46 +        img_file = os.path.join(offline_img_dir, img_file_name)
47 +        if os.path.exists(img_file):
48 +            return img_file
49 +    return False
50  
51  def convert_img_for_local_lookup(img):
52      # This function converts the given image reference to the
53 @@ -178,6 +190,7 @@ def download_and_push_an_image(img):
54      local_img = convert_img_for_local_lookup(img)
55      target_img = get_img_tag_with_registry(img)
56      err_msg = " Image download failed: %s " % target_img
57 +    offline_img_file = get_offline_img_file(img)
58  
59      client = docker.APIClient()
60      auth = get_local_registry_auth()
61 @@ -201,13 +214,18 @@ def download_and_push_an_image(img):
62          return target_img, True
63      except docker.errors.APIError as e:
64          print(str(e))
65 -        print("Image %s not found on local registry, attempt to download..."
66 +        print("Image %s not found on local registry, attempt to load from offline file or download..."
67                % target_img)
68          try:
69 -            response = client.pull(target_img)
70 -            check_response(response)
71 -            print("Image download succeeded: %s" % target_img)
72 -            client.tag(target_img, local_img)
73 +            if offline_img_file:
74 +                with open(offline_img_file, 'rb') as f:
75 +                    client.load_image(f)
76 +                    print("Image loaded from offline file (%s) succeeded: %s" % (offline_img_file, local_img))
77 +            else:
78 +                response = client.pull(target_img)
79 +                check_response(response)
80 +                print("Image download succeeded: %s" % target_img)
81 +                client.tag(target_img, local_img)
82              client.push(local_img, auth_config=auth)
83              print("Image push succeeded: %s" % local_img)
84  
85 diff --git a/playbookconfig/src/playbooks/roles/common/push-docker-images/tasks/main.yml b/playbookconfig/src/playbooks/roles/common/push-docker-images/tasks/main.yml
86 index 1429b9c9..be9e9a49 100644
87 --- a/playbookconfig/src/playbooks/roles/common/push-docker-images/tasks/main.yml
88 +++ b/playbookconfig/src/playbooks/roles/common/push-docker-images/tasks/main.yml
89 @@ -260,6 +260,7 @@
90  - block:
91    - set_fact:
92        download_images: "{{ download_images_list | join(',') }}"
93 +      offline_img_dir: "{{ offline_img_dir | default('') }}"
94  
95    - name: "{{ download_images_task_name }}"
96      script: download_images.py {{ download_images }}
97 @@ -271,6 +272,7 @@
98      environment:
99        REGISTRIES: "{{ registries | to_json }}"
100        ADD_DOCKER_PREFIX: "{{ add_docker_prefix }}"
101 +      OFFLINE_IMG_DIR: "{{ offline_img_dir }}"
102  
103    - debug:
104        msg: "{{ download_images_output.stdout_lines }}"
105 -- 
106 2.30.2
107