-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimageUploader.py
executable file
·171 lines (147 loc) · 4.79 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
#!/usr/bin/python3
####################################################################################
#
# Copyright (c) 2018 Thanasis Vergoulis & Konstantinos Zagganas & Loukas Kavouras
# for the Information Management Systems Institute, "Athena" Research Center.
#
# This file is part of SCHeMa.
#
# SCHeMa is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SCHeMa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <https://www.gnu.org/licenses/>.
#
####################################################################################
import uploadFunctions as uf
import sys
import subprocess
import re
import os.path
import json
import os
import uuid
import psycopg2 as psg
import time
from dockertarpusher import Registry
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]
description=sys.argv[10]
biotools=sys.argv[11]
doiFile=sys.argv[12]
mpi=sys.argv[13]
covid19=sys.argv[14]
instructions=sys.argv[15]
gpu=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()
db=config['database']
host=db['host']
dbuser=db['username']
passwd=db['password']
dbname=db['database']
conn=psg.connect(host=host, user=dbuser, password=passwd, dbname=dbname)
cur=conn.cursor()
uImageFull=''
cImageFull=''
script=''
registry=config['registry'].strip('/')
regAuth=config['registryAuth']
if cwlPath!='':
cwlContent=uf.cwlReadFile(cwlPath)
# print(cwlContent)
cImageFull=uf.cwlReturnDockerImage(cwlContent)
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)
uimage=''
if imagePath!='':
reg=Registry('https://'+ registry, imagePath, regAuth['username'],regAuth['password'])
manifestFile = reg.getManifest()[0]
uimage=manifestFile['RepoTags'][0]
uImageFull=registry + '/' + uimage
#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:
exit(4)
else:
imageFull=uImageFull
dockerHub='f';
#if only the cwl image is provided, pull it locally
elif (uImageFull=='') and (cImageFull!=''):
imageFull=cImageFull
dockerHub='t'
#if image not provided by cwl but has been uploaded
else:
dockerHub='f'
imageFull=uImageFull
# imageName=softName.lower()
# imageVersion=softVersion.lower()
workingDir='/'
if (dockerHub=='t'):
original=imageFull
else:
original=uimage
if (dockerHub=='f'):
sql="SELECT COUNT(*) FROM operation_locks where operation='image_delete'"
cur.execute(sql)
results=cur.fetchone()
opCount=results[0]
while opCount>0:
time.sleep(5)
sql="SELECT COUNT(*) FROM operation_locks where operation='image_delete'"
cur.execute(sql)
results=cur.fetchone()
opCount=results[0]
uniqid=uuid.uuid4()
uniqid=str(uniqid)
sql="INSERT INTO operation_locks(id,operation) VALUES ('" + uniqid + "','image_upload')"
cur.execute(sql)
conn.commit()
command=['docker-tar-push','https://' + registry, imagePath, regAuth['username'],regAuth['password']]
try:
out=subprocess.check_output(command,stderr=subprocess.STDOUT)
print(out)
except subprocess.CalledProcessError as exc:
print(exc.output)
exit(8)
sql="DELETE FROM operation_locks WHERE id='" + uniqid + "'"
cur.execute(sql)
conn.commit()
uf.imageStore(softName,softVersion, imageFull,script,user,visibility,
workingDir,imountPoint,omountPoint,description,cwlPath,biotools,doiFile,mpi,original,dockerHub,covid19,instructions,gpu)
if 'inputs' not in cwlContent:
cwlContent['inputs']=[];
exit_value=uf.inputStore(softName,softVersion, cwlContent['inputs'])
exit(exit_value)