X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/549f4a1deddb41f6abbc493a660d9fb0976da91a..c8de202a8ba2b7e128f9698c4b57f0d7d9e89c1d:/sdk/python/arvados/commands/keepdocker.py diff --git a/sdk/python/arvados/commands/keepdocker.py b/sdk/python/arvados/commands/keepdocker.py index 236d9fc004..fa4e44136b 100644 --- a/sdk/python/arvados/commands/keepdocker.py +++ b/sdk/python/arvados/commands/keepdocker.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import argparse +import datetime import errno import json import os @@ -26,23 +27,30 @@ opt_parser.add_argument( '-f', '--force', action='store_true', default=False, help="Re-upload the image even if it already exists on the server") +opt_parser.add_argument( + '--project-uuid', + help="Add the Docker image and metadata to the specified project. Goes into user 'home' project by default.") +opt_parser.add_argument( + '--name', + help="Name to use for the collection that will contain the docker image.") + _group = opt_parser.add_mutually_exclusive_group() _group.add_argument( - '--pull', action='store_true', default=True, - help="Pull the latest image from Docker repositories first (default)") + '--pull', action='store_true', default=False, + help="Try to pull the latest image from Docker registry") _group.add_argument( '--no-pull', action='store_false', dest='pull', - help="Don't pull images from Docker repositories") + help="Use locally installed image only, don't pull image from Docker registry (default)") opt_parser.add_argument( - 'image', + 'image', nargs='?', help="Docker image to upload, as a repository name or hash") opt_parser.add_argument( 'tag', nargs='?', default='latest', help="Tag of the Docker image to upload (default 'latest')") arg_parser = argparse.ArgumentParser( - description="Upload a Docker image to Arvados", + description="Upload or list Docker images in Arvados", parents=[opt_parser, arv_put.run_opts]) class DockerError(Exception): @@ -152,9 +160,51 @@ def make_link(link_class, link_name, **link_attrs): link_attrs.update({'link_class': link_class, 'name': link_name}) return arvados.api('v1').links().create(body=link_attrs).execute() +def ptimestamp(t): + s = t.split(".") + if len(s) == 2: + t = s[0] + s[1][-1:] + return datetime.datetime.strptime(t, "%Y-%m-%dT%H:%M:%SZ") + +def list_images_in_arv(): + existing_links = arvados.api('v1').links().list(filters=[['link_class', 'in', ['docker_image_hash', 'docker_image_repo+tag']]]).execute()['items'] + images = {} + for link in existing_links: + collection_uuid = link["head_uuid"] + if collection_uuid not in images: + images[collection_uuid]= {"dockerhash": "", + "repo":"", + "tag":"", + "timestamp": ptimestamp("1970-01-01T00:00:01Z")} + + if link["link_class"] == "docker_image_hash": + images[collection_uuid]["dockerhash"] = link["name"] + + if link["link_class"] == "docker_image_repo+tag": + r = link["name"].split(":") + images[collection_uuid]["repo"] = r[0] + if len(r) > 1: + images[collection_uuid]["tag"] = r[1] + + if "image_timestamp" in link["properties"]: + images[collection_uuid]["timestamp"] = ptimestamp(link["properties"]["image_timestamp"]) + else: + images[collection_uuid]["timestamp"] = ptimestamp(link["created_at"]) + + st = sorted(images.items(), lambda a, b: cmp(b[1]["timestamp"], a[1]["timestamp"])) + + fmt = "{:30} {:10} {:12} {:38} {:20}" + print fmt.format("REPOSITORY", "TAG", "IMAGE ID", "KEEP LOCATOR", "CREATED") + for i, j in st: + print(fmt.format(j["repo"], j["tag"], j["dockerhash"][0:11], i, j["timestamp"].strftime("%c"))) + def main(arguments=None): args = arg_parser.parse_args(arguments) + if args.image is None or args.image == 'images': + list_images_in_arv() + sys.exit(0) + # Pull the image if requested, unless the image is specified as a hash # that we already have. if args.pull and not find_image_hashes(args.image): @@ -187,6 +237,15 @@ def main(arguments=None): # Call arv-put with switches we inherited from it # (a.k.a., switches that aren't our own). put_args = opt_parser.parse_known_args(arguments)[1] + + if args.name is None: + put_args += ['--name', 'Docker image {}:{} {}'.format(args.image, args.tag, image_hash[0:11])] + else: + put_args += ['--name', args.name] + + if args.project_uuid is not None: + put_args += ['--project-uuid', args.project_uuid] + coll_uuid = arv_put.main( put_args + ['--filename', outfile_name, image_file.name]).strip() @@ -200,10 +259,11 @@ def main(arguments=None): link_base = {'head_uuid': coll_uuid, 'properties': {}} if 'created' in image_metadata: link_base['properties']['image_timestamp'] = image_metadata['created'] + if args.project_uuid is not None: + link_base['owner_uuid'] = args.project_uuid make_link('docker_image_hash', image_hash, **link_base) if not image_hash.startswith(args.image.lower()): - make_link('docker_image_repository', args.image, **link_base) make_link('docker_image_repo+tag', '{}:{}'.format(args.image, args.tag), **link_base)