Class: UltraDNSUpdater::CLI

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

Class Method Summary collapse

Class Method Details

.configure_logger(opts) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ultradns_updater/cli.rb', line 55

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[:ultradns][:logger] = @logger if opts[:ultradns]
  
  opts
end

.load_config(opts, filename) ⇒ Object



49
50
51
52
53
# File 'lib/ultradns_updater/cli.rb', line 49

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



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ultradns_updater/cli.rb', line 68

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

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

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

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options[:verbose] = v
    end
    
    opts.on("-s", "--strategy name", "Choose strategy") do |s|
      options[:force_strategy] = s.to_sym
    end

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

    opts.on_tail("--version", "Show version") do
      puts UltraDNSUpdater::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
32
33
34
35
36
37
38
39
40
41
# File 'lib/ultradns_updater/cli.rb', line 22

def self.run  
  opts = parse(ARGV)
  
  if opts[:force_strategy]
    opts[:update_strategy][:use] = opts[:force_strategy]
  end

  ultradns = UltraDNSUpdater::UltraDNS.new(opts[:ultradns])    
  strategy_klass = UltraDNSUpdater::Strategies.strategy(opts[:update_strategy][:use])
  hostname = strategy_klass.new(ultradns, opts[:update_strategy], @logger).update()
  
  if hostname.nil?
    error_text = "Error Creating or Updating CNAME for hostname: #{hostname} using: #{opts[:update_strategy][:use]}"
    error_text += " check log file #{opts[:log_to]}" if opts[:log_to]
    @logger.error error_text
    ::Logger.new(STDERR).error error_text
  elsif !opts[:out].empty?
    write_hostname(opts[:out], hostname)
  end
end

.symbolize_keys(hash) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ultradns_updater/cli.rb', line 121

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

.write_hostname(filename, hostname) ⇒ Object



43
44
45
46
47
# File 'lib/ultradns_updater/cli.rb', line 43

def self.write_hostname(filename, hostname)
  File.open(filename, "w+") do |f|
    f.write(hostname)
  end
end