/
module.py
338 lines (253 loc) · 8.25 KB
/
module.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#
# Collective Knowledge (abstracting docker)
#
# See CK LICENSE.txt for licensing details
# See CK COPYRIGHT.txt for copyright details
#
# Developer: Grigori Fursin, Grigori.Fursin@cTuning.org, http://fursin.net
#
cfg={} # Will be updated by CK (meta description of this module)
work={} # Will be updated by CK (temporal data)
ck=None # Will be updated by CK (initialized CK kernel)
# Local settings
##############################################################################
# Initialize module
def init(i):
"""
Input: {}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
return {'return':0}
##############################################################################
# build Docker image
def build(i):
"""
Input: {
data_uoa - CK entry with Docker description
(scenario) - scenario to get CMD (default if empty)
(org) - organization (default - ctuning)
(cmd) - extra CMD
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
i['func']='build'
return call(i)
##############################################################################
# run Docker image
def run(i):
"""
Input: {
data_uoa - CK entry with Docker description
(scenario) - scenario to get CMD (default if empty)
(cmd) - extra CMD
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
i['func']='run'
return call(i)
##############################################################################
# run Docker image
def call(i):
"""
Input: {
data_uoa - CK entry with Docker description
func - (build or run)
(scenario) - scenario to get CMD (default if empty)
(org) - organization (default - ctuning)
(sudo) - if 'yes', use sudo
(cmd) - extra CMD
(browser) - if 'yes', start browser
(filename) - file to save/load external Docker image (data_uoa.tar by default)
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
import os
import platform
o=i.get('out','')
func=i['func']
sudo=i.get('sudo','')
duoa=i.get('data_uoa','')
if duoa=='':
return {'return':1, 'error':'please, specify CK entry with Docker description as following "ck build docker:{CK entry}"'}
filename=i.get('filename','')
if filename=='': filename='docker-image-'+duoa+'.tar'
# Load CK entry
r=ck.access({'action':'load',
'module_uoa':work['self_module_uid'],
'data_uoa':duoa})
if r['return']>0: return r
p=r['path']
d=r['dict']
duoa=r['data_uoa']
duid=r['data_uid']
# Check if reuse other entry
re=d.get('reuse_another_entry','')
if re!='':
r=ck.access({'action':'find',
'module_uoa':work['self_module_uid'],
'data_uoa':re})
if r['return']>0: return r
p=r['path']
# Choose scenario
s=i.get('scenario','')
if s=='': s='default'
# Choose organization
org=i.get('org','')
if org=='':
org=ck.cfg.get('docker_org','')
if org=='':
org='ctuning'
ecmd=i.get('cmd','')
# Find scenario in meta
cc=d.get('cmd',{}).get(s,{})
c=cc.get(func,'')
if c=='':
return {'return':1, 'error':'CMD to '+func+' Docker image is not defined in the CK entry ('+duoa+')'}
# Check if Windows
pl=platform.system().lower()
ps=d.get('platform_specific',{}).get(pl,{})
for k in ps:
v=ps[k]
if v.startswith('$(') and v.endswith(')'):
# Run and get var
r=ck.gen_tmp_file({'prefix':'tmp-', 'suffix':'.tmp'})
if r['return']>0: return r
ftmp=r['file_name']
os.system(v[2:-1]+' > '+ftmp)
# Read file
r=ck.load_text_file({'text_file':ftmp,
'delete_after_read':'yes'})
if r['return']>0: return r
v=r['string'].strip()
i[k]=v
cmd=cc.get(func+'_extra_cmd','')
if ecmd!='':
cmd=ecmd
# Update CMD
c=c.replace('$#CK_DOCKER_ORGANIZATION#$',org)
c=c.replace('$#CK_DOCKER_NAME#$',duoa)
c=c.replace('$#CK_PATH#$',p)
c=c.replace('$#CK_DOCKER_FILE#$',filename)
if cmd!='':
c=cmd+' '+c
c='docker '+func+' '+c
# Update vars from input
citv=d.get('convert_input_to_vars',{})
for k in citv:
x=citv[k]
ki=x.get('key','')
kd=x.get('default','')
vv=i.get(k,'')
if vv=='':
vv=kd
i[k]=vv
c=c.replace('$#'+ki+'#$',vv)
if sudo=='yes':
c='sudo '+c
# Check if has browser
if i.get('browser','')=='yes':
url=d.get('browser',{}).get('url','')
# Update vars:
for k in citv:
x=citv[k]
ki=x.get('key','')
vv=i.get(k,'')
url=url.replace('$#'+ki+'#$',vv)
import webbrowser
webbrowser.open(url)
if o=='con':
ck.out('Executing command line:')
ck.out(' '+c)
ck.out('')
# Run Docker
r=os.system(c)
return {'return':0}
##############################################################################
# login to Docker Hub
def login(i):
"""
Input: {
(sudo) - if 'yes', add sudo
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
import os
s='docker login'
if i.get('sudo','')=='yes':
s='sudo '+s
os.system(s)
return {'return':0}
##############################################################################
# push a given image to the Docker Hub
def push(i):
"""
Input: {
data_uoa - CK entry with Docker description
(scenario) - scenario to get CMD (default if empty)
(org) - organization (default - ctuning)
(cmd) - extra CMD
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
i['func']='push'
return call(i)
##############################################################################
# save Docker image (for example to share via external repositories and digital libraries)
def save(i):
"""
Input: {
data_uoa - CK entry with Docker description
(scenario) - scenario to get CMD (default if empty)
(cmd) - extra CMD
(filename) - file to save image (data_uoa.tar by default)
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
i['func']='save'
return call(i)
##############################################################################
# import external Docker image (tar file)
def ximport(i):
"""
Input: {
data_uoa - CK entry with Docker description
(scenario) - scenario to get CMD (default if empty)
(cmd) - extra CMD
(filename) - file to load image (data_uoa.tar by default)
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
i['func']='import'
return call(i)