forked from Katello/katello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.rb
117 lines (99 loc) · 4.82 KB
/
register.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
76
77
78
79
80
81
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
115
116
117
module Actions
module Katello
module Host
class Register < Actions::EntryAction
middleware.use ::Actions::Middleware::RemoteAction
def plan(host, consumer_params, content_view_environment, activation_keys = [])
sequence do
unless host.new_record?
host.save!
plan_action(Katello::Host::Unregister, host)
host.reload
end
unless activation_keys.empty?
content_view_environment ||= lookup_content_view_environment(activation_keys)
set_host_collections(host, activation_keys)
end
fail _('Content View and Environment not set for registration.') if content_view_environment.nil?
host.save!
host.content_facet = plan_content_facet(host, content_view_environment)
host.subscription_facet = plan_subscription_facet(host, activation_keys, consumer_params)
host.save!
action_subject host
cp_create = plan_action(Candlepin::Consumer::Create, cp_environment_id: content_view_environment.cp_id,
consumer_parameters: consumer_params, activation_keys: activation_keys.map(&:cp_name))
return if cp_create.error
plan_self(uuid: cp_create.output[:response][:uuid], host_id: host.id, hostname: host.name,
user_id: User.current.id, :facts => consumer_params[:facts])
plan_action(Pulp::Consumer::Create, uuid: cp_create.output[:response][:uuid], name: host.name)
end
end
def humanized_name
if input.try(:[], :hostname)
_('Register Host %s') % (input[:hostname] || _('Unknown'))
else
_('Register Host')
end
end
def run
host = ::Host.find(input[:host_id])
unless input[:facts].blank?
::Katello::Host::SubscriptionFacet.update_facts(host, input[:facts])
input[:facts] = 'TRIMMED'
end
end
def finalize
host = ::Host.find(input[:host_id])
host.content_facet.uuid = input[:uuid]
host.subscription_facet.uuid = input[:uuid]
user = ::User.find(input[:user_id])
host.subscription_facet.user = user unless user.nil? || user.hidden?
host.content_facet.save!
host.subscription_facet.update_from_consumer_attributes(host.subscription_facet.candlepin_consumer.
consumer_attributes.except(:installedProducts, :guestIds, :facts))
host.subscription_facet.save!
host.subscription_facet.update_subscription_status
host.content_facet.update_errata_status
host.refresh_global_status!
end
private
def set_host_collections(host, activation_keys)
host_collection_ids = activation_keys.flat_map(&:host_collection_ids).compact.uniq
host_collection_ids.each do |host_collection_id|
host_collection = ::Katello::HostCollection.find(host_collection_id)
if !host_collection.unlimited_hosts && host_collection.max_hosts >= 0
fail _("Host collection '%{name}' exceeds maximum usage limit of '%{limit}'") %
{:limit => host_collection.max_hosts, :name => host_collection.name}
end
end
host.host_collection_ids = host_collection_ids
end
def lookup_content_view_environment(activation_keys)
activation_key = activation_keys.reverse.detect do |act_key|
act_key.environment && act_key.content_view
end
if activation_key
::Katello::ContentViewEnvironment.where(:content_view_id => activation_key.content_view, :environment_id => activation_key.environment).first
else
fail _('At least one activation key must have a lifecycle environment and content view assigned to it')
end
end
def plan_content_facet(host, content_view_environment)
content_facet = host.content_facet || ::Katello::Host::ContentFacet.new(:host => host)
content_facet.content_view = content_view_environment.content_view
content_facet.lifecycle_environment = content_view_environment.environment
content_facet.save!
content_facet
end
def plan_subscription_facet(host, activation_keys, consumer_params)
subscription_facet = host.subscription_facet || ::Katello::Host::SubscriptionFacet.new(:host => host)
subscription_facet.last_checkin = DateTime.now
subscription_facet.update_from_consumer_attributes(consumer_params.except(:installedProducts, :guestIds, :facts))
subscription_facet.save!
subscription_facet.activation_keys = activation_keys
subscription_facet
end
end
end
end
end