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

View File

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

View File

@ -60,6 +60,7 @@ task :deploy do
in_path(fetch(:current_path)) do in_path(fetch(:current_path)) do
invoke :'passenger:restart' invoke :'passenger:restart'
end end
invoke :'whenever:update'
end end
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" require "dnsimple"
class DnsUpdateJob < ApplicationJob class DnsUpdateJob
queue_as :default
def client def client
@client ||= Dnsimple::Client.new(access_token: Rails.application.credentials.simple_token) @client ||= Dnsimple::Client.new(access_token: Rails.application.credentials.simple_token)
@ -9,7 +8,7 @@ class DnsUpdateJob < ApplicationJob
Rails.application.credentials.simple_id Rails.application.credentials.simple_id
end end
def my_ip def my_ip
HTTParty.get("https://jsonip.com/")["ip"] @ip ||= HTTParty.get("https://jsonip.com/")["ip"]
end end
def find_record(name , host) def find_record(name , host)
@ -17,14 +16,12 @@ class DnsUpdateJob < ApplicationJob
zones.data.first zones.data.first
end end
def same_ip def same_ip(name , host)
my_ip == find_record("gateway" , "hubfeenix.fi").content my_ip == find_record(name , host).content
end end
def update_ip def update_ip(name , host)
record = find_record("gateway" , "hubfeenix.fi") record = find_record(name , host)
client.zones.update_zone_record(simple_id,"hubfeenix.fi", record.id , content: my_ip) client.zones.update_zone_record(simple_id, host , record.id , content: my_ip)
end
def perform(*args)
end end
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 'rails_helper'
require "dns_update_job"
RSpec.describe DnsUpdateJob, type: :job do RSpec.describe DnsUpdateJob, type: :task do
ActiveJob::Base.queue_adapter = :test
let(:my_ip){"85.76.133.88"}
it "update ip" do it "update ip" do
updater = DnsUpdateJob.new updater = DnsUpdateJob.new
updater.update_ip updater.update_ip("dnsimple_test","hubfeenix.fi")
zone = updater.find_record("gateway","hubfeenix.fi") zone = updater.find_record("dnsimple_test","hubfeenix.fi")
expect(zone.content).to eq my_ip expect(zone.content).to eq updater.my_ip
end end
it "should get ip" do it "should get ip" do
res = DnsUpdateJob.new.my_ip res = DnsUpdateJob.new.my_ip
expect(res).to eq my_ip expect(res).to start_with "80"
end end
it "should init client" do it "should init client" do
@ -22,9 +21,10 @@ RSpec.describe DnsUpdateJob, type: :job do
end end
it "should find domain" do 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.class).to eq Dnsimple::Struct::ZoneRecord
expect(zone.name).to eq "gateway" expect(zone.name).to eq "dnsimple_test"
expect(zone.content).to eq my_ip expect(zone.content).to eq updater.my_ip
end end
end end