Revision 0c3e15d2
Added by Ohad Levy over 12 years ago
- ID 0c3e15d210a01b712b35ef07ccce31925ec08cd6
app/models/orchestration/dhcp.rb | ||
---|---|---|
|
||
def dhcp_record
|
||
return unless dhcp?
|
||
@dhcp_record ||= Net::DhcpRecord.new dhcp_attrs
|
||
@dhcp_record ||= jumpstart? ? Net::DHCP::SparcRecord.new(dhcp_attrs) : Net::DHCP::Record.new(dhcp_attrs)
|
||
end
|
||
|
||
def sp_dhcp_record
|
||
return unless sp_dhcp?
|
||
@sp_dhcp_record ||= Net::DhcpRecord.new sp_dhcp_attrs
|
||
@sp_dhcp_record ||= Net::DHCP::Record.new sp_dhcp_attrs
|
||
end
|
||
|
||
protected
|
app/models/solaris.rb | ||
---|---|---|
ipath = interpolate_medium_vars(host.medium.media_dir, host.architecture.name, self)
|
||
|
||
{
|
||
"<#{vendor}>root_server_ip" => server_ip, # 192.168.216.241
|
||
"<#{vendor}>root_server_hostname" => server_name, # mediahost
|
||
"<#{vendor}>root_path_name" => "#{ipath}/Solaris_#{minor}/Tools/Boot", # /vol/solgi_5.10/sol10_hw0910/Solaris_10/Tools/Boot
|
||
"<#{vendor}>install_server_ip" => server_ip, # 192.168.216.241
|
||
"<#{vendor}>install_server_name" => server_name, # mediahost
|
||
"<#{vendor}>install_path" => ipath, # /vol/solgi_5.10/sol10_hw0910
|
||
"<#{vendor}>sysid_server_path" => "#{jpath}/sysidcfg/sysidcfg_primary", # 192.168.216.241:/vol/jumpstart/sysidcfg/sysidcfg_primary
|
||
"<#{vendor}>jumpstart_server_path" => jpath, # 192.168.216.241:/vol/jumpstart
|
||
:vendor => "<#{vendor}>",
|
||
:root_server_ip => server_ip, # 192.168.216.241
|
||
:root_server_hostname => server_name, # mediahost
|
||
:root_path_name => "#{ipath}/Solaris_#{minor}/Tools/Boot", # /vol/solgi_5.10/sol10_hw0910/Solaris_10/Tools/Boot
|
||
:install_server_ip => server_ip, # 192.168.216.241
|
||
:install_server_name => server_name, # mediahost
|
||
:install_path => ipath, # /vol/solgi_5.10/sol10_hw0910
|
||
:sysid_server_path => "#{jpath}/sysidcfg/sysidcfg_primary", # 192.168.216.241:/vol/jumpstart/sysidcfg/sysidcfg_primary
|
||
:jumpstart_server_path => jpath, # 192.168.216.241:/vol/jumpstart
|
||
}
|
||
end
|
||
|
lib/net.rb | ||
---|---|---|
module Net
|
||
autoload :DhcpRecord, "net/dhcp_record.rb"
|
||
|
||
class Record
|
||
include Net::Validations
|
||
attr_accessor :hostname, :proxy, :logger
|
lib/net/dhcp.rb | ||
---|---|---|
module Net
|
||
module DHCP
|
||
autoload :Record, "net/dhcp/record.rb"
|
||
autoload :SparcRecord, "net/dhcp/sparc_record.rb"
|
||
end
|
||
end
|
lib/net/dhcp/record.rb | ||
---|---|---|
module Net::DHCP
|
||
class Record < Net::Record
|
||
attr_accessor :ip, :mac, :network, :nextServer, :filename
|
||
|
||
def initialize opts = { }
|
||
super(opts)
|
||
self.mac = validate_mac self.mac
|
||
self.network = validate_network self.network
|
||
self.ip = validate_ip self.ip
|
||
end
|
||
|
||
def to_s
|
||
"#{hostname}-#{mac}/#{ip}"
|
||
end
|
||
|
||
# Deletes the DHCP entry
|
||
def destroy
|
||
logger.debug "Delete DHCP reservation for #{to_s}"
|
||
# it is safe to call destroy even if the entry does not exists, so we don't bother with validating anything here.
|
||
proxy.delete network, mac
|
||
end
|
||
|
||
# Create a DHCP entry
|
||
def create
|
||
logger.debug "Create DHCP reservation for #{to_s}"
|
||
begin
|
||
proxy.set network, attrs
|
||
rescue RestClient::Conflict
|
||
logger.warn "Conflicting DHCP reservation for #{to_s} detected"
|
||
e = Net::Conflict.new
|
||
e.type = "dhcp"
|
||
e.expected = to_s
|
||
e.actual = conflicts
|
||
e.message = "DHCP conflict detected - expected #{to_s}, found #{conflicts.map(&:to_s).join(', ')}"
|
||
raise e
|
||
end
|
||
end
|
||
|
||
# Returns an array of record objects which are conflicting with our own
|
||
def conflicts
|
||
@conflicts ||= [proxy.record(network, mac), proxy.record(network, ip)].delete_if { |c| c == self }.compact
|
||
end
|
||
|
||
# Verifies that are record already exists on the dhcp server
|
||
def valid?
|
||
logger.debug "Fetching DHCP reservation for #{to_s}"
|
||
self == proxy.record(network, mac)
|
||
end
|
||
|
||
def attrs
|
||
{ :hostname => hostname, :mac => mac, :ip => ip, :network => network,
|
||
:nextServer => nextServer, :filename => filename
|
||
}.delete_if { |k, v| v.nil? }
|
||
end
|
||
end
|
||
end
|
lib/net/dhcp/sparc_record.rb | ||
---|---|---|
module Net::DHCP
|
||
class SparcRecord < Record
|
||
attr_accessor :vendor, :root_path_name, :sysid_server_path,
|
||
:install_server_name, :install_server_ip, :jumpstart_server_path,
|
||
:root_server_hostname, :root_server_ip, :install_path
|
||
|
||
def initialize opts = { }
|
||
super(opts)
|
||
raise "Must define a dhcp vendor" if vendor.blank?
|
||
end
|
||
|
||
def attrs
|
||
super.merge({
|
||
"#{vendor}root_path_name" => root_path_name,
|
||
"#{vendor}sysid_server_path" => sysid_server_path,
|
||
"#{vendor}install_server_ip" => install_server_ip,
|
||
"#{vendor}jumpstart_server_path" => jumpstart_server_path,
|
||
"#{vendor}install_server_name" => install_server_name,
|
||
"#{vendor}root_server_hostname" => root_server_hostname,
|
||
"#{vendor}root_server_ip" => root_server_ip,
|
||
"#{vendor}install_path" => install_path
|
||
}).delete_if { |k, v| v.nil? }
|
||
end
|
||
end
|
||
end
|
lib/net/dhcp_record.rb | ||
---|---|---|
module Net
|
||
class DhcpRecord < Record
|
||
attr_accessor :ip, :mac, :network, :nextServer, :filename
|
||
|
||
def initialize opts = { }
|
||
super(opts)
|
||
self.mac = validate_mac self.mac
|
||
self.network = validate_network self.network
|
||
self.ip = validate_ip self.ip
|
||
end
|
||
|
||
def to_s
|
||
"#{hostname}-#{mac}/#{ip}"
|
||
end
|
||
|
||
# Deletes the DHCP entry
|
||
def destroy
|
||
logger.debug "Delete DHCP reservation for #{to_s}"
|
||
# it is safe to call destroy even if the entry does not exists, so we don't bother with validating anything here.
|
||
proxy.delete network, mac
|
||
end
|
||
|
||
# Create a DHCP entry
|
||
def create
|
||
logger.debug "Create DHCP reservation for #{to_s}"
|
||
begin
|
||
proxy.set network, attrs
|
||
rescue RestClient::Conflict
|
||
logger.warn "Conflicting DHCP reservation for #{to_s} detected"
|
||
e = Net::Conflict.new
|
||
e.type = "dhcp"
|
||
e.expected = to_s
|
||
e.actual = conflicts
|
||
e.message = "DHCP conflict detected - expected #{to_s}, found #{conflicts.map(&:to_s).join(', ')}"
|
||
raise e
|
||
end
|
||
end
|
||
|
||
# Returns an array of record objects which are conflicting with our own
|
||
def conflicts
|
||
@conflicts ||= [proxy.record(network, mac), proxy.record(network, ip)].delete_if { |c| c == self }.compact
|
||
end
|
||
|
||
# Verifies that are record already exists on the dhcp server
|
||
def valid?
|
||
logger.debug "Fetching DHCP reservation for #{to_s}"
|
||
self == proxy.record(network, mac)
|
||
end
|
||
|
||
def attrs
|
||
{ :hostname => hostname, :mac => mac, :ip => ip, :network => network,
|
||
:nextServer => nextServer, :filename => filename
|
||
}.delete_if { |k, v| v.nil? }
|
||
end
|
||
end
|
||
end
|
lib/proxy_api.rb | ||
---|---|---|
# Returns : Hash or false
|
||
def record subnet, mac
|
||
response = parse(get("#{subnet}/#{mac}"))
|
||
Net::DhcpRecord.new response.merge(:network => subnet, :proxy => self)
|
||
attrs = response.merge(:network => subnet, :proxy => self)
|
||
if response.keys.grep(/Sun/i).empty?
|
||
Net::DHCP::SparcRecord.new attrs
|
||
else
|
||
Net::DHCP::Record.new attrs
|
||
end
|
||
rescue RestClient::ResourceNotFound
|
||
false
|
||
nil
|
||
end
|
||
|
||
# Sets a DHCP entry
|
test/lib/net/dhcp_test.rb | ||
---|---|---|
|
||
test "dhcp record should not be created without a mac" do
|
||
assert_raise Net::Validations::Error do
|
||
Net::DhcpRecord.new :name => "test", "proxy" => smart_proxies(:one)
|
||
Net::DHCP::Record.new :hostname => "test", "proxy" => smart_proxies(:one)
|
||
end
|
||
end
|
||
|
||
test "dhcp record should not be created without a network" do
|
||
assert_raise Net::Validations::Error do
|
||
Net::DhcpRecord.new :name => "test", :mac => "aa:bb:cc:dd:ee:ff", "proxy" => smart_proxies(:one)
|
||
Net::DHCP::Record.new :hostname => "test", :mac => "aa:bb:cc:dd:ee:ff", "proxy" => smart_proxies(:one)
|
||
end
|
||
end
|
||
|
||
test "dhcp record should not be created without an ip" do
|
||
assert_raise Net::Validations::Error do
|
||
Net::DhcpRecord.new :name => "test", :mac => "aa:bb:cc:dd:ee:ff", :network => "127.0.0.0", "proxy" => smart_proxies(:one)
|
||
Net::DHCP::Record.new :hostname => "test", :mac => "aa:bb:cc:dd:ee:ff", :network => "127.0.0.0", "proxy" => smart_proxies(:one)
|
||
end
|
||
end
|
||
|
||
test "record should have dhcp attributes" do
|
||
record = Net::DhcpRecord.new(:name => "test", :mac => "aa:bb:cc:dd:ee:ff",
|
||
record = Net::DHCP::Record.new(:hostname => "test", :mac => "aa:bb:cc:dd:ee:ff",
|
||
:network => "127.0.0.0", :ip => "127.0.0.1", "proxy" => smart_proxies(:one))
|
||
assert_equal({:name => "test", :mac => "aa:bb:cc:dd:ee:ff",:network => "127.0.0.0", :ip => "127.0.0.1"}, record.send(:attrs))
|
||
assert_equal({:hostname => "test", :mac => "aa:bb:cc:dd:ee:ff",:network => "127.0.0.0", :ip => "127.0.0.1"}, record.send(:attrs))
|
||
|
||
end
|
||
|
||
test "record should be equal if their attrs are the same" do
|
||
record1 = Net::DhcpRecord.new(:name => "test", :mac => "aa:bb:cc:dd:ee:ff",
|
||
record1 = Net::DHCP::Record.new(:hostname => "test", :mac => "aa:bb:cc:dd:ee:ff",
|
||
:network => "127.0.0.0", :ip => "127.0.0.1", "proxy" => smart_proxies(:one))
|
||
record2 = Net::DhcpRecord.new(:name => "test", :mac => "aa:bb:cc:dd:ee:ff",
|
||
record2 = Net::DHCP::Record.new(:hostname => "test", :mac => "aa:bb:cc:dd:ee:ff",
|
||
:network => "127.0.0.0", :ip => "127.0.0.1", "proxy" => smart_proxies(:one))
|
||
assert_equal record1, record2
|
||
end
|
test/lib/net/net_test.rb | ||
---|---|---|
class NetTest < ActiveSupport::TestCase
|
||
|
||
test "Net record should auto assign attributes" do
|
||
record = Net::Record.new :name => "test", "proxy" => smart_proxies(:one)
|
||
assert_equal "test", record.name
|
||
record = Net::Record.new :hostname => "test", "proxy" => smart_proxies(:one)
|
||
assert_equal "test", record.hostname
|
||
end
|
||
|
||
test "should have a logger" do
|
||
record = Net::Record.new :name => "test", "proxy" => smart_proxies(:one)
|
||
record = Net::Record.new :hostname => "test", "proxy" => smart_proxies(:one)
|
||
assert_not_nil record.logger
|
||
end
|
||
|
||
test "should default logger to rails logger" do
|
||
record = Net::Record.new :name => "test", "proxy" => smart_proxies(:one)
|
||
record = Net::Record.new :hostname => "test", "proxy" => smart_proxies(:one)
|
||
assert_equal logger, record.logger
|
||
end
|
||
end
|
test/unit/orchestration/dhcp_test.rb | ||
---|---|---|
h = hosts(:one)
|
||
assert h.valid?
|
||
assert h.dhcp?
|
||
assert_instance_of Net::DhcpRecord, h.dhcp_record
|
||
assert_instance_of Net::DHCP::Record, h.dhcp_record
|
||
end
|
||
end
|
||
|
||
... | ... | |
Resolv::DNS.any_instance.stubs(:getaddress).with("brsla01.yourdomain.net").returns("2.3.4.5").once
|
||
#User.current = users(:admin)
|
||
result = h.os.jumpstart_params h, h.model.vendor_class
|
||
assert_equal result, {"<Sun-Fire-V210>install_path" => "/vol/solgi_5.10/sol10_hw0910_sparc",
|
||
"<Sun-Fire-V210>install_server_ip" => "2.3.4.5",
|
||
"<Sun-Fire-V210>install_server_name" => "brsla01",
|
||
"<Sun-Fire-V210>jumpstart_server_path" => "2.3.4.5:/vol/jumpstart",
|
||
"<Sun-Fire-V210>root_path_name" => "/vol/solgi_5.10/sol10_hw0910_sparc/Solaris_10/Tools/Boot",
|
||
"<Sun-Fire-V210>root_server_hostname" => "brsla01",
|
||
"<Sun-Fire-V210>root_server_ip" => "2.3.4.5",
|
||
"<Sun-Fire-V210>sysid_server_path" => "2.3.4.5:/vol/jumpstart/sysidcfg/sysidcfg_primary"
|
||
}
|
||
assert_equal result, {
|
||
:vendor => "<Sun-Fire-V210>",
|
||
:install_path => "/vol/solgi_5.10/sol10_hw0910_sparc",
|
||
:install_server_ip => "2.3.4.5",
|
||
:install_server_name => "brsla01",
|
||
:jumpstart_server_path => "2.3.4.5:/vol/jumpstart",
|
||
:root_path_name => "/vol/solgi_5.10/sol10_hw0910_sparc/Solaris_10/Tools/Boot",
|
||
:root_server_hostname => "brsla01",
|
||
:root_server_ip => "2.3.4.5",
|
||
:sysid_server_path => "2.3.4.5:/vol/jumpstart/sysidcfg/sysidcfg_primary"
|
||
}
|
||
end
|
||
|
||
end
|
Also available in: Unified diff
fixes #1186 foreman does not forward sparc dhcp vendor options when creating a sparc Solaris host