Module: Host::ManagedExtensions

Extended by:
ActiveSupport::Concern
Defined in:
app/models/host/managed_extensions.rb

Instance Method Summary collapse

Instance Method Details

#delete_discovery_attribute_setObject



69
70
71
72
73
# File 'app/models/host/managed_extensions.rb', line 69

def delete_discovery_attribute_set
  return if new_record?

  DiscoveryAttributeSet.where(:host_id => self.id).destroy_all if saved_change_to_attribute?(:type) && type_previously_changed?
end

#delKexecObject



65
66
67
# File 'app/models/host/managed_extensions.rb', line 65

def delKexec
  # no kexec on orchestration rollback
end

#delRebootObject



40
41
42
# File 'app/models/host/managed_extensions.rb', line 40

def delReboot
  # no reboot on orchestration rollback
end

#queue_rebootObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/host/managed_extensions.rb', line 14

def queue_reboot
  unless errors.empty? && Setting[:discovery_reboot]
    logger.warn("Not queueing Discovery reboot: #{errors.full_messages.to_sentence}")
    return
  end
  return if new_record? # Discovered Hosts already exist, and new_records will break `find`
  return unless will_save_change_to_attribute?(:type, from: 'Host::Discovered')
  # reboot task must be high priority and there is no compensation action apparently
  if facts['discovery_kexec'] && provisioning_template(:kind => 'kexec')
    post_queue.create(:name => _("Reloading kernel on %s") % self, :priority => 10000, :action => [self, :setKexec])
  else
    post_queue.create(:name => _("Rebooting %s") % self, :priority => 10000, :action => [self, :setReboot])
  end
end

#render_kexec_templateObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/host/managed_extensions.rb', line 44

def render_kexec_template
  template = provisioning_template(:kind => 'kexec')
  raise ::Foreman::Exception.new(N_("Kexec template not associated with operating system")) unless template

  json = JSON.parse(render_template(template: template))
  ::Foreman::Exception.new(N_("Kernel kexec URL is invalid: '%s'"), json['kernel']) unless json['kernel'] =~ /\Ahttp.+\Z/
  ::Foreman::Exception.new(N_("Init RAM kexec URL is invalid: '%s'"), json['initrd']) unless json['initrd'] =~ /\Ahttp.+\Z/
  json
rescue StandardError => e
  Foreman::Logging.exception("Unable render or parse kexec template, must be valid JSON", e)
  {status: "Unable to render or parse template: " + e.to_s}
end

#setKexecObject



57
58
59
60
61
62
63
# File 'app/models/host/managed_extensions.rb', line 57

def setKexec
  old.becomes(Host::Discovered).kexec(render_kexec_template.to_json, old.ip, ip, old.ip6, ip6)
  true
rescue ::Foreman::Exception => e
  Foreman::Logging.exception("Unable to kexec", e)
  true
end

#setRebootObject



29
30
31
32
33
34
35
36
37
38
# File 'app/models/host/managed_extensions.rb', line 29

def setReboot
  old.becomes(Host::Discovered).reboot(old.ip, ip, old.ip6, ip6)
  # 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 => e
  Foreman::Logging.exception("Unable to reboot", e)
  true
end

#update_notificationsObject



75
76
77
78
79
80
# File 'app/models/host/managed_extensions.rb', line 75

def update_notifications
  return if new_record?
  return unless saved_change_to_attribute?(:type) || type_previously_changed?

  ForemanDiscovery::UINotifications::DestroyHost.deliver!(self)
end