@@ -67,7 +67,7 @@ def download(
67
67
state = None , host = None ,
68
68
):
69
69
'''
70
- Download files from remote locations using curl or wget.
70
+ Download files from remote locations using `` curl`` or `` wget`` .
71
71
72
72
+ src: source URL of the file
73
73
+ dest: where to save the file
@@ -92,6 +92,8 @@ def download(
92
92
'''
93
93
94
94
info = host .get_fact (File , path = dest )
95
+ host_datetime = host .get_fact (Date ).replace (tzinfo = None )
96
+
95
97
# Destination is a directory?
96
98
if info is False :
97
99
raise OperationError (
@@ -111,8 +113,8 @@ def download(
111
113
if cache_time :
112
114
# Time on files is not tz-aware, and will be the same tz as the server's time,
113
115
# so we can safely remove the tzinfo from the Date fact before comparison.
114
- cache_time = host . get_fact ( Date ). replace ( tzinfo = None ) - timedelta (seconds = cache_time )
115
- if info ['mtime' ] and info ['mtime' ] > cache_time :
116
+ cache_time = host_datetime - timedelta (seconds = cache_time )
117
+ if info ['mtime' ] and info ['mtime' ] < cache_time :
116
118
download = True
117
119
118
120
if sha1sum :
@@ -171,6 +173,24 @@ def download(
171
173
'|| ( echo {2} && exit 1 )'
172
174
), QuoteString (dest ), md5sum , QuoteString ('MD5 did not match!' ))
173
175
176
+ host .create_fact (
177
+ File ,
178
+ kwargs = {'path' : dest },
179
+ data = {'mode' : mode , 'group' : group , 'user' : user , 'mtime' : host_datetime },
180
+ )
181
+
182
+ # Remove any checksum facts as we don't know the correct values
183
+ for value , fact_cls in (
184
+ (sha1sum , Sha1File ),
185
+ (sha256sum , Sha256File ),
186
+ (md5sum , Md5File ),
187
+ ):
188
+ fact_kwargs = {'path' : dest }
189
+ if value :
190
+ host .create_fact (fact_cls , kwargs = fact_kwargs , data = value )
191
+ else :
192
+ host .delete_fact (fact_cls , kwargs = fact_kwargs )
193
+
174
194
else :
175
195
host .noop ('file {0} has already been downloaded' .format (dest ))
176
196
@@ -332,6 +352,12 @@ def line(
332
352
else :
333
353
host .noop ('line "{0}" exists in {1}' .format (replace or line , path ))
334
354
355
+ host .create_fact (
356
+ FindInFile ,
357
+ kwargs = {'path' : path , 'pattern' : match_line },
358
+ data = [replace or line ],
359
+ )
360
+
335
361
# Line(s) exists and we want to remove them, replace with nothing
336
362
elif present_lines and not present :
337
363
yield sed_replace (
@@ -341,11 +367,18 @@ def line(
341
367
interpolate_variables = interpolate_variables ,
342
368
)
343
369
370
+ host .delete_fact (
371
+ FindInFile ,
372
+ kwargs = {'path' : path , 'pattern' : match_line },
373
+ )
374
+
344
375
# Line(s) exists and we have want to ensure they're correct
345
376
elif present_lines and present :
346
377
# If any of lines are different, sed replace them
347
378
if replace and any (line != replace for line in present_lines ):
348
379
yield sed_replace_command
380
+ del present_lines [:] # TODO: use .clear() when py3+
381
+ present_lines .append (replace )
349
382
else :
350
383
host .noop ('line "{0}" exists in {1}' .format (replace or line , path ))
351
384
@@ -395,6 +428,11 @@ def replace(
395
428
backup = backup ,
396
429
interpolate_variables = interpolate_variables ,
397
430
)
431
+ host .create_fact (
432
+ FindInFile ,
433
+ kwargs = {'path' : path , 'pattern' : match },
434
+ data = [],
435
+ )
398
436
else :
399
437
host .noop ('string "{0}" does not exist in {1}' .format (match , path ))
400
438
@@ -578,10 +616,15 @@ def _create_remote_dir(state, host, remote_filename, user, group):
578
616
)
579
617
580
618
581
- @operation (pipeline_facts = {
582
- 'file' : 'src' ,
583
- 'sha1_file' : 'src' ,
584
- })
619
+ @operation (
620
+ # We don't (currently) cache the local state, so there's nothing we can
621
+ # update to flag the local file as present.
622
+ is_idempotent = False ,
623
+ pipeline_facts = {
624
+ 'file' : 'src' ,
625
+ 'sha1_file' : 'src' ,
626
+ },
627
+ )
585
628
def get (
586
629
src , dest ,
587
630
add_deploy_dir = True , create_local_dir = False , force = False ,
@@ -705,6 +748,8 @@ def put(
705
748
if create_remote_dir :
706
749
yield _create_remote_dir (state , host , dest , user , group )
707
750
751
+ local_sum = get_file_sha1 (src )
752
+
708
753
# No remote file, always upload and user/group/mode if supplied
709
754
if not remote_file or force :
710
755
yield FileUploadCommand (local_file , dest )
@@ -717,7 +762,6 @@ def put(
717
762
718
763
# File exists, check sum and check user/group/mode if supplied
719
764
else :
720
- local_sum = get_file_sha1 (src )
721
765
remote_sum = host .get_fact (Sha1File , path = dest )
722
766
723
767
# Check sha1sum, upload if needed
@@ -749,6 +793,14 @@ def put(
749
793
if not changed :
750
794
host .noop ('file {0} is already uploaded' .format (dest ))
751
795
796
+ # Now we've uploaded the file and ensured user/group/mode, update the relevant fact data
797
+ host .create_fact (Sha1File , kwargs = {'path' : dest }, data = local_sum )
798
+ host .create_fact (
799
+ File ,
800
+ kwargs = {'path' : dest },
801
+ data = {'user' : user , 'group' : group , 'mode' : mode },
802
+ )
803
+
752
804
753
805
@operation
754
806
def template (
0 commit comments