dnsomatic 0.1.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,51 +3,53 @@
3
3
  begin
4
4
  require 'dnsomatic'
5
5
  rescue LoadError => e
6
- # a small hack in the case where we aren't a gem
6
+ # a small hack in the case we aren't a gem
7
7
  $: << File.join(File.dirname($0), '..', 'lib')
8
8
  retry
9
9
  end
10
10
 
11
11
  begin
12
- $opts = DNSOMatic::Opts.instance
13
- $opts.parse(ARGV)
12
+ opts = DNSOMatic::Opts.instance
13
+ opts.parse(ARGV)
14
14
 
15
- c = DNSOMatic::Config.new($opts.cf)
15
+ c = DNSOMatic::Config.new(opts.cf)
16
16
 
17
- if $opts.showcf
18
- $stdout.puts "This is your configuration after merging in the defaults."
19
- $stdout.puts "It has been pruned to only display the stanza named: #{$opts.name}" if $opts.name
20
- $stdout.puts c.merged_config($opts.name)
17
+ if opts.showcf
18
+ msg = "This is your configuration after merging in the defaults.\n"
19
+ msg += "It has been pruned to only display the stanza named: #{opts.name}" if opts.name
20
+ msg += c.merged_config(opts.name)
21
+ DNSOMatic::Logger.log(msg)
21
22
  exit
22
23
  end
23
24
 
24
- if $opts.stanza and $opts.verbose
25
- $stdout.puts "Will only update #{$opts.name}."
26
- end
25
+ DNSOMatic::Logger.log("Will only update #{opts.name}.") if opts.stanza
27
26
 
28
- updaters = c.updaters($opts.name)
27
+ updaters = c.updaters(opts.name)
29
28
 
30
29
  updaters.each_pair do |name, obj|
31
- $stdout.puts "Working with host update definition: #{name}" if $opts.verbose
32
- if $opts.print
33
- puts obj
30
+ DNSOMatic::Logger.log("Working with host update definition: #{name}")
31
+ if opts.print
32
+ DNSOMatic::Logger.warn(obj.to_s)
34
33
  else
35
- obj.send( $opts.force ? 'update!' : 'update')
34
+ obj.send( opts.force ? 'update!' : 'update')
36
35
  end
37
36
  end
38
- rescue DNSOMatic::Error => e
39
- $stderr.puts e
40
- if $opts.debug
41
- $stderr.puts "Backtrace:"
42
- $stderr.puts e.backtrace
43
- end
44
37
  rescue => e
45
- $stderr.puts "Rescued an unhandled exception of type: #{e.class}"
46
- $stderr.puts "The exception contains the following message:"
47
- $stderr.puts e.message
48
- if $opts.debug
49
- $stderr.puts "Backtrace:"
50
- $stderr.puts e.backtrace
38
+ if e.kind_of?(DNSOMatic::Error)
39
+ msg = e
40
+ else
41
+ msg = "Rescued an unhandled exception of type: #{e.class}\n"
42
+ end
43
+
44
+ msg += "The exception contains the following message:\n"
45
+ msg += e.message
46
+
47
+ if opts.debug
48
+ msg += "Backtrace:\n"
49
+ msg += e.backtrace.to_s
50
+ else
51
+ msg += "If you want to see where this error was generated, use -x"
51
52
  end
53
+ DNSOMatic::Logger.warn(msg)
52
54
  exit 1
53
55
  end
@@ -106,7 +106,7 @@ require 'open-uri'
106
106
  # -h, --help Display this help text
107
107
 
108
108
  module DNSOMatic
109
- VERSION = '0.1.1'
109
+ VERSION = '0.2.2'
110
110
  USERAGENT = "Ruby_DNS-o-Matic/#{VERSION}"
111
111
 
112
112
  # We provide our easily distinguishable exception class so that we can easily
@@ -11,6 +11,7 @@ module DNSOMatic
11
11
  UNCHANGED = false
12
12
 
13
13
  attr_reader :ip
14
+ @@opts = Opts.instance
14
15
 
15
16
  # A URL must be provided that returns the IP of the system that requests
16
17
  # the URL. A commonly used example is http://www.whatismyip.org
@@ -52,8 +53,8 @@ module DNSOMatic
52
53
 
53
54
  private
54
55
  def min_elapsed?
55
- if Time.now - @last_update <= $opts.minimum
56
- Logger::log("Minimum lookup interval not expired.")
56
+ if Time.now - @last_update <= @@opts.minimum
57
+ Logger::log("Minimum lookup interval (#{@@opts.minimum}s) not expired.")
57
58
  true
58
59
  else
59
60
  false
@@ -61,8 +62,8 @@ module DNSOMatic
61
62
  end
62
63
 
63
64
  def max_elapsed?
64
- if Time.now - @last_update >= $opts.maximum
65
- Logger::log("Maximum interval between updates has elapsed. Update will be forced.")
65
+ if Time.now - @last_update >= @@opts.maximum
66
+ Logger::log("Maximum update interval (#{@@opts.maximum}s) has elapsed. Update will be forced.")
66
67
  true
67
68
  else
68
69
  false
@@ -122,7 +123,11 @@ module DNSOMatic
122
123
  #because an access for a key that doesn't exist returns and inserts
123
124
  #a new IPStatus object, we don't differntiate between seen and unseen
124
125
  #here.
125
- (@cache[url] ||= IPStatus.new(url)).update
126
+ if @cache[url]
127
+ @cache[url].update
128
+ else
129
+ @cache[url] = IPStatus.new(url)
130
+ end
126
131
 
127
132
  save() #ensure that we get spooled to disk.
128
133
  @cache[url]
@@ -2,20 +2,21 @@ module DNSOMatic
2
2
  # A simple class to provide consistent logging throughout the various other
3
3
  # classes. All methods are class methods, so no instance is required.
4
4
  class Logger
5
+ @@opts = Opts.instance
5
6
  # Output a message to stdout if the user specified the verbose command
6
7
  # line option.
7
8
  def self.log(msg)
8
- $stdout.puts msg if $opts.verbose
9
+ $stdout.puts msg if @@opts.verbose
9
10
  end
10
11
 
11
- # Output a message to stderr regardless of verbose command line option.
12
+ # Output a message to regardless of verbose command line option.
12
13
  def self.warn(msg)
13
- $stderr.puts msg
14
+ $stdout.puts msg
14
15
  end
15
16
 
16
17
  # Output a message to stdout if either verbose or alert was specified.
17
18
  def self.alert(msg)
18
- $stdout.puts msg if $opts.verbose or $opts.alert
19
+ $stdout.puts msg if @@opts.verbose or @@opts.alert
19
20
  end
20
21
  end
21
22
  end
@@ -62,15 +62,18 @@ module DNSOMatic
62
62
  end
63
63
 
64
64
  o.on('-V', '--version', 'Display version and exit') do
65
- $stdout.puts DNSOMatic::VERSION
66
- exit 0
65
+ DNSOMatic::Logger.warn(DNSOMatic::VERSION)
66
+ exit
67
67
  end
68
68
 
69
69
  o.on('-x', '--debug', 'Output additional info in error situations') do
70
70
  @@opts.debug = true
71
71
  end
72
72
 
73
- o.on('-h', '--help', 'Display this help text') { puts o; exit; }
73
+ o.on('-h', '--help', 'Display this help text') do
74
+ DNSOMatic::Logger.warn(o)
75
+ exit
76
+ end
74
77
  end
75
78
  opts.parse!(args)
76
79
 
@@ -74,5 +74,4 @@ class TestOpts < Test::Unit::TestCase
74
74
  def test_extra_args_raise_exception
75
75
  assert_raise(DNSOMatic::Error) { $opts.parse(%w(somearg)) }
76
76
  end
77
-
78
77
  end
@@ -9,6 +9,7 @@ require 'dnsomatic'
9
9
  class TestUpdater < Test::Unit::TestCase
10
10
  def setup
11
11
  $opts = DNSOMatic::Opts.instance
12
+ $opts.parse([]) #just to override any values setup by previous tests
12
13
  $fp = File.join('/tmp', 'dnsomatic.testcache-' + Process.pid.to_s)
13
14
  $iplookup = DNSOMatic::IPLookup.instance
14
15
  $iplookup.setcachefile($fp)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dnsomatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Walton
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-26 00:00:00 -04:00
12
+ date: 2008-08-27 00:00:00 -04:00
13
13
  default_executable: dnsomatic
14
14
  dependencies: []
15
15