2021-07-31 15:05:22 +08:00
|
|
|
import argparse
|
|
|
|
import docker
|
|
|
|
import gzip
|
2021-08-10 11:41:27 +08:00
|
|
|
import os
|
2021-07-31 15:05:22 +08:00
|
|
|
import yaml
|
|
|
|
from nested_lookup import nested_lookup
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(
|
2021-08-10 11:41:27 +08:00
|
|
|
description="Save Docker images")
|
2021-07-31 15:05:22 +08:00
|
|
|
|
|
|
|
parser.add_argument("--manifest",
|
|
|
|
required=True,
|
|
|
|
help="Path to the manifest yaml")
|
2021-08-10 11:41:27 +08:00
|
|
|
parser.add_argument("--save_path",
|
|
|
|
type=str,
|
|
|
|
default='images',
|
|
|
|
help='Directory to save images to')
|
2021-07-31 15:05:22 +08:00
|
|
|
arguments = parser.parse_args()
|
|
|
|
|
|
|
|
with open(arguments.manifest, 'r') as file:
|
|
|
|
template = file.read()
|
2021-08-10 11:41:27 +08:00
|
|
|
|
2021-07-31 15:05:22 +08:00
|
|
|
images=[]
|
|
|
|
parts = template.split('---')
|
|
|
|
for p in parts:
|
|
|
|
y = yaml.safe_load(p)
|
|
|
|
matches = nested_lookup("image", y)
|
|
|
|
if (len(matches)):
|
|
|
|
images += matches
|
2021-08-10 11:41:27 +08:00
|
|
|
|
|
|
|
save_path = arguments.save_path
|
|
|
|
if not os.path.isdir(save_path):
|
|
|
|
os.mkdir(save_path)
|
|
|
|
|
2021-07-31 15:05:22 +08:00
|
|
|
client = docker.from_env()
|
|
|
|
for image_name in set(images):
|
|
|
|
file_name = (image_name.split(':')[0].replace("/", "-"))
|
2021-08-10 11:41:27 +08:00
|
|
|
f = gzip.open(save_path + "/" + file_name + '.tar.gz', 'wb')
|
2021-07-31 15:05:22 +08:00
|
|
|
try:
|
|
|
|
image = client.images.get(image_name)
|
|
|
|
if image.id:
|
|
|
|
print ("docker image \"" + image_name + "\" already exists.")
|
|
|
|
except docker.errors.ImageNotFound:
|
|
|
|
print ("docker pull " + image_name + " ...")
|
|
|
|
image = client.images.pull(image_name)
|
|
|
|
image_tar = image.save(named=True)
|
|
|
|
f.writelines(image_tar)
|
|
|
|
f.close()
|
2021-08-10 11:41:27 +08:00
|
|
|
|
|
|
|
print("Save docker images to \"" + save_path + "\"")
|