-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathset_content.rb
More file actions
83 lines (80 loc) · 3.83 KB
/
Copy pathset_content.rb
File metadata and controls
83 lines (80 loc) · 3.83 KB
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
module Actions
module Candlepin
module Environment
class SetContent < Actions::Base
middleware.use ::Actions::Middleware::RemoteAction
middleware.use ::Actions::Middleware::CandlepinServicesCheck
middleware.use ::Actions::Middleware::KeepSessionId
def plan(content_view, environment, content_view_environment, new_content_id = nil)
plan_self(:content_view_id => content_view.id,
:environment_id => environment.id,
:cp_environment_id => content_view_environment.cp_id,
:new_content_id => new_content_id)
end
def finalize # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
content_view = ::Katello::ContentView.find(input[:content_view_id])
environment = ::Katello::KTEnvironment.find(input[:environment_id])
content_ids = content_view.repos(environment).map(&:content_id).uniq.compact
# in case we create new custom repository that doesn't have the
# content_id set yet in the plan phase, we allow to pass it as
# additional argument
content_ids << input[:new_content_id] if input[:new_content_id] && !content_ids.include?(input[:new_content_id])
saved_cp_ids = existing_ids
output[:add_ids] = content_ids - saved_cp_ids
output[:delete_ids] = saved_cp_ids - content_ids
max_retries = 4
retries = 0
until output[:add_ids].empty?
begin
output[:add_response] = ::Katello::Resources::Candlepin::Environment.add_content(input[:cp_environment_id], output[:add_ids])
break
rescue HttpResource::HttpError => e
case e.code
when '409'
# Candlepin raises a 409 in case it gets a duplicate content id add to an environment.
# Refresh the existing ids list and try again.
raise e if ((retries += 1) == max_retries)
output[:add_ids] = content_ids - existing_ids
when '404'
# Set a higher limit for retries just in case the missing content is not being parsed correctly.
# If the content is not found after the retries, assume it is gone and continue.
raise e if ((retries += 1) == 1_000)
missing_content = e.message.split(' ')[-1].gsub(/[".]/, '')
Rails.logger.debug "Content #{missing_content} not found in the environment. Removing it from the add_ids list."
output[:add_ids].delete(missing_content)
else
raise
end
end
end
retries = 0
until output[:delete_ids].empty?
begin
output[:delete_response] = ::Katello::Resources::Candlepin::Environment.delete_content(input[:cp_environment_id], output[:delete_ids])
break
rescue HttpResource::HttpError => e
raise unless e.code == '404'
# Candlepin raises a 404 in case a content id is not found in this environment
# If thats the case lets just refresh the existing ids list (which hopefully will not have the 404'd content)
# and try again.
break if ((retries += 1) == max_retries)
output[:delete_ids] = existing_ids - content_ids
end
end
end
def existing_ids
::Katello::Resources::Candlepin::Environment.
find(input[:cp_environment_id])[:environmentContent].map do |content|
if content.key?('contentId')
# Supports Candlepin 4.2.11 and up
content['contentId']
else
# Supports Candlepin versions below 4.2.11
content[:content][:id]
end
end
end
end
end
end
end