add whenever and make rake for dns update

This commit is contained in:
2022-04-09 13:59:36 +03:00
parent c39b2e39fd
commit 21bbc6dcd7
7 changed files with 46 additions and 20 deletions

27
lib/dns_update_job.rb Normal file
View File

@ -0,0 +1,27 @@
require "dnsimple"
class DnsUpdateJob
def client
@client ||= Dnsimple::Client.new(access_token: Rails.application.credentials.simple_token)
end
def simple_id
Rails.application.credentials.simple_id
end
def my_ip
@ip ||= HTTParty.get("https://jsonip.com/")["ip"]
end
def find_record(name , host)
zones = client.zones.list_zone_records(simple_id, host, filter: { type: 'A' , name: name })
zones.data.first
end
def same_ip(name , host)
my_ip == find_record(name , host).content
end
def update_ip(name , host)
record = find_record(name , host)
client.zones.update_zone_record(simple_id, host , record.id , content: my_ip)
end
end

9
lib/tasks/dns.rake Normal file
View File

@ -0,0 +1,9 @@
namespace :dns do
desc "Update own dyn dns records"
task update: :environment do
job = DnsUpdateJob.new
puts "My ip is #{job.my_ip}"
job.update_ip("gateway" , "hubfeenix.fi")
end
end