diff --git a/lib/linux_admin/registration_system/rhn.rb b/lib/linux_admin/registration_system/rhn.rb index 07ded11..bcc25fc 100644 --- a/lib/linux_admin/registration_system/rhn.rb +++ b/lib/linux_admin/registration_system/rhn.rb @@ -33,19 +33,24 @@ def register(options) end def subscribe(options) - raise ArgumentError, "pools, username and password are required" if options[:pools].blank? || options[:username].blank? || options[:password].blank? + raise ArgumentError, "channels, username and password are required" if options[:channels].blank? || options[:username].blank? || options[:password].blank? cmd = "rhn-channel -a" - pools = options[:pools].collect {|pool| ["--channel=", pool]} + channels = options[:channels].collect {|channel| ["--channel=", channel]} params = {} params["--user="] = options[:username] params["--password="] = options[:password] - params = params.to_a + pools + params = params.to_a + channels run!(cmd, :params => params) end + def subscribed_products + cmd = "rhn-channel -l" + run!(cmd).output.split("\n").compact + end + private def systemid_file diff --git a/lib/linux_admin/registration_system/subscription_manager.rb b/lib/linux_admin/registration_system/subscription_manager.rb index f1a5314..ed19a24 100644 --- a/lib/linux_admin/registration_system/subscription_manager.rb +++ b/lib/linux_admin/registration_system/subscription_manager.rb @@ -47,12 +47,25 @@ def register(options) def subscribe(options) cmd = "subscription-manager attach" - pools = options[:pools].collect {|pool| ["--pool", pool]} - params = proxy_params(options).to_a + pools + params = proxy_params(options) + + if options[:pools].blank? + params.merge!({"--auto" => nil}) + else + pools = options[:pools].collect {|pool| ["--pool", pool]} + params = params.to_a + pools + end run!(cmd, :params => params) end + def subscribed_products + cmd = "subscription-manager list --installed" + output = run!(cmd).output + + parse_output(output).select {|p| p[:status].downcase == "subscribed"}.collect {|p| p[:product_id]} + end + def available_subscriptions cmd = "subscription-manager list --all --available" output = run!(cmd).output @@ -80,7 +93,8 @@ def parse_content(content) end def format_values(content_group) - content_group[:ends] = Date.strptime(content_group[:ends], "%m/%d/%Y") if content_group[:ends] + content_group[:ends] = Date.strptime(content_group[:ends], "%m/%d/%Y") if content_group[:ends] + content_group[:starts] = Date.strptime(content_group[:starts], "%m/%d/%Y") if content_group[:starts] content_group end diff --git a/spec/data/rhn/output_rhn-channel_list b/spec/data/rhn/output_rhn-channel_list new file mode 100644 index 0000000..144637c --- /dev/null +++ b/spec/data/rhn/output_rhn-channel_list @@ -0,0 +1,2 @@ +rhel-x86_64-server-6 +rhel-x86_64-server-6-cf-me-2 diff --git a/spec/data/subscription_manager/output_list_installed_not_subscribed b/spec/data/subscription_manager/output_list_installed_not_subscribed new file mode 100644 index 0000000..2e242af --- /dev/null +++ b/spec/data/subscription_manager/output_list_installed_not_subscribed @@ -0,0 +1,19 @@ ++-------------------------------------------+ + Installed Product Status ++-------------------------------------------+ +Product Name: Red Hat Enterprise Linux Server +Product ID: 69 +Version: 6.3 +Arch: x86_64 +Status: Not Subscribed +Starts: 09/27/2012 +Ends: 09/26/2013 + +Product Name: Red Hat CloudForms +Product ID: 167 +Version: 2.1 +Arch: x86_64 +Status: Subscribed +Starts: 01/03/2013 +Ends: 01/03/2014 + diff --git a/spec/data/subscription_manager/output_list_installed_subscribed b/spec/data/subscription_manager/output_list_installed_subscribed new file mode 100644 index 0000000..48a1edf --- /dev/null +++ b/spec/data/subscription_manager/output_list_installed_subscribed @@ -0,0 +1,19 @@ ++-------------------------------------------+ + Installed Product Status ++-------------------------------------------+ +Product Name: Red Hat Enterprise Linux Server +Product ID: 69 +Version: 6.3 +Arch: x86_64 +Status: Subscribed +Starts: 09/27/2012 +Ends: 09/26/2013 + +Product Name: Red Hat CloudForms +Product ID: 167 +Version: 2.1 +Arch: x86_64 +Status: Subscribed +Starts: 01/03/2013 +Ends: 01/03/2014 + diff --git a/spec/rhn_spec.rb b/spec/rhn_spec.rb index 7e78ee9..7b16799 100644 --- a/spec/rhn_spec.rb +++ b/spec/rhn_spec.rb @@ -52,13 +52,18 @@ expect { described_class.new.subscribe({}) }.to raise_error(ArgumentError) end - it "with pools" do + it "with channels" do described_class.any_instance.should_receive(:run!).once.with("rhn-channel -a", {:params=>[["--user=", "SomeUser"], ["--password=", "SomePass"], ["--channel=", 123], ["--channel=", 456]]}) described_class.new.subscribe({ :username => "SomeUser", :password => "SomePass", - :pools => [123, 456] + :channels => [123, 456] }) end end + + it "#subscribed_products" do + described_class.any_instance.should_receive(:run!).once.with("rhn-channel -l").and_return(double(:output => sample_output("rhn/output_rhn-channel_list"))) + expect(described_class.new.subscribed_products).to eq(["rhel-x86_64-server-6", "rhel-x86_64-server-6-cf-me-2"]) + end end \ No newline at end of file diff --git a/spec/subscription_manager_spec.rb b/spec/subscription_manager_spec.rb index cdfa7d7..f89df60 100644 --- a/spec/subscription_manager_spec.rb +++ b/spec/subscription_manager_spec.rb @@ -24,10 +24,10 @@ end it "with username and password" do - run_options = ["subscription-manager register", {:params=>{"--username="=>"SomeUser", "--password="=>"SomePass", "--org="=>"IT", "--proxy="=>"1.2.3.4", "--proxyuser="=>"ProxyUser", "--proxypassword="=>"ProxyPass", "--serverurl="=>"192.168.1.1"}}] + run_options = ["subscription-manager register", {:params=>{"--username="=>"SomeUser@SomeDomain.org", "--password="=>"SomePass", "--org="=>"IT", "--proxy="=>"1.2.3.4", "--proxyuser="=>"ProxyUser", "--proxypassword="=>"ProxyPass", "--serverurl="=>"192.168.1.1"}}] described_class.any_instance.should_receive(:run!).once.with(*run_options) described_class.new.register( - :username => "SomeUser", + :username => "SomeUser@SomeDomain.org", :password => "SomePass", :org => "IT", :proxy_address => "1.2.3.4", @@ -38,9 +38,28 @@ end end - it "#subscribe" do - described_class.any_instance.should_receive(:run!).once.with("subscription-manager attach", {:params=>[["--pool", 123], ["--pool", 456]]}) - described_class.new.subscribe({:pools => [123, 456]}) + context "#subscribe" do + it "with pools" do + described_class.any_instance.should_receive(:run!).once.with("subscription-manager attach", {:params=>[["--pool", 123], ["--pool", 456]]}) + described_class.new.subscribe({:pools => [123, 456]}) + end + + it "without pools" do + described_class.any_instance.should_receive(:run!).once.with("subscription-manager attach --auto", {:params=>{}}) + described_class.new.subscribe({}) + end + end + + context "#subscribed_products" do + it "subscribed" do + described_class.any_instance.should_receive(:run!).once.with("subscription-manager list --installed").and_return(double(:output => sample_output("subscription_manager/output_list_installed_subscribed"))) + expect(described_class.new.subscribed_products).to eq(["69", "167"]) + end + + it "not subscribed" do + described_class.any_instance.should_receive(:run!).once.with("subscription-manager list --installed").and_return(double(:output => sample_output("subscription_manager/output_list_installed_not_subscribed"))) + expect(described_class.new.subscribed_products).to eq(["167"]) + end end it "#available_subscriptions" do