diff options
author | Utku Demir <me@utdemir.com> | 2020-06-21 11:46:15 +1200 |
---|---|---|
committer | Utku Demir <me@utdemir.com> | 2020-06-21 12:35:38 +1200 |
commit | 26402290bfdec5b012dc66c1d0418ad7ffdb284e (patch) | |
tree | 16a1ba8bd45fd3c35872cd59060cfe1c3608073f | |
parent | 307804d97d1b2b81dcb792cee62afdad435b28a8 (diff) |
stream_layered_image: Add main method
-rw-r--r-- | pkgs/build-support/docker/stream_layered_image.py | 127 |
1 files changed, 65 insertions, 62 deletions
diff --git a/pkgs/build-support/docker/stream_layered_image.py b/pkgs/build-support/docker/stream_layered_image.py index 6687f49e1bc81..44c6cbba21c65 100644 --- a/pkgs/build-support/docker/stream_layered_image.py +++ b/pkgs/build-support/docker/stream_layered_image.py @@ -181,70 +181,73 @@ def add_bytes(tar, path, content, mtime): tar.addfile(ti, io.BytesIO(content)) -# Main - -with open(sys.argv[1], "r") as f: - conf = json.load(f) - -created = ( - datetime.now(tz=datetime.timezone.utc) - if conf["created"] == "now" - else datetime.fromisoformat(conf["created"]) -) -mtime = int(created.timestamp()) - -with tarfile.open(mode="w|", fileobj=sys.stdout.buffer) as tar: - layers = [] - for num, store_layer in enumerate(conf["store_layers"]): - print( - "Creating layer", num, - "from paths:", store_layer, - file=sys.stderr) - info = add_layer_dir(tar, store_layer, mtime=mtime) - layers.append(info) - - print("Creating the customisation layer...", file=sys.stderr) - layers.append( - add_customisation_layer( - tar, - conf["customisation_layer"], - mtime=mtime - ) +def main(): + with open(sys.argv[1], "r") as f: + conf = json.load(f) + + created = ( + datetime.now(tz=datetime.timezone.utc) + if conf["created"] == "now" + else datetime.fromisoformat(conf["created"]) ) + mtime = int(created.timestamp()) + + with tarfile.open(mode="w|", fileobj=sys.stdout.buffer) as tar: + layers = [] + for num, store_layer in enumerate(conf["store_layers"]): + print( + "Creating layer", num, + "from paths:", store_layer, + file=sys.stderr) + info = add_layer_dir(tar, store_layer, mtime=mtime) + layers.append(info) + + print("Creating the customisation layer...", file=sys.stderr) + layers.append( + add_customisation_layer( + tar, + conf["customisation_layer"], + mtime=mtime + ) + ) + + print("Adding manifests...", file=sys.stderr) + + image_json = { + "created": datetime.isoformat(created), + "architecture": conf["architecture"], + "os": "linux", + "config": conf["config"], + "rootfs": { + "diff_ids": [f"sha256:{layer.checksum}" for layer in layers], + "type": "layers", + }, + "history": [ + { + "created": conf["created"], + "comment": f"store paths: {layer.paths}" + } + for layer in layers + ], + } + + image_json = json.dumps(image_json, indent=4).encode("utf-8") + image_json_checksum = hashlib.sha256(image_json).hexdigest() + image_json_path = f"{image_json_checksum}.json" + add_bytes(tar, image_json_path, image_json, mtime=mtime) - print("Adding manifests...", file=sys.stderr) - - image_json = { - "created": datetime.isoformat(created), - "architecture": conf["architecture"], - "os": "linux", - "config": conf["config"], - "rootfs": { - "diff_ids": [f"sha256:{layer.checksum}" for layer in layers], - "type": "layers", - }, - "history": [ + manifest_json = [ { - "created": conf["created"], - "comment": f"store paths: {layer.paths}" + "Config": image_json_path, + "RepoTags": [conf["repo_tag"]], + "Layers": [layer.path for layer in layers], } - for layer in layers - ], - } - - image_json = json.dumps(image_json, indent=4).encode("utf-8") - image_json_checksum = hashlib.sha256(image_json).hexdigest() - image_json_path = f"{image_json_checksum}.json" - add_bytes(tar, image_json_path, image_json, mtime=mtime) - - manifest_json = [ - { - "Config": image_json_path, - "RepoTags": [conf["repo_tag"]], - "Layers": [layer.path for layer in layers], - } - ] - manifest_json = json.dumps(manifest_json, indent=4).encode("utf-8") - add_bytes(tar, "manifest.json", manifest_json, mtime=mtime) + ] + manifest_json = json.dumps(manifest_json, indent=4).encode("utf-8") + add_bytes(tar, "manifest.json", manifest_json, mtime=mtime) + + print("Done.", file=sys.stderr) + - print("Done.", file=sys.stderr) +if __name__ == "__main__": + main() |