-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_docker.py
366 lines (261 loc) · 10.6 KB
/
magic_docker.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
##############################################
"""
Author : Babak.EA
Date : 2020-05-04,
App : Magic_Docker # an automated python tools to speed up the containerizing process
"""
a=" Hi "
b= " You are using the Magic_Docker Tools. It is an automated python application to create an error-free Docker Image. "+ " The current version has been programmed for Linux Os if you are a Linux user, good news! Magic _docker will take care of all the steps. Please sweet yourself and enjoy the show. " + " If you are windows or MAC user, Magic docker still will help you. I will create the Dockerfile, requirements, and the shell script to build and save the created docker image. "
c=" You need to run the shell command manually."
d=" Please feel free to contact me if you needed any further assistance. "
e="Thank you. "
f=" Babak.EA"
from stdlib_list import stdlib_list
from ipywidgets import FileUpload
import ipywidgets
from ipywidgets import widgets
import os,sys
import re
import pkg_resources
import datetime
import subprocess
import platform
import numpy as np
#from gtts import gTTS
"""
%%html
<style>
.output_wrapper, .output {
height:auto !important;
max-height: none;
}
.output_scroll {
box-shadow:none !important;
webkit-box-shadow:none !important;
}
.package_header {
width: 100%;
background-color: #0e2b59;
color: #ffffff;
font-size: 14px;
text-align: center;
padding-top: 8px;
padding-right: 8px;
padding-bottom: 8px;
padding-left: 8px;
}
.placeholder {
width: 100%;
background-color: gray;
height: 100px;
}
.passed_test_table {
display: table;
width: 100%;
background-color: #ebffd3;
border-spacing: 5px;
}
# (...) rest of css classes omitted
</style>
"""
display(a,
b,
c,
d,
e,
f)
#del(a,b,c,d,e,f)
"""
if __name__ == "__main__":
load_model()
"""
class J_Magic_Docker:
def __init__(self,manual_reject=0):
#print(Greeting_Message)
self.lib=[]
if manual_reject==0 :
self.manual_reject=["pkg_resources"]
else:
self.manual_reject=["pkg_resources"]+manual_reject
self.python_ver=sys.version[0:3]
self.embeded_lib = stdlib_list(self.python_ver)#python version detection
self.Tag=datetime.date.today().strftime("%m%d%Y")#Docker Tag declaration
self.OS_name=platform.system()
#base line for Dockerfile
self.DOC_TEXT="FROM ubuntu:latest \n"+\
"RUN apt-get update && apt-get -y update \n"+\
"RUN apt-get install -y build-essential python{} python3-pip python3-dev \n".format(self.python_ver)+\
"RUN pip3 -q install pip --upgrade \n"+\
"RUN mkdir src \n"+\
"WORKDIR src/ \n"+\
"COPY . . \n"+\
"RUN pip3 install -r requirements.txt \n" +\
"RUN pip3 install jupyter \n"+\
"WORKDIR /src/Jupyter \n"+\
"EXPOSE 8888 \n"+\
'CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]'
self.File_reader()
def File_reader(self):
upload=widgets.FileUpload()
output_class = widgets.Output()
display("please select the python model :",upload)
self.model=upload
self.model.observe(self.upload_class_eventhandler, names='value')
def upload_class_eventhandler(self,names):
self.Docker_name="Docker_"+self.model.metadata[0]['name'].split(".")[0].lower()
self.Lib_add()
def lib_f(text):
out=[]
if re.match(r'^import', text):
if " as " in text:
text=text.replace("import ","").replace("\n","")
text=text.split(" ")[0]
text=text.split(".")[0]
out+=[text]
else:
text=text.replace("import ","").replace(" ","").replace("\n","")
text=text.split(",")
if type(text)==str:
out+=[text]
else:
#text=[x.split(".")[0] for x in text]
#print(text)
out+=text
return list(set(out))
elif re.match(r'from',text):
text=text.replace("from ","")
text=text.split(" ")[0]
text=text.split(".")[0]
#text=[x.split(".")[0] for x in text]
out+=[text]
return list(set(out))
else:
return np.NAN
def Lib_add(self):
Lines=str(self.model.data[0]).split("\\n")
#print(Lines)
#self.lib=["numpy","pandas"]
for line in Lines:
if re.findall(r"import",line):
line=line.replace("\\r","").replace('"','').replace("\\","")
tem=J_Magic_Docker.lib_f(str(line).strip())
if tem==tem:
self.lib+=tem
self.lib=list(set(self.lib))
self.lib=[x for x in self.lib if x not in self.embeded_lib+self.manual_reject]
display("The following Libraries have been detected : ")
display(self.lib)
display("Please kindly add more file if there is any; otherwise press continue ... ")
self.reset_btn(0)
def reset_btn(self,flag):
Continue_BTN = widgets.Button(description='Continue..')
display(Continue_BTN)
if flag==0:
Continue_BTN.on_click(self.Continue_BTN_eventhandler)
elif flag==1:
Continue_BTN.on_click(self.Continue_BTN_eventhandler_W_DOCKER)
def Continue_BTN_eventhandler(self,a):
self.Lib_Version()
def Continue_BTN_eventhandler_W_DOCKER(self,a):
self.Write_Requirments(0)
self.Write_Requirments(1)
display("Docker is creating the environment, it may take 5 to 10 minutes ")
if self.OS_name == "Windows":
self.Write_Windows()
elif self.OS_name == "Linux":
self.Write_Linux()
else :
self.Write_MAC()
def Lib_Version(self):
#self.Requirment_dic=dict()
self.STR=""
for i in self.lib:
try:
self.STR+= i + "=="+ pkg_resources.get_distribution(i).version +"\n"
except:
self.STR+= i+"\n"
"""
try:
self.Requirment_dic[i]=pkg_resources.get_distribution(i).version
except:
self.Requirment_dic[i]=np.nan
"""
display("The following Text boxes are your Docker baselines:")
display("**************************************************** ")
display("Requirements : is including all the required libraries, You can add if you need more ...")
display("**************************************************** ")
text = widgets.Textarea(
value=self.STR,
placeholder='Paste ticket description here!',
description='Requirements:'
)
#disabled=False)
#text.value="wfr3gt4h234"
#text.add_class("placeholder")
text.width ='15cm'
text.height='10cm'
display(text)
self.STR=text.value
display("****************************************************")
display("Docker File : is including all the requirments such as OS, python, and the model requirments. you can install more tools if you want ...")
display("**************************************************** ")
DOCKER_text = widgets.Textarea(
value=self.DOC_TEXT,
placeholder='Paste ticket description here!',
description='Dockerfile:'
)
#disabled=False)
#DOCKER_text.add_class("placeholder")
DOCKER_text.width ='600px'
DOCKER_text.height='350px'
display(DOCKER_text)
self.DOC_TEXT=DOCKER_text.value
display("****************************************************")
display("Please kindly edit the text boxes and press the conticue key to complete the process ...")
self.reset_btn(1)
def Write_Requirments(self,flag):
if flag==0:
filehandle = open("./requirements.txt", "w")
STR=self.STR
elif flag==1:
filehandle = open("./Dockerfile", "w")
STR=self.DOC_TEXT
#filebuffer = ["hi","welcome","yes yes welcome"]
filehandle.writelines(STR)
filehandle.close()
#self.reset_btn(1)
def Write_Linux(self):
shel_file = open('./Permission_set.sh', 'w')
Shel_TEXT=["echo 'please type your password to complete the process:' \n",
"sudo chmod 777 /var/run/docker.sock"+"\n",
"\n",
"docker build -t {}:{} .".format(self.Docker_name,self.Tag)+" \n",
"docker save {} > {}".format(self.Docker_name,self.Docker_name+".tar")+" \n",
"echo 'Thnak you' \n"]
shel_file.writelines(Shel_TEXT)
shel_file.close()
subprocess.call(["sh","Permission_set.sh"])
def Write_Windows(self):
display("****************************************************")
shel_file = open('./Permission_set.sh', 'w')
Shel_TEXT=["\n",
"docker build -t {}:{} .".format(self.Docker_name,self.Tag)+" \n",
"docker save {} > {}".format(self.Docker_name,self.Docker_name+".tar")+" \n",
"echo 'Thnak you' \n"]
shel_file.writelines(Shel_TEXT)
shel_file.close()
#subprocess.call(["powershell","Permission_set.sh"])
display(" the process has been completed")
display(" please run the 'Permission_set.ssh' using shell command and the Docker image will be created automatically")
def Write_MAC(self):
display("**************************************************** ")
shel_file = open('./Permission_set.sh', 'w')
Shel_TEXT=[
"echo 'please type your password to complete the process:' \n",
"\n",
"docker build -t {}:{} .".format(self.Docker_name,self.Tag)+" \n",
"docker save {} > {}".format(self.Docker_name,self.Docker_name+".tar")+" \n",
"echo 'Thnak you' \n"]
shel_file.writelines(Shel_TEXT)
shel_file.close()
subprocess.call(["sh","Permission_set.sh"])