reverie 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a16fb53c6a3336f4e30f595f703462173159ae46
4
- data.tar.gz: b353004ce2dd8796f981f9080e2faefa0308e133
3
+ metadata.gz: 2fb22911434d9be837fe65b4501636427a964ffd
4
+ data.tar.gz: 9021a07c63cc95a0e3c5b15fb59e523ddb6930b4
5
5
  SHA512:
6
- metadata.gz: c4d53f9ff6e597d11a8e98a6581f5441d708a4eb765e09bfb0774b051d6cd1cfe72823b45e4d8c2f0fbb589bd425af5533a2d189562ae83c87784c9e1948e617
7
- data.tar.gz: d54c9d8093a4b067fd1dc4e6baa8c007c31f5c143e894ac593fa57a1a55a010672c24110580d3d668a3733396d354e7947eb07cfe5a8232202f2194cc4577b18
6
+ metadata.gz: 2c0ff4ad9779d50c494aca3cd16c879822f8ef107f8680bda4f10aa088c5770da335ab0f4e5766dfb5aa3429619d192d5dfab3d6454bf2db034c3c5e0cd2d85c
7
+ data.tar.gz: 4630eda64eb8711f3627a096cb295b4c6ad7814dc2f9e7d58a9b3d35248c346593389893c22d5b99e2a7905ab0be8dd9b7ae0a0758655a062f1bdd8919ba70a4
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
- task :default => :test
4
+ task default: :test
5
5
 
6
6
  Rake::TestTask.new do |t|
7
7
  t.libs.push 'lib'
@@ -1,3 +1,4 @@
1
+ # Reverie version
1
2
  class Reverie
2
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
3
4
  end
data/lib/reverie.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # Reverie
2
- #
2
+
3
3
  # Ported from Ian McKellar's dreamhost-ddns
4
4
  # https://github.com/ianloic/dreamhost-ddns
5
5
 
@@ -16,41 +16,67 @@ class Reverie
16
16
  DH_URI = URI 'https://api.dreamhost.com/'
17
17
  IP_URI = URI 'http://myexternalip.com/raw'
18
18
 
19
- Settings.define :conf, type: :filename, description: 'The location of the configuration file',
20
- default: Configliere::DEFAULT_CONFIG_LOCATION[:user].call('reverie')
21
- Settings.define :log, type: :filename, description: 'The location of the log file'
22
-
23
- attr_accessor :options, :args
19
+ OPENSSL_V3 = {
20
+ use_ssl: true,
21
+ ssl_version: :SSLv3,
22
+ verify_mode: OpenSSL::SSL::VERIFY_PEER
23
+ }
24
+
25
+ MSGS = {
26
+ success: '%s updated to %s',
27
+ fail: '%s failed',
28
+ found: 'get_ip found %s',
29
+ too_soon: 'too soon, updated %ds ago',
30
+ same: 'not updating %s',
31
+ timeout: '%s timed out on %s',
32
+ kv: '%s: %s'
33
+ }
34
+
35
+ Settings.define :conf,
36
+ type: :filename,
37
+ description: 'The location of the configuration file',
38
+ default: Configliere::DEFAULT_CONFIG_LOCATION[:user].call('reverie')
39
+
40
+ Settings.define :log,
41
+ type: :filename,
42
+ description: 'The location of the log file'
24
43
 
25
44
  def self.update_dns
26
45
  Reverie.new.tap { |r| r.update_dns }
27
46
  end
28
47
 
29
48
  def initialize
30
- @options = Settings
31
- @options.resolve!
32
- @options.read(@conf = @options.delete('conf'))
49
+ Settings.resolve!
50
+ Settings.read(@conf = Settings.delete('conf'))
51
+
52
+ @log = Logger.new(Settings.log || STDOUT)
53
+ @log.level = Settings.delete('debug') ? Logger::DEBUG : Logger::INFO
54
+ Settings.delete('log') unless Settings.log
33
55
 
34
- @log = Logger.new(@options.log || STDOUT)
35
- @log.level = @options.delete('debug') ? Logger::DEBUG : Logger::INFO
36
- @options.delete('log') unless @options.log
56
+ @args = {
57
+ key: Settings[:key],
58
+ record: Settings[:record],
59
+ format: 'yaml'
60
+ }
61
+ end
37
62
 
38
- @args = { key: @options[:key],
39
- record: @options[:record],
40
- format: 'yaml' }
63
+ def settings
64
+ Settings
41
65
  end
42
66
 
43
67
  def update_dns
44
- t = Time.now - (@options[:updated_at] || Time.mktime(0))
45
- return if
46
- (t < 900 and @log.debug "too soon, updated #{ t.to_int }s ago") ||
47
- ((ip = get_ip).nil? and @log.debug 'get_ip failed') ||
48
- (ip == @options[:ip] and @log.debug "not updating #{ @options[:record ]}") ||
49
- (!replace_record @options[:record], ip and @log.debug 'replace_record failed')
50
-
51
- @options.merge! ip: ip, updated_at: Time.now
52
- @options.save! @conf
53
- @log.info "#{ @options[:record] } updated to #{ ip }"
68
+ t = Time.now - (Settings[:updated_at] || Time.mktime(0))
69
+
70
+ if (t < 900 and d :too_soon, t) ||
71
+ ((ip = get_ip).nil? and d :fail, 'get_ip') ||
72
+ (ip == Settings[:ip] and d :same, Settings[:record]) ||
73
+ (!replace_record Settings[:record], ip and d :fail, 'replace_record')
74
+ return
75
+ end
76
+
77
+ Settings.merge! ip: ip, updated_at: Time.now
78
+ Settings.save! @conf
79
+ i :success, Settings[:record], ip
54
80
  end
55
81
 
56
82
  def replace_record(record, ip)
@@ -59,41 +85,62 @@ class Reverie
59
85
  status, res = api_call :list_records
60
86
  return false unless status == 'success'
61
87
 
62
- res.detect { |r| r['record'] == record && r['editable'] == 1 }.tap { |r|
63
- api_call :remove_record, record: record, type: r['type'], value: r['value'] if r
64
- }
65
-
66
- status, res = api_call :add_record, record: record, type: 'A', value: ip, comment: "Reverie (#{ Time.now })"
88
+ res.detect { |r| r['record'] == record && r['editable'] == 1 }.tap do |r|
89
+ api_call :remove_record,
90
+ record: record,
91
+ type: r['type'],
92
+ value: r['value'] if r
93
+ end
94
+
95
+ status, _ = api_call :add_record,
96
+ record: record,
97
+ type: 'A',
98
+ value: ip,
99
+ comment: "Reverie (#{ Time.now })"
67
100
  status == 'success'
68
101
  end
69
102
 
70
103
  def get_ip
71
- ip = Net::HTTP.get_response(IP_URI).body.chomp
72
- @log.debug "get_ip found #{ ip }"
104
+ ip = Net::HTTP.get_response(IP_URI).body.strip
105
+ d :found, ip
73
106
  ip if ip =~ Resolv::IPv4::Regex
74
107
  rescue Net::ReadTimeout
75
- @log.warn 'IP lookup timed out'
108
+ w :timeout, 'IP Lookup', IP_URI
76
109
  end
77
110
 
78
- private
111
+ private
79
112
 
80
113
  def api_call(cmd, args = {})
81
- a = @args.merge cmd: "dns-#{ cmd }", unique_id: SecureRandom.uuid
114
+ a = @args.merge(cmd: "dns-#{ cmd }", unique_id: SecureRandom.uuid)
82
115
  a.merge! args
83
116
 
84
- DH_URI.query = URI.encode_www_form a
85
- @log.debug DH_URI.query
117
+ d(DH_URI.query = URI.encode_www_form(a))
86
118
 
87
- opts = {
88
- use_ssl: true,
89
- ssl_version: :SSLv3,
90
- verify_mode: OpenSSL::SSL::VERIFY_PEER
91
- }
92
- res = YAML.load(Net::HTTP.start(DH_URI.host, DH_URI.port, opts) { |http| http.get DH_URI }.body)
93
- @log.debug "#{ a[:cmd] }: #{ res['result'] }"
119
+ req = Net::HTTP.start(DH_URI.host, DH_URI.port, OPENSSL_V3) do |http|
120
+ http.get DH_URI
121
+ end
122
+
123
+ res = YAML.load(req.body)
124
+ d :kv, a[:cmd], res['result']
94
125
 
95
126
  return res['result'], res['data']
96
127
  rescue Net::ReadTimeout
97
- @log.warn "Dreamhost API timed out on #{ a[:cmd] }"
128
+ w :timeout, 'Dreamhost API', a[:cmd]
129
+ end
130
+
131
+ def d(msg, *args)
132
+ log :debug, msg, *args
133
+ end
134
+
135
+ def w(msg, *args)
136
+ log :warn, msg, *args
137
+ end
138
+
139
+ def i(msg, *args)
140
+ log :info, msg, *args
141
+ end
142
+
143
+ def log(level, msg, *args)
144
+ @log.send level, MSGS[msg] ? MSGS[msg] % args : msg
98
145
  end
99
146
  end
data/reverie.gemspec CHANGED
@@ -15,9 +15,9 @@ Gem::Specification.new do |s|
15
15
  s.summary = 'Dreamhost DNS updater'
16
16
  s.description = 'A ruby script to update Dreamhost DNS'
17
17
 
18
- s.files = `git ls-files`.split $/
19
- s.executables = s.files.grep(%r{^bin/}) { |f| File.basename f }
20
- s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+ s.files = `git ls-files`.split "\n"
19
+ s.executables = s.files.grep(/^bin\//) { |f| File.basename f }
20
+ s.test_files = s.files.grep(/^(test|spec|features)\//)
21
21
  s.require_paths = ['lib']
22
22
 
23
23
  s.add_runtime_dependency 'configliere', '~> 0.4'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reverie
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fission Xuiptz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-18 00:00:00.000000000 Z
11
+ date: 2014-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: configliere