diff --git a/app/models/job_template.rb b/app/models/job_template.rb index 6b7bbfb..5177b53 100644 --- a/app/models/job_template.rb +++ b/app/models/job_template.rb @@ -1,82 +1,77 @@ 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 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}\"" + "Type = \"#{job_type.capitalize}\"", + "Schedule = \"#{schedule.name_for_config}\"" ] - if restore? - result += ["Where = \"#{restore_location}\""] - else - result += ["Schedule = \"#{schedule.name_for_config}\""] - end - result end end diff --git a/app/views/clients/_job.html.erb b/app/views/clients/_job.html.erb index d467920..5bd70dc 100644 --- a/app/views/clients/_job.html.erb +++ b/app/views/clients/_job.html.erb @@ -1,14 +1,13 @@ <%= 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 'Backup Now', backup_now_host_job_path(job.host, job), method: :post, class: "btn btn-success", role: "button" %> <% end %> diff --git a/app/views/clients/_jobs.html.erb b/app/views/clients/_jobs.html.erb index f1f2c09..1ba5d78 100644 --- a/app/views/clients/_jobs.html.erb +++ b/app/views/clients/_jobs.html.erb @@ -1,20 +1,19 @@
- <%= render partial: 'clients/job', collection: @client.persisted_jobs, object: :job %>
Name Type FilesetRestore Location Schedule Created Actions
diff --git a/app/views/jobs/_job_template_details.html.erb b/app/views/jobs/_job_template_details.html.erb index 0280fec..88f7e2a 100644 --- a/app/views/jobs/_job_template_details.html.erb +++ b/app/views/jobs/_job_template_details.html.erb @@ -1,18 +1,17 @@ <%= link_to job.name, host_job_path(@host, job) %> <%= job.job_type %> <%= job.fileset.name %> - <%= job.restore_location %> <%= job.schedule_human %> <%= job.priority %> <% if job.enabled? %> <%= link_to 'Disable', toggle_enable_host_job_path(@host, job), method: :patch, class: "btn btn-warning", role: "button" %> <% else %> <%= link_to 'Enable', toggle_enable_host_job_path(@host, job), method: :patch, class: "btn btn-info", role: "button" %> <% end %> diff --git a/app/views/jobs/_job_templates.html.erb b/app/views/jobs/_job_templates.html.erb index 350809c..be9d8d4 100644 --- a/app/views/jobs/_job_templates.html.erb +++ b/app/views/jobs/_job_templates.html.erb @@ -1,24 +1,23 @@
- <%= render partial: 'jobs/job_template_details', collection: @host.job_templates, as: :job %>
Name Type FileSetRestore Location Schedule Priority Enabled
<%= link_to 'Add Job', new_host_job_path(host_id: @host.id), class: "btn btn-success", role: "button" %>
diff --git a/spec/models/job_template_spec.rb b/spec/models/job_template_spec.rb index a183b64..1c51825 100644 --- a/spec/models/job_template_spec.rb +++ b/spec/models/job_template_spec.rb @@ -1,156 +1,143 @@ 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 context 'when enabling a job' do [:pending, :dispatched].each do |status| context "of a #{status} host" do let(:host) { FactoryGirl.create(:host) } let!(:job) { FactoryGirl.create(:job_template, host: host) } before { host.update_column(:status, Host::STATUSES[status]) } it 'becomes configured' do expect { job.enabled = true job.save }.to change { host.reload.human_status_name }.from(status.to_s).to('configured') end end end context 'of a configured host' do let(:host) { FactoryGirl.create(:host) } let!(:job) { FactoryGirl.create(:job_template, host: host) } before { host.update_column(:status, Host::STATUSES[:configured]) } it 'stays configured' do expect { job.enabled = true job.save }.to_not change { host.reload.human_status_name } end end context 'of a updated host' do let(:host) { FactoryGirl.create(:host) } let!(:job) { FactoryGirl.create(:job_template, host: host) } before { host.update_column(:status, Host::STATUSES[:updated]) } it 'stays updated' do expect { job.enabled = true job.save }.to_not change { host.reload.human_status_name } end end [:deployed, :redispatched].each do |status| context "of a #{status} host" do let(:host) { FactoryGirl.create(:host) } let!(:job) { FactoryGirl.create(:job_template, host: host) } before { host.update_column(:status, Host::STATUSES[status]) } it 'becomes updated' do expect { job.enabled = true job.save }.to change { host.reload.human_status_name }.from(status.to_s).to('updated') end end 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_for_config}\"") 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_for_config}\"") 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 end