Sha256: aa325788b0766be663c52d864768732ebc958150a88ccf9a2894165ee5c34c16

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Host::ManagedExtensions
  extend ActiveSupport::Concern

  included do
    # execute standard callbacks
    after_validation :queue_reboot
    after_save :delete_discovery_attribute_set

    belongs_to :discovery_rule
  end

  def queue_reboot
    return unless errors.empty? && Setting[:discovery_reboot]
    return if new_record? # Discovered Hosts already exist, and new_records will break `find`
    return unless type_changed? and ::Host::Base.find(self.id).type == "Host::Discovered"
    # reboot task must be high priority and there is no compensation action apparently
    post_queue.create(:name => _("Rebooting %s") % self, :priority => 10000,
                      :action => [self, :setReboot])
  end

  def setReboot
    old.becomes(Host::Discovered).reboot
    # It is too late to report error in the post_queue, we catch them and
    # continue. If flash is implemented for new hosts (http://projects.theforeman.org/issues/10559)
    # we can report the error to the user perhaps.
    true
  rescue ::Foreman::Exception
    true
  end

  def delete_discovery_attribute_set
    return if new_record?
    DiscoveryAttributeSet.destroy_all(:host_id => self.id) if type_changed?
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foreman_discovery-4.0.0 app/models/host/managed_extensions.rb