mirror of
https://github.com/HeyPuter/firefox.git
synced 2026-11-25 15:43:10 -10:00
A limitation of traditional docker build context generation is it only includes files from the same directory as the Dockerfile. When repositories have multiple, related Dockerfiles, this limitation results file duplication or putting all Dockerfiles in the same directory (which isn't feasible for mozilla-central since they would need to be in the root directory). This commit enhances Dockerfiles to allow *any* file from the repository checkout to be ADDed to the docker build context. Using the syntax "# %include <path>" you are able to include paths or directories (relative from the top source directory root) in the generated context archive. Files add this way are available under the "topsrcdir/" path and can be ADDed to Docker images. Since context archive generation is deterministic and the hash of the resulting archive is used to determine when images need to be rebuilt, any extra included file that changes will change the hash of the context archive and force image regeneration. Basic tests for the new feature have been added. MozReview-Commit-ID: 4hPZesJuGQV
117 lines
4.1 KiB
Python
117 lines
4.1 KiB
Python
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
import hashlib
|
|
import os
|
|
import tempfile
|
|
|
|
from mozpack.archive import (
|
|
create_tar_gz_from_files,
|
|
)
|
|
|
|
|
|
GECKO = os.path.realpath(os.path.join(__file__, '..', '..', '..', '..'))
|
|
DOCKER_ROOT = os.path.join(GECKO, 'testing', 'docker')
|
|
ARTIFACT_URL = 'https://queue.taskcluster.net/v1/task/{}/artifacts/{}'
|
|
|
|
|
|
def docker_image(name):
|
|
'''Determine the docker image name, including repository and tag, from an
|
|
in-tree docker file.'''
|
|
try:
|
|
with open(os.path.join(DOCKER_ROOT, name, 'REGISTRY')) as f:
|
|
registry = f.read().strip()
|
|
except IOError:
|
|
with open(os.path.join(DOCKER_ROOT, 'REGISTRY')) as f:
|
|
registry = f.read().strip()
|
|
|
|
with open(os.path.join(DOCKER_ROOT, name, 'VERSION')) as f:
|
|
version = f.read().strip()
|
|
|
|
return '{}/{}:{}'.format(registry, name, version)
|
|
|
|
|
|
def generate_context_hash(topsrcdir, image_path, image_name):
|
|
"""Generates a sha256 hash for context directory used to build an image."""
|
|
|
|
# It is a bit unfortunate we have to create a temp file here - it would
|
|
# be nicer to use an in-memory buffer.
|
|
fd, p = tempfile.mkstemp()
|
|
os.close(fd)
|
|
try:
|
|
return create_context_tar(topsrcdir, image_path, p, image_name)
|
|
finally:
|
|
os.unlink(p)
|
|
|
|
|
|
def create_context_tar(topsrcdir, context_dir, out_path, prefix):
|
|
"""Create a context tarball.
|
|
|
|
A directory ``context_dir`` containing a Dockerfile will be assembled into
|
|
a gzipped tar file at ``out_path``. Files inside the archive will be
|
|
prefixed by directory ``prefix``.
|
|
|
|
We also scan the source Dockerfile for special syntax that influences
|
|
context generation.
|
|
|
|
If a line in the Dockerfile has the form ``# %include <path>``,
|
|
the relative path specified on that line will be matched against
|
|
files in the source repository and added to the context under the
|
|
path ``topsrcdir/``. If an entry is a directory, we add all files
|
|
under that directory.
|
|
|
|
Returns the SHA-256 hex digest of the created archive.
|
|
"""
|
|
archive_files = {}
|
|
|
|
for root, dirs, files in os.walk(context_dir):
|
|
for f in files:
|
|
source_path = os.path.join(root, f)
|
|
rel = source_path[len(context_dir) + 1:]
|
|
archive_path = os.path.join(prefix, rel)
|
|
archive_files[archive_path] = source_path
|
|
|
|
# Parse Dockerfile for special syntax of extra files to include.
|
|
with open(os.path.join(context_dir, 'Dockerfile'), 'rb') as fh:
|
|
for line in fh:
|
|
line = line.rstrip()
|
|
if not line.startswith('# %include'):
|
|
continue
|
|
|
|
p = line[len('# %include '):].strip()
|
|
if os.path.isabs(p):
|
|
raise Exception('extra include path cannot be absolute: %s' % p)
|
|
|
|
fs_path = os.path.normpath(os.path.join(topsrcdir, p))
|
|
# Check for filesystem traversal exploits.
|
|
if not fs_path.startswith(topsrcdir):
|
|
raise Exception('extra include path outside topsrcdir: %s' % p)
|
|
|
|
if not os.path.exists(fs_path):
|
|
raise Exception('extra include path does not exist: %s' % p)
|
|
|
|
if os.path.isdir(fs_path):
|
|
for root, dirs, files in os.walk(fs_path):
|
|
for f in files:
|
|
source_path = os.path.join(root, f)
|
|
archive_path = os.path.join(prefix, 'topsrcdir', p, f)
|
|
archive_files[archive_path] = source_path
|
|
else:
|
|
archive_path = os.path.join(prefix, 'topsrcdir', p)
|
|
archive_files[archive_path] = fs_path
|
|
|
|
with open(out_path, 'wb') as fh:
|
|
create_tar_gz_from_files(fh, archive_files, '%s.tar.gz' % prefix)
|
|
|
|
h = hashlib.sha256()
|
|
with open(out_path, 'rb') as fh:
|
|
while True:
|
|
data = fh.read(32768)
|
|
if not data:
|
|
break
|
|
h.update(data)
|
|
return h.hexdigest()
|