forked from WIPACrepo/docker-icecube-icetray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
162 lines (142 loc) · 7.97 KB
/
deploy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import shutil
import subprocess
import glob
import argparse
from pprint import pprint
from datetime import datetime
image_name = 'icecube/icetray'
def get_date():
return datetime.utcnow().strftime("%Y%m%d-%H%M%S")
def call(*args, **kwargs):
print('call: '+' '.join(str(a) for a in args), flush=True)
subprocess.call(*args, **kwargs)
def check_call(*args, **kwargs):
print('check_call: '+' '.join(str(a) for a in args), flush=True)
subprocess.check_call(*args, **kwargs)
def skip(metaproject, version, target, base_os):
if not version.startswith('V'):
return False
tag = metaproject+'-'+version+'-'+target+'-'+base_os
try:
check_call(['wget','-q','-O','-','https://hub.docker.com/v2/repositories/'+image_name+'/tags/'+tag])
except Exception:
return False
return True
def build_docker(metaproject, version, target, base_os):
tag = metaproject+'-'+version+'-'+target+'-'+base_os
full_tag = image_name+':'+tag
dockerfile = os.path.join(base_os, metaproject, version, 'Dockerfile')
if os.path.exists(dockerfile+'_'+target):
dockerfile = dockerfile+'_'+target
call(['docker', 'pull', full_tag])
if 'GITHUB_PASS' in os.environ:
creds = os.environ['GITHUB_USER']+':'+os.environ['GITHUB_PASS']
elif 'GITHUB_TOKEN' in os.environ:
creds = os.environ['GITHUB_USER']+':'+os.environ['GITHUB_TOKEN']
else:
raise Exception('need to define GITHUB_PASS or GITHUB_TOKEN in env')
icetray_dir = os.path.join(os.getcwd(), 'icetray')
os.makedirs(icetray_dir)
try:
if target == 'install':
check_call(['git', 'clone', 'https://'+creds+'@github.com/icecube/icetray.git', icetray_dir])
branch = 'tags/'+version if version.startswith('V') else version
check_call(['git', 'checkout', branch], cwd=icetray_dir)
check_call(['docker', 'build', '--pull', '-f', dockerfile, '--target', target, '-t', full_tag, '.'])
check_call(['docker', 'push', full_tag])
finally:
if os.path.exists(icetray_dir):
shutil.rmtree(icetray_dir)
def retag(old, new):
full_tag_old = image_name+':'+old
full_tag_new = image_name+':'+new
check_call(['docker', 'tag', full_tag_old, full_tag_new])
check_call(['docker', 'push', full_tag_new])
def get_tags(metaproject, version, base_os):
dockerfiles = glob.glob(os.path.join(base_os, metaproject, version, 'Dockerfile*'))
tags = []
for dockerfile in dockerfiles:
with open(dockerfile) as f:
for line in f:
parts = line.strip().split()
if len(parts) >= 3 and parts[0].lower() == 'from' and parts[2].lower() == 'as':
tags.append(parts[-1])
return tags
def build_metaproject(metaproject, version, base_os):
print('now working on '+metaproject+'-'+version+'-XXX-'+base_os)
date = get_date()
tags = get_tags(metaproject, version, base_os)
if version == 'stable' or (metaproject == 'combo' and version > 'V01-02'):
if 'base-devel' in tags and not skip(metaproject, version, 'base-devel', base_os):
build_docker(metaproject, version, 'base-devel', base_os)
if metaproject == 'combo' and version == 'stable':
retag(metaproject+'-'+version+'-base-devel-'+base_os, metaproject+'-'+version+'-base-devel')
retag(metaproject+'-'+version+'-base-devel-'+base_os, metaproject+'-'+version+'-base-devel-'+base_os+'-'+date)
if 'base' in tags and not skip(metaproject, version, 'base', base_os):
build_docker(metaproject, version, 'base', base_os)
if metaproject == 'combo' and version == 'stable':
retag(metaproject+'-'+version+'-base-'+base_os, metaproject+'-'+version+'-base')
retag(metaproject+'-'+version+'-base-'+base_os, metaproject+'-'+version+'-base-'+base_os+'-'+date)
if 'install' in tags and not skip(metaproject, version, 'install', base_os):
build_docker(metaproject, version, 'install', base_os)
if metaproject == 'combo' and version == 'stable':
retag(metaproject+'-'+version+'-install-'+base_os, metaproject+'-'+version+'-install')
retag(metaproject+'-'+version+'-install-'+base_os, metaproject+'-'+version+'-install-'+base_os+'-'+date)
if 'slim' in tags and not skip(metaproject, version, 'slim', base_os):
build_docker(metaproject, version, 'slim', base_os)
retag(metaproject+'-'+version+'-slim-'+base_os, metaproject+'-'+version+'-slim')
if metaproject == 'combo' and version == 'stable':
retag(metaproject+'-'+version+'-slim-'+base_os, version+'-slim')
retag(metaproject+'-'+version+'-slim-'+base_os, metaproject+'-'+version+'-slim-'+base_os+'-'+date)
if 'prod' in tags and not skip(metaproject, version, 'prod', base_os):
build_docker(metaproject, version, 'prod', base_os)
retag(metaproject+'-'+version+'-prod-'+base_os, metaproject+'-'+version+'-prod')
if metaproject == 'combo' and version == 'stable':
retag(metaproject+'-'+version+'-prod-'+base_os, version+'-prod')
retag(metaproject+'-'+version+'-prod-'+base_os, metaproject+'-'+version+'-prod-'+base_os+'-'+date)
if 'devel' in tags and not skip(metaproject, version, 'devel', base_os):
build_docker(metaproject, version, 'devel', base_os)
retag(metaproject+'-'+version+'-devel-'+base_os, metaproject+'-'+version+'-devel')
retag(metaproject+'-'+version+'-devel-'+base_os, metaproject+'-'+version)
if metaproject == 'combo' and version == 'stable':
retag(metaproject+'-'+version+'-devel-'+base_os, version+'-devel')
retag(metaproject+'-'+version+'-devel-'+base_os, version)
retag(metaproject+'-'+version+'-devel-'+base_os, 'latest')
retag(metaproject+'-'+version+'-devel-'+base_os, metaproject+'-'+version+'-devel-'+base_os+'-'+date)
for t in tags:
if 'cuda' in t or 'tensorflow' in t:
if not skip(metaproject, version, t, base_os):
build_docker(metaproject, version, t, base_os)
retag(metaproject+'-'+version+'-'+t+'-'+base_os, metaproject+'-'+version+'-'+t)
if 'cuda' in t:
retag(metaproject+'-'+version+'-'+t+'-'+base_os, metaproject+'-'+version+'-cuda')
elif 'tensorflow' in t:
retag(metaproject+'-'+version+'-'+t+'-'+base_os, metaproject+'-'+version+'-tensorflow')
if metaproject == 'combo' and version == 'stable':
retag(metaproject+'-'+version+'-'+t+'-'+base_os, version+'-'+t)
if 'cuda' in t:
retag(metaproject+'-'+version+'-'+t+'-'+base_os, version+'-cuda')
elif 'tensorflow' in t:
retag(metaproject+'-'+version+'-'+t+'-'+base_os, version+'-tensorflow')
retag(metaproject+'-'+version+'-'+t+'-'+base_os, metaproject+'-'+version+'-'+t+'-'+base_os+'-'+date)
def main():
parser = argparse.ArgumentParser()
os_options = ['ubuntu18.04','ubuntu20.04']
parser.add_argument('--os', action='append', choices=os_options, default=os_options)
parser.add_argument('--metaproject', action='append')
parser.add_argument('--version', action='append')
parser.add_argument('--print-tags', action='store_true')
args = parser.parse_args()
for base_os in args.os:
for metaproject in os.listdir(base_os):
if (not args.metaproject) or metaproject in args.metaproject:
for version in os.listdir(os.path.join(base_os, metaproject)):
if (not args.version) or version in args.version:
if args.print_tags:
print('now working on '+metaproject+'-'+version+'-XXX-'+base_os)
pprint(get_tags(metaproject, version, base_os))
else:
build_metaproject(metaproject, version, base_os)
if __name__ == '__main__':
main()