-
Notifications
You must be signed in to change notification settings - Fork 4
/
write_manifest.py
executable file
·150 lines (129 loc) · 4.42 KB
/
write_manifest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import argparse
import datetime
import logging
from pathlib import Path
from docker.models.containers import Container
from tagging.docker_runner import DockerRunner
from tagging.get_platform import get_platform
from tagging.get_taggers_and_manifests import get_taggers_and_manifests
from tagging.git_helper import GitHelper
from tagging.manifests import ManifestHeader, ManifestInterface
LOGGER = logging.getLogger(__name__)
# We use a manifest creation timestamp, which happens right after a build
BUILD_TIMESTAMP = datetime.datetime.utcnow().isoformat()[:-7] + "Z"
MARKDOWN_LINE_BREAK = "<br />"
def write_build_history_line(
short_image_name: str,
registry: str,
owner: str,
hist_lines_dir: Path,
filename: str,
all_tags: list[str],
) -> None:
LOGGER.info("Appending build history line")
date_column = f"`{BUILD_TIMESTAMP}`"
image_column = MARKDOWN_LINE_BREAK.join(
f"`{registry}/{owner}/{short_image_name}:{tag_value}`" for tag_value in all_tags
)
commit_hash = GitHelper.commit_hash()
links_column = MARKDOWN_LINE_BREAK.join(
[
f"[Git diff](https://github.com/jupyter/docker-stacks/commit/{commit_hash})",
f"[Dockerfile](https://github.com/jupyter/docker-stacks/blob/{commit_hash}/images/{short_image_name}/Dockerfile)",
f"[Build manifest](./{filename})",
]
)
build_history_line = f"| {date_column} | {image_column} | {links_column} |"
hist_lines_dir.mkdir(parents=True, exist_ok=True)
(hist_lines_dir / f"{filename}.txt").write_text(build_history_line)
def write_manifest_file(
short_image_name: str,
registry: str,
owner: str,
manifests_dir: Path,
filename: str,
manifests: list[ManifestInterface],
container: Container,
) -> None:
manifest_names = [manifest.__class__.__name__ for manifest in manifests]
LOGGER.info(f"Using manifests: {manifest_names}")
markdown_pieces = [
ManifestHeader.create_header(short_image_name, registry, owner, BUILD_TIMESTAMP)
] + [manifest.markdown_piece(container) for manifest in manifests]
markdown_content = "\n\n".join(markdown_pieces) + "\n"
manifests_dir.mkdir(parents=True, exist_ok=True)
(manifests_dir / f"{filename}.md").write_text(markdown_content)
def write_manifest(
short_image_name: str,
registry: str,
owner: str,
hist_lines_dir: Path,
manifests_dir: Path,
) -> None:
LOGGER.info(f"Creating manifests for image: {short_image_name}")
taggers, manifests = get_taggers_and_manifests(short_image_name)
image = f"{registry}/{owner}/{short_image_name}:latest"
file_prefix = get_platform()
commit_hash_tag = GitHelper.commit_hash_tag()
filename = f"{file_prefix}-{short_image_name}-{commit_hash_tag}"
with DockerRunner(image) as container:
tags_prefix = get_platform()
all_tags = [
tags_prefix + "-" + tagger.tag_value(container) for tagger in taggers
]
write_build_history_line(
short_image_name, registry, owner, hist_lines_dir, filename, all_tags
)
write_manifest_file(
short_image_name,
registry,
owner,
manifests_dir,
filename,
manifests,
container,
)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"--short-image-name",
required=True,
help="Short image name",
)
arg_parser.add_argument(
"--hist-lines-dir",
required=True,
type=Path,
help="Directory to save history line",
)
arg_parser.add_argument(
"--manifests-dir",
required=True,
type=Path,
help="Directory to save manifest file",
)
arg_parser.add_argument(
"--registry",
required=True,
type=str,
choices=["docker.io", "quay.io"],
help="Image registry",
)
arg_parser.add_argument(
"--owner",
required=True,
help="Owner of the image",
)
args = arg_parser.parse_args()
LOGGER.info(f"Current build timestamp: {BUILD_TIMESTAMP}")
write_manifest(
args.short_image_name,
args.registry,
args.owner,
args.hist_lines_dir,
args.manifests_dir,
)