Improve error handling and reporting in run-cwl-test.sh script. no issue #
[arvados-dev.git] / jenkins / install-arvados-jobs-image
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4
5 import argparse
6 import arvados
7 import logging
8 import subprocess
9
10
11 def uuid_prefix(arv):
12     return arv.users().current().execute()['uuid'][:5]
13
14
15 def findormake_project(arv, prefix, project_name):
16     system_user_uuid = prefix+'-tpzed-000000000000000'
17     groups = arv.groups().list(
18         filters=[
19             ['name', '=', project_name],
20             ['owner_uuid', '=', system_user_uuid],
21         ],
22         select=['uuid', 'group_class']).execute()['items']
23     if groups:
24         proj = groups[0]
25         logger.debug('found project %s', proj['uuid'])
26         if proj['group_class'] != 'project':
27             logger.info('project %s has group_class %s, changing to %s',
28                         proj['uuid'], proj['group_class'], 'project')
29             arv.groups().update(
30                 uuid=proj['uuid'],
31                 body={
32                     'group_class': 'project',
33                 }).execute()
34         return proj['uuid']
35     uuid = arv.groups().create(body={
36         'group_class': 'project',
37         'name': project_name,
38         'owner_uuid': system_user_uuid,
39     }).execute()['uuid']
40     logger.info('created project %s: %s', uuid, project_name)
41     return uuid
42
43
44 def findormake_project_perm(arv, prefix, project_uuid):
45     all_users_group_uuid = prefix+'-j7d0g-fffffffffffffff'
46     links = arv.links().list(
47         filters=[
48             ['tail_uuid', '=', all_users_group_uuid],
49             ['head_uuid', '=', project_uuid],
50             ['link_class', '=', 'permission'],
51             ['name', '=', 'can_read'],
52         ],
53         select=['uuid']).execute()['items']
54     if links:
55         logger.debug('found permission link %s', links[0]['uuid'])
56         return
57     uuid = arv.links().create(body={
58         'tail_uuid': all_users_group_uuid,
59         'head_uuid': project_uuid,
60         'link_class': 'permission',
61         'name': 'can_read',
62     }).execute()['uuid']
63     logger.info('created permission link %s: %s -> %s',
64                 uuid, all_users_group_uuid, project_uuid)
65     return uuid
66
67
68 def findorpull_image(want_repo, want_tag, project_uuid):
69     for line in subprocess.check_output(['arv-keepdocker']).splitlines():
70         try:
71             repo, tag, image_id, collection, created = line.split(None, 4)
72         except ValueError:
73             continue
74         if repo == want_repo and tag == want_tag:
75             logger.debug('found image tag %s collection %s created %s',
76                         tag, collection, created)
77             return collection
78     logger.info('pulling repo %s tag %s', want_repo, want_tag)
79     subprocess.check_call([
80         'arv-keepdocker',
81         '--pull', '--project-uuid='+project_uuid,
82         want_repo, want_tag])
83
84
85 def tag_latest(repo, tag, project_uuid):
86     logger.debug('tagging %s:%s as %s:latest', repo, tag, repo)
87     tag_cmd = ['docker', 'tag']
88     if subprocess.check_output(tag_cmd + ['--help']).find('--force') >= 0:
89         tag_cmd += ['--force']
90     tag_cmd += [repo+':'+tag, repo+':latest']
91     subprocess.check_call(tag_cmd)
92
93     logger.debug('arv-keepdocker %s:latest', repo)
94     subprocess.check_call([
95         'arv-keepdocker',
96         '--project-uuid='+project_uuid,
97         repo, 'latest'])
98
99
100 logging.basicConfig()
101 logger = logging.getLogger(__name__)
102 logger.setLevel(logging.INFO)
103
104 parser = argparse.ArgumentParser(
105     description='Pull a specific version of the arvados/jobs image to Keep, and tag it as :latest')
106 parser.add_argument(
107     '--git-commit', type=str, required=True,
108     help='git commit hash of desired version')
109 parser.add_argument(
110     '--verbose', '-v', action='count', default=0,
111     help='print more debug messages')
112 args = parser.parse_args()
113 logger.setLevel(logging.INFO - 10 * args.verbose)
114
115 arv = arvados.api('v1')
116 prefix = uuid_prefix(arv)
117 project_uuid = findormake_project(arv, prefix, 'Arvados Standard Docker Images')
118 findormake_project_perm(arv, prefix, project_uuid)
119 findorpull_image('arvados/jobs', args.git_commit, project_uuid)
120 tag_latest('arvados/jobs', args.git_commit, project_uuid)