Class: DynamicDns::Daemon
- Inherits:
-
Object
- Object
- DynamicDns::Daemon
- Defined in:
- lib/dynamic_dns/daemon.rb
Constant Summary collapse
- @@GET_IP_URI =
create URI to fetch IP address with
URI('https://ipinfo.io/ip')
Instance Attribute Summary collapse
-
#INTERVAL ⇒ Object
readonly
Returns the value of attribute INTERVAL.
-
#ip ⇒ Object
readonly
accessor methods.
Instance Method Summary collapse
-
#fetch_ip ⇒ Object
helper to fetch the IP address returns a string represents the ip address.
-
#initialize(domain, subdomain, interval) ⇒ Daemon
constructor
A new instance of Daemon.
- #log(msg) ⇒ Object
- #on_new_ip(new_ip) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(domain, subdomain, interval) ⇒ Daemon
Returns a new instance of Daemon.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dynamic_dns/daemon.rb', line 12 def initialize(domain, subdomain, interval) log 'created instance' # initialize ip address @ip = 'NO_IP_SET' # initialize the daemon IP interval if necessary interval.is_a? Integer or interval = 60 @INTERVAL = interval # set up the dns updater instance @UPDATER = DynamicDns::DnsUpdater.new domain, subdomain end |
Instance Attribute Details
#INTERVAL ⇒ Object (readonly)
Returns the value of attribute INTERVAL.
25 26 27 |
# File 'lib/dynamic_dns/daemon.rb', line 25 def INTERVAL @INTERVAL end |
#ip ⇒ Object (readonly)
accessor methods
24 25 26 |
# File 'lib/dynamic_dns/daemon.rb', line 24 def ip @ip end |
Instance Method Details
#fetch_ip ⇒ Object
helper to fetch the IP address returns a string represents the ip address
29 30 31 32 33 34 |
# File 'lib/dynamic_dns/daemon.rb', line 29 def fetch_ip log 'fetching ip' request = Net::HTTP::Get.new @@GET_IP_URI response = Net::HTTP.start(@@GET_IP_URI.host, @@GET_IP_URI.port, :use_ssl => true) { |http| http.request request } response.body.strip! end |
#log(msg) ⇒ Object
79 80 81 |
# File 'lib/dynamic_dns/daemon.rb', line 79 def log(msg) puts "[dynamic-dns:daemon] #{msg}" end |
#on_new_ip(new_ip) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/dynamic_dns/daemon.rb', line 69 def on_new_ip(new_ip) log "ip has changed from #@ip to #{new_ip}" #persist new ip to instance @ip = new_ip #notify aws log "update DNS with new IP #@ip" # interval corresponds to ttl in seconds @UPDATER.update_record_ip @ip, @INTERVAL end |
#start ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dynamic_dns/daemon.rb', line 36 def start log "starting daemon, will check for new ip every #@INTERVAL seconds" # set up the thread to loop thread = Thread.new do log 'set up thread' loop do log 'start thread loop iteration' # get the ip address # if it has changed, update DNS new_ip = fetch_ip if new_ip != @ip on_new_ip(new_ip) else log "ip has not changed from #@ip" end # sleep the number of seconds in the interval sleep @INTERVAL end end # ensure thread exits on an exception thread.abort_on_exception = true # trap SIGINT to exit threads trap 'SIGINT' do log "received SIGINT, killing thread" thread.kill end # keep main thread attached while background thread runs thread.join end |