Class: Ec2UltraDNSUpdater::CLI
- Inherits:
-
Object
- Object
- Ec2UltraDNSUpdater::CLI
- Defined in:
- lib/ec2_ultradns_updater/cli.rb
Class Method Summary collapse
- .configure_logger(opts) ⇒ Object
- .load_config(opts, filename) ⇒ Object
- .parse(args) ⇒ Object
- .run ⇒ Object
- .symbolize_keys(hash) ⇒ Object
- .write_hostname(filename, hostname) ⇒ Object
Class Method Details
.configure_logger(opts) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ec2_ultradns_updater/cli.rb', line 58 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[:ultradns][:logger] = @logger if opts[:ultradns] opts end |
.load_config(opts, filename) ⇒ Object
52 53 54 55 56 |
# File 'lib/ec2_ultradns_updater/cli.rb', line 52 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
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 |
# File 'lib/ec2_ultradns_updater/cli.rb', line 72 def self.parse(args) = {} [:aws] = {} [:ultradns] = {} [:log_to] = nil [:verbose] = false [:out] = '' [:config] = nil opts = OptionParser.new do |opts| opts. = "Usage: ec2_ultradns_updater [options]" opts.separator "" opts.separator "Specific options:" opts.on("-c", "--config config.yaml", "Configuration for the updater") do |config| [:config] = config end opts.on("-o", "--out filename", "Write out the name assigned") do |out| [:out] = out end opts.separator "" opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| [:verbose] = v end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts.on_tail("--version", "Show version") do puts Ec2UltraDNSUpdater::VERSION exit end end opts.parse!(args) # load the config load_config(, [:config]) end |
.run ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ec2_ultradns_updater/cli.rb', line 22 def self.run opts = parse(ARGV) ec2 = Ec2UltraDNSUpdater::Ec2.new(opts[:aws]) ultradns = Ec2UltraDNSUpdater::UltraDNS.new(opts[:ultradns]) name_tag_value = ec2.get_name_tag result = false if ec2.get_instance_public_hostname != '' result = ultradns.create_or_update_cname(name_tag_value, ec2.get_instance_public_hostname) else # try using an ip that reaches the internet result = ultradns.create_or_update_a(name_tag_value, Ec2UltraDNSUpdater::IpInfo.local_ipv4) end if !result error_text = "Error Creating or Updating CNAME for #{ec2.get_instance_public_hostname} to #{name_tag_value}" @logger.error error_text ::Logger.new(STDERR).error error_text elsif !opts[:out].empty? write_hostname(opts[:out], name_tag_value) end end |
.symbolize_keys(hash) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ec2_ultradns_updater/cli.rb', line 118 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
46 47 48 49 50 |
# File 'lib/ec2_ultradns_updater/cli.rb', line 46 def self.write_hostname(filename, hostname) File.open(filename, "w+") do |f| f.write(hostname) end end |