Skip to content

Commit

Permalink
fixed guards for missing when renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
abudhu committed May 28, 2016
1 parent c2bb0c7 commit dab8854
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
1 change: 0 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
default['azcopy']['dotnet_release_versions'] = [
#111111
378389, 378675, 378758, 379893, 393295, 393297, 394254, 394271, 394747, 394748
]

Expand Down
65 changes: 38 additions & 27 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,40 +71,52 @@ def download_file(key,blob,folder,file,destination,journal,guard_type)
if current_file.class == Hash
# We are renaming items as we download them
current_file.each do |remote_name, dest_name|
# Lets make a backup of the Remote and Destination Files

if (File.exists?("#{destination}\\#{remote_name}"))
Chef::Log.info("Renaming #{remote_name} to .old as it exists at #{destination}")
File.rename("#{destination}\\#{remote_name}", "#{destination}\\#{remote_name}.old")
end
if (File.exists?("#{destination}\\#{dest_name}"))
Chef::Log.info("Renaming #{dest_name} to .old as it exists at #{destination}")
File.rename("#{destination}\\#{dest_name}", "#{destination}\\#{dest_name}.old")
end
ruby_block "Backup Remote File if exists on Local machine" do
block do
if (File.exists?("#{destination}\\#{remote_name}"))
Chef::Log.info("Renaming #{remote_name} to .old as it exists at #{destination}")
File.rename("#{destination}\\#{remote_name}", "#{destination}\\#{remote_name}.old")
end
end
not_if {guard_type == :download_if_missing && File.exists?("#{destination}#{remote_name}")}
end # Ruby Block

ruby_block "Backup Destination File if exists on Local machine" do
block do
if (File.exists?("#{destination}\\#{dest_name}"))
Chef::Log.info("Renaming #{dest_name} to .old as it exists at #{destination}")
File.rename("#{destination}\\#{dest_name}", "#{destination}\\#{dest_name}.old")
end
end
end # Ruby Block

# Download and Rename the files
powershell_script "Downloading: #{remote_name} From: #{blob}\\#{folder} to: #{destination} and renaming to: #{dest_name}" do
powershell_script "Downloading: #{remote_name} From: #{blob}\\#{folder} to: #{destination}" do
code <<-EOH
Out-File C:\\temp\\amit.txt
rename-item -path c:\\temp\\amit.txt -newname BUDHU.txt -force
"#{node['azcopy']['executable']}" /Source:#{blob}/#{folder} /Dest:#{destination} /SourceKey:"#{key}" /Pattern:"#{remote_name}"
EOH

#TODO: Fix this guard logic. You will always redownload since you moved them to .old
not_if {guard_type == :download_if_missing && File.exists?("#{destination}#{remote_name}") or guard_type == :download_if_missing && File.exists?("#{destination}#{dest_name}") }
not_if{guard_type == :download_if_missing && File.exists?("#{destination}#{remote_name}")}
end

# "#{node['azcopy']['executable']}" /Source:#{blob}/#{folder} /Dest:#{destination} /SourceKey:"#{key}" /Pattern:"#{remote_name}"
# Rename-Item -path #{destination}\\#{remote_name} -newname #{dest_name} -force
powershell_script "Renaming #{remote_name} to: #{dest_name}" do
code <<-EOH
Rename-Item -path #{destination}\\#{remote_name} -newname #{dest_name} -force
EOH
end

# Delete those backups
if (File.exists?("#{destination}\\#{dest_name}.old") || File.exists?("#{destination}\\#{remote_name}.old") )
begin
File.delete("#{destination}\\#{dest_name}.old")
File.delete("#{destination}\\#{remote_name}.old")
rescue
# Empty Rescue since we are throwing away Ruby's error if the file might not exists
ruby_block "Remove Backup files" do
block do
if (File.exists?("#{destination}\\#{dest_name}.old") || File.exists?("#{destination}\\#{remote_name}.old") )
begin
File.delete("#{destination}\\#{dest_name}.old")
File.delete("#{destination}\\#{remote_name}.old")
rescue
# Empty Rescue since we are throwing away Ruby's error if the file might not exists
end # Begin / Rescue
end
end
end
end # Ruby Block

end # current_file loop
else
Expand All @@ -115,10 +127,9 @@ def download_file(key,blob,folder,file,destination,journal,guard_type)
EOH
not_if {guard_type == :download_if_missing && File.exists?("#{destination}#{current_file}")}
end
end # If
end # If class = hash
end # File Loop
end # Download_file


end # Helpers
end # AZCopyLWRP

0 comments on commit dab8854

Please sign in to comment.