move dns update job here

This commit is contained in:
Torsten
2023-02-11 22:35:45 +02:00
parent df6af95ff9
commit 41771ad5c1
13 changed files with 97 additions and 26 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

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

@ -0,0 +1,10 @@
require "dns_update_job"
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