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
18 changes: 2 additions & 16 deletions lib/linux_admin.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'more_core_extensions/all'
require 'active_support/core_ext'

require 'linux_admin/registration_system'

require 'linux_admin/common'
require 'linux_admin/exceptions'
require 'linux_admin/rhn'
require 'linux_admin/rpm'
require 'linux_admin/subscription_manager'
require 'linux_admin/version'
require 'linux_admin/yum'

Expand All @@ -22,18 +22,4 @@
class LinuxAdmin
extend Common
include Common

def self.registered?
!!self.registration_type
end

def self.registration_type
if SubscriptionManager.registered?
SubscriptionManager
elsif Rhn.registered?
Rhn
else
nil
end
end
end
45 changes: 45 additions & 0 deletions lib/linux_admin/registration_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class LinuxAdmin
class RegistrationSystem < LinuxAdmin
def self.registration_type(reload = false)
return @registration_type if @registration_type && !reload
@registration_type = registration_type_uncached
end

def self.method_missing(meth, *args, &block)
if white_list_methods.include?(meth)
r = self.registration_type.new
raise NotImplementedError, "#{meth} not implemented for #{self.name}" unless r.respond_to?(meth)
r.send(meth, *args, &block)
else
super
end
end

def registered?
false
end

private

def self.registration_type_uncached
if SubscriptionManager.new.registered?
SubscriptionManager
elsif Rhn.new.registered?
Rhn
else
self
end
end
private_class_method :registration_type_uncached

def self.white_list_methods
@white_list_methods ||= begin
all_methods = RegistrationSystem.instance_methods(false) + Rhn.instance_methods(false) + SubscriptionManager.instance_methods(false)
all_methods.uniq
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be shortened to:

def self.white_list_methods
  all_methods = RegistrationSystem.instance_methods(false) + Rhn.instance_methods(false) + SubscriptionManager.instance_methods(false)
  all_methods.uniq
end

private_class_method :white_list_methods
end
end

Dir.glob(File.join(File.dirname(__FILE__), "registration_system", "*.rb")).each { |f| require f }
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
require 'nokogiri'

class LinuxAdmin
class Rhn < LinuxAdmin
def self.systemid_file
"/etc/sysconfig/rhn/systemid"
end

def self.registered?
class Rhn < RegistrationSystem
def registered?
id = ""
if File.exists?(systemid_file)
xml = Nokogiri.XML(File.read(systemid_file))
Expand All @@ -15,7 +11,7 @@ def self.registered?
id.length > 0
end

def self.register(options)
def register(options)
cmd = "rhnreg_ks"
params = {}

Expand All @@ -36,7 +32,7 @@ def self.register(options)
run(cmd, :params => params)
end

def self.subscribe(options)
def subscribe(options)
raise ArgumentError, "pools, username and password are required" if options[:pools].blank? || options[:username].blank? || options[:password].blank?
cmd = "rhn-channel -a"

Expand All @@ -49,5 +45,11 @@ def self.subscribe(options)

run(cmd, :params => params)
end

private

def systemid_file
"/etc/sysconfig/rhn/systemid"
end
end
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
require 'date'

class LinuxAdmin
class SubscriptionManager < LinuxAdmin
def self.registered?
class SubscriptionManager < RegistrationSystem

def validate_credentials(options)
!!organizations(options)
end

def registered?
run("subscription-manager identity", :return_exitstatus => true) == 0
end

def self.refresh
def refresh
run("subscription-manager refresh")
end

def self.organizations(options)
def organizations(options)
raise ArgumentError, "username and password are required" unless options[:username] && options[:password]
cmd = "subscription-manager orgs"

Expand All @@ -22,7 +27,7 @@ def self.organizations(options)
parse_output(output).index_by {|i| i[:name]}
end

def self.register(options)
def register(options)
raise ArgumentError, "username and password are required" unless options[:username] && options[:password]
cmd = "subscription-manager register"

Expand All @@ -34,29 +39,29 @@ def self.register(options)
run(cmd, :params => params)
end

def self.subscribe(options)
def subscribe(options)
cmd = "subscription-manager attach"
pools = options[:pools].collect {|pool| ["--pool", pool]}
params = proxy_params(options).to_a + pools

run(cmd, :params => params)
end

def self.available_subscriptions
def available_subscriptions
cmd = "subscription-manager list --all --available"
output = run(cmd, :return_output => true)
parse_output(output).index_by {|i| i[:pool_id]}
end

private

def self.parse_output(output)
def parse_output(output)
# Strip the 3 line header off the top
content = output.split("\n")[3..-1].join("\n")
parse_content(content)
end

def self.parse_content(content)
def parse_content(content)
# Break into content groupings by "\n\n" then process each grouping
content.split("\n\n").each_with_object([]) do |group, group_array|
group = group.split("\n").each_with_object({}) do |line, hash|
Expand All @@ -68,12 +73,12 @@ def self.parse_content(content)
end
end

def self.format_values(content_group)
def format_values(content_group)
content_group[:ends] = Date.strptime(content_group[:ends], "%m/%d/%Y") if content_group[:ends]
content_group
end

def self.proxy_params(options)
def proxy_params(options)
config = {}
config["--proxy="] = options[:proxy_address] if options[:proxy_address]
config["--proxyuser="] = options[:proxy_username] if options[:proxy_username]
Expand Down
38 changes: 0 additions & 38 deletions spec/linux_admin_spec.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
require 'spec_helper'

describe LinuxAdmin do
context ".registered?" do
it "when registered Subscription Manager" do
stub_registered_to_system(:sm)
expect(described_class.registered?).to be_true
end

it "when registered RHN" do
stub_registered_to_system(:rhn)
expect(described_class.registered?).to be_true
end

it "when unregistered" do
stub_registered_to_system(nil)
expect(described_class.registered?).to be_false
end
end

context ".registration_type" do
it "when registered Subscription Manager" do
stub_registered_to_system(:sm)
expect(described_class.registration_type).to eq(LinuxAdmin::SubscriptionManager)
end

it "when registered RHN" do
stub_registered_to_system(:rhn)
expect(described_class.registration_type).to eq(LinuxAdmin::Rhn)
end

it "when unregistered" do
stub_registered_to_system(nil)
expect(described_class.registration_type).to be_nil
end
end

def stub_registered_to_system(system)
described_class::SubscriptionManager.stub(:registered? => (system == :sm))
described_class::Rhn.stub(:registered? => (system == :rhn))
end
end
60 changes: 60 additions & 0 deletions spec/registration_system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'spec_helper'

describe LinuxAdmin::RegistrationSystem do
context ".registration_type" do
it "when registered Subscription Manager" do
stub_registered_to_system(:sm)
expect(described_class.registration_type).to eq(LinuxAdmin::SubscriptionManager)
end

it "when registered RHN" do
stub_registered_to_system(:rhn)
expect(described_class.registration_type).to eq(LinuxAdmin::Rhn)
end

it "when unregistered" do
stub_registered_to_system(nil)
expect(described_class.registration_type).to eq(described_class)
end

it "should memoize results" do
described_class.should_receive(:registration_type_uncached).once.and_call_original
described_class.registration_type
described_class.registration_type
end

it "with reload should refresh results" do
described_class.should_receive(:registration_type_uncached).twice.and_call_original
described_class.registration_type
described_class.registration_type(true)
end
end

it "#registered? when unregistered" do
stub_registered_to_system(nil)
expect(described_class.registered?).to be_false
end

context ".method_missing" do
before do
stub_registered_to_system(:rhn)
end

it "exists on the subclass" do
expect(LinuxAdmin::RegistrationSystem.registered?).to be_true
end

it "does not exist on the subclass" do
expect { LinuxAdmin::RegistrationSystem.organizations }.to raise_error(NotImplementedError)
end

it "is an unknown method" do
expect { LinuxAdmin::RegistrationSystem.method_does_not_exist }.to be_true
end
end

def stub_registered_to_system(system)
LinuxAdmin::SubscriptionManager.any_instance.stub(:registered? => (system == :sm))
LinuxAdmin::Rhn.any_instance.stub(:registered? => data_file_path("rhn/systemid")) if system == :rhn
end
end
38 changes: 17 additions & 21 deletions spec/rhn_spec.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
require 'spec_helper'

describe LinuxAdmin::Rhn do
it ".systemid_file" do
expect(described_class.systemid_file).to be_kind_of(String)
end

context ".registered?" do
context "#registered?" do
it "with registered system_id" do
described_class.stub(:systemid_file => data_file_path("rhn/systemid"))
expect(described_class).to be_registered
described_class.any_instance.stub(:systemid_file => data_file_path("rhn/systemid"))
expect(described_class.new).to be_registered
end

it "with unregistered system_id" do
described_class.stub(:systemid_file => data_file_path("rhn/systemid.missing_system_id"))
expect(described_class).to_not be_registered
described_class.any_instance.stub(:systemid_file => data_file_path("rhn/systemid.missing_system_id"))
expect(described_class.new).to_not be_registered
end

it "with missing systemid file" do
described_class.stub(:systemid_file => data_file_path("rhn/systemid.missing_file"))
expect(described_class).to_not be_registered
described_class.any_instance.stub(:systemid_file => data_file_path("rhn/systemid.missing_file"))
expect(described_class.new).to_not be_registered
end
end

context ".register" do
context "#register" do
it "no username or activation key" do
expect { described_class.register({}) }.to raise_error(ArgumentError)
expect { described_class.new.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(
described_class.any_instance.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.new.register(
:username => "SomeUser",
:password => "SomePass",
:proxy_address => "1.2.3.4",
Expand All @@ -40,8 +36,8 @@
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(
described_class.any_instance.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.new.register(
:activationkey => "123abc",
:proxy_address => "1.2.3.4",
:proxy_username => "ProxyUser",
Expand All @@ -51,14 +47,14 @@
end
end

context ".subscribe" do
context "#subscribe" do
it "without arguments" do
expect { described_class.subscribe({}) }.to raise_error(ArgumentError)
expect { described_class.new.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({
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]
Expand Down
Loading