23
23
abort ('Add SAFSME_PEM_FILE location var to system path. Aborting operation ...' )
24
24
25
25
env .roledefs = {
26
- "ci" : ["jenkins" ],
27
- "dev" : ["localhost" ],
28
- "flexisaf" : ["ec2-user" ]
26
+ "flexisaf" : [
"[email protected] " ],
29
27
}
30
28
31
29
env .key_filename = SSH_PEM_FILE
@@ -59,9 +57,22 @@ def remove_old_build():
59
57
local ("rm -r build" )
60
58
local ("rm -r dist" )
61
59
local ("rm *.deb" )
62
- local ("rm -r SHINKAFA .egg-info" )
60
+ local ("rm -r SAFTIMS_HR .egg-info" )
63
61
64
62
63
+ def run_create_js_dist ():
64
+ """
65
+ Use Node and Npm to bundle all our js and saas
66
+ file and put then inside our static folder, which
67
+ will be collected when django run it own
68
+ collect static command, and serve by Nginx
69
+ """
70
+ with settings (warn_only = True ):
71
+ install_js_dep = local ("npm install" )
72
+ dist_cmd = local ("npm run dist" )
73
+ if install_js_dep .failed or dist_cmd .failed :
74
+ abort ("Fail create js bundle" )
75
+ print ("Bundling and minifying of JS completed" )
65
76
66
77
67
78
@@ -145,45 +156,56 @@ def start_docker_process(docker_host="staging"):
145
156
mode and always restart the docker container if for
146
157
any reason the process inside crashes
147
158
"""
148
- host_machine_pwd = local ( 'echo $HOME ' )
149
- host_log_directory = '/home/ubuntu/ webapp/log/shinkafa'
159
+ host_machine_pwd = run ( 'pwd ' )
160
+ host_log_directory = os . path . join ( host_machine_pwd , ' webapp/log/shinkafa')
150
161
# check if there is a log directory on the host machine
151
- if not os . path . exists (host_log_directory ):
162
+ if not exists (host_log_directory ):
152
163
# then create the log directory
153
- local ("mkdir -p %s" % host_log_directory )
164
+ run ("mkdir -p %s" % host_log_directory )
154
165
docker_tag = "flexisaf/shinkafa:latest"
155
166
client_db_name = "shinkafa_" + docker_host
156
- secret_key = "your_secrete393939_key_here_please_not_so$secret "
157
- docker_env = "-e DB_NAME='%s' -e CLIENT_S3_FOLDER='%s' -e SECRET_KEY='%s'" % (client_db_name , docker_host ,secret_key )
158
- local ("docker run %s --name=shinkafa --detach=true --restart=always --publish=8083 :80 --volume=%s:%s %s"
167
+ secret_key = "not-so_secret_right%%%$$$09 "
168
+ docker_env = "-e DB_NAME='%s' -e CLIENT_S3_FOLDER='%s' -e SECRET_KEY='%s'" % (client_db_name , docker_host , secret_key )
169
+ run ("docker run %s --name=shinkafa --detach=true --restart=always --publish=80 :80 --volume=%s:%s %s"
159
170
% (docker_env , host_log_directory , DOCKER_LOG_DIR , docker_tag ))
160
171
161
172
162
173
@task ()
163
174
def stop_container ():
164
- local ("docker stop shinkafa" )
175
+ run ("docker stop shinkafa" )
165
176
166
177
167
178
@task ()
168
179
def start_container ():
169
- local ("docker start shinkafa" )
180
+ run ("docker start shinkafa" )
170
181
171
182
172
183
@task ()
173
184
def restart_container ():
174
- local ("docker restart shinkafa" )
185
+ run ("docker restart shinkafa" )
175
186
176
187
188
+ def send_compress_docker_to_remote ():
189
+ """
190
+ Send our compressed docker image to the
191
+ the remote instances that was specified
192
+ """
193
+ with settings (colorize_errors = True ):
194
+ put ('shinkafa.tgz' , '~/' )
195
+
177
196
178
197
@task ()
179
198
def start_build_pipeline ():
180
199
remove_old_build ()
200
+ run_create_js_dist ()
181
201
package_tar ()
182
202
copy_tar_to_docker_folder ()
183
203
build_docker_image ()
204
+ zip_docker_image ()
184
205
185
206
186
207
@task ()
208
+ @roles (["flexisaf" ])
187
209
def ship_docker ():
188
210
"""
189
211
Ship the compressed docker image to the host machine
@@ -193,13 +215,19 @@ def ship_docker():
193
215
and offload the tar ball on the production
194
216
machine which then start the docker process
195
217
"""
218
+ send_compress_docker_to_remote ()
219
+ # offload the compressed docker image on the remote and use docker load to add it
220
+ with settings (warn_only = True ):
221
+ docker_load = run ("gunzip -c shinkafa.tgz | docker load" )
222
+ if docker_load .failed : # during unziping did the process failed on
223
+ pass # this machine if so just pass and continue to other machine
224
+
196
225
with settings (warn_only = True , colorize_error = True ):
197
226
# check if this is the first time we are loading this docker on the machine
198
- docker_restart = local ("docker restart shinkafa" )
227
+ docker_restart = run ("docker restart shinkafa" )
199
228
if docker_restart .failed :
200
- print (red ("Docker process restarted, failed Starting a new process" ))
229
+ print (red ("Docker process restarted, Starting a new process" ))
201
230
# only start a new docker process if there is no current process running
202
- start_docker_process (docker_host = "shinkafa" ) # start a new docker process
203
-
231
+ start_docker_process (docker_host = get_client_name_from_host (env .host )) # start a new docker process
204
232
205
233
0 commit comments