docker.py (1859B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 6 import os 7 from io import BytesIO 8 9 from taskgraph.util.docker import create_context_tar, docker_image, stream_context_tar 10 11 from gecko_taskgraph.util import docker 12 13 from . import GECKO 14 15 16 def build_context(name, outputFile, args=None): 17 """Build a context.tar for image with specified name.""" 18 if not name: 19 raise ValueError("must provide a Docker image name") 20 if not outputFile: 21 raise ValueError("must provide a outputFile") 22 23 image_dir = docker.image_path(name) 24 if not os.path.isdir(image_dir): 25 raise Exception("image directory does not exist: %s" % image_dir) 26 27 create_context_tar(GECKO, image_dir, outputFile, args=args) 28 29 30 def build_image(name, tag, args=None): 31 """Build a Docker image of specified name. 32 33 Output from image building process will be printed to stdout. 34 """ 35 if not name: 36 raise ValueError("must provide a Docker image name") 37 38 image_dir = docker.image_path(name) 39 if not os.path.isdir(image_dir): 40 raise Exception("image directory does not exist: %s" % image_dir) 41 42 tag = tag or docker_image(name, by_tag=True) 43 44 buf = BytesIO() 45 stream_context_tar(GECKO, image_dir, buf, args) 46 docker.post_to_docker(buf.getvalue(), "/build", nocache=1, t=tag) 47 48 print(f"Successfully built {name} and tagged with {tag}") 49 50 if tag.endswith(":latest"): 51 print("*" * 50) 52 print("WARNING: no VERSION file found in image directory.") 53 print("Image is not suitable for deploying/pushing.") 54 print("Create an image suitable for deploying/pushing by creating") 55 print("a VERSION file in the image directory.") 56 print("*" * 50)