forked from LuoDi-Nate/d_builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
261 lines (181 loc) · 6.16 KB
/
Copy pathbuilder.py
File metadata and controls
261 lines (181 loc) · 6.16 KB
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
import getopt
import sys
import os
import time
import jar_dockerfile as jar
import webapp_dockerfile as war
"""
config 4 builder
"""
setup_path = "/tmp/data3/docker_file/"
tomcat_path = "/tmp/data3/tomcat8"
print 'builder start ...'
# def args, only enum in ("jar", "war", "html")
app_type = "jar"
app_path = "~/"
app_bind = {}
app_server_name = ""
app_version = "0.0"
app_author = "nobody"
app_author_mail = "nobody@sample.com"
jar_docker_file = ""
war_docker_file = ""
html_docker_file = ""
def exit_with_msg(msg):
print msg
exit(1)
# args resolver
opts, args = getopt.getopt(sys.argv[1:], "t:u:p:n:v:")
for opt, value in opts:
if opt == '-t':
print "-t :", value
if value not in ("jar", "war"):
exit_with_msg("invalid value : \'-t\' :" + value)
app_type = value
elif opt == '-u':
print "-u :", value
if not os.path.exists(value):
exit_with_msg("file not found : \'-u\' :" + value)
app_path = value
elif opt == '-p':
print '-p', value
# like "80:8080#443:8443"
if value:
bind_list = value.split('#')
for bind in bind_list:
host_port, virtual_port = bind.split(":")
app_bind[host_port] = virtual_port
print app_bind
elif opt == '-n':
print '-n :', value
if not value:
exit_with_msg("invalid value \'-n\'" + value)
app_server_name = value
elif opt == '-v':
print '-v :', value
if not value:
exit_with_msg("invalid value \'-v\'" + value)
app_version = value
else:
exit_with_msg("unknown arg:" + opt)
# check necessary
def check_args_4_jar():
if not app_path:
exit_with_msg("no path for deploy apps.")
if not app_bind:
print("[warn] this jar app has no port binding.")
if not app_server_name:
exit_with_msg("no app name is invalid.")
if app_version == "0.0":
print("[warn] no version set, use default 0.0")
def check_args_4_webapp():
if not app_path:
exit_with_msg("no path for deploy apps.")
if not app_bind:
exit_with_msg("webapp need last 1 port binding.")
if not app_server_name:
exit_with_msg("no app name is invalid.")
if app_version == "0.0":
print("[warn] no version set, use default 0.0")
def check_args_4_html():
pass
# check app_type validation
if app_type not in ("war", "jar", "html"):
exit_with_msg("unknown app type for %s" % app_type)
# replace dockerfile
def replace_args_4_jar():
print "make docker file 4 jar"
global jar_docker_file, app_author, app_author_mail
jar_docker_file = jar_docker_file.replace("<<._ AUTHOR>>", app_author)
jar_docker_file = jar_docker_file.replace("<<._ AUTHOR_MAIL>>", app_author_mail)
def replace_args_4_war():
print "make docker file 4 war"
global war_docker_file, app_author, app_author_mail
war_docker_file = war_docker_file.replace("<<._ AUTHOR>>", app_author)
war_docker_file = war_docker_file.replace("<<._ AUTHOR_MAIL>>", app_author_mail)
def replace_args_4_html():
print "make docker file 4 html"
pass
# exec shell with this func, if result not 0 , exit with cmd
def exec_cmd_via_shell_result_true(cmd):
print "exec shell : [%s]" % cmd
result = os.system(cmd)
# in linux , 0 is true, any else will be regarded error
if result:
exit_with_msg("error during exec this :[%s]" % cmd)
print "done."
# check args with app type
if app_type == "war":
check_args_4_webapp()
war_docker_file = war.webapp_dockerfile
replace_args_4_war()
elif app_type == "jar":
check_args_4_jar()
jar_docker_file = jar.jar_dockerfile
replace_args_4_jar()
elif app_type == "html":
check_args_4_html()
# TODO make html docker file
replace_args_4_html()
# generator dir name 4 dockerfile
time_str = time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime(time.time()))
print "time now:%s" % time_str
dir_name = "%s%s_%s_%s_%s" % (setup_path, time_str, app_type, app_server_name, app_version)
print "making working dir... [%s]" % dir_name
cmd = "mkdir -p %s" % dir_name
exec_cmd_via_shell_result_true(cmd)
# move app to setup path
# cmd = "cp -r %s %s" % (app_path, dir_name)
# exec_cmd_via_shell_result_true(cmd)
# generator docker file
dockerfile_path = "%s/%s" % (dir_name, "Dockerfile")
def gen_dockerfile_by_apptype(app_type, dockerfile_path):
#create file
print "touch Dockerfile..."
cmd = "touch %s" % dockerfile_path
exec_cmd_via_shell_result_true(cmd)
file = open(dockerfile_path, "w")
print "generator docker file..."
if app_type == "jar":
file.write(jar_docker_file)
elif app_type == "war":
file.write(war_docker_file)
elif app_type == "html":
pass
else:
exit_with_msg("wrong app type.")
file.flush()
file.close()
# gen
gen_dockerfile_by_apptype(app_type, dockerfile_path)
# jar build operation
def build_jar():
cmd = "mkdir -p %s/firstblood" % dir_name
exec_cmd_via_shell_result_true(cmd)
# unzip zipfile to deploy
cmd = "unzip %s/%s.zip -d %s/firstblood" % (app_path, app_server_name, dir_name)
exec_cmd_via_shell_result_true(cmd)
cmd = "cp %s/start.sh %s/firstblood/start.sh" % (app_path, dir_name)
exec_cmd_via_shell_result_true(cmd)
print "build done! exec downstairs shell ..."
print "[ cd %s ; sudo docker build -t \"%s-%s-%s\" . ]" % (dir_name, app_type, app_version, app_server_name)
# war build operation
def build_war():
# copy tomcat here
cmd = "cp -r %s %s" % (tomcat_path, dir_name)
exec_cmd_via_shell_result_true(cmd)
# deploy
cmd = "rm -rf %s/tomcat8/webapps/ROOT/*" % (dir_name)
exec_cmd_via_shell_result_true(cmd)
cmd = "unzip %s/%s.war -d %s/tomcat8/webapps/ROOT" % (app_path, app_server_name, dir_name)
exec_cmd_via_shell_result_true(cmd)
print "build done! exec downstairs shell ..."
print "[ cd %s ; sudo docker build -t \"%s-%s-%s\" . ]" % (dir_name, app_type, app_server_name, app_version)
# begin build
print "begin build..."
if app_type == "jar":
build_jar()
elif app_type == "war":
build_war()
elif app_type == "html":
pass