#!/usr/bin/env python

# Copyright (C) The Arvados Authors. All rights reserved.
#
# SPDX-License-Identifier: AGPL-3.0

from __future__ import print_function

import argparse
import arvados
import logging
import subprocess


def uuid_prefix(arv):
    return arv.users().current().execute()['uuid'][:5]


def findormake_project(arv, prefix, project_name):
    system_user_uuid = prefix+'-tpzed-000000000000000'
    groups = arv.groups().list(
        filters=[
            ['name', '=', project_name],
            ['owner_uuid', '=', system_user_uuid],
        ],
        select=['uuid', 'group_class']).execute()['items']
    if groups:
        proj = groups[0]
        logger.debug('found project %s', proj['uuid'])
        if proj['group_class'] != 'project':
            logger.info('project %s has group_class %s, changing to %s',
                        proj['uuid'], proj['group_class'], 'project')
            arv.groups().update(
                uuid=proj['uuid'],
                body={
                    'group_class': 'project',
                }).execute()
        return proj['uuid']
    uuid = arv.groups().create(body={
        'group_class': 'project',
        'name': project_name,
        'owner_uuid': system_user_uuid,
    }).execute()['uuid']
    logger.info('created project %s: %s', uuid, project_name)
    return uuid


def findormake_project_perm(arv, prefix, project_uuid):
    all_users_group_uuid = prefix+'-j7d0g-fffffffffffffff'
    links = arv.links().list(
        filters=[
            ['tail_uuid', '=', all_users_group_uuid],
            ['head_uuid', '=', project_uuid],
            ['link_class', '=', 'permission'],
            ['name', '=', 'can_read'],
        ],
        select=['uuid']).execute()['items']
    if links:
        logger.debug('found permission link %s', links[0]['uuid'])
        return
    uuid = arv.links().create(body={
        'tail_uuid': all_users_group_uuid,
        'head_uuid': project_uuid,
        'link_class': 'permission',
        'name': 'can_read',
    }).execute()['uuid']
    logger.info('created permission link %s: %s -> %s',
                uuid, all_users_group_uuid, project_uuid)
    return uuid


def findorpull_image(want_repo, want_tag, project_uuid):
    logger.info('pulling repo %s tag %s', want_repo, want_tag)
    subprocess.check_call([
        'arv-keepdocker',
        '--pull', '--project-uuid='+project_uuid,
        want_repo, want_tag])


def tag_latest(repo, tag, project_uuid):
    logger.debug('tagging %s:%s as %s:latest', repo, tag, repo)
    tag_cmd = ['docker', 'tag']
    if subprocess.check_output(tag_cmd + ['--help']).find('--force') >= 0:
        tag_cmd += ['--force']
    tag_cmd += [repo+':'+tag, repo+':latest']
    subprocess.check_call(tag_cmd)

    logger.debug('arv-keepdocker %s:latest', repo)
    subprocess.check_call([
        'arv-keepdocker',
        '--project-uuid='+project_uuid,
        repo, 'latest'])


logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

parser = argparse.ArgumentParser(
    description='Pull a specific version of the arvados/jobs image to Keep, and tag it as :latest')
parser.add_argument(
    '--git-commit', type=str, required=True,
    help='git commit hash of desired version')
parser.add_argument(
    '--verbose', '-v', action='count', default=0,
    help='print more debug messages')
args = parser.parse_args()
logger.setLevel(logging.INFO - 10 * args.verbose)

arv = arvados.api('v1')
prefix = uuid_prefix(arv)
project_uuid = findormake_project(arv, prefix, 'Arvados Standard Docker Images')
findormake_project_perm(arv, prefix, project_uuid)
findorpull_image('arvados/jobs', args.git_commit, project_uuid)
tag_latest('arvados/jobs', args.git_commit, project_uuid)