Skip to content

Instantly share code, notes, and snippets.

@rydurham
Created August 11, 2019 20:24
Show Gist options
  • Save rydurham/3e2fd7fa7a1b7a1bbfbb2a1e0c1a52e4 to your computer and use it in GitHub Desktop.
Save rydurham/3e2fd7fa7a1b7a1bbfbb2a1e0c1a52e4 to your computer and use it in GitHub Desktop.
Rake with AWS Glacier
source 'https://rubygems.org'
gem 'aws-sdk', '3.0.1'
gem 'irb', '1.0.0'
gem 'rake', '12.3.1'
gem 'tilt', '2.0.8'
require 'aws-sdk-glacier'
require 'json'
desc 'list glacier vaults'
task :list do
glacier = Aws::Glacier::Client.new
vault_list = glacier.list_vaults().vault_list;
puts "Found " + vault_list.length.to_s + " vault(s)."
glacier.list_vaults().vault_list.each do |vault|
puts vault.vault_name + ": " + vault.number_of_archives.to_s + " archives."
end
end
desc "list vault jobs"
task :jobs, [:vault] do |t, args|
glacier = Aws::Glacier::Client.new
vault_name = args.vault
jobs = glacier.list_jobs({
vault_name: vault_name
}).job_list
puts "Found " + jobs.length.to_s + " jobs"
jobs.each do |job|
puts "ID: " + job.job_id
puts "Description: " + job.job_description
puts "Status: " + job.status_code
puts "Started At: " + job.creation_date.to_s
puts "Completed At: " + (job.completion_date.to_s || "N/A")
puts ""
# puts job
end
end
desc "initiate an inventory retrieval job"
task :request_inventory, [:vault] do |t, args|
glacier = Aws::Glacier::Client.new
resp = glacier.initiate_job({
job_parameters: {
sns_topic: "arn:aws:sns:us-west-2:506051929789:glacier_notifications",
description: "Get Inventory - Attempt #3",
type: "inventory-retrieval",
},
vault_name: args.vault
})
puts resp
end
desc "fetch job output"
task :fetch_output, [:vault, :job_id] do |t, args|
glacier = Aws::Glacier::Client.new
resp = glacier.get_job_output({
account_id: "-",
job_id: args.job_id,
range: "",
vault_name: args.vault,
})
File.open(args.job_id, 'w') do |f|
f.puts(resp.body.read)
end
puts "success"
end
desc "delete archives"
task :delete_archives, [:vault, :job_id] do |t, args|
glacier = Aws::Glacier::Client.new
file = File.open args.job_id
data = JSON.load file
archives = data["ArchiveList"]
puts "Vault ARN: " + data["VaultARN"]
puts "Inventory Date: " + data["InventoryDate"]
puts "Archive Count: " + archives.length.to_s
puts "Deleting Archives...."
puts ""
i = 0;
archives.each do |archive|
glacier.delete_archive({
archive_id: archive["ArchiveId"],
vault_name: args.vault
})
i = i + 1
puts i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment