Class: Katello::RegistrationManager

Inherits:
Object
  • Object
show all
Defined in:
app/services/katello/registration_manager.rb

Class Method Summary collapse

Class Method Details

.check_registration_servicesObject



193
194
195
196
197
198
199
# File 'app/services/katello/registration_manager.rb', line 193

def check_registration_services
  ping_results = {}
  User.as_anonymous_admin do
    ping_results = Katello::Ping.ping
  end
  ping_results[:services][:candlepin][:status] == "ok"
end

.determine_host_dmi_uuid(rhsm_params) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'app/services/katello/registration_manager.rb', line 7

def determine_host_dmi_uuid(rhsm_params)
  host_uuid = rhsm_params.dig(:facts, 'dmi.system.uuid')

  if Katello::Host::SubscriptionFacet.override_dmi_uuid?(host_uuid)
    return [SecureRandom.uuid, true]
  end

  [host_uuid, false]
end

.dmi_uuid_allowed_dupsObject



45
46
47
# File 'app/services/katello/registration_manager.rb', line 45

def dmi_uuid_allowed_dups
  Katello::Host::SubscriptionFacet::DMI_UUID_ALLOWED_DUPS
end

.dmi_uuid_change_allowed?(host, host_uuid_overridden) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'app/services/katello/registration_manager.rb', line 49

def dmi_uuid_change_allowed?(host, host_uuid_overridden)
  if host_uuid_overridden
    true
  elsif host.build && Setting[:host_profile_assume_build_can_change]
    true
  else
    Setting[:host_profile_assume]
  end
end

.find_existing_hosts(host_name, host_uuid) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'app/services/katello/registration_manager.rb', line 59

def find_existing_hosts(host_name, host_uuid)
  query = ::Host.unscoped.where("#{::Host.table_name}.name = ?", host_name)

  unless host_uuid.nil? || dmi_uuid_allowed_dups.include?(host_uuid) # no need to include the dmi uuid lookup
    query = query.left_outer_joins(:subscription_facet).or(::Host.unscoped.left_outer_joins(:subscription_facet)
      .where("#{Katello::Host::SubscriptionFacet.table_name}.dmi_uuid = ?", host_uuid)).distinct
  end

  query
end

.joined_hostnames(hosts) ⇒ Object



120
121
122
# File 'app/services/katello/registration_manager.rb', line 120

def joined_hostnames(hosts)
  hosts.pluck(:name).sort.join(', ')
end

.process_registration(rhsm_params, content_view_environments, activation_keys = []) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/katello/registration_manager.rb', line 17

def process_registration(rhsm_params, content_view_environments, activation_keys = [])
  host_name = propose_existing_hostname(rhsm_params[:facts])
  host_uuid, host_uuid_overridden = determine_host_dmi_uuid(rhsm_params)

  rhsm_params[:facts]['dmi.system.uuid'] = host_uuid # ensure we find & validate against a potentially overridden UUID

  organization = validate_content_view_environment_org(content_view_environments, activation_keys.first)

  hosts = find_existing_hosts(host_name, host_uuid)

  validate_hosts(hosts, organization, host_name, host_uuid, host_uuid_overridden: host_uuid_overridden)

  host = hosts.first || new_host_from_facts(
    rhsm_params[:facts],
    organization,
    Location.default_host_subscribe_location!
  )
  host.organization = organization unless host.organization

  register_host(host, rhsm_params, content_view_environments, activation_keys)

  if host_uuid_overridden
    host.subscription_facet.update_dmi_uuid_override(host_uuid)
  end

  host
end

.register_host(host, consumer_params, content_view_environments, activation_keys = []) ⇒ Object

rubocop:disable Metrics/MethodLength



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/services/katello/registration_manager.rb', line 153

def register_host(host, consumer_params, content_view_environments, activation_keys = []) # rubocop:disable Metrics/MethodLength
  new_host = host.new_record?
  unless new_host
    host.save!
    unregister_host(host, :unregistering => true)
    host.reload
  end

  if activation_keys.present?
    if content_view_environments.blank?
      content_view_environments = [lookup_content_view_environment(activation_keys)]
    end
    set_host_collections(host, activation_keys)
  end
  fail _('Content view and environment not set for registration.') if content_view_environments.blank?

  host.save! #the host is in foreman db at this point

  host_uuid = get_uuid(consumer_params)
  consumer_params[:uuid] = host_uuid
  host.content_facet = populate_content_facet(host, content_view_environments, host_uuid)
  host.content_facet.cves_changed = false # prevent backend_update_needed from triggering an update on a nonexistent consumer
  host.subscription_facet = populate_subscription_facet(host, activation_keys, consumer_params, host_uuid)
  host.save! # the host has content and subscription facets at this point
  create_initial_subscription_status(host)

  User.as_anonymous_admin do
    begin
      create_in_candlepin(host, content_view_environments, consumer_params, activation_keys)
    rescue StandardError => e
      # we can't call CP here since something bad already happened. Just clean up our DB as best as we can.
      host.subscription_facet.try(:destroy!)
      new_host ? remove_partially_registered_new_host(host) : remove_host_artifacts(host)
      raise e
    end

    finalize_registration(host)
  end
end

.registration_error(message, meta = {}) ⇒ Object



116
117
118
# File 'app/services/katello/registration_manager.rb', line 116

def registration_error(message, meta = {})
  fail(Katello::Errors::RegistrationError, _(message) % meta)
end

.unregister_host(host, options = {}) ⇒ Object

options:

* organization_destroy: destroy some data associated with host, but
  leave items alone that will be removed later as part of org destroy
* unregistering: unregister the host but don't destroy it


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/services/katello/registration_manager.rb', line 128

def unregister_host(host, options = {})
  organization_destroy = options.fetch(:organization_destroy, false)
  unregistering = options.fetch(:unregistering, false)

  # if the first operation fails, just raise the error since there's nothing to clean up yet.
  candlepin_consumer_destroy(host.subscription_facet.uuid) if !organization_destroy && host.subscription_facet.try(:uuid)

  # if this fails, there is not much to do about it right now. We can't really re-create the candlepin consumer.
  # This can be cleaned up later via clean_backend_objects.

  delete_agent_queue(host) if host.content_facet.try(:uuid)

  host.subscription_facet.try(:destroy!)

  if unregistering
    remove_host_artifacts(host)
  elsif organization_destroy
    host.content_facet.try(:destroy!)
    remove_host_artifacts(host, clear_content_facet: false)
  else
    host.content_facet.try(:destroy!)
    destroy_host_record(host.id)
  end
end

.validate_content_view_environment_org(content_view_environments, activation_key) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/katello/registration_manager.rb', line 70

def validate_content_view_environment_org(content_view_environments, activation_key)
  orgs = Set.new([activation_key&.organization])
  content_view_environments&.each do |cve|
    orgs << cve&.environment&.organization
  end
  orgs.delete(nil)
  if orgs.size != 1
    registration_error(_("Content view environments and activation key must all belong to the same organization"))
  end
  orgs.first
end

.validate_hosts(hosts, organization, host_name, host_uuid, host_uuid_overridden: false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/services/katello/registration_manager.rb', line 82

def validate_hosts(hosts, organization, host_name, host_uuid, host_uuid_overridden: false)
  return if hosts.empty?

  hosts = hosts.where(organization_id: [organization.id, nil])
  hosts_size = hosts.size

  if hosts_size == 0 # not in the correct org
    #TODO: http://projects.theforeman.org/issues/11532
    registration_error("Host with name %{host_name} is currently registered to a different org, please migrate host to %{org_name}.",
                       org_name: organization.name, host_name: host_name)
  end

  if hosts_size == 1
    host = hosts.first

    if host.name == host_name
      if !host.build && Setting[:host_re_register_build_only]
        registration_error("Host with name %{host_name} is currently registered but not in build mode (host_re_register_build_only==True). Unregister the host manually or put it into build mode to continue.", host_name: host_name)
      end

      current_dmi_uuid = host.subscription_facet&.dmi_uuid
      dmi_uuid_changed = current_dmi_uuid && current_dmi_uuid != host_uuid
      if dmi_uuid_changed && !dmi_uuid_change_allowed?(host, host_uuid_overridden)
        registration_error("This host is reporting a DMI UUID that differs from the existing registration.")
      end

      return true
    end
  end

  hosts = hosts.where.not(name: host_name)
  registration_error("The DMI UUID of this host (%{uuid}) matches other registered hosts: %{existing}", uuid: host_uuid, existing: joined_hostnames(hosts))
end