diff --git a/lib/linux_admin/common.rb b/lib/linux_admin/common.rb index 50c0124..89ebc13 100644 --- a/lib/linux_admin/common.rb +++ b/lib/linux_admin/common.rb @@ -33,14 +33,14 @@ def run(cmd, options = {}) private def sanitize(params) - return {} if params.blank? - params.each_with_object({}) do |(k, v), h| - h[k] = - case v - when Array; v.collect {|s| s.shellescape} - when NilClass; v - else v.shellescape - end + return [] if params.blank? + params.collect do |k, v| + v = case v + when Array; v.collect(&:shellescape) + when NilClass; v + else v.to_s.shellescape + end + [k, v] end end diff --git a/lib/linux_admin/rhn.rb b/lib/linux_admin/rhn.rb index 5bb752d..c686706 100644 --- a/lib/linux_admin/rhn.rb +++ b/lib/linux_admin/rhn.rb @@ -14,5 +14,40 @@ def self.registered? end id.length > 0 end + + def self.register(options) + cmd = "rhnreg_ks" + params = {} + + if options[:activationkey] + params["--activationkey="] = options[:activationkey] + elsif options[:username] && options[:password] + params["--username="] = options[:username] + params["--password="] = options[:password] + else + raise ArgumentError, "activation key or username and password are required" + end + + params["--proxy="] = options[:proxy_address] if options[:proxy_address] + params["--proxyUser="] = options[:proxy_username] if options[:proxy_username] + params["--proxyPassword="] = options[:proxy_password] if options[:proxy_password] + params["--serverUrl="] = options[:server_url] if options[:server_url] + + run(cmd, :params => params) + end + + def self.subscribe(options) + raise ArgumentError, "pools, username and password are required" if options[:pools].blank? || options[:username].blank? || options[:password].blank? + cmd = "rhn-channel -a" + + pools = options[:pools].collect {|pool| ["--channel=", pool]} + + params = {} + params["--user="] = options[:username] + params["--password="] = options[:password] + params = params.to_a + pools + + run(cmd, :params => params) + end end end \ No newline at end of file diff --git a/lib/linux_admin/subscription_manager.rb b/lib/linux_admin/subscription_manager.rb index 0942e57..dc44e2e 100644 --- a/lib/linux_admin/subscription_manager.rb +++ b/lib/linux_admin/subscription_manager.rb @@ -10,28 +10,24 @@ def self.refresh run("subscription-manager refresh") end - def self.register(options = {}) + def self.register(options) raise ArgumentError, "username and password are required" unless options[:username] && options[:password] cmd = "subscription-manager register" - params = {} - if options[:username] && options[:password] - params["--username="] = options[:username] - params["--password="] = options[:password] - end - params["--org="] = options[:org] if options[:org] && options[:server_url] - params["--proxy="] = options[:proxy_address] if options[:proxy_address] - params["--proxyuser="] = options[:proxy_username] if options[:proxy_username] - params["--proxypassword="] = options[:proxy_password] if options[:proxy_password] - params["--serverurl="] = options[:server_url] if options[:server_url] + params = {"--username=" => options[:username], "--password=" => options[:password]} + params.merge!(proxy_params(options)) + params["--org="] = options[:org] if options[:server_url] && options[:org] + params["--serverurl="] = options[:server_url] if options[:server_url] run(cmd, :params => params) end - def self.subscribe(pool_id) - params = {"--pool" => pool_id} + def self.subscribe(options) + cmd = "subscription-manager attach" + pools = options[:pools].collect {|pool| ["--pool", pool]} + params = proxy_params(options).to_a + pools - run("subscription-manager attach", :params => params) + run(cmd, :params => params) end def self.available_subscriptions @@ -51,5 +47,15 @@ def self.available_subscriptions subscriptions_hash[hash[:pool_id]] = hash end end + + private + + def self.proxy_params(options) + config = {} + config["--proxy="] = options[:proxy_address] if options[:proxy_address] + config["--proxyuser="] = options[:proxy_username] if options[:proxy_username] + config["--proxypassword="] = options[:proxy_password] if options[:proxy_password] + config + end end end \ No newline at end of file diff --git a/spec/common_spec.rb b/spec/common_spec.rb index a0c7558..e11b579 100644 --- a/spec/common_spec.rb +++ b/spec/common_spec.rb @@ -21,6 +21,10 @@ class TestClass } end + let (:modified_params) do + params.to_a + [123, 456].collect {|pool| ["--pool", pool]} + end + subject { TestClass } context ".write" do @@ -32,8 +36,8 @@ class TestClass context ".run" do context "with params" do it "sanitizes crazy params" do - subject.should_receive(:launch).once.with("true --user bob --pass P@\\$sw0\\^\\&\\ \\|\\<\\>/-\\+\\*d\\% --db --desc=Some\\ Description pkg1 some\\ pkg") - subject.run("true", :params => params, :return_exitstatus => true) + subject.should_receive(:launch).once.with("true --user bob --pass P@\\$sw0\\^\\&\\ \\|\\<\\>/-\\+\\*d\\% --db --desc=Some\\ Description pkg1 some\\ pkg --pool 123 --pool 456") + subject.run("true", :params => modified_params, :return_exitstatus => true) end it "as empty hash" do diff --git a/spec/rhn_spec.rb b/spec/rhn_spec.rb index 7eea79a..e813b4d 100644 --- a/spec/rhn_spec.rb +++ b/spec/rhn_spec.rb @@ -21,4 +21,48 @@ expect(described_class).to_not be_registered end end + + context ".register" do + it "no username or activation key" do + expect { described_class.register({}) }.to raise_error(ArgumentError) + end + + it "with username and password" do + described_class.should_receive(:run).once.with("rhnreg_ks", {:params=>{"--username="=>"SomeUser", "--password="=>"SomePass", "--proxy="=>"1.2.3.4", "--proxyUser="=>"ProxyUser", "--proxyPassword="=>"ProxyPass", "--serverUrl="=>"192.168.1.1"}}).and_return(0) + described_class.register( + :username => "SomeUser", + :password => "SomePass", + :proxy_address => "1.2.3.4", + :proxy_username => "ProxyUser", + :proxy_password => "ProxyPass", + :server_url => "192.168.1.1", + ) + end + + it "with activation key" do + described_class.should_receive(:run).once.with("rhnreg_ks", {:params=>{"--activationkey="=>"123abc", "--proxy="=>"1.2.3.4", "--proxyUser="=>"ProxyUser", "--proxyPassword="=>"ProxyPass", "--serverUrl="=>"192.168.1.1"}}).and_return(0) + described_class.register( + :activationkey => "123abc", + :proxy_address => "1.2.3.4", + :proxy_username => "ProxyUser", + :proxy_password => "ProxyPass", + :server_url => "192.168.1.1", + ) + end + end + + context ".subscribe" do + it "without arguments" do + expect { described_class.subscribe({}) }.to raise_error(ArgumentError) + end + + it "with pools" do + described_class.should_receive(:run).once.with("rhn-channel -a", {:params=>[["--user=", "SomeUser"], ["--password=", "SomePass"], ["--channel=", 123], ["--channel=", 456]]}) + described_class.subscribe({ + :username => "SomeUser", + :password => "SomePass", + :pools => [123, 456] + }) + end + end end \ No newline at end of file diff --git a/spec/subscription_manager_spec.rb b/spec/subscription_manager_spec.rb index 8a7d417..127ac40 100644 --- a/spec/subscription_manager_spec.rb +++ b/spec/subscription_manager_spec.rb @@ -38,8 +38,8 @@ end it ".subscribe" do - described_class.should_receive(:run).once - described_class.subscribe(nil) + described_class.should_receive(:run).once.with("subscription-manager attach", {:params=>[["--pool", 123], ["--pool", 456]]}) + described_class.subscribe({:pools => [123, 456]}) end it ".available_subscriptions" do