2015-12-09 19:00:06 +01:00
|
|
|
from bootstrapvz.base import Task
|
|
|
|
from bootstrapvz.common import phases
|
|
|
|
from bootstrapvz.common.tools import log_check_call
|
|
|
|
|
|
|
|
|
2015-12-13 16:10:07 +01:00
|
|
|
class CreateDockerfileEntry(Task):
|
2016-06-04 11:35:59 +02:00
|
|
|
description = 'Creating the Dockerfile entry'
|
|
|
|
phase = phases.preparation
|
2015-12-13 16:10:07 +01:00
|
|
|
|
2016-06-04 11:35:59 +02:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2016-06-05 17:19:27 +02:00
|
|
|
info._docker['dockerfile'] = []
|
2015-12-13 16:10:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CreateImage(Task):
|
2016-06-04 11:35:59 +02:00
|
|
|
description = 'Creating docker image'
|
|
|
|
phase = phases.image_registration
|
2015-12-13 16:10:07 +01:00
|
|
|
|
2016-06-04 11:35:59 +02:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
from pipes import quote
|
|
|
|
tar_cmd = ['tar', '--create', '--numeric-owner',
|
|
|
|
'--directory', info.volume.path, '.']
|
2016-06-05 17:19:27 +02:00
|
|
|
docker_cmd = ['docker', 'import']
|
|
|
|
for instruction in info._docker['dockerfile']:
|
|
|
|
docker_cmd.extend(['--change', instruction])
|
|
|
|
docker_cmd.extend(['-', info.manifest.name.format(**info.manifest_vars)])
|
2016-06-04 11:35:59 +02:00
|
|
|
cmd = ' '.join(map(quote, tar_cmd)) + ' | ' + ' '.join(map(quote, docker_cmd))
|
|
|
|
[info._docker['image_id']] = log_check_call([cmd], shell=True)
|
2015-12-13 16:10:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PopulateLabels(Task):
|
2016-06-04 11:35:59 +02:00
|
|
|
description = 'Populating docker labels'
|
|
|
|
phase = phases.image_registration
|
|
|
|
successors = [CreateImage]
|
2015-12-13 15:36:00 +01:00
|
|
|
|
2016-06-04 11:35:59 +02:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
import pyrfc3339
|
|
|
|
from datetime import datetime
|
|
|
|
import pytz
|
|
|
|
labels = {}
|
|
|
|
labels['name'] = info.manifest.name.format(**info.manifest_vars)
|
|
|
|
# Inspired by https://github.com/projectatomic/ContainerApplicationGenericLabels
|
|
|
|
# See here for the discussion on the debian-cloud mailing list
|
|
|
|
# https://lists.debian.org/debian-cloud/2015/05/msg00071.html
|
|
|
|
labels['architecture'] = info.manifest.system['architecture']
|
|
|
|
labels['build-date'] = pyrfc3339.generate(datetime.utcnow().replace(tzinfo=pytz.utc))
|
|
|
|
if 'labels' in info.manifest.provider:
|
|
|
|
for label, value in info.manifest.provider['labels'].items():
|
|
|
|
labels[label] = value.format(**info.manifest_vars)
|
2015-12-13 15:36:00 +01:00
|
|
|
|
2016-06-04 11:35:59 +02:00
|
|
|
# pipes.quote converts newlines into \n rather than just prefixing
|
|
|
|
# it with a backslash, so we need to escape manually
|
|
|
|
def escape(value):
|
|
|
|
value = value.replace('"', '\\"')
|
|
|
|
value = value.replace('\n', '\\\n')
|
|
|
|
value = '"' + value + '"'
|
|
|
|
return value
|
2016-06-05 17:19:27 +02:00
|
|
|
for label, value in labels.items():
|
|
|
|
info._docker['dockerfile'].append('LABEL {}={}'.format(label, escape(value)))
|
2015-12-13 15:36:00 +01:00
|
|
|
|
|
|
|
|
2015-12-13 16:10:07 +01:00
|
|
|
class AppendManifestDockerfile(Task):
|
2016-06-04 11:35:59 +02:00
|
|
|
description = 'Appending Dockerfile instructions from the manifest'
|
|
|
|
phase = phases.image_registration
|
|
|
|
predecessors = [PopulateLabels]
|
|
|
|
successors = [CreateImage]
|
2015-12-09 19:00:06 +01:00
|
|
|
|
2016-06-04 11:35:59 +02:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2016-06-05 17:19:27 +02:00
|
|
|
info._docker['dockerfile'].extend(info.manifest.provider['dockerfile'])
|