Skip to content

Commit 7e2c1bd

Browse files
fix: removed hardcoding from docker-image dir and kinds
1 parent f8c4bfb commit 7e2c1bd

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed

src/taskgraph/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
# The trust-domain for this graph.
2727
# (See https://firefox-source-docs.mozilla.org/taskcluster/taskcluster/taskgraph.html#taskgraph-trust-domain) # noqa
2828
Required("trust-domain"): str,
29+
Optional(
30+
"docker-image-kind",
31+
description="What kind of docker image to use for the task.",
32+
): [str],
2933
Required("task-priority"): optionally_keyed_by(
3034
"project",
3135
"level",
@@ -157,6 +161,10 @@ def vcs_root(self):
157161
def taskcluster_yml(self):
158162
return os.path.join(self.vcs_root, ".taskcluster.yml")
159163

164+
@property
165+
def docker_dir(self):
166+
return os.path.join(self.root_dir, "docker")
167+
160168

161169
def validate_graph_config(config):
162170
validate_schema(graph_config_schema, config, "Invalid graph configuration:")

src/taskgraph/docker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,29 @@ def load_image_by_task_id(task_id, tag=None):
9797
return tag
9898

9999

100-
def build_context(name, outputFile, args=None):
100+
def build_context(name, outputFile, args=None, graph_config=None):
101101
"""Build a context.tar for image with specified name."""
102102
if not name:
103103
raise ValueError("must provide a Docker image name")
104104
if not outputFile:
105105
raise ValueError("must provide a outputFile")
106106

107-
image_dir = docker.image_path(name)
107+
image_dir = docker.image_path(name, graph_config)
108108
if not os.path.isdir(image_dir):
109109
raise Exception(f"image directory does not exist: {image_dir}")
110110

111111
docker.create_context_tar(".", image_dir, outputFile, args)
112112

113113

114-
def build_image(name, tag, args=None):
114+
def build_image(name, tag, args=None, graph_config=None):
115115
"""Build a Docker image of specified name.
116116
117117
Output from image building process will be printed to stdout.
118118
"""
119119
if not name:
120120
raise ValueError("must provide a Docker image name")
121121

122-
image_dir = docker.image_path(name)
122+
image_dir = docker.image_path(name, graph_config)
123123
if not os.path.isdir(image_dir):
124124
raise Exception(f"image directory does not exist: {image_dir}")
125125

src/taskgraph/main.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,12 @@ def show_taskgraph(options):
565565

566566
@command("build-image", help="Build a Docker image")
567567
@argument("image_name", help="Name of the image to build")
568+
@argument(
569+
"--root",
570+
"-r",
571+
default="taskcluster",
572+
help="relative path for the root of the taskgraph definition",
573+
)
568574
@argument(
569575
"-t", "--tag", help="tag that the image should be built as.", metavar="name:tag"
570576
)
@@ -575,13 +581,20 @@ def show_taskgraph(options):
575581
metavar="context.tar",
576582
)
577583
def build_image(args):
584+
from taskgraph.config import load_graph_config # noqa: PLC0415
578585
from taskgraph.docker import build_context, build_image # noqa: PLC0415
579586

580587
validate_docker()
588+
589+
root = args["root"]
590+
graph_config = load_graph_config(root)
591+
581592
if args["context_only"] is None:
582-
build_image(args["image_name"], args["tag"], os.environ)
593+
build_image(args["image_name"], args["tag"], os.environ, graph_config)
583594
else:
584-
build_context(args["image_name"], args["context_only"], os.environ)
595+
build_context(
596+
args["image_name"], args["context_only"], os.environ, graph_config
597+
)
585598

586599

587600
@command(

src/taskgraph/transforms/task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def build_docker_worker_payload(config, task, task_def):
575575
}
576576

577577
# Find VOLUME in Dockerfile.
578-
volumes = dockerutil.parse_volumes(name)
578+
volumes = dockerutil.parse_volumes(name, config.graph_config)
579579
for v in sorted(volumes):
580580
if v in worker["volumes"]:
581581
raise Exception(

src/taskgraph/util/docker.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,28 +206,37 @@ def stream_context_tar(topsrcdir, context_dir, out_file, args=None):
206206

207207

208208
@functools.lru_cache(maxsize=None)
209-
def image_paths():
209+
def image_paths(graph_config=None):
210210
"""Return a map of image name to paths containing their Dockerfile."""
211-
config = load_yaml("taskcluster", "kinds", "docker-image", "kind.yml")
211+
if graph_config:
212+
config = load_yaml(
213+
graph_config.docker_dir,
214+
"kinds",
215+
graph_config["docker_image_kind"],
216+
"kind.yml",
217+
)
218+
else:
219+
config = load_yaml("taskcluster", "kinds", "docker-image", "kind.yml")
220+
212221
return {
213222
k: os.path.join(IMAGE_DIR, v.get("definition", k))
214223
for k, v in config["tasks"].items()
215224
}
216225

217226

218-
def image_path(name):
219-
paths = image_paths()
227+
def image_path(name, graph_config=None):
228+
paths = image_paths(graph_config)
220229
if name in paths:
221230
return paths[name]
222231
return os.path.join(IMAGE_DIR, name)
223232

224233

225234
@functools.lru_cache(maxsize=None)
226-
def parse_volumes(image):
235+
def parse_volumes(image, graph_config=None):
227236
"""Parse VOLUME entries from a Dockerfile for an image."""
228237
volumes = set()
229238

230-
path = image_path(image)
239+
path = image_path(image, graph_config)
231240

232241
with open(os.path.join(path, "Dockerfile"), "rb") as fh:
233242
for line in fh:

0 commit comments

Comments
 (0)