-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimageUploader.py
executable file
·216 lines (187 loc) · 6.17 KB
/
imageUploader.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
import uploadFunctions as uf
import sys
import subprocess
import re
import os.path
import json
import os
softName=sys.argv[1]
softVersion=sys.argv[2]
imagePath=sys.argv[3]
imageExtension=sys.argv[4]
cwlPath=sys.argv[5]
user=sys.argv[6]
visibility=sys.argv[7]
imountPoint=sys.argv[8]
omountPoint=sys.argv[9]
commandRetr=sys.argv[10]
description=sys.argv[11]
biotools=sys.argv[12]
doiFile=sys.argv[13]
mpi=sys.argv[14]
covid19=sys.argv[15]
instructions=sys.argv[16]
def quoteEnclose(string):
return "'" + string + "'"
configFileName=os.path.dirname(os.path.abspath(__file__)) + '/configuration.json'
configFile=open(configFileName,'r')
config=json.load(configFile)
configFile.close()
uImageFull=''
cImageFull=''
script=''
registry=config['registry']
if cwlPath!='':
cwlContent=uf.cwlReadFile(cwlPath)
cImageFull=uf.cwlReturnDockerImage(cwlContent)
if (commandRetr=='cwl'):
if ('baseCommand' not in cwlContent):
exit(12)
else:
if isinstance(cwlContent['baseCommand'],list):
script=' '.join(cwlContent['baseCommand'])
else:
script=cwlContent['baseCommand']
else:
exit(11)
if (imountPoint=='/') or (omountPoint=='/'):
exit(13)
if imagePath!='':
if imageExtension=='gz':
# command=['gzip', '-d', imagePath, '|', 'docker', 'load'];
command='gzip -cd ' + quoteEnclose(imagePath) + ' | docker load'
else:
# command=['docker', 'load', '-i', imagePath]
command='docker load -i ' + quoteEnclose(imagePath)
# print(command)
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(2)
# print(out)
patt=re.compile('Loaded image: (.+:.+)')
uImageFull=patt.match(out).groups()[0]
# uImageSplit=uImageFull.split(':')
# uImageName=uImageSplit[0]
# uImageVersion=uImageSplit[1]
# print(uImageFull,cImageFull)
# exit(0)
#if an image is not provied by cwl or direct upload
if (uImageFull=='') and (cImageFull==''):
exit(3)
#if both images are provided
elif (uImageFull!='') and (cImageFull!=''):
#if the user has already uploaded an image which is different than the cwl definition
#delete previous image and exit
if uImageFull!=cImageFull:
command=['docker','image','rm',uImageFull]
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(4)
else:
# imageName=uImageName
imageFull=uImageFull
dockerHub='f';
# imageVersion=uImageVersion
#if only the cwl image is provided, pull it locally
elif (uImageFull=='') and (cImageFull!=''):
imageFull=cImageFull
dockerHub='t'
# imageSplit=imageFull.split(':')
# imageName=imageSplit[0]
# if len(imageSplit)<2:
# imageVersion=softVersion
# else:
# imageVersion=imageSplit[1]
command=['docker','pull',imageFull]
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(5)
#if image not provided by cwl but has been uploaded
else:
dockerHub='f'
imageFull=uImageFull
imageName=softName.lower()
imageVersion=softVersion.lower()
# print('/data/registry/docker/registry/v2/repositories/' + imageName + ':' + softVersion +'/_manifests/tags/latest')
# if os.path.exists('/data/registry/docker/registry/v2/repositories/' + imageName + '-' + imageVersion +'/_manifests/tags/latest'):
# # if os.path.exists('/data/docker/registrytest/docker/registry/v2/repositories/' + imageName + '-' + softVersion +'/_manifests/tags/latest'):
# command=['docker','image','rm',imageFull]
# try:
# out=subprocess.check_output(command,stderr=subprocess.STDOUT)
# except subprocess.CalledProcessError as exc:
# print(exc.output)
# exit(6)
#read image information regarding working directory and container config
command=['docker','image','inspect',imageFull]
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(9)
# print (json.loads(out)[0]['ContainerConfig'])
# exit(9)
try:
decoded=json.loads(out)[0]
containerConfig=decoded['ContainerConfig']
config=decoded['Config']
if 'WorkingDir' in config:
workingDir=config['WorkingDir']
else:
workingDir='/'
except:
workingDir='/'
# if the user instructed the interface to get the base command from the image
if (commandRetr=='image'):
if 'Cmd' in config:
script=' '.join(config['Cmd'])
else:
exit(15)
# first save the name of the local image as original
# and then add the registry in the string
# and replace invalid characters
imageNew= imageName + '-' + imageVersion + ':latest';
if (dockerHub=='t'):
original=imageFull
else:
original=imageNew
imageNew=registry + imageNew
imageNew=imageNew.replace(' ','_')
imageNew=imageNew.replace('\t','_')
command=['docker','image','tag',imageFull,imageNew]
# print command
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(7)
command=['docker','push',imageNew]
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(8)
command=['docker','image','rm',imageNew]
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(9)
command=['docker','image','rm',imageFull]
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(10)
uf.imageStoreAndClassify(softName,softVersion, imageNew,script,user,visibility,
workingDir,imountPoint,omountPoint,description,cwlPath,biotools,doiFile,mpi,original,dockerHub,covid19,instructions)
if 'inputs' not in cwlContent:
cwlContent['inputs']=[];
exit_value=uf.inputStore(softName,softVersion, cwlContent['inputs'])
exit(exit_value)