diff --git a/Gemfile b/Gemfile index 69d4f13..b5ba106 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 2e292ff..a4af6d5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/config/deploy.rb b/config/deploy.rb index bd80985..d5bd9cf 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -60,6 +60,7 @@ task :deploy do in_path(fetch(:current_path)) do invoke :'passenger:restart' end + invoke :'whenever:update' end end diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 0000000..7fd5ce8 --- /dev/null +++ b/config/schedule.rb @@ -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 diff --git a/app/jobs/dns_update_job.rb b/lib/dns_update_job.rb similarity index 51% rename from app/jobs/dns_update_job.rb rename to lib/dns_update_job.rb index bff9e80..b0a04dc 100644 --- a/app/jobs/dns_update_job.rb +++ b/lib/dns_update_job.rb @@ -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 diff --git a/lib/tasks/dns.rake b/lib/tasks/dns.rake new file mode 100644 index 0000000..7a3c03d --- /dev/null +++ b/lib/tasks/dns.rake @@ -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 diff --git a/spec/jobs/dns_update_job_spec.rb b/spec/jobs/dns_update_job_spec.rb index 03b3e1a..5e64ee4 100644 --- a/spec/jobs/dns_update_job_spec.rb +++ b/spec/jobs/dns_update_job_spec.rb @@ -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