Class: ForemanExpireHosts::SafeDestroy

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_expire_hosts/safe_destroy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ SafeDestroy

Returns a new instance of SafeDestroy.



8
9
10
# File 'app/services/foreman_expire_hosts/safe_destroy.rb', line 8

def initialize(subject)
  self.subject = subject
end

Instance Attribute Details

#subjectObject



6
7
8
# File 'app/services/foreman_expire_hosts/safe_destroy.rb', line 6

def subject
  @subject
end

Instance Method Details

#destroy!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/foreman_expire_hosts/safe_destroy.rb', line 12

def destroy!
  # If Katello is installed, we can't just destroy the host
  # but have to delete it the Katello way.
  # See https://community.theforeman.org/t/how-to-properly-destroy-a-content-host/8621
  # for reasoning.
  if subject.is_a?(Host::Base) && with_katello?
    Katello::RegistrationManager.unregister_host(subject)
  else
    subject.destroy!
  end
rescue ActiveRecord::RecordNotDestroyed => e
  message = _('Failed to delete %{class_name} %{subject}: %{message} - Errors: %{errors}') % {
    :class_name => subject.class.name,
    :subject => subject,
    :message => e.message,
    :errors => e.record.errors.full_messages.to_sentence
  }
  Foreman::Logging.exception(message, e)
  false
end