add whenever and make rake for dns update

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

View File

@ -19,6 +19,7 @@ gem 'high_voltage', '~> 3.1'
gem "passenger", "6.0.13" , require: "phusion_passenger/rack_handler"
gem "io-wait" , "0.2.0"
gem "dnsimple"
gem 'whenever', require: false
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
@ -33,6 +34,7 @@ group :development do
gem "web-console"
gem "rack-mini-profiler"
gem "mina"
gem 'mina-whenever'
gem 'guard-rails'
gem 'guard-rspec'
end

View File

@ -82,6 +82,7 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
childprocess (4.1.0)
chronic (0.10.2)
coderay (1.1.3)
concurrent-ruby (1.1.10)
crass (1.0.6)
@ -160,6 +161,8 @@ GEM
mina (1.2.4)
open4 (~> 1.3.4)
rake
mina-whenever (1.0.1)
mina (~> 1.0)
mini_mime (1.1.2)
minitest (5.15.0)
msgpack (1.4.5)
@ -302,6 +305,8 @@ GEM
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
chronic (>= 0.6.3)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.5.4)
@ -321,6 +326,7 @@ DEPENDENCIES
importmap-rails
io-wait (= 0.2.0)
mina
mina-whenever
net-ssh
passenger (= 6.0.13)
rack-mini-profiler
@ -333,6 +339,7 @@ DEPENDENCIES
stimulus-rails
web-console
webdrivers
whenever
RUBY VERSION
ruby 3.0.3p157

View File

@ -60,6 +60,7 @@ task :deploy do
in_path(fetch(:current_path)) do
invoke :'passenger:restart'
end
invoke :'whenever:update'
end
end

10
config/schedule.rb Normal file
View File

@ -0,0 +1,10 @@
# Use this file to easily define all of your cron jobs.
#
set :output, "/home/feenix/gateway/shared/log/whenever.log"
every :hour do
rake "dns:update"
end
# Learn more: http://github.com/javan/whenever

View File

@ -1,6 +1,5 @@
require "dnsimple"
class DnsUpdateJob < ApplicationJob
queue_as :default
class DnsUpdateJob
def client
@client ||= Dnsimple::Client.new(access_token: Rails.application.credentials.simple_token)
@ -9,7 +8,7 @@ class DnsUpdateJob < ApplicationJob
Rails.application.credentials.simple_id
end
def my_ip
HTTParty.get("https://jsonip.com/")["ip"]
@ip ||= HTTParty.get("https://jsonip.com/")["ip"]
end
def find_record(name , host)
@ -17,14 +16,12 @@ class DnsUpdateJob < ApplicationJob
zones.data.first
end
def same_ip
my_ip == find_record("gateway" , "hubfeenix.fi").content
def same_ip(name , host)
my_ip == find_record(name , host).content
end
def update_ip
record = find_record("gateway" , "hubfeenix.fi")
client.zones.update_zone_record(simple_id,"hubfeenix.fi", record.id , content: my_ip)
end
def perform(*args)
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

View File

@ -1,19 +1,18 @@
require 'rails_helper'
require "dns_update_job"
RSpec.describe DnsUpdateJob, type: :job do
ActiveJob::Base.queue_adapter = :test
let(:my_ip){"85.76.133.88"}
RSpec.describe DnsUpdateJob, type: :task do
it "update ip" do
updater = DnsUpdateJob.new
updater.update_ip
zone = updater.find_record("gateway","hubfeenix.fi")
expect(zone.content).to eq my_ip
updater.update_ip("dnsimple_test","hubfeenix.fi")
zone = updater.find_record("dnsimple_test","hubfeenix.fi")
expect(zone.content).to eq updater.my_ip
end
it "should get ip" do
res = DnsUpdateJob.new.my_ip
expect(res).to eq my_ip
expect(res).to start_with "80"
end
it "should init client" do
@ -22,9 +21,10 @@ RSpec.describe DnsUpdateJob, type: :job do
end
it "should find domain" do
zone = DnsUpdateJob.new.find_record("gateway","hubfeenix.fi")
updater = DnsUpdateJob.new
zone = updater.find_record("dnsimple_test","hubfeenix.fi")
expect(zone.class).to eq Dnsimple::Struct::ZoneRecord
expect(zone.name).to eq "gateway"
expect(zone.content).to eq my_ip
expect(zone.name).to eq "dnsimple_test"
expect(zone.content).to eq updater.my_ip
end
end