Class: Cleanfb::Clean

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/cleanfb.rb', line 9

def options
  @options
end

Instance Method Details

#parse_inputObject



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

def parse_input
  OptionParser.new do |opts|
    opts.banner = ""
    opts.on("-h", "--help", "Display help information") {show_help}
    opts.on("-f", "--force", "Ignore all prompts") { |f| @options[:force] = f }
    opts.on("-nNAME", "--name=NAME", "Hostname") { |n| @options[:hostname] = n }
  end.parse!
end

#removeObject



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cleanfb.rb', line 25

def remove

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

  if(@options[:force] || ans =~ /^y/) 
    cmd = `puppet filebucket list -l|grep -i #{@options[:hostname]}`
      
    unless @options[: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 "Ending run"	unless (@options[:force] || ans =~/^y/)
      end
    end			
    

    unless cmd.nil? || cmd.empty?
      cmd.each_line do |line|
        path = "/opt/puppetlabs/puppet/cache/bucket"
        sum = line.split(" ")[0]
      
        start = (sum.scan /\w/).join("/")
            
        name = file.split("_")[0]
        date = line.split(" ")[1]
        time = line.split(" ")[2]
        file = line.split(" ")[3].split("/").last
            
        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 #{@options[:hostname]} found."
        end
      end
    end
  
  else
    puts "No file #{@options[:hostname]} found."
  end
  
  return 
end

#restoreObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cleanfb.rb', line 125

def restore

  return "Please supply a file" if (ARGV[0].nil? || ARGV[0].empty?)
    
  arg = ARGV[1]	

  host = arg.scan(/_\w+\.\w+\.?\w+?_/).join("").gsub!("_", "")
  store(host)
  file = arg
  
  if File.exist? file
    puts  "puppet filebucket -l backup #{file}"
    cmd = `puppet filebucket -l backup #{file}`
  else
    puts "No file #{arg} found."
  end
 
  return
end

#show_helpObject



11
12
13
14
# File 'lib/cleanfb.rb', line 11

def show_help
  puts "HELP"
  exit 0
end

#store(host) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cleanfb.rb', line 88

def store host

  cmd = `puppet filebucket list -l|grep -i #{host}`
  unless(cmd.nil? || cmd.empty?)
    cmd.each_line do |line|
      path = "/opt/puppetlabs/puppet/cache/bucket"
      sum = line.split(" ")[0]
      start = (sum.scan /\w/).join("/")
                
      name = file.split("_")[0]
      date = line.split(" ")[1]
      time = line.split(" ")[2]
      file = line.split(" ")[3].split("/").last
                
      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 #{host} found."
      end
    end
  else
    puts "No file #{host} found."
  end
    
  return 
end