2016-03-22 18:09:52 +00:00
|
|
|
import json
|
|
|
|
import re
|
2016-05-23 23:52:17 +00:00
|
|
|
import calendar
|
2016-03-22 18:09:52 +00:00
|
|
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
2015-01-13 22:46:11 +00:00
|
|
|
from app import app
|
2015-08-03 19:49:10 +00:00
|
|
|
from util.registry.streamlayerformat import StreamLayerMerger
|
2016-12-20 19:01:06 +00:00
|
|
|
from util.dict_wrappers import JSONPathDict
|
2016-08-30 19:05:15 +00:00
|
|
|
from image.common import TarImageFormatter
|
2015-01-13 22:46:11 +00:00
|
|
|
|
2016-03-22 18:09:52 +00:00
|
|
|
|
|
|
|
ACNAME_REGEX = re.compile(r'[^a-z-]+')
|
|
|
|
|
2015-01-13 22:46:11 +00:00
|
|
|
|
2016-08-02 22:45:30 +00:00
|
|
|
class AppCImageFormatter(TarImageFormatter):
|
|
|
|
"""
|
|
|
|
Image formatter which produces an tarball according to the AppC specification.
|
2015-01-13 22:46:11 +00:00
|
|
|
"""
|
|
|
|
|
2018-11-13 15:13:51 +00:00
|
|
|
def stream_generator(self, tag, parsed_manifest, synthetic_image_id, layer_iterator,
|
2018-03-23 18:39:38 +00:00
|
|
|
tar_stream_getter_iterator, reporter=None):
|
2016-05-23 23:52:17 +00:00
|
|
|
image_mtime = 0
|
2018-09-13 18:12:23 +00:00
|
|
|
created = parsed_manifest.created_datetime
|
2016-05-23 23:52:17 +00:00
|
|
|
if created is not None:
|
|
|
|
image_mtime = calendar.timegm(created.utctimetuple())
|
|
|
|
|
2015-01-13 22:46:11 +00:00
|
|
|
# ACI Format (.tar):
|
|
|
|
# manifest - The JSON manifest
|
|
|
|
# rootfs - The root file system
|
|
|
|
|
|
|
|
# Yield the manifest.
|
2018-08-29 02:58:19 +00:00
|
|
|
aci_manifest = json.dumps(DockerV1ToACIManifestTranslator.build_manifest(
|
2017-01-11 20:16:49 +00:00
|
|
|
tag,
|
2018-09-13 18:12:23 +00:00
|
|
|
parsed_manifest,
|
2016-12-20 19:01:06 +00:00
|
|
|
synthetic_image_id
|
|
|
|
))
|
2018-08-29 02:58:19 +00:00
|
|
|
yield self.tar_file('manifest', aci_manifest, mtime=image_mtime)
|
2015-01-13 22:46:11 +00:00
|
|
|
|
|
|
|
# Yield the merged layer dtaa.
|
2016-05-23 23:52:17 +00:00
|
|
|
yield self.tar_folder('rootfs', mtime=image_mtime)
|
2015-01-13 22:46:11 +00:00
|
|
|
|
2018-03-23 18:39:38 +00:00
|
|
|
layer_merger = StreamLayerMerger(tar_stream_getter_iterator, path_prefix='rootfs/',
|
|
|
|
reporter=reporter)
|
2015-01-13 22:46:11 +00:00
|
|
|
for entry in layer_merger.get_generator():
|
2015-02-02 21:12:23 +00:00
|
|
|
yield entry
|
2015-01-13 22:46:11 +00:00
|
|
|
|
2016-12-20 19:01:06 +00:00
|
|
|
|
|
|
|
class DockerV1ToACIManifestTranslator(object):
|
2015-02-02 21:13:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def _build_isolators(docker_config):
|
2016-08-02 22:45:30 +00:00
|
|
|
"""
|
|
|
|
Builds ACI isolator config from the docker config.
|
|
|
|
"""
|
2015-01-13 22:46:11 +00:00
|
|
|
|
|
|
|
def _isolate_memory(memory):
|
|
|
|
return {
|
|
|
|
"name": "memory/limit",
|
2015-03-13 19:31:36 +00:00
|
|
|
"value": {
|
|
|
|
"request": str(memory) + 'B',
|
|
|
|
}
|
2015-01-13 22:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _isolate_swap(memory):
|
|
|
|
return {
|
|
|
|
"name": "memory/swap",
|
2015-03-13 19:31:36 +00:00
|
|
|
"value": {
|
|
|
|
"request": str(memory) + 'B',
|
|
|
|
}
|
2015-01-13 22:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _isolate_cpu(cpu):
|
|
|
|
return {
|
|
|
|
"name": "cpu/shares",
|
2015-03-13 19:31:36 +00:00
|
|
|
"value": {
|
|
|
|
"request": str(cpu),
|
|
|
|
}
|
2015-01-13 22:46:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 22:29:59 +00:00
|
|
|
def _isolate_capabilities(capabilities_set_value):
|
|
|
|
capabilities_set = re.split(r'[\s,]', capabilities_set_value)
|
2015-01-13 22:46:11 +00:00
|
|
|
return {
|
2015-03-13 19:31:36 +00:00
|
|
|
"name": "os/linux/capabilities-retain-set",
|
|
|
|
"value": {
|
|
|
|
"set": capabilities_set,
|
|
|
|
}
|
2015-01-13 22:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mappers = {
|
|
|
|
'Memory': _isolate_memory,
|
|
|
|
'MemorySwap': _isolate_swap,
|
|
|
|
'CpuShares': _isolate_cpu,
|
|
|
|
'Cpuset': _isolate_capabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
isolators = []
|
|
|
|
|
|
|
|
for config_key in mappers:
|
|
|
|
value = docker_config.get(config_key)
|
|
|
|
if value:
|
|
|
|
isolators.append(mappers[config_key](value))
|
|
|
|
|
|
|
|
return isolators
|
|
|
|
|
2015-02-02 21:13:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def _build_ports(docker_config):
|
2016-08-02 22:45:30 +00:00
|
|
|
"""
|
|
|
|
Builds the ports definitions for the ACI.
|
|
|
|
|
|
|
|
Formats:
|
|
|
|
port/tcp
|
|
|
|
port/udp
|
|
|
|
port
|
|
|
|
"""
|
2015-01-13 22:46:11 +00:00
|
|
|
ports = []
|
|
|
|
|
2016-12-20 19:01:06 +00:00
|
|
|
exposed_ports = docker_config['ExposedPorts']
|
|
|
|
if exposed_ports is not None:
|
|
|
|
port_list = exposed_ports.keys()
|
|
|
|
else:
|
|
|
|
port_list = docker_config['Ports'] or docker_config['ports'] or []
|
|
|
|
|
|
|
|
for docker_port in port_list:
|
2015-01-13 22:46:11 +00:00
|
|
|
protocol = 'tcp'
|
|
|
|
port_number = -1
|
|
|
|
|
2016-08-02 22:45:30 +00:00
|
|
|
if '/' in docker_port:
|
|
|
|
(port_number, protocol) = docker_port.split('/')
|
2015-01-13 22:46:11 +00:00
|
|
|
else:
|
2016-08-02 22:45:30 +00:00
|
|
|
port_number = docker_port
|
2015-01-13 22:46:11 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
port_number = int(port_number)
|
|
|
|
ports.append({
|
2015-02-03 17:15:08 +00:00
|
|
|
"name": "port-%s" % port_number,
|
2015-01-13 22:46:11 +00:00
|
|
|
"port": port_number,
|
2015-07-28 16:05:11 +00:00
|
|
|
"protocol": protocol,
|
2015-01-13 22:46:11 +00:00
|
|
|
})
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return ports
|
|
|
|
|
2016-03-22 18:09:52 +00:00
|
|
|
@staticmethod
|
|
|
|
def _ac_name(value):
|
|
|
|
sanitized = ACNAME_REGEX.sub('-', value.lower()).strip('-')
|
|
|
|
if sanitized == '':
|
|
|
|
return str(uuid4())
|
|
|
|
return sanitized
|
|
|
|
|
2015-02-02 21:13:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def _build_volumes(docker_config):
|
2015-01-13 22:46:11 +00:00
|
|
|
""" Builds the volumes definitions for the ACI. """
|
|
|
|
volumes = []
|
|
|
|
|
|
|
|
def get_name(docker_volume_path):
|
2016-12-20 19:01:06 +00:00
|
|
|
volume_name = DockerV1ToACIManifestTranslator._ac_name(docker_volume_path)
|
|
|
|
return "volume-%s" % volume_name
|
2015-01-13 22:46:11 +00:00
|
|
|
|
2017-01-31 20:37:16 +00:00
|
|
|
volume_list = docker_config['Volumes'] or docker_config['volumes'] or {}
|
|
|
|
for docker_volume_path in volume_list.iterkeys():
|
2015-12-01 17:21:07 +00:00
|
|
|
if not docker_volume_path:
|
|
|
|
continue
|
2015-01-13 22:46:11 +00:00
|
|
|
|
|
|
|
volumes.append({
|
|
|
|
"name": get_name(docker_volume_path),
|
|
|
|
"path": docker_volume_path,
|
2015-07-28 16:05:11 +00:00
|
|
|
"readOnly": False,
|
2015-01-13 22:46:11 +00:00
|
|
|
})
|
|
|
|
return volumes
|
|
|
|
|
2015-02-02 21:13:37 +00:00
|
|
|
@staticmethod
|
2018-08-28 21:30:15 +00:00
|
|
|
def build_manifest(tag, manifest, synthetic_image_id):
|
2016-09-01 23:00:11 +00:00
|
|
|
""" Builds an ACI manifest of an existing repository image. """
|
2018-09-13 18:12:23 +00:00
|
|
|
docker_layer_data = JSONPathDict(json.loads(manifest.leaf_layer.raw_v1_metadata))
|
2017-01-11 20:17:12 +00:00
|
|
|
config = docker_layer_data['config'] or JSONPathDict({})
|
2015-01-13 22:46:11 +00:00
|
|
|
|
2018-08-28 21:30:15 +00:00
|
|
|
namespace = tag.repository.namespace_name
|
|
|
|
repo_name = tag.repository.name
|
2015-01-13 22:46:11 +00:00
|
|
|
source_url = "%s://%s/%s/%s:%s" % (app.config['PREFERRED_URL_SCHEME'],
|
|
|
|
app.config['SERVER_HOSTNAME'],
|
2018-08-28 21:30:15 +00:00
|
|
|
namespace, repo_name, tag.name)
|
2015-01-13 22:46:11 +00:00
|
|
|
|
2015-01-13 22:55:27 +00:00
|
|
|
# ACI requires that the execution command be absolutely referenced. Therefore, if we find
|
|
|
|
# a relative command, we give it as an argument to /bin/sh to resolve and execute for us.
|
2016-12-20 19:01:06 +00:00
|
|
|
entrypoint = config['Entrypoint'] or []
|
|
|
|
exec_path = entrypoint + (config['Cmd'] or [])
|
2015-01-13 22:55:27 +00:00
|
|
|
if exec_path and not exec_path[0].startswith('/'):
|
|
|
|
exec_path = ['/bin/sh', '-c', '""%s""' % ' '.join(exec_path)]
|
2015-01-13 22:46:11 +00:00
|
|
|
|
2015-02-02 21:16:10 +00:00
|
|
|
# TODO(jschorr): ACI doesn't support : in the name, so remove any ports.
|
2015-01-13 22:46:11 +00:00
|
|
|
hostname = app.config['SERVER_HOSTNAME']
|
|
|
|
hostname = hostname.split(':', 1)[0]
|
|
|
|
|
2016-04-20 18:53:10 +00:00
|
|
|
# Calculate the environment variables.
|
2017-01-11 20:17:12 +00:00
|
|
|
docker_env_vars = config.get('Env') or []
|
2016-04-20 18:53:10 +00:00
|
|
|
env_vars = []
|
|
|
|
for var in docker_env_vars:
|
|
|
|
pieces = var.split('=')
|
|
|
|
if len(pieces) != 2:
|
|
|
|
continue
|
|
|
|
|
|
|
|
env_vars.append(pieces)
|
|
|
|
|
2015-01-13 22:46:11 +00:00
|
|
|
manifest = {
|
|
|
|
"acKind": "ImageManifest",
|
2015-07-28 16:05:11 +00:00
|
|
|
"acVersion": "0.6.1",
|
2016-12-20 19:01:06 +00:00
|
|
|
"name": '%s/%s/%s' % (hostname.lower(), namespace.lower(), repo_name.lower()),
|
2015-01-13 22:46:11 +00:00
|
|
|
"labels": [
|
2016-03-22 18:10:16 +00:00
|
|
|
{
|
|
|
|
"name": "version",
|
2018-09-13 18:12:23 +00:00
|
|
|
"value": tag.name,
|
2016-03-22 18:10:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "arch",
|
2017-01-11 20:17:12 +00:00
|
|
|
"value": docker_layer_data.get('architecture') or 'amd64'
|
2016-03-22 18:10:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "os",
|
2017-01-11 20:17:12 +00:00
|
|
|
"value": docker_layer_data.get('os') or 'linux'
|
2016-03-22 18:10:16 +00:00
|
|
|
}
|
2015-01-13 22:46:11 +00:00
|
|
|
],
|
|
|
|
"app": {
|
|
|
|
"exec": exec_path,
|
2015-02-02 21:16:10 +00:00
|
|
|
# Below, `or 'root'` is required to replace empty string from Dockerfiles.
|
2017-01-11 20:17:12 +00:00
|
|
|
"user": config.get('User') or 'root',
|
|
|
|
"group": config.get('Group') or 'root',
|
2015-01-13 22:46:11 +00:00
|
|
|
"eventHandlers": [],
|
2017-01-11 20:17:12 +00:00
|
|
|
"workingDirectory": config.get('WorkingDir') or '/',
|
2016-04-20 18:53:10 +00:00
|
|
|
"environment": [{"name": key, "value": value} for (key, value) in env_vars],
|
2016-12-20 19:01:06 +00:00
|
|
|
"isolators": DockerV1ToACIManifestTranslator._build_isolators(config),
|
|
|
|
"mountPoints": DockerV1ToACIManifestTranslator._build_volumes(config),
|
|
|
|
"ports": DockerV1ToACIManifestTranslator._build_ports(config),
|
2015-01-13 22:46:11 +00:00
|
|
|
"annotations": [
|
2017-01-11 20:17:12 +00:00
|
|
|
{"name": "created", "value": docker_layer_data.get('created') or ''},
|
2015-01-13 22:46:11 +00:00
|
|
|
{"name": "homepage", "value": source_url},
|
2015-02-03 17:15:08 +00:00
|
|
|
{"name": "quay.io/derived-image", "value": synthetic_image_id},
|
2015-01-13 22:46:11 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-12-20 19:01:06 +00:00
|
|
|
return manifest
|