Class: Cleanfb::Clean

Inherits:
Object
  • Object
show all
Defined in:
lib/cleanfb.rb

Instance Method Summary collapse

Instance Method Details

#remove ⇒ Object

Method to remove ALL files backed up on the Puppet masters filebucket from a specified agent.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cleanfb.rb', line 11

def remove()

  # return if no args
  return "Please supply a file" if ARGV[0].nil? || ARGV[0].empty?
      
  # return the help output if requested
  if ((ARGV.include? "-h") or (ARGV.include? "--help"))
    return "Remove backups of an agent from the Puppet master's filebucket.\n\n/
    Usage:   cleanfb <client>\n\n   options: -h or --help    | help and information\n/
    -y            | defaults all input to yes\n"

  end

  arg = ARGV[0]  

  # set ans to yes automatically if -y option
  if !ARGV.nil? and (ARGV.include? "-y") and ARGV.length > 1
       ans = "y"
    arg = ARGV[1] if (ARGV.index("-y") == 0)

  # else prompt for answer
  else
      print "Remove #{arg} ? y|n: "
      ans = STDIN.gets.chomp
  end

  # if yes, retrieve the files and remove them
    if ans == "y" || ans == "yes"
      cmd = `puppet filebucket list -l|grep -i #{arg}`
    unless cmd.nil? || cmd.empty?
       cmd.each_line do |line|
        os = `facter os`
        if(os != 'windows')
          path = "/opt/puppetlabs/puppet/cache/bucket"
        else
          path = "C:/ProgramData/PuppetLabs/puppet/cache/bucket"
        end
          sum = line.split(" ")[0]
        start = (sum.scan /\w/).join("/")

           path += "/" + start[0..15] + sum + "/"
        puts "Removing " + path
        Dir.foreach(path) { |file| File.delete(file) }
      end
     else
      puts "No file #{arg} found."
    end
  end
  return 
end