puppet4-clientbucket-restore/clientbucket.rb

148 lines
3.5 KiB
Ruby
Raw Normal View History

#!/usr/bin/env ruby
2013-01-04 16:43:10 +01:00
require 'rubygems'
require 'find'
2016-05-21 04:46:38 +02:00
require 'optparse'
ARGV << '-h' if ARGV.empty?
2016-05-21 04:46:38 +02:00
options = {:target_path => nil, :clientbucket_path => nil}
OptionParser.new do |opts|
2016-05-21 04:46:38 +02:00
opts.banner = "Usage: clientbucket.rb -t target_path -c clientbucket_path"
opts.on("-t", "--target_path target_path", "path of the file to restore") do |t|
2016-05-21 04:46:38 +02:00
options[:target_path] = t
end
opts.on("-c", "--clientbucket_path clientbucket_path", "where to restore from. Defaults to /var/lib/puppet/clientbucket") do |c|
2016-05-21 04:46:38 +02:00
options[:clientbucket_path] = c
end
2013-01-04 16:43:10 +01:00
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
2013-01-04 16:43:10 +01:00
2016-05-21 04:46:38 +02:00
if options[:clientbucket_path] == nil
# We are keeping it on old path, as there are installations that have not moved on
# to Puppet 4
options[:clientbucket_path] = "/var/lib/puppet/clientbucket"
2016-05-21 04:46:38 +02:00
end
2013-01-04 16:43:10 +01:00
available_files = Array.new
2016-05-21 03:53:58 +02:00
begin
Find.find(options[:clientbucket_path]) do |file_path|
2013-01-04 16:43:10 +01:00
2016-05-21 03:53:58 +02:00
# Skip directories and "contents" files
next if FileTest.directory?(file_path)
next unless File.basename(file_path) =~ /paths$/
2013-01-04 16:43:10 +01:00
2016-05-21 03:53:58 +02:00
# See if this file has a path the user was looking for
path = File.open(file_path).first
path.chomp!
if options[:target_path] == path
2013-01-04 16:43:10 +01:00
2016-05-21 03:53:58 +02:00
# Get the md5 string the file is referred to by
file_path =~ /([^\/]+)\/paths$/;
file_hash = $1
2013-01-04 16:43:10 +01:00
2016-05-21 03:53:58 +02:00
# Get the "contents" file containing the old file's contents
contents_path = File.dirname(file_path) + "/contents"
2013-01-04 16:43:10 +01:00
2016-05-21 03:53:58 +02:00
# Get the creation time
ctime = File.stat(file_path).ctime
2013-01-04 16:43:10 +01:00
2016-05-21 03:53:58 +02:00
details = {:hash => file_hash, :ctime => ctime, :contents_path => contents_path}
available_files.push(details)
2013-01-04 16:43:10 +01:00
2016-05-21 03:53:58 +02:00
end
2013-01-04 16:43:10 +01:00
2016-05-21 03:53:58 +02:00
end
rescue
puts "Unable to open file path #{options[:clientbucket_path]}"
2013-01-04 16:43:10 +01:00
end
# See if we found any files for the user
if available_files.length == 0
puts "No files with path #{options[:target_path]} exist in the clientbucket"
2013-01-04 16:43:10 +01:00
exit 2
end
while true
# Sort by creation time
available_files.sort! { |x, y| x[:ctime] <=> y[:ctime] }
n = 0
available_files.each {|file|
puts "[#{n}]: #{file[:hash]} #{file[:ctime]}"
# Stash the option number for this file
available_files[n][:option_number] = n
n+=1
}
puts "------------------------"
print "Pick a file, or x to exit: "
number = gets.strip
number.chomp!
if number !~ /^(x|\d+)$/
puts "Invalid input"
next
end
if number == "x"
puts "Exiting"
exit 0
end
number = number.to_i
if number > n
puts "Invalid file number"
next
end
while true
print "Restore (r), view (v), diff (d), unified diff (u), or x to go back: "
choice = gets.strip
choice.chomp!
if choice !~ /^[rvdux]$/
puts "Invalid choice"
next
end
case choice
when "x"
break
when "v"
system("vim #{available_files[number][:contents_path]}")
when "d"
system("diff #{available_files[number][:contents_path]} #{options[:target_path]}")
2013-01-04 16:43:10 +01:00
when "u"
system("diff -u #{available_files[number][:contents_path]} #{options[:target_path]}")
2013-01-04 16:43:10 +01:00
when "r"
print "Restore to (default is to restore to #{options[:target_path]}): "
2013-01-04 16:43:10 +01:00
choice = gets.strip
choice.chomp!
# If not specified, use the default
restore_path = options[:target_path]
2013-01-04 16:43:10 +01:00
if choice != ''
restore_path = choice
end
puts "Restoring to #{restore_path}"
system("cp -p #{available_files[number][:contents_path]} #{restore_path}")
puts "Done"
exit 0
end
end
end