|
34 | 34 | end
|
35 | 35 |
|
36 | 36 | if ::File.exists?(new_resource.path)
|
| 37 | + s3_etag = S3FileLib::get_md5_from_s3(new_resource.bucket, new_resource.s3_url, remote_path, aws_access_key_id, aws_secret_access_key, token) |
| 38 | + |
37 | 39 | if decryption_key.nil?
|
38 | 40 | if new_resource.decrypted_file_checksum.nil?
|
39 |
| - s3_md5 = S3FileLib::get_md5_from_s3(new_resource.bucket, new_resource.s3_url, remote_path, aws_access_key_id, aws_secret_access_key, token) |
40 |
| - |
41 |
| - if S3FileLib::verify_md5_checksum(s3_md5, new_resource.path) |
| 41 | + if S3FileLib::verify_md5_checksum(s3_etag, new_resource.path) |
42 | 42 | Chef::Log.debug 'Skipping download, md5sum of local file matches file in S3.'
|
43 | 43 | download = false
|
44 | 44 | end
|
|
59 | 59 | end
|
60 | 60 | end
|
61 | 61 | end
|
| 62 | + |
| 63 | + # Don't download if content and etag match prior download |
| 64 | + if node['s3_file']['use_catalog'] |
| 65 | + catalog_data = S3FileLib::catalog.fetch(new_resource.path, nil) |
| 66 | + existing_file_md5 = S3FileLib::buffered_md5_checksum(new_resource.path) |
| 67 | + if catalog_data && existing_file_md5 == catalog_data['local_md5'] && s3_etag == catalog_data['etag'] |
| 68 | + Chef::Log.debug 'Skipping download, md5 of local file and etag matches prior download.' |
| 69 | + download = false |
| 70 | + end |
| 71 | + end |
62 | 72 | end
|
63 | 73 |
|
64 | 74 | if download
|
|
78 | 88 | raise e
|
79 | 89 | end
|
80 | 90 |
|
81 |
| - ::FileUtils.mv(decrypted_file.path, new_resource.path) |
| 91 | + downloaded_file = decrypted_file |
82 | 92 | else
|
83 |
| - ::FileUtils.mv(response.file.path, new_resource.path) |
| 93 | + downloaded_file = response.file |
| 94 | + end |
| 95 | + |
| 96 | + # Write etag and md5 to catalog for future reference |
| 97 | + if node['s3_file']['use_catalog'] |
| 98 | + catalog = S3FileLib::catalog |
| 99 | + catalog[new_resource.path] = { |
| 100 | + 'etag' => response.headers[:etag].gsub('"',''), |
| 101 | + 'local_md5' => S3FileLib::buffered_md5_checksum(downloaded_file.path) |
| 102 | + } |
| 103 | + S3FileLib::write_catalog(catalog) |
| 104 | + end |
| 105 | + |
| 106 | + # Take ownership and permissions from existing object |
| 107 | + if ::File.exist?(new_resource.path) |
| 108 | + stat = ::File::Stat.new(new_resource.path) |
| 109 | + ::FileUtils.chown(stat.uid, stat.gid, downloaded_file) |
| 110 | + ::FileUtils.chmod(stat.mode, downloaded_file) |
84 | 111 | end
|
| 112 | + ::FileUtils.mv(downloaded_file.path, new_resource.path) |
85 | 113 | end
|
86 | 114 |
|
87 | 115 | f = file new_resource.path do
|
88 | 116 | action :create
|
89 |
| - owner new_resource.owner || ENV['user'] |
90 |
| - group new_resource.group || ENV['user'] |
| 117 | + owner new_resource.owner || ENV['USER'] |
| 118 | + group new_resource.group || ENV['USER'] |
91 | 119 | mode new_resource.mode || '0644'
|
92 | 120 | end
|
93 | 121 |
|
|
0 commit comments