diff --git a/app/controllers/admin/jobs_controller.rb b/app/controllers/admin/jobs_controller.rb index 5243ca9..48d6d8c 100644 --- a/app/controllers/admin/jobs_controller.rb +++ b/app/controllers/admin/jobs_controller.rb @@ -1,22 +1,41 @@ module Admin class JobsController < ApplicationController before_action :authenticate_user! before_action :admin_only! + before_action :job, only: [:destroy, :update] + # GET /jobs def index @job_categories = { 'Pending' => Job.includes(:domain).pending, 'Completed' => Job.includes(:domain).completed.order('id desc') } end # DELETE /jobs/1 def destroy - @job = Job.find(params[:id]) @job.destroy redirect_to admin_jobs_url, notice: "#{@job.id} was successfully destroyed." end + # PUT /jobs/1 + def update + if @job.update(job_params) + redirect_to admin_jobs_url, notice: 'Job was successfully updated.' + else + render :edit + end + end + + private + + def job + @job = Job.find(params[:id]) + end + + def job_params + params.require(:job).permit(:status) + end end end diff --git a/app/models/job.rb b/app/models/job.rb index 20fbc38..09e6548 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -1,104 +1,112 @@ class Job < ActiveRecord::Base belongs_to :domain scope :pending, -> { where(status: 0) } scope :completed, -> { where(status: [1, 2]) } def failed? status == 2 end + def done? + status == 1 + end + + def pending? + status == 0 + end + def arguments JSON.parse(args) end class << self def add_domain(domain) ActiveRecord::Base.transaction do jobs_for_domain(domain, :add_domain) trigger_event(domain, :installed) end end def shutdown_domain(domain) ActiveRecord::Base.transaction do job_for_domain(domain, :remove_domain) job_for_domain(domain, :opendnssec_remove) if domain.dnssec? trigger_event(domain, :cleaned_up) end end def dnssec_sign(domain) ActiveRecord::Base.transaction do job_for_domain(domain, :opendnssec_add, policy: domain.dnssec_policy.name) job_for_domain(domain, :bind_convert_to_dnssec) trigger_event(domain, :signed) end end def wait_for_ready(domain) jobs_for_domain(domain, :wait_for_ready_to_push_ds) end def dnssec_push_ds(domain, dss) opts = Hash[:dnssec_parent, domain.dnssec_parent, :dnssec_parent_authority, domain.dnssec_parent_authority, :dss, dss] keytag = dss.map { |ds| ds.split.first }.first # Both records should have the same keytag ActiveRecord::Base.transaction do job_for_domain(domain, :publish_ds, opts) job_for_domain(domain, :wait_for_active, keytag: keytag) trigger_event(domain, :converted) end end def dnssec_rollover_ds(domain, dss) opts = Hash[:dnssec_parent, domain.dnssec_parent, :dnssec_parent_authority, domain.dnssec_parent_authority, :dss, dss] keytag = dss.map { |ds| ds.split.first }.first # Both records should have the same keytag ActiveRecord::Base.transaction do job_for_domain(domain, :publish_ds, opts) job_for_domain(domain, :wait_for_active, keytag: keytag) trigger_event(domain, :complete_rollover) end end def convert_to_plain(domain) ActiveRecord::Base.transaction do jobs_for_domain(domain, :remove_domain, :add_domain, :opendnssec_remove) trigger_event(domain, :converted) end end private def trigger_event(domain, event) job_for_domain(domain, :trigger_event, event: event) end def jobs_for_domain(domain, *job_names) job_names.each { |job_name| job_for_domain(domain, job_name) } end def job_for_domain(domain, job_name, args = {}) args = { zone: domain.name }.merge!(args) create!(domain: domain, job_type: job_name, args: args.to_json) end end end diff --git a/app/views/admin/jobs/index.html.erb b/app/views/admin/jobs/index.html.erb index 3658522..eb795fe 100644 --- a/app/views/admin/jobs/index.html.erb +++ b/app/views/admin/jobs/index.html.erb @@ -1,49 +1,53 @@
<% @job_categories.each_with_index do |pair, idx| %> <% title, jobs = pair %>
- + <% jobs.each do |job| %> <% if job.domain %> <% else %> <% end %> <% if job.args.size > 70 %> <% else %> <% end %> + <% end %>
Id Created at Domain Type Args RetriesControlsControls
<%= job.id %> <%= job.created_at %><%= link_to job.domain.name, domain_path(job.domain) %><%= job.arguments['zone'] %><%= job.job_type %><%= truncate(job.args, length: 70) %><%= job.args %><%= job.retries %> + <%= link_to('done', done_admin_job_path(job), method: :put) unless job.done? %> + <%= link_to('pending', pending_admin_job_path(job), method: :put) if job.failed? %> + <%= link_to_destroy admin_job_path(job), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
diff --git a/config/routes.rb b/config/routes.rb index 5334499..faeaa80 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,57 +1,62 @@ Rails.application.routes.draw do # Override devise user removal devise_scope :users do delete :users, to: redirect('/') end devise_for :users get '/auth/saml', to: 'auth#saml' root to: redirect('/domains') resources :groups, only: [:show] do get :search_member, to: 'groups#search_member', on: :member post :members, to: 'groups#create_member', as: :create_member, on: :member delete 'member/:user_id', to: 'groups#destroy_member', as: :destroy_member, on: :member end resources :domains do get :edit_dnssec, to: 'domains#edit_dnssec', on: :member resources :records, except: [:index, :show] do # Reuse records#update instead of introducing new controller actions # # rubocop:disable Style/AlignHash put :disable, to: 'records#update', on: :member, defaults: { record: { disabled: true } } put :enable, to: 'records#update', on: :member, defaults: { record: { disabled: false } } put :editable, to: 'records#editable', on: :collection post :valid, to: 'records#valid', on: :collection post :bulk, to: 'records#bulk', on: :collection # rubocop:enable Style/AlignHash end end get '/records/search', to: 'records#search' # Admin namespace :admin do root to: redirect('/admin/groups') resources :groups, except: [:show] - resources :jobs, only: [:index, :destroy] + resources :jobs, only: [:index, :destroy] do + put :done, to: 'jobs#update', on: :member, + defaults: { job: { status: 1 } } + put :pending, to: 'jobs#update', on: :member, + defaults: { job: { status: 0 } } + end resources :users, only: [:destroy] do get :orphans, to: 'users#orphans', on: :collection put :update_groups, to: 'users#update_groups', on: :collection end end # Private put 'private/replace_ds', to: 'private#replace_ds' put 'private/trigger_event', to: 'private#trigger_event' end