diff --git a/lib/linux_admin/registration_system/rhn.rb b/lib/linux_admin/registration_system/rhn.rb index b187e6f..f8109f4 100644 --- a/lib/linux_admin/registration_system/rhn.rb +++ b/lib/linux_admin/registration_system/rhn.rb @@ -40,27 +40,65 @@ def register(options) run!(cmd, :params => params) end - def subscribe(options) - raise ArgumentError, "channels, username and password are required" if options[:channels].blank? || options[:username].blank? || options[:password].blank? - cmd = "rhn-channel -a" + def enable_channel(repo, options) + cmd = "rhn-channel -a" + params = user_pwd(options).merge("--channel=" => repo) - channels = options[:channels].collect {|channel| ["--channel=", channel]} + run!(cmd, :params => params) + end + alias_method :subscribe, :enable_channel + alias_method :enable_repo, :enable_channel - params = {} - params["--user="] = options[:username] - params["--password="] = options[:password] - params = params.to_a + channels + def disable_channel(repo, options) + cmd = "rhn-channel -r" + params = user_pwd(options).merge("--channel=" => repo) run!(cmd, :params => params) end + alias_method :disable_repo, :disable_channel - def subscribed_products + def enabled_channels cmd = "rhn-channel -l" + run!(cmd).output.split("\n").compact end + alias_method :enabled_repos, :enabled_channels + alias_method :subscribed_products, :enabled_channels + + def available_channels(options) + cmd = "rhn-channel -L" + params = user_pwd(options) + + run!(cmd, :params => params).output.chomp.split("\n").compact + end + + def all_repos(options) + available = available_channels_with_status(options) + merge_enabled_channels_with_status(available) + end private + def available_channels_with_status(options) + available_channels(options).collect { |ac| {:repo_id => ac, :enabled => false} } + end + + def merge_enabled_channels_with_status(available) + enabled_channels.each_with_object(available) do |enabled, all| + if repo = all.detect { |i| i[:repo_id] == enabled } + repo[:enabled] = true + else + all.push({:repo_id => enabled, :enabled => true}) + end + end + end + + def user_pwd(options) + raise ArgumentError, "username and password are required" if options[:username].blank? || options[:password].blank? + + {"--user=" => options[:username], "--password=" => options[:password]} + end + def systemid_file "/etc/sysconfig/rhn/systemid" end diff --git a/lib/linux_admin/registration_system/subscription_manager.rb b/lib/linux_admin/registration_system/subscription_manager.rb index d69d77b..5754991 100644 --- a/lib/linux_admin/registration_system/subscription_manager.rb +++ b/lib/linux_admin/registration_system/subscription_manager.rb @@ -80,6 +80,31 @@ def available_subscriptions parse_output(output).index_by {|i| i[:pool_id]} end + def enable_repo(repo, options = nil) + cmd = "subscription-manager repos" + params = {"--enable=" => repo} + + run!(cmd, :params => params) + end + + def disable_repo(repo, options = nil) + cmd = "subscription-manager repos" + params = {"--disable=" => repo} + + run!(cmd, :params => params) + end + + def all_repos(options = nil) + cmd = "subscription-manager repos" + output = run!(cmd).output + + parse_output(output) + end + + def enabled_repos + all_repos.select { |i| i[:enabled] }.collect { |r| r[:repo_id] } + end + private def parse_output(output) @@ -101,6 +126,7 @@ def parse_content(content) end def format_values(content_group) + content_group[:enabled] = content_group[:enabled].to_i == 1 if content_group[:enabled] 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 diff --git a/spec/data/rhn/output_rhn-channel_list_available b/spec/data/rhn/output_rhn-channel_list_available new file mode 100644 index 0000000..6f1fe81 --- /dev/null +++ b/spec/data/rhn/output_rhn-channel_list_available @@ -0,0 +1,4 @@ +rhel-x86_64-server-6-cf-me-2 +rhel-x86_64-server-6-cf-me-2-beta +rhel-x86_64-server-6-cf-me-3 +rhel-x86_64-server-6-cf-me-3-beta diff --git a/spec/data/subscription_manager/output_repos b/spec/data/subscription_manager/output_repos new file mode 100644 index 0000000..96e76c1 --- /dev/null +++ b/spec/data/subscription_manager/output_repos @@ -0,0 +1,18 @@ ++----------------------------------------------------------+ + Available Repositories in /etc/yum.repos.d/redhat.repo ++----------------------------------------------------------+ +Repo ID: some-repo-source-rpms +Repo Name: Some Repo (Source RPMs) +Repo URL: https://my.host.example.com/repos/some-repo/source/rpms +Enabled: 1 + +Repo ID: some-repo-rpms +Repo Name: Some Repo +Repo URL: https://my.host.example.com/repos/some-repo/rpms +Enabled: 1 + +Repo ID: some-repo-2-beta-rpms +Repo Name: Some Repo (Beta RPMs) +Repo URL: https://my.host.example.com/repos/some-repo-2/beta/rpms +Enabled: 0 + diff --git a/spec/rhn_spec.rb b/spec/rhn_spec.rb index 20c702a..d49ecd7 100644 --- a/spec/rhn_spec.rb +++ b/spec/rhn_spec.rb @@ -70,23 +70,64 @@ end end - context "#subscribe" do - it "without arguments" do - expect { described_class.new.subscribe({}) }.to raise_error(ArgumentError) - end + it "#enable_channel" do + described_class.any_instance.should_receive(:run!).once.with("rhn-channel -a", {:params=>{"--user="=>"SomeUser", "--password="=>"SomePass", "--channel="=>123}}) - 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", - :channels => [123, 456] - }) - end + described_class.new.enable_channel(123, :username => "SomeUser", :password => "SomePass") end - it "#subscribed_products" do + it "#enabled_channels" 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"]) + + expect(described_class.new.enabled_channels).to eq(["rhel-x86_64-server-6", "rhel-x86_64-server-6-cf-me-2"]) + end + + it "#disable_channel" do + described_class.any_instance.should_receive(:run!).once.with("rhn-channel -r", {:params=>{"--user="=>"SomeUser", "--password="=>"SomePass", "--channel="=>123}}) + + described_class.new.disable_channel(123, :username => "SomeUser", :password => "SomePass") + end + + it "#available_channels" do + credentials = { + :username => "some_user", + :password => "password" + } + expected = [ + "rhel-x86_64-server-6-cf-me-2", + "rhel-x86_64-server-6-cf-me-2-beta", + "rhel-x86_64-server-6-cf-me-3", + "rhel-x86_64-server-6-cf-me-3-beta" + ] + cmd = "rhn-channel -L" + params = { + :params => { + "--user=" => "some_user", + "--password=" => "password" + } + } + + described_class.any_instance.should_receive(:run!).once.with(cmd, params).and_return(double(:output => sample_output("rhn/output_rhn-channel_list_available"))) + + expect(described_class.new.available_channels(credentials)).to eq(expected) + end + + it "#all_repos" do + credentials = { + :username => "some_user", + :password => "password" + } + expected = [ + {:repo_id => "rhel-x86_64-server-6-cf-me-2", :enabled => true}, + {:repo_id => "rhel-x86_64-server-6-cf-me-2-beta", :enabled => false}, + {:repo_id => "rhel-x86_64-server-6-cf-me-3", :enabled => false}, + {:repo_id => "rhel-x86_64-server-6-cf-me-3-beta", :enabled => false}, + {:repo_id => "rhel-x86_64-server-6", :enabled => true} + ] + + described_class.any_instance.should_receive(:run!).once.and_return(double(:output => sample_output("rhn/output_rhn-channel_list_available"))) + 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.all_repos(credentials)).to eq(expected) end -end \ No newline at end of file +end diff --git a/spec/subscription_manager_spec.rb b/spec/subscription_manager_spec.rb index 155a7a9..6868aa0 100644 --- a/spec/subscription_manager_spec.rb +++ b/spec/subscription_manager_spec.rb @@ -144,4 +144,51 @@ expect { described_class.new.organizations({:username=>"BadUser", :password=>"BadPass"}) }.to raise_error(LinuxAdmin::CredentialError) end end + + it "#enable_repo" do + described_class.any_instance.should_receive(:run!).once.with("subscription-manager repos", {:params=>{"--enable="=>"abc"}}) + + described_class.new.enable_repo("abc") + end + + it "#disable_repo" do + described_class.any_instance.should_receive(:run!).once.with("subscription-manager repos", {:params=>{"--disable="=>"abc"}}) + + described_class.new.disable_repo("abc") + end + + it "#all_repos" do + expected = [ + { + :repo_id => "some-repo-source-rpms", + :repo_name => "Some Repo (Source RPMs)", + :repo_url => "https://my.host.example.com/repos/some-repo/source/rpms", + :enabled => true + }, + { + :repo_id => "some-repo-rpms", + :repo_name => "Some Repo", + :repo_url => "https://my.host.example.com/repos/some-repo/rpms", + :enabled => true + }, + { + :repo_id => "some-repo-2-beta-rpms", + :repo_name => "Some Repo (Beta RPMs)", + :repo_url => "https://my.host.example.com/repos/some-repo-2/beta/rpms", + :enabled => false + } + ] + + described_class.any_instance.should_receive(:run!).once.with("subscription-manager repos").and_return(double(:output => sample_output("subscription_manager/output_repos"))) + + expect(described_class.new.all_repos).to eq(expected) + end + + it "#enabled_repos" do + expected = ["some-repo-source-rpms", "some-repo-rpms"] + + described_class.any_instance.should_receive(:run!).once.with("subscription-manager repos").and_return(double(:output => sample_output("subscription_manager/output_repos"))) + + expect(described_class.new.enabled_repos).to eq(expected) + end end