Class: Katello::RegistrationManager

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

Constant Summary collapse

REGISTRATION_CRITICAL_FACTS =

Facts strictly required at registration time so that RhelLifecycleStatus can be computed immediately. These drive OS and architecture detection in HostFactImporter. All remaining facts arrive via the subsequent PUT /rhsm/consumers/:id call (step 6).

%w[
  distribution.name
  distribution.version
  uname.machine
].freeze

Class Method Summary collapse

Class Method Details

.check_registration_servicesObject

rubocop:enable Metrics/MethodLength



226
227
228
# File 'app/services/katello/registration_manager.rb', line 226

def check_registration_services
  Katello::Resources::Candlepin::CandlepinPing.ok?
end

.determine_host_dmi_uuid(rhsm_params) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'app/services/katello/registration_manager.rb', line 18

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



56
57
58
# File 'app/services/katello/registration_manager.rb', line 56

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

.dmi_uuid_change_allowed?(host, host_uuid_overridden) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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



131
132
133
# File 'app/services/katello/registration_manager.rb', line 131

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

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



28
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
# File 'app/services/katello/registration_manager.rb', line 28

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

  consumer_data = 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, consumer_data]
end

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

rubocop:disable Metrics/MethodLength



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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/services/katello/registration_manager.rb', line 163

def register_host(host, consumer_params, content_view_environments, activation_keys = [])
  new_host = host.new_record?
  unless new_host
    host.save!
    # Unregister the host before re-registering
    # The retain_build_profile_upon_unregistration setting controls whether
    # provisioning information (including kickstart repository) is preserved
    begin
      unregister_host(host, :unregistering => true, :preserve_for_provisioning => true)
    rescue RestClient::Gone
      Rails.logger.debug("Host %s has been removed in preparation for reregistration" % host&.name)
    end
    host.reload
  end

  if activation_keys.present?
    if content_view_environments.blank?
      content_view_environments = lookup_content_view_environments(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.content_facet.cvenvs_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

  consumer_data = nil
  User.as_anonymous_admin do
    begin
      consumer_data = begin
        create_in_candlepin(host, content_view_environments, consumer_params, activation_keys)
      rescue StandardError => e
        # Invalidate the cached Candlepin status immediately so subsequent
        # registrations fail fast at check_registration_services rather than
        # proceeding through DB writes only to roll back when CP is down.
        Katello::Resources::Candlepin::CandlepinPing.clear_cache
        raise e
      end

      # Import only the facts needed for OS/architecture detection so that
      # RhelLifecycleStatus is correct immediately after registration.
      # The full fact set arrives via PUT /rhsm/consumers/:id (step 6).
      import_critical_registration_facts(host, consumer_params[:facts])

      finalize_registration(host, consumer_data)
    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
  end

  consumer_data
end

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



127
128
129
# File 'app/services/katello/registration_manager.rb', line 127

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


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/services/katello/registration_manager.rb', line 139

def unregister_host(host, options = {})
  organization_destroy = options.fetch(:organization_destroy, false)
  unregistering = options.fetch(:unregistering, false)
  preserve_for_provisioning = options.fetch(:preserve_for_provisioning, 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.

  host.subscription_facet.try(:destroy!)

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

.validate_content_view_environment_org(content_view_environments, activation_key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'app/services/katello/registration_manager.rb', line 81

def validate_content_view_environment_org(content_view_environments, activation_key)
  orgs = Set.new([activation_key&.organization])
  content_view_environments&.each do |cvenv|
    orgs << cvenv&.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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/services/katello/registration_manager.rb', line 93

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