Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 8 additions & 8 deletions lib/linux_admin/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions lib/linux_admin/rhn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 20 additions & 14 deletions lib/linux_admin/subscription_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 6 additions & 2 deletions spec/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions spec/rhn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/subscription_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down