diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index b524827..c6af6b8 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,60 +1,67 @@ class JobsController < ApplicationController - before_action :fetch_host, only: [:new, :edit, :show, :create, :update, :toggle_enable] - before_action :fetch_job, only: [:show, :edit, :update, :destroy, :toggle_enable] + before_action :fetch_host, only: [:new, :edit, :show, :create, :update, + :toggle_enable, :backup_now] + before_action :fetch_job, only: [:show, :edit, :update, :destroy, :toggle_enable, :backup_now] # GET /jobs def new @job = @host.job_templates.new end # POST /jobs def create @job = @host.job_templates.new(fetch_params) @restore_location = params[:job_template][:restore_location] if @restore_location && @job.save_and_create_restore_job(@restore_location) redirect_to host_path(@host) else render :new end end # GET /jobs/1 def show; end # GET /jobs/1/edit def edit;end # PUT /jobs/1 def update if @job.update_attributes(fetch_params) redirect_to host_job_path(@host, @job) else render :edit end end # DELETE /jobs/1 def destroy end # PATCH /hosts/1/jobs/1/enable def toggle_enable @job.enabled = !@job.enabled @job.save redirect_to host_path(@host) end + # POST /hosts/1/jobs/1/backup_now + def backup_now + @job.backup_now + redirect_to client_path(@host.client) + end + private def fetch_job @job = @host.job_templates.find(params[:id]) end def fetch_host @host = current_user.hosts.find(params[:host_id]) end def fetch_params params.require(:job_template).permit(:name, :fileset_id, :schedule_id) end end diff --git a/app/models/job_template.rb b/app/models/job_template.rb index 60df951..b3460c4 100644 --- a/app/models/job_template.rb +++ b/app/models/job_template.rb @@ -1,88 +1,94 @@ class JobTemplate < ActiveRecord::Base establish_connection Baas::settings[:local_db] enum job_type: { backup: 0, restore: 1, verify: 2, admin: 3 } belongs_to :host belongs_to :fileset belongs_to :schedule validates :name, :fileset_id, presence: true validates :schedule_id, presence: true, unless: :restore? validates :name, uniqueness: { scope: :host } before_save :set_job_type after_save :notify_host scope :enabled, -> { where(enabled: true) } # configurable DEFAULT_OPTIONS = { storage: :File, pool: :Default, messages: :Standard, priority: 10, :'Write Bootstrap' => '"/var/lib/bacula/%c.bsr"' } def to_bacula_config_array ['Job {'] + options_array.map { |x| " #{x}" } + DEFAULT_OPTIONS.map { |k,v| " #{k.capitalize} = #{v}" } + ['}'] end def priority DEFAULT_OPTIONS[:priority] end def enabled_human enabled? ? 'yes' : 'no' end def schedule_human schedule.present? ? schedule.name : '-' end def name_for_config "#{host.name} #{name}" end + # Sends a hot backup request to Bacula via BaculaHandler + def backup_now + return false if not (enabled? && baculized? && backup?) + BaculaHandler.new(host).backup_now(name) + end + def save_and_create_restore_job(location) if save_status = save restore_job = JobTemplate.new( host: host, job_type: :restore, fileset: fileset, name: 'Restore ' + name, restore_location: location ) restore_job.save end save_status end private def notify_host host.recalculate end # Sets the default job_type as backup def set_job_type self.job_type = :backup if job_type.nil? end def options_array result = [ "Name = \"#{name_for_config}\"", "FileSet = \"#{fileset.name_for_config}\"", "Client = \"#{host.name}\"", "Type = \"#{job_type.capitalize}\"" ] if restore? result += ["Where = \"#{restore_location}\""] else result += ["Schedule = \"#{schedule.name_for_config}\""] end result end end diff --git a/app/views/clients/_client_details.html.erb b/app/views/clients/_client_details.html.erb index 7da5967..fe365ec 100644 --- a/app/views/clients/_client_details.html.erb +++ b/app/views/clients/_client_details.html.erb @@ -1,48 +1,47 @@
Name <%= @client.name %>
Uname <%= @client.uname %>
Active Jobs <%= @client.running_jobs %>
Last Backup <%= @client.last_job_date_formatted %>
File Retention <%= @client.file_retention_days %> days
Job Retention <%= @client.job_retention_days %> days
Total Space Used <%= number_to_human_size @client.backup_jobs_size %>
Files count <%= number_by_magnitude(@client.files_count) %>
Auto Prune <%= @client.auto_prune_human %>
<%= link_to 'Restore Files', '#', class: "btn btn-warning", role: "button" %> - <%= link_to 'Take Backup', '#', class: "btn btn-success", role: "button" %>
diff --git a/app/views/clients/_job.html.erb b/app/views/clients/_job.html.erb index 7e65907..d467920 100644 --- a/app/views/clients/_job.html.erb +++ b/app/views/clients/_job.html.erb @@ -1,13 +1,14 @@ <%= job.name %> <%= job.job_type %> <%= job.fileset.try(:name) %> <%= job.restore_location %> <%= job.schedule_human %> <%= I18n.l(job.created_at, format: :long) %> <% if job.enabled? && job.baculized? && job.backup? %> - <%= link_to 'Take Backup', '#', class: "btn btn-success", role: "button" %> + <%= link_to 'Backup Now', backup_now_host_job_path(job.host, job), + method: :post, class: "btn btn-success", role: "button" %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 1c5a7b3..e4ad367 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,21 +1,22 @@ Rails.application.routes.draw do resources :clients, only: [:index, :show] resources :hosts, only: [:new, :create, :show, :edit, :update, :destroy] do member do post :submit_config delete :revoke end resources :jobs, only: [:new, :create, :show, :edit, :update, :destroy] do member do patch :toggle_enable + post :backup_now end end resources :filesets, only: [:show, :new, :create, :destroy] resources :schedules, only: [:show, :new, :edit, :create, :update, :destroy] end root 'clients#index' end diff --git a/spec/routing/job_routing_spec.rb b/spec/routing/job_routing_spec.rb index 7bff3c6..1588a84 100644 --- a/spec/routing/job_routing_spec.rb +++ b/spec/routing/job_routing_spec.rb @@ -1,32 +1,37 @@ require 'spec_helper' describe HostsController do it 'routes GET /hosts/1/jobs/new' do expect(get('/hosts/1/jobs/new')).to route_to(controller: 'jobs', action: 'new', host_id: '1') end it 'routes POST /hosts/1/jobs' do expect(post('/hosts/1/jobs')).to route_to(controller: 'jobs', action: 'create', host_id: '1') end it 'routes GET /hosts/1/jobs/1' do expect(get('/hosts/1/jobs/1')).to route_to(controller: 'jobs', action: 'show', host_id: '1', id: '1') end it 'routes GET /hosts/1/jobs/1/edit' do expect(get('/hosts/1/jobs/1/edit')).to route_to(controller: 'jobs', action: 'edit', host_id: '1', id: '1') end it 'routes PUT /hosts/1/jobs/1' do expect(put('/hosts/1/jobs/1')).to route_to(controller: 'jobs', action: 'update', host_id: '1', id: '1') end it 'routes PATCH /hosts/1/jobs/1/toggle_enable' do expect(patch('/hosts/1/jobs/1/toggle_enable')). to route_to(controller: 'jobs', action: 'toggle_enable', host_id: '1', id: '1') end it 'routes DELETE /hosts/1/jobs/1' do expect(delete('/hosts/1/jobs/1')).to route_to(controller: 'jobs', action: 'destroy', host_id: '1', id: '1') end + + it 'routes POST /hosts/1/jobs/1/backup_now' do + expect(post('/hosts/1/jobs/1/backup_now')). + to route_to(controller: 'jobs', action: 'backup_now', host_id: '1', id: '1') + end end