2024-05-17 20:33:48 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Helper Functions
|
|
|
|
|
|
|
|
def usage
|
|
|
|
puts "#{__FILE__} [ACTION <arg>] [<flags>]
|
|
|
|
|
2024-05-18 18:12:27 +00:00
|
|
|
Description:
|
|
|
|
This utlity is meant to be used to interact with & manage the filebucket on P4 nodes due to the
|
|
|
|
utility for that `puppet filebucket -l <action>` being nonfunctional.
|
|
|
|
|
|
|
|
This implements the same functionality (minus the puppet tie-in) and will allow the user to
|
|
|
|
search the filebucket and restore from it.
|
|
|
|
|
2024-05-17 20:33:48 +00:00
|
|
|
Actions:
|
2024-05-24 13:54:04 +00:00
|
|
|
search <term> : Search for bucket entries matching a portion of the filepath
|
|
|
|
list : List all Bucket entries
|
|
|
|
list-files : List all files/paths that have been backed up to the bucket
|
|
|
|
get <entry-hash> : Get the content of a specific entry (by hash)
|
|
|
|
restore-hash <entry-hash> : Restore previous state of file stored in bucket (by-hash)
|
|
|
|
restore-file <filepath> : Restore previous state of file stored in bucket (by-filepath/filename)
|
2024-05-17 20:33:48 +00:00
|
|
|
|
|
|
|
Global Flags:
|
2024-05-24 13:54:04 +00:00
|
|
|
-d | --debug : Set debug flag
|
|
|
|
-h | --help : This help message
|
2024-05-18 18:12:27 +00:00
|
|
|
|
|
|
|
Author:
|
|
|
|
Name: Tristan Ancelet
|
|
|
|
Email: tristan.ancelet@acumera.com
|
|
|
|
Phone (Work) #: +1 (337) 965-1855
|
|
|
|
|
2024-05-17 20:33:48 +00:00
|
|
|
"
|
|
|
|
end
|
|
|
|
|
|
|
|
def log (message, level = 0)
|
|
|
|
if $DEBUG == true or $CONFIG[:log_file] != ""
|
|
|
|
if message == ""
|
|
|
|
puts "log was called without providing a message"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
case level
|
|
|
|
when 0
|
|
|
|
level="INFO"
|
|
|
|
when 1
|
|
|
|
level="WARN"
|
|
|
|
when 2
|
|
|
|
level="CRIT"
|
|
|
|
else
|
|
|
|
level="UNDF"
|
|
|
|
end
|
|
|
|
datestamp=Time.now
|
|
|
|
log_message="#{datestamp} : #{$HOSTNAME} : #{level} : #{caller[0]} : #{message}"
|
|
|
|
|
|
|
|
if $CONFIG[:log_file] != ""
|
|
|
|
File.open($CONFIG[:log_file],'a') do |file|
|
|
|
|
file.write("#{log_message}\n")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
puts log_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-18 23:40:58 +00:00
|
|
|
def get_verification (prompt = "Do you want to continue?")
|
|
|
|
while true
|
|
|
|
puts "#{prompt} (y/n): "
|
|
|
|
answer = STDIN.gets
|
|
|
|
answer = answer.strip()
|
|
|
|
case answer
|
|
|
|
when 'y','yes'
|
|
|
|
return true
|
|
|
|
when 'n', 'no'
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
puts "#{answer} is not an acceptible answer"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-17 20:33:48 +00:00
|
|
|
# END: Helper Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-18 18:12:27 +00:00
|
|
|
# BEGIN: Variables
|
|
|
|
|
|
|
|
if not (ARGV & ['-h', '--help']).empty?
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
if ENV["USER"] != 'root'
|
|
|
|
puts "This script should only be run by root (permissions issues). Please rerun it as root or prepend \"sudo\""
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
$DEBUG=false
|
|
|
|
|
|
|
|
$CONFIG = Hash.new
|
2024-05-18 23:40:58 +00:00
|
|
|
$CONFIG[:bucket_dir]=` puppet agent --configprint clientbucketdir `.strip()
|
2024-05-18 18:12:27 +00:00
|
|
|
$CONFIG[:action]=""
|
|
|
|
$CONFIG[:search_term]=""
|
|
|
|
$CONFIG[:log_file]=""
|
|
|
|
File.open('/etc/hostname') do |file|
|
|
|
|
$HOSTNAME=file.read().strip()
|
|
|
|
end
|
|
|
|
FLAG_REGEX=/\-+\S+/
|
|
|
|
|
|
|
|
# END: Variables
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-17 20:33:48 +00:00
|
|
|
# BEGIN: Handle CLI Args
|
|
|
|
|
2024-05-18 18:12:27 +00:00
|
|
|
|
2024-05-17 20:33:48 +00:00
|
|
|
if ARGV.count == 0
|
|
|
|
puts "No arguments were provided"
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
if not (ARGV & ['-d', '--debug']).empty?
|
|
|
|
$DEBUG=true
|
|
|
|
end
|
|
|
|
i=0
|
|
|
|
case ARGV[i]
|
2024-05-18 23:40:58 +00:00
|
|
|
when 'search', 'get', 'restore'
|
|
|
|
$CONFIG[:action]=ARGV[i]
|
2024-05-17 20:33:48 +00:00
|
|
|
log "$CONFIG[:action] was set to #{ARGV[i]}"
|
|
|
|
log "user provided search action ARGV[i.next] == #{ARGV[i.next]}"
|
|
|
|
if ARGV[i.next] != "" and not ARGV[i.next] =~ FLAG_REGEX
|
|
|
|
$CONFIG[:search_term]=ARGV[i.next]
|
|
|
|
log "search_term was set to #{ARGV[i.next]}"
|
|
|
|
i+=2
|
|
|
|
else
|
|
|
|
puts "Flag[#{ARGV[i]}] : Argument[#{ARGV[i.next]}] : Either the argument was not provided or it was a flag"
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2024-05-18 23:40:58 +00:00
|
|
|
when 'list', 'list-files'
|
|
|
|
$CONFIG[:action] = ARGV[i]
|
2024-05-17 20:33:48 +00:00
|
|
|
log "$CONFIG[:action] was set to #{ARGV[i]}"
|
|
|
|
i+=1
|
|
|
|
|
|
|
|
else
|
|
|
|
puts "#{ARGV[i]} is not a valid action. Please make sure you use a valid action as the first argument of the script"
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
## BEGIN: Checks
|
|
|
|
|
|
|
|
if $CONFIG[:action] == ""
|
|
|
|
puts "Action was not provided"
|
|
|
|
end
|
|
|
|
|
|
|
|
case $CONFIG[:action]
|
|
|
|
when 'search', 'get'
|
|
|
|
if $CONFIG[:search_term] == ""
|
|
|
|
puts "Search Term was not provided"
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
## END: Checks
|
|
|
|
|
|
|
|
# END: Handle CLI Args
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Classes
|
|
|
|
|
|
|
|
class BucketEntry
|
2024-05-18 23:07:50 +00:00
|
|
|
attr_reader :hash, :filepaths, :mtime
|
2024-05-17 20:33:48 +00:00
|
|
|
def initialize (entry_dir)
|
|
|
|
@entry_dir = entry_dir
|
2024-05-18 23:07:50 +00:00
|
|
|
@hash = File.basename(entry_dir)
|
2024-05-17 20:33:48 +00:00
|
|
|
@filepaths = Array.new
|
|
|
|
File.open("#{entry_dir}/paths") do |file|
|
|
|
|
file.read().split(/\n/).each do |path|
|
2024-05-18 23:07:50 +00:00
|
|
|
log "BucketEntry[#{@hash}] adding #{path} to @filepaths"
|
2024-05-17 20:33:48 +00:00
|
|
|
@filepaths.push(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@mtime = File.mtime(entry_dir)
|
|
|
|
log "BucketEntry was created from #{entry_dir}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def path_include? (path_string)
|
2024-05-18 23:07:50 +00:00
|
|
|
log "BucketEntry[#{hash}] was called with #{path_string}"
|
2024-05-17 20:33:48 +00:00
|
|
|
@filepaths.each.any? {|path| path.include? path_string}
|
|
|
|
end
|
|
|
|
|
|
|
|
def infostring
|
2024-05-18 23:07:50 +00:00
|
|
|
"Entry [#{@hash}]:
|
2024-05-17 20:33:48 +00:00
|
|
|
Paths: #{@filepaths.join(',')}
|
|
|
|
MTIME: #{@mtime}
|
|
|
|
|
|
|
|
"
|
|
|
|
end
|
|
|
|
|
|
|
|
def inline_info
|
2024-05-18 23:07:50 +00:00
|
|
|
"#{@mtime} : #{@hash} : #{@filepaths.join(',')}"
|
2024-05-17 20:33:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
2024-05-18 23:07:50 +00:00
|
|
|
log "BucketEntry[#{@hash}] getting contents"
|
2024-05-17 20:33:48 +00:00
|
|
|
File.open("#{@entry_dir}/contents",'r') do |file|
|
|
|
|
file.read()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Bucket
|
|
|
|
attr_reader :bucketdir, :entries
|
|
|
|
def initialize (clientbucketdir)
|
|
|
|
log "Bucket is being created from #{clientbucketdir}"
|
|
|
|
@bucketdir = clientbucketdir
|
2024-05-18 23:17:03 +00:00
|
|
|
@entries = Hash.new
|
2024-05-17 20:33:48 +00:00
|
|
|
load_bucket
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_bucket
|
|
|
|
log "Bucket[#{@bucketdir}] is loading entries"
|
|
|
|
Dir["#{@bucketdir}/**/paths"].each.map{|path| File.dirname(path)}.each do |directory|
|
|
|
|
log "\"#{directory}\" was grabbed from bucket directory. Making new BucketEntry"
|
|
|
|
entry = BucketEntry.new(directory)
|
2024-05-18 23:17:03 +00:00
|
|
|
@entries[entry.hash]=entry
|
2024-05-18 23:07:50 +00:00
|
|
|
log "BucketEntry[#{entry.hash}] was added to @entries Size=#{@entries.count()}"
|
2024-05-17 20:33:48 +00:00
|
|
|
end
|
|
|
|
log "Bucket[#{@bucketdir}] was loaded"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# END: Classes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Work Functions
|
|
|
|
|
|
|
|
def search_entries_paths (bucket)
|
|
|
|
log "user entered"
|
2024-05-24 13:54:04 +00:00
|
|
|
puts bucket.entries.each_value.select {|entry| entry.path_include? $CONFIG[:search_term]}.map{|entry| entry.inline_info}.sort.join("\n")
|
2024-05-17 20:33:48 +00:00
|
|
|
end
|
|
|
|
|
2024-05-18 23:07:50 +00:00
|
|
|
def get_content_of_entry_hash (bucket)
|
2024-05-17 20:33:48 +00:00
|
|
|
log "user entered"
|
2024-05-18 23:17:03 +00:00
|
|
|
if bucket.entries.has_key? $CONFIG[:search_term]
|
|
|
|
puts bucket.entries[$CONFIG[:search_term]].content
|
|
|
|
else
|
|
|
|
puts "There were no entries corresponding to #{$CONFIG[:search_term]}"
|
|
|
|
exit
|
2024-05-17 20:33:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_all_entries (bucket)
|
2024-05-18 23:17:03 +00:00
|
|
|
puts bucket.entries.each_value.each.map{|entry| entry.inline_info}.sort.join("\n")
|
2024-05-17 20:33:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_entry_files (bucket)
|
|
|
|
filenames = Array.new
|
2024-05-18 23:17:03 +00:00
|
|
|
bucket.entries.each_value do |entry|
|
2024-05-17 20:33:48 +00:00
|
|
|
entry.filepaths.each do |path|
|
|
|
|
if not filenames.include? path
|
|
|
|
filenames.push(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
puts filenames.sort.join("\n")
|
|
|
|
end
|
|
|
|
|
2024-05-18 23:40:58 +00:00
|
|
|
def restore_entry (bucket)
|
|
|
|
if bucket.entries.has_key? $CONFIG[:search_term]
|
|
|
|
entry = bucket.entries[$CONFIG[:search_term]]
|
|
|
|
if entry.filepaths.count == 1
|
|
|
|
filepath = entry.filepaths[0]
|
|
|
|
if filepath[0] != '/'
|
|
|
|
filepath = "/#{filepath}"
|
|
|
|
end
|
|
|
|
if get_verification "Are you sure you want to overwrite #{filepath}?"
|
|
|
|
File.open(filepath,'w') do |file|
|
|
|
|
file.write(entry.content)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "Ok not overwriting."
|
|
|
|
end
|
|
|
|
else
|
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "There were no entries corresponding to #{$CONFIG[:search_term]}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-17 20:33:48 +00:00
|
|
|
# END: Work Functions
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Work
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
bucket = Bucket.new($CONFIG[:bucket_dir])
|
|
|
|
|
|
|
|
case $CONFIG[:action]
|
|
|
|
when 'search'
|
|
|
|
search_entries_paths bucket
|
|
|
|
|
|
|
|
when 'get'
|
2024-05-18 23:07:50 +00:00
|
|
|
get_content_of_entry_hash bucket
|
2024-05-17 20:33:48 +00:00
|
|
|
|
|
|
|
when 'list'
|
|
|
|
list_all_entries bucket
|
|
|
|
|
|
|
|
when 'list-files'
|
|
|
|
list_entry_files bucket
|
|
|
|
|
2024-05-18 23:40:58 +00:00
|
|
|
when 'restore'
|
|
|
|
restore_entry bucket
|
|
|
|
|
2024-05-17 20:33:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
# END: Work
|
|
|
|
|
|
|
|
|