Class: ForemanWreckingball::VmwareHypervisorImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_wreckingball/vmware_hypervisor_importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ VmwareHypervisorImporter

Returns a new instance of VmwareHypervisorImporter.



7
8
9
10
11
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 7

def initialize(options = {})
  @compute_resource = options.fetch(:compute_resource)
  @hypervisors = {}
  @counters = {}
end

Instance Attribute Details

#compute_resourceObject

Returns the value of attribute compute_resource.



5
6
7
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 5

def compute_resource
  @compute_resource
end

#countersObject

Returns the value of attribute counters.



5
6
7
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 5

def counters
  @counters
end

Instance Method Details

#create_host_for_hypervisor(name) ⇒ Object



97
98
99
100
101
102
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 97

def create_host_for_hypervisor(name)
  host = ::Host::Managed.new(:name => name, :organization => organization,
                             :location => location, :managed => false, :enabled => false)
  host.save!
  host
end

#delete_removed_hypervisors(cluster) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 104

def delete_removed_hypervisors(cluster)
  hypervisor_names = hypervisors(cluster).map(&:name)
  hypervisor_uuids = hypervisors(cluster).map(&:uuid)
  delete_query = ::ForemanWreckingball::VmwareHypervisorFacet.joins(:host).where(vmware_cluster: cluster).where.not('hosts.name': hypervisor_names).where.not(uuid: hypervisor_uuids)
  counters[:deleted] = if ActiveRecord::Base.connection.adapter_name.downcase.starts_with?('mysql')
                         ::ForemanWreckingball::VmwareHypervisorFacet.where(:id => delete_query.pluck(:id)).delete_all
                       else
                         delete_query.delete_all
                       end
end

#find_host_for_hypervisor(hypervisor) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 81

def find_host_for_hypervisor(hypervisor)
  name = ::ForemanWreckingball::VmwareHypervisorFacet.sanitize_name(hypervisor.name)
  hostname = ::ForemanWreckingball::VmwareHypervisorFacet.sanitize_name([hypervisor.hostname, hypervisor.domainname].join('.'))
  hostname = nil if hostname.blank?
  katello_name = katello_hypervisor_hostname(hostname || name)
  katello_uuid = katello_hypervisor_hostname(hypervisor.uuid)

  host = ::ForemanWreckingball::VmwareHypervisorFacet.find_by(:uuid => hypervisor.uuid).try(:host)
  host ||= ::Host.find_by(:name => hostname) if hostname
  host ||= ::Host.find_by(:name => name)
  host ||= ::Host.find_by(:name => katello_name) if katello_name
  host ||= ::Host.find_by(:name => katello_uuid) if katello_uuid
  host ||= create_host_for_hypervisor(hostname || name)
  host
end

#hypervisors(cluster) ⇒ Object



77
78
79
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 77

def hypervisors(cluster)
  @hypervisors[cluster.name.to_sym] ||= compute_resource.hypervisors(:cluster_id => cluster.name)
end

#import!Object



13
14
15
16
17
18
19
20
21
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 13

def import!
  logger.info "Can not determine organization for compute resource #{compute_resource}." if SETTINGS[:organizations_enabled]
  compute_resource.refresh_cache
  compute_resource.vmware_clusters.each do |cluster|
    import_hypervisors(cluster)
    delete_removed_hypervisors(cluster)
  end
  logger.info("Import hypervisors for '#{compute_resource}' completed. Added: #{counters[:added] || 0}, Updated: #{counters[:updated] || 0}, Deleted: #{counters[:deleted] || 0} hypervisors") # rubocop:disable Metrics/LineLength
end

#import_hypervisor(cluster, hypervisor) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 29

def import_hypervisor(cluster, hypervisor)
  host = find_host_for_hypervisor(hypervisor)

  counter = host.vmware_hypervisor_facet ? :updated : :added

  ipaddress6 = hypervisor.ipaddress6
  ipaddress6 = nil if begin
                         IPAddr.new('fe80::/10').include?(ipaddress6)
                       rescue StandardError
                         false
                       end

  hostname = hypervisor.hostname
  domainname = hypervisor.domainname

  unless hostname.present? && domainname.present?
    logger.info "Trying to guess host- and domainname for #{hypervisor.name}."
    hostname, domainname = hypervisor.name.split('.', 2)
  end

  unless hostname.present? && domainname.present?
    logger.error "Could not guess host- and domainname for #{hypervisor.name}. Skipping."
    return false
  end

  result = host.update(
    :name => hostname,
    :domain => ::Domain.where(:name => domainname).first_or_create,
    :model => ::Model.where(:name => hypervisor.model.strip).first_or_create,
    :ip => hypervisor.ipaddress,
    :ip6 => ipaddress6,
    :vmware_hypervisor_facet_attributes => {
      :vmware_cluster => cluster,
      :cpu_cores => hypervisor.cpu_cores,
      :cpu_sockets => hypervisor.cpu_sockets,
      :cpu_threads => hypervisor.cpu_threads,
      :memory => hypervisor.memory,
      :uuid => hypervisor.uuid,
      :feature_capabilities => hypervisor.feature_capabilities
    }
  )
  if result
    increment_counter(counter)
  else
    logger.error "Failed to save host #{host}. Reason: #{host.errors.full_messages.to_sentence}"
  end
end

#import_hypervisors(cluster) ⇒ Object



23
24
25
26
27
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 23

def import_hypervisors(cluster)
  hypervisors(cluster).each do |hypervisor|
    import_hypervisor(cluster, hypervisor)
  end
end

#katello_hypervisor_hostname(hostname) ⇒ Object



125
126
127
128
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 125

def katello_hypervisor_hostname(hostname)
  return unless organization
  "virt-who-#{hostname}-#{organization.id}"
end

#locationObject



120
121
122
123
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 120

def location
  return unless SETTINGS[:locations_enabled]
  compute_resource.locations.first
end

#organizationObject



115
116
117
118
# File 'app/services/foreman_wreckingball/vmware_hypervisor_importer.rb', line 115

def organization
  return unless SETTINGS[:organizations_enabled]
  compute_resource.organizations.first
end