Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible layering for subscription rules #43

Merged
merged 40 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7194ba8
Upload of a possibile solution for issue #5 #17 #20 #21 #22
mossicrue Oct 26, 2018
2bc4713
make the api object global
evgeni Oct 31, 2018
9057771
always use the host_subscriptions api
evgeni Oct 31, 2018
8b9248c
drop support for old :systems api endpoint
evgeni Oct 31, 2018
bb21b1a
drop registered_by support
evgeni Oct 31, 2018
a91d2eb
move fetching of results into a helper function
evgeni Oct 31, 2018
a53c244
page is not needed anymore here
evgeni Oct 31, 2018
d04c4d8
update code as request by Evgeni in #26
mossicrue Nov 8, 2018
10733de
update code as request by Evgeni in #26, added a possible solution fo…
mossicrue Nov 23, 2018
91ae83b
Upload of a possibile solution for issue #5 #17 #20 #21 #22
mossicrue Oct 26, 2018
e6a24d3
update code as request by Evgeni in #26
mossicrue Nov 8, 2018
f535e55
update code as request by Evgeni in #26, added a possible solution fo…
mossicrue Nov 23, 2018
584a7a8
Merge branch 'master' of https://github.com/mossicrue/katello-attach-…
mossicrue Nov 30, 2018
888c865
Update code as requested by evgeni on 11/30
mossicrue Dec 3, 2018
9d653c6
Updated code as requested by Evgeni on 12/03
mossicrue Dec 6, 2018
96042f3
Fixed output string for subscription report
mossicrue Dec 7, 2018
e356773
Merge branch 'master' of https://github.com/RedHatSatellite/katello-a…
mossicrue Dec 7, 2018
d71d703
possible solution for fact matcher
mossicrue Dec 7, 2018
dd3d38b
Merge remote-tracking branch 'upstream/master'
mossicrue Dec 21, 2018
212a60b
Revert "possible solution for fact matcher"
mossicrue Dec 21, 2018
f932a7a
Merge remote-tracking branch 'upstream/master'
mossicrue Dec 21, 2018
b7e8bce
fixed and clean some of some code after test, add facter test for mul…
mossicrue Dec 21, 2018
6a08da7
fixed gathering host type with fact_analyzer
mossicrue Dec 21, 2018
99e4dcf
Merge branch 'master' into master
mossicrue Dec 21, 2018
d69d61a
update code as requested by Evgeni
mossicrue Dec 21, 2018
99fcae2
Merge branch 'master' of https://github.com/mossicrue/katello-attach-…
mossicrue Dec 21, 2018
93f0959
Added possible layering for sub
mossicrue Dec 24, 2018
d7e8262
Fixed some error for sub_layer and merge.subs utils function
mossicrue Dec 28, 2018
8eb24fd
Merge branch 'master' into sub_merger_test
mossicrue Jan 14, 2019
be82327
Merge branch 'master' into sub_merger_test
mossicrue Jan 14, 2019
13313dc
make test changes requested by evgeni
mossicrue Jan 18, 2019
e0e6509
fixed hash
mossicrue Jan 18, 2019
8996163
another fix on test
mossicrue Jan 18, 2019
c3195b5
make some change as requested by evgeni
mossicrue Jan 21, 2019
083b801
Fixed for test
mossicrue Jan 23, 2019
7e63d23
Change from clone to dub
mossicrue Jan 23, 2019
c2d933f
updated test code
mossicrue Jan 23, 2019
aeebad9
Update sub_merger_test.rb
mossicrue Jan 23, 2019
dde54dd
remove if sub.has_key?('facts')
mossicrue Jan 23, 2019
e77e57a
revert search_args change
evgeni Jan 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions katello-attach-subscription
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,11 @@ def vdcupdate()
end
system_details[system['name']]=sys
# check if the type requested match the host one
system_type = KatelloAttachSubscription::FactAnalyzer.system_type(sys)
if sys.has_key?('facts')
evgeni marked this conversation as resolved.
Show resolved Hide resolved
system_type = KatelloAttachSubscription::FactAnalyzer.system_type(sys, sys['facts'])
else
system_type = KatelloAttachSubscription::FactAnalyzer.system_type_from_host(sys)
end
# for each item in yaml extract sub
if @options[:debug]
puts " DEBUG: YAML dump with all definitions for the current system"
Expand Down Expand Up @@ -902,8 +906,17 @@ def vdcupdate()
if @options[:verbose]
puts "VERBOSE: Start testing facts value for #{sys['name']}"
end
fact_test_passed = KatelloAttachSubscription::HostMatcher.match_host(sys, sub)

if sub.has_key?('facts')
evgeni marked this conversation as resolved.
Show resolved Hide resolved
fact_test_passed = KatelloAttachSubscription::HostMatcher.match_host(sys, sub)
else
if @options[:verbose]
puts " VERBOSE: Fact test passed as no test on it"
end
fact_test_passed = true
end
unless fact_test_passed

if @options[:verbose]
puts " VERBOSE: Host #{sys['name']} doesn't pass facts test. Skip to the next sub entry."
end
Expand Down Expand Up @@ -1093,11 +1106,15 @@ def vdcupdate()
puts " DEBUG: System '#{system['name']}' in scope, proceeding with assignment of variables"
end
# set the desidered subscription to be associated
sub_layer = sub['sub_layer'] || "stop_parsing"
if @options[:debug]
puts " DEBUG: Sub Layer of this sub entry: #{sub_layer}"
end
if sub.has_key?('sub_parsed')
if has_derived_sub
desired_sub_hash = derived_sub
desired_sub_hash = KatelloAttachSubscription::Utils.merge_subs(desired_sub_hash, derived_sub, sub_layer)
else
desired_sub_hash = sub['sub_parsed']
desired_sub_hash = KatelloAttachSubscription::Utils.merge_subs(desired_sub_hash, sub['sub_parsed'], sub_layer)
end
end
# if "remove_other" has been set, set the flag
Expand All @@ -1121,8 +1138,10 @@ def vdcupdate()
keep_virt_only = sub['keep_virt_only']
end

# if the system is found, stop cyclyng over yaml,
break
# if the system is found and sub_layer is "stop_parsing", stop cyclyng over yaml
evgeni marked this conversation as resolved.
Show resolved Hide resolved
if sub_layer == "stop_parsing"
break
end

end
if skip_host
Expand Down Expand Up @@ -1598,7 +1617,8 @@ end
# count the total number of subs available, consumed and free
def getsubdetailfor(sub_name)
detail_hash = {"consumed" => 0, "free" => 0}
search_options = KatelloAttachSubscription::Utils.search_args(name: sub_name)
search_params = 'name="'+sub_name+'"'
search_options = KatelloAttachSubscription::Utils.search_args(search_params)
parsed_subscription = fetch_all_results(:subscriptions,:index,{:search => search_options, :available_for => "host"})
detail_hash["available"] = getavailablesubfor(parsed_subscription)
detail_hash["consumed"] = getconsumedsubfor(parsed_subscription)
Expand Down
21 changes: 20 additions & 1 deletion lib/katello_attach_subscription/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ def self.search_args(search)
search
end
end

def self.merge_subs(current_sub, sub_to_merge, command)
evgeni marked this conversation as resolved.
Show resolved Hide resolved
if not current_sub.nil?
current = current_sub.clone
evgeni marked this conversation as resolved.
Show resolved Hide resolved
else
current = {}
end
new_sub = sub_to_merge.clone
evgeni marked this conversation as resolved.
Show resolved Hide resolved
case command
when "override"
current = new_sub
else
if current.nil?
evgeni marked this conversation as resolved.
Show resolved Hide resolved
current = new_sub
else
current.merge!(new_sub)
end
end
return current.clone
evgeni marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

110 changes: 110 additions & 0 deletions test/fixtures/merge_sub_file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
subs:
-
hostname: .*
type: Hypervisor
skip_density: true
facts:
-
name: hypervisor::cluster
value: example_cluster
matcher: regexp
sub:
rhel:
- name = "Red Hat Enterprise Linux for Virtual Datacenters, Premium"
evgeni marked this conversation as resolved.
Show resolved Hide resolved
- name = "Red Hat Enterprise Linux for Virtual Datacenters, Standard"
els:
- name = "Red Hat Enterprise Linux Extended Life Cycle Support (Unlimited Guests)"
smartmanagement:
- name = "Smart Management for Unlimited Guests"
sub_parsed:
rhel:
- d2e16e6ef52a45b7468f1da56bba1953
- e78f5438b48b39bcbdea61b73679449d
- a98931d104a7fb8f30450547d97e7ca5
els:
- 7f9a983a540e00931a69382161bdd265
smartmanagement:
- 439a7d9b0548adbedcce838e37e84ba1
-
hostname: .*
type: Hypervisor
sub:
rhel:
- name = "Red Hat Enterprise Linux for Virtual Datacenters, Premium"
- name = "Red Hat Enterprise Linux for Virtual Datacenters, Standard"
els:
- name = "Red Hat Enterprise Linux Extended Life Cycle Support (Unlimited Guests)"
smartmanagement:
- name = "Smart Management for Unlimited Guests"
sub_parsed:
rhel:
- d2e16e6ef52a45b7468f1da56bba1953
- e78f5438b48b39bcbdea61b73679449d
- a98931d104a7fb8f30450547d97e7ca5
els:
- 7f9a983a540e00931a69382161bdd265
smartmanagement:
- 439a7d9b0548adbedcce838e37e84ba1
-
hostname: .*
type: Physical
sub_layer: keep_parsing
facts:
-
name: distribution::name
value: Server
matcher: regexp
sub:
rhel:
- name = "Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)"
- name = "Red Hat Enterprise Linux Server, Premium (Physical or Virtual Nodes)"
smartmanagement:
- name = "Smart Management"
sub_parsed:
rhel:
- b1a5d251fa4fe598cb947ffc42b9cbed
- 1337d38747e659ed836548ae6cda7cc2
smartmanagement:
- f180623caa42379bc4518d06c9c9be05
-
hostname: .*
type: Physical
sub_layer: keep_parsing
facts:
-
name: distribution::name
value: Server
matcher: regexp
-
name: distribution::version
value: <6.0
matcher: version
sub:
els:
- name = "Red Hat Enterprise Linux Extended Life Cycle Support (Physical or Virtual Nodes)"
sub_parsed:
els:
- 523af537946b79c4f8369ed39ba78605
-
hostname: .*
type: Physical
sub_layer: override
facts:
-
name: distribution::name
value: Client|Workstation
matcher: regexp
sub:
rhel:
- name = "Red Hat Enterprise Linux Server for HPC Compute Node, Self-support (1-2 sockets) (Up to 1 guest)"
smartmanagement:
- name = "Smart Management for Red Hat Enterprise Linux Server for HPC Compute Node (Up to 1 guest)"
sub_parsed:
rhel:
- bb98d4e9c281b175ea84c517b59308ea
smartmanagement:
- af03af10d57b7b17f26a0130562d6b6e
-
hostname: .*
type: Guest
auto_attach: true
117 changes: 117 additions & 0 deletions test/sub_merger_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env ruby
require 'yaml'
require_relative '../lib/katello_attach_subscription'

class SubMergerTest
evgeni marked this conversation as resolved.
Show resolved Hide resolved

DUMMY_KEEP_PARSE = {
evgeni marked this conversation as resolved.
Show resolved Hide resolved
"type" => "Physical",
"host" => {
'name' => 'physical_example',
'facts' => {
"distribution::name" => "Red Hat Server",
"distribution::version" => "6.1"
}
}
}

DUMMY_MERGE = {
"type" => "Physical",
"host" => {
'name' => 'physical_example',
'facts' => {
"distribution::name" => "Red Hat Client",
"distribution::version" => "5.2"
}
}
}

DUMMY_OVERRIDE = {
"type" => "Physical",
"host" => {
'name' => 'physical_example',
'facts' => {
"distribution::name" => "Red Hat Client"
}
}
}

DUMMY_STOP = {
"type" => "Hypervisor",
"host" => {
'name' => 'physical_example',
'facts' => {
'hypervisor::cluster' => 'example_cluster'
}
}
}

DUMMY_NORMAL = {
"type" => "Hypervisor",
"host" => {
'name' => 'physical_example',
'facts' => {}
}
}

def test_normal_parse
evgeni marked this conversation as resolved.
Show resolved Hide resolved
yaml_sub = readSubsFromYAML()
parsed_sub = getSubForHost(yaml_sub, DUMMY_NORMAL)
puts "NORMAL PARSE"
puts "Expected Sub:"
p parsed_sub
end

def test_stop_parse
yaml_sub = readSubsFromYAML()
parsed_sub = getSubForHost(yaml_sub, DUMMY_STOP)
puts "PARSE STOP"
puts "Expected Sub:"
p parsed_sub
end

def test_overdrive_parse
yaml_sub = readSubsFromYAML()
parsed_sub = getSubForHost(yaml_sub, DUMMY_OVERRIDE)
puts "PARSE OVERDRIVE"
puts "Expected Sub:"
p parsed_sub
end

def test_merge_parse
yaml_sub = readSubsFromYAML()
parsed_sub = getSubForHost(yaml_sub, DUMMY_MERGE)
puts "PARSE OVERDRIVE"
puts "Expected Sub:"
p parsed_sub
end

def test_keep_parse
yaml_sub = readSubsFromYAML()
parsed_sub = getSubForHost(yaml_sub, DUMMY_KEEP_PARSE)
puts "PARSE OVERDRIVE"
puts "Expected Sub:"
p parsed_sub
end

def readSubsFromYAML
evgeni marked this conversation as resolved.
Show resolved Hide resolved
yaml_file = YAML.load_file('fixtures/merge_sub_file.yaml')
return yaml_file['sub']
end

def getSubForHost(subs, host)
evgeni marked this conversation as resolved.
Show resolved Hide resolved
parsed_final = nil
subs.each do |single_sub|
parsed = single_sub['parsed_sub']
evgeni marked this conversation as resolved.
Show resolved Hide resolved
if KatelloAttachSubscription::HostMatcher.match_host(host, single_sub)
layer_command = single_sub['sub_layer'] || 'stop_parsing'
parsed_final = KatelloAttachSubscription::Utils.merge_subs(parsed_final, parsed, layer_command)
if layer_command == 'stop_parsing'
break
end
end
end
return parsed_final.clone
evgeni marked this conversation as resolved.
Show resolved Hide resolved
end

end