meta-starlingx/meta-stx-flock/stx-ansible-playbooks/files/0009-download_images-add-support-to-load-image-offline-fi.patch
Jackie Huang b48d6097a4 playbookconfig: set default values for offline_img_dir
Set default values for offline_img_dir if it is undefined.

Closes-bug: 1966377

Signed-off-by: Jackie Huang <jackie.huang@windriver.com>
Change-Id: Ifb284c753449864ec9a792fc353d5711ce8ab2df
2022-03-25 14:12:00 +08:00

108 lines
4.5 KiB
Diff

From 79662e16a5a8d08e7dd24613f9afde23dfb26329 Mon Sep 17 00:00:00 2001
From: Jackie Huang <jackie.huang@windriver.com>
Date: Fri, 26 Nov 2021 16:14:32 +0800
Subject: [PATCH] download_images: add support to load image offline file
Add support to load image offline file, the steps to use
this feature:
1. Get and place all offline image files in a dir:
e.g. /home/sysadmin/docker_images_stx50
2. Add the following line in local.conf:
offline_img_dir: /home/sysadmin/docker_images_stx50
3. run bootstrap playbook as usual.
Upstream-Status: Pending
Signed-off-by: Jackie Huang <jackie.huang@windriver.com>
---
.../push-docker-images/files/download_images.py | 26 ++++++++++++++++++----
.../roles/common/push-docker-images/tasks/main.yml | 5 +++++
2 files changed, 27 insertions(+), 4 deletions(-)
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
index 7793820..e59dd89 100644
--- 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
@@ -28,6 +28,8 @@ DEFAULT_REGISTRIES = {
registries = json.loads(os.environ['REGISTRIES'])
+offline_img_suffix = ['.tar.gz', '.tar.bz2', '.tar']
+offline_img_dir = os.environ.get('OFFLINE_IMG_DIR', '')
def get_local_registry_auth():
password = keyring.get_password("sysinv", "services")
@@ -35,6 +37,16 @@ def get_local_registry_auth():
raise Exception("Local registry password not found.")
return dict(username="sysinv", password=str(password))
+def get_offline_img_file(img):
+ if not offline_img_dir:
+ return False
+
+ for suffix in offline_img_suffix:
+ img_file_name = img.replace("/", "_").replace(":", "_") + suffix
+ img_file = os.path.join(offline_img_dir, img_file_name)
+ if os.path.exists(img_file):
+ return img_file
+ return False
def download_an_image(img):
# This function is to pull image from public/private
@@ -63,6 +75,7 @@ def download_an_image(img):
target_img = get_img_tag_with_registry(img)
local_img = 'registry.local:9001/' + new_img
err_msg = " Image download failed: %s" % target_img
+ offline_img_file = get_offline_img_file(img)
client = docker.APIClient()
auth = get_local_registry_auth()
@@ -73,12 +86,17 @@ def download_an_image(img):
return target_img, True
except docker.errors.APIError as e:
print(str(e))
- print("Image {} not found on local registry, attempt to download...".format(target_img))
+ print("Image {} not found on local registry, attempt to load from file or download...".format(target_img))
for i in range(MAX_DOWNLOAD_ATTEMPTS):
try:
- client.pull(target_img)
- print("Image download succeeded: %s" % target_img)
- client.tag(target_img, local_img)
+ if offline_img_file:
+ with open(offline_img_file, 'rb') as f:
+ client.load_image(f)
+ print("Image loaded from file (%s) succeeded: %s" % (offline_img_file, local_img))
+ else:
+ client.pull(target_img)
+ print("Image download succeeded: %s" % target_img)
+ client.tag(target_img, local_img)
client.push(local_img, auth_config=auth)
print("Image push succeeded: %s" % local_img)
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
index e1738bf..b4e755d 100644
--- a/playbookconfig/src/playbooks/roles/common/push-docker-images/tasks/main.yml
+++ b/playbookconfig/src/playbooks/roles/common/push-docker-images/tasks/main.yml
@@ -129,11 +129,16 @@
local_registry_credentials: "{{ local_registry_credentials_output.stdout }}"
when: local_registry_credentials is not defined
+- name: Set default values for offline_img_dir if it is undefined
+ set_fact:
+ offline_img_dir: "{{ offline_img_dir | default('') }}"
+
- name: Download images and push to local registry
script: download_images.py {{ download_images }}
register: download_images_output
environment:
REGISTRIES: "{{ registries | to_json }}"
+ OFFLINE_IMG_DIR: "{{ offline_img_dir }}"
- debug:
msg: "{{ download_images_output.stdout_lines }}"
--
2.7.4