diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index 02c85ef..510676f 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,59 +1,59 @@ 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 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, :job_type, :fileset_id, :schedule_id) + 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 3bac6fa..d440906 100644 --- a/app/models/job_template.rb +++ b/app/models/job_template.rb @@ -1,50 +1,57 @@ 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, :schedule_id, :fileset_id, presence: true + 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 {'] + DEFAULT_OPTIONS.map { |k,v| " #{k.capitalize} = #{v}" } + options_array.map { |x| " #{x}" } + ['}'] end def priority DEFAULT_OPTIONS[:priority] end def enabled_human enabled? ? 'yes' : 'no' 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 = restore? ? ['Where = "/tmp/bacula-restores"'] : [] result += [ "Name = \"#{name}\"", "FileSet = \"#{fileset.name}\"", "Client = \"#{host.name}\"", "Type = \"#{job_type.capitalize}\"", "Schedule = \"#{schedule.name}\"" ] end end diff --git a/app/views/jobs/_form.html.erb b/app/views/jobs/_form.html.erb index dc0e1e3..c082ba3 100644 --- a/app/views/jobs/_form.html.erb +++ b/app/views/jobs/_form.html.erb @@ -1,56 +1,54 @@ <%= 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? %>