Skip to content

Commit

Permalink
download file helper
Browse files Browse the repository at this point in the history
  • Loading branch information
abudhu committed May 28, 2016
1 parent 6c2d8ec commit c2bb0c7
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
9 changes: 7 additions & 2 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
default['azcopy']['dotnet_release_versions'] = [
111111
#378389, 378675, 378758, 379893, 393295, 393297, 394254, 394271, 394747, 394748
#111111
378389, 378675, 378758, 379893, 393295, 393297, 394254, 394271, 394747, 394748
]

default['azcopy']['dotnet_url'] = "https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe"

default['azcopy']['azcopy_url'] = "http://az635501.vo.msecnd.net/azcopy-5-0-0/MicrosoftAzureStorageTools.msi"

default['azcopy']['executable'] = "C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\AzCopy\\azcopy.exe"

default['azcopy']['journal_path'] = [
"C:\\Users\\",
"\\AppData\\Local\\Microsoft\\Azure\\AZCopy\\"
]
83 changes: 83 additions & 0 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,89 @@ def azcopy_installed?

end # AZcopy_installed

def delete_journal
current_user = `whoami`
current_user = (current_user.split('\\')[1]).strip

# This actually deletes the AZCopy folder as well, which is a bummer
directory "Delete Journal Entries" do
path "#{node['azcopy']['journal_path'][0]}#{current_user}#{node['azcopy']['journal_path'][1]}"
recursive true
action :delete
not_if {Dir[("#{node['azcopy']['journal_path'][0]}#{current_user}#{node['azcopy']['journal_path'][1]}*").gsub("\\", "/")].empty?}
end

# So in the event we delete it, lets recreate it (just in case)
directory "Recreate Journal Folder" do
path "#{node['azcopy']['journal_path'][0]}#{current_user}#{node['azcopy']['journal_path'][1]}"
action :create
end

end # Delete_journal

def download_file(key,blob,folder,file,destination,journal,guard_type)

if journal
delete_journal
end

directory "Validating Destination Directory" do
path destination
recursive true
action :create
end

file.each do |current_file|
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

# Download and Rename the files
powershell_script "Downloading: #{remote_name} From: #{blob}\\#{folder} to: #{destination} and renaming to: #{dest_name}" do
code <<-EOH
Out-File C:\\temp\\amit.txt
rename-item -path c:\\temp\\amit.txt -newname BUDHU.txt -force
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}") }
end

# "#{node['azcopy']['executable']}" /Source:#{blob}/#{folder} /Dest:#{destination} /SourceKey:"#{key}" /Pattern:"#{remote_name}"
# Rename-Item -path #{destination}\\#{remote_name} -newname #{dest_name} -force

# 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
end
end

end # current_file loop
else
# Download the file!
powershell_script "Downloading: #{current_file} From: #{blob}\\#{folder} to: #{destination}" do
code <<-EOH
"#{node['azcopy']['executable']}" /Source:#{blob}/#{folder} /Dest:#{destination} /SourceKey:"#{key}" /Pattern:"#{current_file}"
EOH
not_if {guard_type == :download_if_missing && File.exists?("#{destination}#{current_file}")}
end
end # If
end # File Loop
end # Download_file


end # Helpers
Expand Down
18 changes: 18 additions & 0 deletions libraries/provider_azcopy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,29 @@ def whyrun_supported?
action :download do
dotnet_installed?
azcopy_installed?
download_file(
new_resource.key,
new_resource.blob,
new_resource.folder,
new_resource.file,
new_resource.destination,
new_resource.ignore_journal,
:download
)
end

action :download_if_missing do
dotnet_installed?
azcopy_installed?
download_file(
new_resource.key,
new_resource.blob,
new_resource.folder,
new_resource.file,
new_resource.destination,
new_resource.ignore_journal,
:download_if_missing
)
end

end # AZCopy
Expand Down
2 changes: 1 addition & 1 deletion libraries/resource_azcopy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AZCopy < Chef::Resource::LWRPBase
self.resource_name = :azcopy

actions :download, :download_if_missing
default_action :download
default_action :download_if_missing

attribute :key, kind_of: String, default: nil
attribute :blob, kind_of: String, default: nil
Expand Down

0 comments on commit c2bb0c7

Please sign in to comment.