Finished feature
This commit is contained in:
		
							
								
								
									
										97
									
								
								bucket-tool
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										97
									
								
								bucket-tool
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							@@ -6,6 +6,8 @@ require 'digest'
 | 
			
		||||
 | 
			
		||||
# END: Requires
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# BEGIN: Helper Functions
 | 
			
		||||
 | 
			
		||||
def usage
 | 
			
		||||
@@ -24,6 +26,7 @@ Actions:
 | 
			
		||||
  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 <value>           : Restore previous state of file stored in bucket. Value can be hash or filename/filepath
 | 
			
		||||
  backup <file>             : Backup the file to the bucket
 | 
			
		||||
 | 
			
		||||
Global Flags:
 | 
			
		||||
  -d | --debug              : Set debug flag
 | 
			
		||||
@@ -180,6 +183,7 @@ end
 | 
			
		||||
 | 
			
		||||
$CONFIG = Hash.new
 | 
			
		||||
$CONFIG[:bucket_dir]=` #{puppet_exe} agent --configprint clientbucketdir `.strip()
 | 
			
		||||
$CONFIG[:puppet_version]=` #{puppet_exe} --version `
 | 
			
		||||
$CONFIG[:action]=""
 | 
			
		||||
$CONFIG[:search_term]=""
 | 
			
		||||
$CONFIG[:log_file]=""
 | 
			
		||||
@@ -187,7 +191,7 @@ $CONFIG[:info_format]="inline"
 | 
			
		||||
File.open('/etc/hostname') do |file|
 | 
			
		||||
  $HOSTNAME=file.read().strip()
 | 
			
		||||
end
 | 
			
		||||
FLAG_REGEX=/\-+\S+/
 | 
			
		||||
FLAG_REGEX=/^\-+\S+/
 | 
			
		||||
 | 
			
		||||
# END: Variables
 | 
			
		||||
 | 
			
		||||
@@ -207,7 +211,7 @@ if not (ARGV & ['-d', '--debug']).empty?
 | 
			
		||||
end
 | 
			
		||||
i=0
 | 
			
		||||
case ARGV[i] 
 | 
			
		||||
  when 'search', 'get', 'restore'
 | 
			
		||||
  when 'search', 'get', 'restore', 'backup'
 | 
			
		||||
    $CONFIG[:action]=ARGV[i]
 | 
			
		||||
    log "$CONFIG[:action] was set to #{ARGV[i]}"
 | 
			
		||||
    log "user provided search action ARGV[i.next] == #{ARGV[i.next]}"
 | 
			
		||||
@@ -265,6 +269,11 @@ case $CONFIG[:action]
 | 
			
		||||
      exit
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
  when 'backup'
 | 
			
		||||
    if not File.exist? $CONFIG[:search_term]
 | 
			
		||||
      puts "File #{$CONFIG[:search_term]} does not exist. Please check to make sure that there are not typos and attempt the run again."
 | 
			
		||||
      exit
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
## END: Checks
 | 
			
		||||
@@ -470,6 +479,88 @@ def restore_entry (bucket)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
def backup_file (bucket)
 | 
			
		||||
  hash=""
 | 
			
		||||
  if  Gem::Version.new($CONFIG[:puppet_version]) >= Gem::Version.new("7.0.0")
 | 
			
		||||
      log "Puppet Version is +7.* so hashing algo will be SHA256"
 | 
			
		||||
      File.open($CONFIG[:search_term],'r') do |file|
 | 
			
		||||
        hash = Digest::SHA2.hexdigest file.read()
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
  else
 | 
			
		||||
      log "Puppet Version is not 7.* so hashing algo will be MD5"
 | 
			
		||||
      File.open($CONFIG[:search_term],'r') do |file|
 | 
			
		||||
        hash = Digest::MD5.hexdigest file.read()
 | 
			
		||||
      end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  log "Hash for #{$CONFIG[:search_term]} was generated to be #{hash}"
 | 
			
		||||
 | 
			
		||||
  if bucket.entries.has_key? hash
 | 
			
		||||
    log "Hash was found to already be backed up to bucket"
 | 
			
		||||
    puts "This file (hash: #{hash} has already been backed up in the bucket"
 | 
			
		||||
    puts bucket.entries[hash].info
 | 
			
		||||
    exit
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  puppet_uid=0
 | 
			
		||||
  puppet_gid=0
 | 
			
		||||
  File.open('/etc/passwd','r') do |file|
 | 
			
		||||
    log "Getting puppet UID & GID from /etc/passwd"
 | 
			
		||||
    passd = file.read.split(/\n/).select{|line| line =~ /puppet.+/}.first
 | 
			
		||||
    puppet_uid=Integer(passd.split(':')[2])
 | 
			
		||||
    log "Retrieved Puppet UID as #{puppet_uid}"
 | 
			
		||||
    puppet_gid=Integer(passd.split(':')[3])
 | 
			
		||||
    log "Retrieved Puppet GID as #{puppet_gid}"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  log "Created preceeding directorys to hash directory"
 | 
			
		||||
  dir=$CONFIG[:bucket_dir]
 | 
			
		||||
  hash.chars[0,8].map{|char|
 | 
			
		||||
    dir+="/#{char}"
 | 
			
		||||
    log "Checking to make sure that #{dir} doesn't exist"
 | 
			
		||||
    if not Dir.exist? dir
 | 
			
		||||
      log "#{dir} didn't exist so creating the directory & changing the UID to #{puppet_uid} & GID #{puppet_gid}"
 | 
			
		||||
      Dir.mkdir dir
 | 
			
		||||
      File.chown(puppet_gid, puppet_uid, dir)
 | 
			
		||||
    end
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  entry_dir="#{dir}/#{hash}"
 | 
			
		||||
  if not Dir.exist? entry_dir
 | 
			
		||||
    log "#{entry_dir} didn't exist so creating the directory & changing the UID to #{puppet_uid} & GID #{puppet_gid}"
 | 
			
		||||
    Dir.mkdir  entry_dir
 | 
			
		||||
    File.chown(puppet_gid, puppet_uid, entry_dir)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  contents_path="#{entry_dir}/contents"
 | 
			
		||||
  log "#{contents_path} will be created"
 | 
			
		||||
  paths_path="#{entry_dir}/paths"
 | 
			
		||||
  log "#{paths_path} will be created"
 | 
			
		||||
 | 
			
		||||
  log "Creating #{contents_path}"
 | 
			
		||||
  File.open(contents_path, 'w') do |contents_file|
 | 
			
		||||
    log "Opened #{contents_path} to be written to"
 | 
			
		||||
    File.open($CONFIG[:search_term], 'r') do |source_file|
 | 
			
		||||
      log "Opened #{$CONFIG[:search_term]} to be read from"
 | 
			
		||||
      contents_file.write(source_file.read)
 | 
			
		||||
      contents_file.chown(puppet_gid, puppet_uid)
 | 
			
		||||
      log "#{contents_path} was created & was chowned to UID to #{puppet_uid} & GID #{puppet_gid}"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  log "Creating #{paths_path}"
 | 
			
		||||
  File.open(paths_path, 'w') do |paths_file|
 | 
			
		||||
    log "Opened #{paths_path} to be written to"
 | 
			
		||||
    paths_file.write($CONFIG[:search_term])
 | 
			
		||||
    log "Just wrote #{$CONFIG[:search_term]} to #{paths_path}"
 | 
			
		||||
    paths_file.chown(puppet_gid, puppet_uid)
 | 
			
		||||
    log "#{paths_path} was created & was chowned to UID to #{puppet_uid} & GID #{puppet_gid}"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  puts "File #{$CONFIG[:search_term]} was backed up to #{entry_dir}"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
# END: Work Functions
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -494,6 +585,8 @@ if __FILE__ == $0
 | 
			
		||||
    when 'restore'
 | 
			
		||||
      restore_entry bucket
 | 
			
		||||
 | 
			
		||||
    when 'backup'
 | 
			
		||||
      backup_file bucket
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user