diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 2852c7e..a9ba18c 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -1,43 +1,47 @@ /* * This is a manifest file that'll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. * * You're free to add application-wide styles to this file and they'll appear at the bottom of the * compiled file so the styles you add here take precedence over styles defined in any styles * defined in the other CSS/SCSS files in this directory. It is generally better to create a new * file per style scope. * *= require bootstrap.min *= require_tree . *= require_self */ /* Make sure navbar does not overlay body */ body { padding-top: 70px; } .right { text-align: right; margin-right: 0px; float: right; } .graybox { max-width: 500px; margin-right: 0; margin-left: 0; background-color: #F7F7F9; margin-bottom: 30px; } .graybox form { padding-top: 30px; } #_days_back { margin: auto 10px; } + +tr.fatal > td { + background-color: #EA7272; +} diff --git a/app/helpers/flash_helper.rb b/app/helpers/flash_helper.rb index 805d878..ae16674 100644 --- a/app/helpers/flash_helper.rb +++ b/app/helpers/flash_helper.rb @@ -1,24 +1,30 @@ module FlashHelper def bootstrap_class_for(flash_type) { success: 'alert-success', error: 'alert-danger', alert: 'alert-warning', notice: 'alert-info' }.fetch(flash_type.to_sym, flash_type.to_s) end def flash_messages flash.each do |msg_type, message| concat( content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do concat content_tag(:button, 'x', class: 'close', data: { dismiss: 'alert' }) concat message end ) end nil end + + def notifier(msg) + content_tag(:div, class: "alert #{bootstrap_class_for(msg[:severity])} fade in") do + content_tag(:p, msg[:message]) + end + end end diff --git a/app/helpers/hosts_helper.rb b/app/helpers/hosts_helper.rb new file mode 100644 index 0000000..80055c5 --- /dev/null +++ b/app/helpers/hosts_helper.rb @@ -0,0 +1,23 @@ +module HostsHelper + # Returns an html span with the host's status + def host_status_label(host) + case + when host.pending? + klass = "default" + when host.configured? + klass = "warning" + when host.dispatched? + klass = "info" + when host.deployed? + klass = "success" + when host.updated? + klass = "info" + when host.redispatched? + klass = "primary" + when host.for_removal? + klass = "danger" + end + + content_tag(:span, class: "label label-#{klass}") { host.human_status_name.upcase } + end +end diff --git a/app/models/host.rb b/app/models/host.rb index 00c4425..dad6f63 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -1,183 +1,199 @@ class Host < ActiveRecord::Base establish_connection Baas::settings[:local_db] FILE_RETENTION_DAYS = 60 JOB_RETENTION_DAYS = 180 CATALOG = 'MyCatalog' AUTOPRUNE = 1 STATUSES = { pending: 0, configured: 1, dispatched: 2, deployed: 3, updated: 4, redispatched: 5, for_removal: 6 } has_many :ownerships has_many :users, through: :ownerships, inverse_of: :hosts belongs_to :client, class_name: :Client, foreign_key: :name, primary_key: :name has_many :filesets, dependent: :destroy has_many :job_templates, dependent: :destroy has_many :schedules, dependent: :destroy validates :file_retention, :job_retention, :port, :password, presence: true validates :port, numericality: true validates :fqdn, presence: true, uniqueness: true validate :fqdn_format scope :not_baculized, -> { joins("left join Client on Client.Name = hosts.name").where(Client: { Name: nil }) } before_validation :set_retention, :unset_baculized, :sanitize_name state_machine :status, initial: :pending do STATUSES.each do |status_name, value| state status_name, value: value end after_transition [:dispatched, :redispatched, :configured, :updated] => :deployed do |host| host.job_templates.enabled. update_all(baculized: true, baculized_at: Time.now, updated_at: Time.now) end event :add_configuration do transition [:pending, :dispatched] => :configured end event :dispatch do transition :configured => :dispatched end event :redispatch do transition :updated => :redispatched end event :set_deployed do transition [:dispatched, :redispatched, :configured, :updated] => :deployed end event :change_deployed_config do transition [:deployed, :redispatched, :for_removal] => :updated end event :mark_for_removal do transition [:dispatched, :deployed, :updated, :redispatched] => :for_removal end event :disable do transition all => :pending end end def baculize_config templates = job_templates.enabled.includes(:fileset, :schedule) result = [self] + templates.map {|x| [x, x.fileset, x.schedule] }.flatten.compact.uniq result.map(&:to_bacula_config_array) end def to_bacula_config_array [ "Client {", " Name = #{name}", " Address = #{fqdn}", " FDPort = #{port}", " Catalog = #{CATALOG}", " Password = \"#{password}\"", " File Retention = #{file_retention} days", " Job Retention = #{job_retention} days", " AutoPrune = yes", "}" ] end def auto_prune_human AUTOPRUNE == 1 ? 'yes' : 'no' end # Uploads the host's config to bacula # Reloads bacula server # # It updates the host's status accordingly def dispatch_to_bacula return false if not needs_dispatch? bacula_handler.deploy_config end # Removes a Host from bacula configuration. # Reloads bacula server # # If all go well it changes the host's status and returns true def remove_from_bacula return false unless needs_revoke? bacula_handler.undeploy_config end # Restores a host's backup to a preselected location def restore return false if client.jobs.backup_type.empty? bacula_handler.restore end # Runs the given backup job ASAP def backup_now(job_name) bacula_handler.backup_now(job_name) end def needs_dispatch? verified? && (can_dispatch? || can_redispatch?) end def needs_revoke? for_removal? end # Handles the host's job changes by updating the host's status def recalculate if job_templates(true).enabled.any? add_configuration || change_deployed_config else mark_for_removal || disable end end + def display_message + if !verified? + { message: 'Your host needs to be verified by an admin', severity: :alert } + elsif pending? + { message: 'client not configured yet', severity: :alert } + elsif configured? || dispatched? + { message: 'client not deployed to Bacula', severity: :alert } + elsif updated? || redispatched? + { message: 'client configuration changed, deploy needed', severity: :alert } + elsif for_removal? + { message: 'pending client configuration withdraw', severity: :error } + elsif inactive? + { message: 'client disabled', severity: :alert } + end + end + private # automatic setters def sanitize_name self.name = fqdn end def set_retention self.file_retention = FILE_RETENTION_DAYS self.job_retention = JOB_RETENTION_DAYS end def unset_baculized self.baculized = false if new_record? true end # validation def fqdn_format regex = /(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<%= notice %>

+<%= notifier(@host.display_message) unless @host.deployed? %>
<%= link_to 'Remove host', host_path(@host), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger", role: "button" %>
-

Configuration for <%= @host.name %>

+

Configuration for <%= @host.name %> + <%= host_status_label(@host) %> +


Host Details

Jobs

<%= render partial: "host_details" %> <%= render partial: "jobs/job_templates" %>

Config File

 <%= @host.baculize_config.join("\n") %>