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