Bug 1288567 - Extract function for creating context tars; r=dustin

Upcoming commits will refactor how context tarballs are created. In
preparation for this, we establish a standalone function for creating
context tarballs and refactor docker_image.py to use it.

MozReview-Commit-ID: KEW6ppO1vCl

--HG--
extra : rebase_source : b81decf9ca14ff0216514f47419e96eb57d6f851
This commit is contained in:
Gregory Szorc 2016-07-22 10:20:06 -07:00
Родитель 217ec4c7cc
Коммит edbd669eca
2 изменённых файлов: 26 добавлений и 4 удалений

Просмотреть файл

@ -9,13 +9,13 @@ import json
import os
import re
import urllib2
import tarfile
import time
from . import base
from taskgraph.util.docker import (
create_context_tar,
docker_image,
generate_context_hash
generate_context_hash,
)
from taskgraph.util.templates import Templates
@ -138,8 +138,7 @@ class DockerImageTask(base.Task):
if not os.path.exists(os.path.dirname(destination)):
os.makedirs(os.path.dirname(destination))
with tarfile.open(destination, 'w:gz') as tar:
tar.add(context_dir, arcname=image_name)
create_context_tar(context_dir, destination, image_name)
@classmethod
def from_json(cls, task_dict):

Просмотреть файл

@ -6,6 +6,7 @@ from __future__ import absolute_import, print_function, unicode_literals
import hashlib
import os
import tarfile
GECKO = os.path.realpath(os.path.join(__file__, '..', '..', '..', '..'))
DOCKER_ROOT = os.path.join(GECKO, 'testing', 'docker')
@ -53,3 +54,25 @@ def generate_context_hash(image_path):
context_hash.update(file_hash.hexdigest() + '\t' + relative_filename + '\n')
return context_hash.hexdigest()
def create_context_tar(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``.
Returns the SHA-256 hex digest of the created archive.
"""
with tarfile.open(out_path, 'w:gz') as tar:
tar.add(context_dir, arcname=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()