Class: Cleanfb::Clean

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

Instance Method Summary collapse

Instance Method Details

#backup(cmd) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cleanfb.rb', line 69

def backup cmd 
      
  cmd.each_line do |line|
    path = "/opt/puppetlabs/puppet/cache/bucket"
    sum = line.split(" ")[0]
      
    start = (sum.scan /\w/).join("/")
        
    file = line.split(" ")[3].split("/").last
    name = file.split("_")[0]
    date = line.split(" ")[1]
    time = line.split(" ")[2]
        
    save_dir = "/root/saved_configs/"
      
    Dir.mkdir(File.join(Dir.home("root"), "saved_configs"), 0700) unless (Dir.exist? save_dir)
    Dir.mkdir(File.join("#{save_dir}", "#{name}"), 0700) unless (Dir.exist? "#{save_dir}/#{name}/")
        
    path += "/" + start[0..15] + sum + "/"
            
    if File.exist? "#{path}/contents"
      puts "Storing " + path
      puts "at /root/saved_configs/#{name}/#{date}_#{time}_#{file}"
      cmd = `mv -f #{path}contents /root/saved_configs/#{name}/#{date}_#{time}_#{file}`
      cmd = `rm -rf #{path}`
    else
      puts "No file #{@hostname} found."
    end
  end
end

#parse_inputObject



26
27
28
29
30
31
32
33
34
# File 'lib/cleanfb.rb', line 26

def parse_input
  OptionParser.new do |opts|
    opts.banner = "Usage: cleanfb [options]..."
    opts.on("-f", "--force", "ignore all prompts") { |f| @force = f }
    opts.on("-nNAME", "--name=NAME", "hostname to clean") { |n| @hostname = n }
    opts.on("-rRESTORE", "--restore=FILE", "file to restore") { |r| @restore = r } 
    opts.on_tail("-h", "--help", "display this message") { puts opts; exit }
  end.parse!
end

#removeObject



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
61
62
63
64
65
66
67
# File 'lib/cleanfb.rb', line 36

def remove

  if @hostname == ""
    puts "Hostname required. See cleanfb --help for usage."
    return
  end

  unless @force  
    print "Remove #{@hostname}? (y|n): "
    ans = STDIN.gets.chomp
  end

  if(@force || ans =~ /^y/) 
    cmd = `puppet filebucket list -l|grep -i #{@hostname}`
    
    unless @force 
      test = cmd.split(" ").last
      multi_match = false

      cmd.each_line { |line| (multi_match = true) if (!line.include? test) }
        
      if multi_match
        print "Matched multiple hosts. Remove all? (y|n): "
        ans = STDIN.gets.chomp
            
        return unless (@force || ans =~/^y/)
      end
    end     
    
    (backup cmd) unless (cmd.nil? || cmd.empty?)
  end
end

#restoreObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cleanfb.rb', line 100

def restore
 
  if @restore == ""
    puts "File to restore required. See cleanfb --help for usage."
    return
  end

  host = @restore.scan(/_\w+\.\w+\.?\w+?_/).join("").gsub!("_", "")
  cmd = `puppet filebucket list -l| grep -i #{host}`
  backup cmd
  
  if File.exist? @restore
    puts  "puppet filebucket -l backup #{@restore}"
    cmd = `puppet filebucket -l backup #{@restore}`
  else
    puts "No file #{@restore} found to restore."
  end
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cleanfb.rb', line 13

def run
  puts @hostname
  puts @restore
  
  if @hostname.nil? || @hostname.empty?
    restore
  elsif @restore.nil? || @restore.empty?
    remove
  else
    puts "Invalid input. See cleanfb --help for usage"
  end
end