/
scan_cdn.rb
108 lines (94 loc) · 3.36 KB
/
scan_cdn.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
module Actions
module Katello
module RepositorySet
class ScanCdn < Actions::AbstractAsyncTask
input_format do
param :product_id
param :content_id
end
output_format do
param :results, array_of(Hash) do
param :substitutions
param :path
param :repo_id
param :enabled
end
end
def plan(product, content_id)
prod_content = product.product_content_by_id(content_id).content
plan_self(product_id: product.id, content_id: prod_content.id)
end
def run
output[:results] = fetch_results
end
private
def fetch_results
if content.type == ::Katello::Repository::CANDLEPIN_DOCKER_TYPE
prepare_results_docker_content
else
substitutor = cdn_var_substitutor
return [] unless substitutor
substitutor.substitute_vars(content.contentUrl).map do |(substitutions, path)|
prepare_result(substitutions, path)
end
end
end
def cdn_var_substitutor
return unless (cdn_resource = product.cdn_resource)
cdn_resource.substitutor
end
def prepare_results_docker_content
registries = product.cdn_resource.get_docker_registries(content.contentUrl)
registries.map do |registry|
mapper = ::Katello::Candlepin::Content::DockerRepositoryMapper.new(product,
content,
registry['name'])
mapper.registries = registries
mapper.registry_repo = registry
repo = mapper.find_repository
{
substitutions: {},
path: mapper.feed_url,
repo_name: mapper.name,
pulp_id: mapper.pulp_id,
registry_name: registry["name"],
enabled: !repo.nil?,
promoted: (!repo.nil? && repo.promoted?)
}
end
end
def prepare_result(substitutions, _path)
mapper = repository_mapper(substitutions)
repo = mapper.find_repository
{ substitutions: substitutions,
path: mapper.path,
repo_name: mapper.name,
name: mapper.content.name,
pulp_id: mapper.pulp_id,
enabled: !repo.nil?,
promoted: (!repo.nil? && repo.promoted?)
}
end
def product
@product ||= ::Katello::Product.find(input[:product_id])
end
def content
return @content if defined? @content
if product_content = product.product_content_by_id(input[:content_id])
@content = product_content.content
else
fail "Couldn't find content '%s'" % input[:content_id]
end
end
def repository_mapper(substitutions)
::Katello::Candlepin::Content::RepositoryMapper.new(product,
content,
substitutions)
end
def cdn_url
product.provider[:repository_url]
end
end
end
end
end