Class: EbsSnapper::CLI

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

Class Method Summary collapse

Class Method Details

.configure_logger(opts) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ebs_snapper/cli.rb', line 39

def self.configure_logger(opts)
  if opts[:log_to]
    @logger = ::Logger.new(opts[:log_to])
  end
  @logger ||= ::Logger.new(STDOUT)

  @logger.level = opts[:verbose] ? Logger::DEBUG : Logger::INFO
  
  opts[:aws][:logger] = @logger if opts[:aws]
  
  opts
end

.load_config(opts, filename) ⇒ Object



33
34
35
36
37
# File 'lib/ebs_snapper/cli.rb', line 33

def self.load_config(opts, filename)
  config = symbolize_keys(YAML.load_file(filename))
  # merge the new file config & update the logger config
  configure_logger(opts.merge!(config))
end

.parse(args) ⇒ Object



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
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ebs_snapper/cli.rb', line 52

def self.parse(args)
  options = {}
  options[:aws] = {}
  options[:dry_run] = false
  options[:ultradns] = {}
  options[:log_to] = nil
  options[:verbose] = false
  options[:out] = ''
  options[:config] = nil
  
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: ebs_snapper [options]"

    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-c", "--config config.yaml",
            "Configuration for the updater") do |config|
      options[:config] = config
    end
          
    opts.separator ""

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options[:verbose] = v
    end

    opts.on("-d", "--dry-run", "Dry run, will not create or delete snapshots") do
      options[:dry_run] = true
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.on_tail("--version", "Show version") do
      puts EbsSnapper::VERSION
      exit
    end
  end

  opts.parse!(args)
  # load the config
  load_config(options, options[:config])
end

.runObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/ebs_snapper/cli.rb', line 22

def self.run  
  opts = parse(ARGV)
  if opts[:dry_run]
     @logger.info "Dry run mode enabled"
  end
  ebs = EbsSnapper::Ebs.new(opts[:aws], opts[:dry_run])
  ebs.snapshot_and_purge
rescue => e
  @logger.error "Exception: #{e}\n" + e.backtrace().join("\n")
end

.symbolize_keys(hash) ⇒ Object



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

def self.symbolize_keys(hash)
  unless hash.nil?
    hash.replace(
      hash.each_key.inject({}) do |h, k|
        v = hash.delete(k)
        key = k.to_sym rescue k
        if v.is_a? (Hash)
          h[key] = symbolize_keys(v)
        else
          h[key] = v
        end
        h
      end
    )
  end
  hash
end