diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb
index baae6b3..ac83ec2 100644
--- a/app/controllers/jobs_controller.rb
+++ b/app/controllers/jobs_controller.rb
@@ -1,59 +1,60 @@
class JobsController < ApplicationController
before_action :fetch_job, only: [:show, :edit, :update, :destroy, :toggle_enable]
before_action :fetch_host, only: [:new, :edit, :show, :create, :update, :toggle_enable]
# GET /jobs
def new
@job = @host.job_templates.new
end
# POST /jobs
def create
@job = @host.job_templates.new(fetch_params)
- if @job.save_and_create_restore_job
+ @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
private
def fetch_job
@job = JobTemplate.find(params[:id])
end
def fetch_host
@host = Host.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 e8d4e7b..b053f23 100644
--- a/app/models/job_template.rb
+++ b/app/models/job_template.rb
@@ -1,80 +1,83 @@
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
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
- def save_and_create_restore_job
+ 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_job = JobTemplate.new(
+ host: host, job_type: :restore,
+ fileset: fileset, name: 'Restore ' + name,
+ restore_location: location
+ )
restore_job.save
end
save_status
end
private
# 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}\"",
"Client = \"#{host.name}\"",
"Type = \"#{job_type.capitalize}\""
]
if restore?
result += ['Where = "/tmp/bacula-restores"']
else
result += ["Schedule = \"#{schedule.name}\""]
end
result
end
end
diff --git a/app/views/jobs/_form.html.erb b/app/views/jobs/_form.html.erb
index c082ba3..77d7b4a 100644
--- a/app/views/jobs/_form.html.erb
+++ b/app/views/jobs/_form.html.erb
@@ -1,54 +1,56 @@
<%= bootstrap_form_for(@job, url: @job.new_record? ? host_jobs_path : host_job_path(@host, @job), layout: :horizontal, label_col: 'col-xs-4', control_col: 'col-xs-6') do |f| %>
<% if @job.errors.any? %>
<%= pluralize(@job.errors.count, "error") %>
prohibited this host from being saved:
<% @job.errors.full_messages.each do |message| %>
- <%= message %>
<% end %>
<% end %>
<%= f.text_field :name %>
+ <%= f.text_field :restore_location, label: 'Restore Location', placeholder: '/tmp/bacula' %>
+
<% end %>
diff --git a/app/views/jobs/_job_details.html.erb b/app/views/jobs/_job_details.html.erb
index 1d15ac0..00630e7 100644
--- a/app/views/jobs/_job_details.html.erb
+++ b/app/views/jobs/_job_details.html.erb
@@ -1,34 +1,41 @@
Name |
<%= @job.name %> |
Type |
<%= @job.job_type %> |
Host |
<%= @host.name %> |
Fileset |
<%= @job.fileset.name %> |
-
- Schedule |
- <%= @job.schedule.name %> days |
-
+ <% if @job.restore? %>
+
+ Restore Location |
+ <%= @job.restore_location %> |
+
+ <% else %>
+
+ Schedule |
+ <%= @job.schedule.name %> |
+
+ <% end %>
Created |
<%= I18n.l(@job.created_at, format: :long) %> |
Enabled |
<%= @job.enabled_human %> |
diff --git a/spec/controllers/jobs_controller_spec.rb b/spec/controllers/jobs_controller_spec.rb
index c57b2b7..7533db6 100644
--- a/spec/controllers/jobs_controller_spec.rb
+++ b/spec/controllers/jobs_controller_spec.rb
@@ -1,69 +1,70 @@
require 'spec_helper'
describe JobsController do
let!(:host) { FactoryGirl.create(:host) }
describe 'GET #new' do
before { get :new, host_id: host.id }
it 'initializes a job' do
expect(assigns(:job)).to be
end
it 'renders' do
expect(response).to render_template(:new)
end
end
describe 'POST #create' do
context 'with valid params' do
let(:params) do
{
host_id: host.id,
- job_template: FactoryGirl.build(:job_template).attributes.symbolize_keys.
- slice(:name, :schedule_id, :fileset_id)
+ job_template: FactoryGirl.build(:job_template, restore_location: '/foo').
+ attributes.symbolize_keys.slice(:name, :schedule_id, :fileset_id, :restore_location)
}
end
it 'creates the jobs (:backup, :restore)' do
expect { post :create, params }.
to change { JobTemplate.count }.by(2)
end
it 'redirects to host' do
post :create, params
expect(response).to redirect_to(host_path(host))
end
it 'calls save_and_create_restore_job' do
- JobTemplate.any_instance.should_receive(:save_and_create_restore_job)
+ JobTemplate.any_instance.
+ should_receive(:save_and_create_restore_job).with('/foo')
post :create, params
end
end
context 'with invalid params' do
let(:params) do
{
host_id: host.id,
job_template: FactoryGirl.build(:job_template).attributes.symbolize_keys.
slice(:name, :fileset_id)
}
end
it 'initializes a job with errors' do
post :create, params
expect(assigns(:job)).to be
end
it 'does not create the job' do
expect { post :create, params }.
to_not change { JobTemplate.count }
end
it 'renders :new' do
post :create, params
expect(response).to render_template(:new)
end
end
end
end
diff --git a/spec/factories/job_template.rb b/spec/factories/job_template.rb
index 2d1e885..a7de946 100644
--- a/spec/factories/job_template.rb
+++ b/spec/factories/job_template.rb
@@ -1,18 +1,19 @@
FactoryGirl.define do
factory :jobtemplate, class: JobTemplate do
host
fileset
sequence(:name) { |n| "Job #{n}" }
trait :backup do
job_type :backup
schedule
end
trait :restore do
job_type :restore
+ restore_location '/tmp'
end
factory :job_template, traits: [:backup]
end
end
diff --git a/spec/models/job_template_spec.rb b/spec/models/job_template_spec.rb
index 946dac3..5290f1c 100644
--- a/spec/models/job_template_spec.rb
+++ b/spec/models/job_template_spec.rb
@@ -1,112 +1,118 @@
require 'spec_helper'
describe JobTemplate do
context 'validates' do
it 'name must be present' do
expect(JobTemplate.new).to have(1).errors_on(:name)
end
it 'name must be unique on host\'s scope' do
job_1 = FactoryGirl.create(:job_template, name: 'a name')
job_2 = FactoryGirl.build(:job_template, name: 'a name')
job_3 = FactoryGirl.build(:job_template, name: 'a name', host: job_1.host)
expect(job_2).to be_valid
expect(job_3).to_not be_valid
end
it 'fileset_id must be present' do
expect(JobTemplate.new).to have(1).errors_on(:fileset_id)
end
it 'schedule_id must be present' do
expect(JobTemplate.new).to have(1).errors_on(:schedule_id)
end
it 'schedule_id must NOT be present for :restore jobs' do
expect(JobTemplate.new(job_type: :restore)).to have(0).errors_on(:schedule_id)
end
end
# automatic assignments
context 'when no job_type is given' do
let(:job_template) { FactoryGirl.create(:job_template) }
it 'sets the job_type to :backup' do
expect(job_template).to be_backup
end
end
describe '#to_bacula_config_array' do
let(:job_template) { FactoryGirl.create(:job_template) }
subject { job_template.to_bacula_config_array }
it 'has a Job structure' do
expect(subject.first).to eq('Job {')
expect(subject.last).to eq('}')
end
JobTemplate::DEFAULT_OPTIONS.each do |k, v|
it "assigns #{k.capitalize} param" do
expect(subject).to include(" #{k.capitalize} = #{v}")
end
end
it 'assigns Name param prefixed with the host\'s name' do
expect(subject).to include(" Name = \"#{job_template.name_for_config}\"")
end
it 'assigns FileSet param' do
expect(subject).to include(" FileSet = \"#{job_template.fileset.name}\"")
end
it 'assigns Client param' do
expect(subject).to include(" Client = \"#{job_template.host.name}\"")
end
it 'assigns Type param' do
expect(subject).to include(" Type = \"#{job_template.job_type.capitalize}\"")
end
it 'assigns Schedule param' do
expect(subject).to include(" Schedule = \"#{job_template.schedule.name}\"")
end
context 'for a restore job' do
let(:restore_job) { FactoryGirl.create(:job_template, :restore) }
subject { restore_job.to_bacula_config_array }
it 'does not assign a Schedule param' do
expect(subject).to_not include(" Schedule = \"#{restore_job.schedule.name}\"")
end
it 'assigns Where param' do
expect(subject).to include(" Where = \"#{restore_job.restore_location}\"")
end
end
end
describe '#save_and_create_restore_job' do
let(:host) { FactoryGirl.create(:host) }
let(:backup_job_template) do
FactoryGirl.build(:job_template, job_type: nil, host: host)
end
it 'calls save' do
backup_job_template.should_receive(:save)
- backup_job_template.save_and_create_restore_job
+ backup_job_template.save_and_create_restore_job('/foo')
end
it 'creates a restore job for the same host' do
- expect { backup_job_template.save_and_create_restore_job }.
+ expect { backup_job_template.save_and_create_restore_job('/foo') }.
to change { host.job_templates.restore.count }.by(1)
end
it 'creates a restore job for fileset' do
- backup_job_template.save_and_create_restore_job
+ backup_job_template.save_and_create_restore_job('/foo')
expect(host.job_templates.restore.pluck(:fileset_id)).
to eq([backup_job_template.fileset_id])
end
+
+ it 'sets the correct restore location' do
+ backup_job_template.save_and_create_restore_job('/foo')
+ expect(host.job_templates.restore.pluck(:restore_location)).
+ to eq(['/foo'])
+ end
end
end