7
7
import time
8
8
from fabric .api import *
9
9
from os .path import expanduser
10
+ import sys
10
11
11
12
from fabfile .util import install_public_key , role_is_active
12
13
from util import venv , get_value_from_password_store , PASSWORD_FILE_FTP_USERNAME_PARAM_NAME , PASSWORD_FILE_FTP_PASSWORD_PARAM_NAME
@@ -157,15 +158,21 @@ def tar(filename, target_path, command='czvf', test=False):
157
158
run ('tar %s %s *' % (command , filename ))
158
159
159
160
160
- def untar (filename , target_path , command = 'xzvf' , use_sudo = False , test = False ):
161
+ def untar (filename , target_path , command = 'xzvf' , use_sudo = False , test = False , is_local = False ):
161
162
tar_cmd = 'tar %s %s' % (command , filename )
162
- with cd (target_path ):
163
- if test :
164
- print "Simulating tar command %s" % tar_cmd
165
- elif use_sudo :
166
- sudo (tar_cmd )
167
- else :
168
- run (tar_cmd )
163
+ if test :
164
+ print "Simulating tar command %s" % tar_cmd
165
+ return
166
+
167
+ if is_local :
168
+ with lcd (target_path ):
169
+ local (tar_cmd )
170
+ else :
171
+ with cd (target_path ):
172
+ if use_sudo :
173
+ sudo (tar_cmd )
174
+ else :
175
+ run (tar_cmd )
169
176
170
177
171
178
def ftp_push (filename , ftp_address , username , password , test = False ):
@@ -176,14 +183,21 @@ def ftp_push(filename, ftp_address, username, password, test=False):
176
183
run ('curl -T %s %s --user %s:%s --ftp-create-dirs' % (filename , ftp_address , username , password ))
177
184
178
185
179
- def ftp_fetch (filename , ftp_address , target_path , username , password , test = False ):
180
- with cd (target_path ):
181
- if test :
182
- print "ftp fetch %s from %s to %s" % (filename , ftp_address , target_path )
183
- else :
186
+ def ftp_fetch (filename , ftp_address , target_path , username , password , test = False , is_local = False ):
187
+ if test :
188
+ print "ftp fetch %s from %s to %s" % (filename , ftp_address , target_path )
189
+ return
190
+
191
+ cmd = 'curl %s/%s --user %s:%s --ftp-create-dirs -o %s' % (
192
+ ftp_address , filename , username , password , filename )
193
+ if is_local :
194
+ with lcd (target_path ):
195
+ local (cmd )
196
+ else :
197
+ with cd (target_path ):
184
198
with hide ('output' ,'running' ):
185
- run ('curl %s/%s --user %s:%s --ftp-create-dirs -o %s' % ( ftp_address , filename , username , password ,
186
- filename ))
199
+ run (cmd )
200
+
187
201
188
202
189
203
def tar_and_ftp_push (snapshot_name , name , password , source_tar_path , target_path , username , test = False ):
@@ -193,52 +207,74 @@ def tar_and_ftp_push(snapshot_name, name, password, source_tar_path, target_path
193
207
test = test )
194
208
195
209
196
- def ftp_fetch_and_untar (snapshot_name , name , tmp_path , target_tar_unpack_path , username , password , test = False ):
210
+ def ftp_fetch_and_untar (snapshot_name , name , tmp_path , target_tar_unpack_path , username , password , test = False , is_local = False ):
197
211
file_to_download = '%s.tar.gz' % name
198
- ftp_fetch (file_to_download , "%s/%s" % (env .ftp_server_url , snapshot_name ), tmp_path , username , password , test = test )
199
- clean_path (target_tar_unpack_path , use_sudo = True , test = test )
200
- untar ('%s/%s.tar.gz' % (tmp_path , name ), target_tar_unpack_path , use_sudo = True , test = test )
212
+ ftp_fetch (file_to_download , "%s/%s" % (env .ftp_server_url , snapshot_name ), tmp_path , username , password , test = test , is_local = is_local )
213
+ clean_path (target_tar_unpack_path , use_sudo = True , test = test , is_local = is_local )
214
+ untar ('%s/%s.tar.gz' % (tmp_path , name ), target_tar_unpack_path , use_sudo = True , test = test , is_local = is_local )
201
215
202
216
203
- @parallel
204
217
@roles ('main' )
205
218
def take_main_snapshot_and_push_to_ftp (snapshot_name , target_path , username , password , test = False ):
206
219
if not role_is_active ('main' ):
207
220
return
221
+ if not test :
222
+ tomcat_stop ()
223
+
208
224
tar_and_ftp_push (snapshot_name , 'depot' , password , '/opt/rinfo/store/' , target_path , username , test = test )
209
225
226
+ if not test :
227
+ tomcat_start ()
210
228
211
- @parallel
212
229
@roles ('service' )
213
230
def take_service_snapshot_and_push_to_ftp (snapshot_name , target_path , username , password , use_sesame = True ,
214
231
use_elasticsearch = True , test = False ):
215
232
if not role_is_active ('service' ):
216
233
return
234
+
235
+ if not test :
236
+ tomcat_stop ()
237
+ try :
238
+ sudo ("/etc/init.d/elasticsearch stop" )
239
+ except :
240
+ e = sys .exc_info ()[0 ]
241
+ print "WARNING! Problems stopping elastic search because %s" % e
242
+
243
+
217
244
if use_sesame :
218
245
tar_and_ftp_push (snapshot_name , 'sesame' , password , '/opt/rinfo/sesame-repo/' , target_path , username ,
219
246
test = test )
220
247
if use_elasticsearch :
221
248
tar_and_ftp_push (snapshot_name , 'elasticsearch' , password , '/opt/elasticsearch/var/data/' , target_path ,
222
249
username , test = test )
223
250
251
+ if not test :
252
+ tomcat_start ()
253
+ sudo ("/etc/init.d/elasticsearch start" )
224
254
225
- def clean_path (tar_target_path , use_sudo = False , test = False ):
255
+ def clean_path (tar_target_path , use_sudo = False , test = False , is_local = False ):
256
+ cmd = "rm -rf %s*" % tar_target_path
226
257
if test :
227
258
print "remove files %s" % tar_target_path
259
+ elif is_local :
260
+ local (cmd )
228
261
elif use_sudo :
229
- sudo ("rm -rf %s*" % tar_target_path )
262
+ sudo (cmd )
230
263
else :
231
- run ("rm -rf %s*" % tar_target_path )
264
+ run (cmd )
232
265
233
266
234
- def create_path (target_path , test = False , use_sudo = False ):
267
+ def create_path (target_path , test = False , use_sudo = False , is_local = False ):
235
268
if test :
236
269
print "Make directory: %s" % target_path
237
270
return
238
- if use_sudo :
239
- sudo ("mkdir -p %s" % target_path )
271
+ cmd = "mkdir -p %s" % target_path
272
+ if is_local :
273
+ local (cmd )
274
+ elif use_sudo :
275
+ sudo (cmd )
240
276
else :
241
- run ("mkdir -p %s" % target_path )
277
+ run (cmd )
242
278
243
279
244
280
def take_ownership (target_path , test = False , use_sudo = False ):
@@ -273,39 +309,62 @@ def take_snapshot_and_push_to_ftp(name='snapshot', username='', password='', tes
273
309
username = get_value_from_password_store (PASSWORD_FILE_FTP_USERNAME_PARAM_NAME , username )
274
310
password = get_value_from_password_store (PASSWORD_FILE_FTP_PASSWORD_PARAM_NAME , password )
275
311
276
- if not test :
277
- tomcat_stop ()
278
- clean_path (tar_target_path , test = test )
279
- create_path (tar_target_path , test = test )
312
+ clean_path (tar_target_path , test = False )
313
+ create_path (tar_target_path , test = False )
314
+
280
315
try :
281
316
take_main_snapshot_and_push_to_ftp (snapshot_name , tar_target_path , username , password , test = test )
282
317
take_service_snapshot_and_push_to_ftp (snapshot_name , tar_target_path , username , password , test = test )
283
318
finally :
284
- clean_path (tar_target_path , test = test )
285
- if not test :
286
- tomcat_start ()
287
-
319
+ clean_path (tar_target_path , test = False )
288
320
289
321
@parallel
290
322
@roles ('main' )
291
- def fetch_main_snapshot_from_ftp_and_install (snapshot_name , tar_target_path , username , password , test = False ):
323
+ def fetch_main_snapshot_from_ftp_and_install (snapshot_name , tar_target_path , username , password , test = False ,
324
+ is_local = False ):
292
325
if not role_is_active ('main' ):
293
326
return
294
- ftp_fetch_and_untar (snapshot_name , 'depot' , tar_target_path , '/opt/rinfo/store/' , username , password , test = test )
327
+ if not test :
328
+ tomcat_stop ()
329
+
330
+ clean_path (tar_target_path , test = test )
331
+ create_path (tar_target_path , test = test )
332
+
333
+ ftp_fetch_and_untar (snapshot_name , 'depot' , tar_target_path , '/opt/rinfo/store/' , username , password , test = test ,
334
+ is_local = is_local )
335
+
336
+ if not test :
337
+ tomcat_start ()
295
338
296
339
297
340
@parallel
298
341
@roles ('service' )
299
342
def fetch_service_snapshot_from_ftp_and_install (snapshot_name , tar_target_path , username , password , use_sesame = True ,
300
- use_elasticsearch = True , test = False ):
343
+ use_elasticsearch = True , test = False , is_local = False ):
301
344
if not role_is_active ('service' ):
302
345
return
346
+
347
+ if not test :
348
+ tomcat_stop ()
349
+ try :
350
+ sudo ("/etc/init.d/elasticsearch stop" )
351
+ except :
352
+ e = sys .exc_info ()[0 ]
353
+ print "Warning: problems stopping elastic search because %s" % e
354
+
355
+ clean_path (tar_target_path , test = test )
356
+ create_path (tar_target_path , test = test )
357
+
303
358
if use_sesame :
304
359
ftp_fetch_and_untar (snapshot_name , 'sesame' , tar_target_path , '/opt/rinfo/sesame-repo/' , username , password ,
305
- test = test )
360
+ test = test , is_local = is_local )
306
361
if use_elasticsearch :
307
362
ftp_fetch_and_untar (snapshot_name , 'elasticsearch' , tar_target_path , '/opt/elasticsearch/var/data/' , username ,
308
- password , test = test )
363
+ password , test = test , is_local = is_local )
364
+
365
+ if not test :
366
+ tomcat_start ()
367
+ sudo ("/etc/init.d/elasticsearch start" )
309
368
310
369
311
370
@task
@@ -320,20 +379,27 @@ def fetch_snapshot_from_ftp_and_install(name='snapshot' ,username='', password='
320
379
username = get_value_from_password_store (PASSWORD_FILE_FTP_USERNAME_PARAM_NAME , username )
321
380
password = get_value_from_password_store (PASSWORD_FILE_FTP_PASSWORD_PARAM_NAME , password )
322
381
323
- if not test :
382
+ is_local = env .target in ['dev_unix' ]
383
+
384
+ print "is_local %s" % is_local
385
+
386
+ if not test and not is_local :
324
387
tomcat_stop ()
325
- clean_path (tar_target_path , test = test )
326
- create_path (tar_target_path , test = test )
388
+
389
+ clean_path (tar_target_path , test = test , is_local = is_local )
390
+ create_path (tar_target_path , test = test , is_local = is_local )
391
+
327
392
try :
328
- fetch_main_snapshot_from_ftp_and_install (snapshot_name , tar_target_path , username , password , test = test )
329
- fetch_service_snapshot_from_ftp_and_install (snapshot_name , tar_target_path , username , password , test = test )
393
+ fetch_main_snapshot_from_ftp_and_install (snapshot_name , tar_target_path , username , password , test = test ,
394
+ is_local = is_local )
395
+ fetch_service_snapshot_from_ftp_and_install (snapshot_name , tar_target_path , username , password , test = test ,
396
+ is_local = is_local )
330
397
finally :
331
- clean_path (tar_target_path , test = test )
398
+ clean_path (tar_target_path , test = test , is_local = is_local )
332
399
# todo empty varnish cache
333
- if not test :
400
+ if not test and not is_local :
334
401
tomcat_start ()
335
402
336
-
337
403
def prefere_ipv4_to_speed_up_debian_updates ():
338
404
sudo ('echo "precedence ::ffff:0:0/96 100" >> /etc/gai.conf' )
339
405
0 commit comments