Skip to content

Commit

Permalink
Fixes #12633 - Pxegrub2 variant and multiple configs
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed May 5, 2016
1 parent 4f0fafb commit 15e9e59
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 44 deletions.
77 changes: 46 additions & 31 deletions modules/tftp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@ class Server
# Creates TFTP pxeconfig file
def set mac, config
raise "Invalid parameters received" if mac.nil? || config.nil?

FileUtils.mkdir_p pxeconfig_dir

File.open(pxeconfig_file(mac), 'w') {|f| f.write(config) }
logger.info "TFTP: entry for #{mac} created successfully"
pxeconfig_file(mac).each do |file|
File.open(file, 'w') {|f| f.write(config) }
logger.info "TFTP: #{file} created successfully"
end
end

# Removes pxeconfig files
def del mac
file = pxeconfig_file(mac)
if File.exist?(file)
FileUtils.rm_f file
logger.debug "TFTP: entry for #{mac} removed successfully"
else
logger.info "TFTP: Skipping a request to delete a file which doesn't exists"
pxeconfig_file(mac).each do |file|
if File.exist?(file)
FileUtils.rm_f file
logger.info "TFTP: #{file} removed successfully"
else
logger.warn "TFTP: Skipping a request to delete a file which doesn't exists"
end
end
end

# Gets the contents of a pxeconfig file
def get mac
file = pxeconfig_file(mac)
if File.exist?(file)
config = File.open(pxeconfig_file(mac), 'r') {|f| f.readlines }
logger.debug "TFTP: entry for #{mac} read successfully"
else
logger.info "TFTP: Skipping a request to read a file which doesn't exists"
raise "File #{file} not found"
config = nil
pxeconfig_file(mac).each do |file|
if File.exist?(file)
config = File.open(file, 'r') {|f| f.readlines }
logger.debug "TFTP: #{file} read successfully"
else
raise "File #{file} not found"
end
end
config
end
Expand All @@ -42,14 +44,16 @@ def get mac
def create_default config
raise "Default config not supplied" if config.nil?

FileUtils.mkdir_p File.dirname pxe_default
File.open(pxe_default, 'w') {|f| f.write(config) }
logger.info "TFTP: #{pxe_default} entry created successfully"
pxe_default.each do |file|
FileUtils.mkdir_p File.dirname file
File.open(file, 'w') {|f| f.write(config) }
logger.info "TFTP: #{file} entry created successfully"
end
end

protected

# returns the absolute path
# TODO:
def path(p = nil)
p ||= Proxy::TFTP::Plugin.settings.tftproot
return (p =~ /^\//) ? p : Pathname.new(File.expand_path(File.dirname(__FILE__))).join(p).to_s
Expand All @@ -61,22 +65,34 @@ def pxeconfig_dir
"#{path}/pxelinux.cfg"
end
def pxe_default
"#{pxeconfig_dir}/default"
["#{pxeconfig_dir}/default"]
end
def pxeconfig_file mac
"#{pxeconfig_dir}/01-"+mac.gsub(/:/,"-").downcase
["#{pxeconfig_dir}/01-"+mac.gsub(/:/,"-").downcase]
end
end

class Pxegrub < Server
def pxeconfig_dir
path.to_s
"#{path}/grub"
end
def pxe_default
["#{pxeconfig_dir}/menu.lst", "#{pxeconfig_dir}/efidefault"]
end
def pxeconfig_file mac
["#{pxeconfig_dir}/menu.lst.01"+mac.gsub(/:/,"").upcase, "#{pxeconfig_dir}/01-"+mac.gsub(/:/,'-').upcase]
end
end

class Pxegrub2 < Server
def pxeconfig_dir
"#{path}/grub2"
end
def pxe_default
"#{pxeconfig_dir}/boot/grub/menu.lst"
["#{pxeconfig_dir}/grub.cfg"]
end
def pxeconfig_file mac
"#{pxeconfig_dir}/menu.lst.01"+mac.gsub(/:/,"").upcase
["#{pxeconfig_dir}/grub.cfg-"+mac.gsub(/:/,'-').downcase]
end
end

Expand All @@ -85,10 +101,10 @@ def pxeconfig_dir
"#{path}/ztp.cfg"
end
def pxe_default
pxeconfig_dir
[pxeconfig_dir]
end
def pxeconfig_file mac
"#{pxeconfig_dir}/"+mac.gsub(/:/,"").upcase
["#{pxeconfig_dir}/"+mac.gsub(/:/,"").upcase]
end
end

Expand All @@ -97,15 +113,14 @@ def pxeconfig_dir
"#{path}/poap.cfg"
end
def pxe_default
pxeconfig_dir
[pxeconfig_dir]
end
def pxeconfig_file mac
"#{pxeconfig_dir}/"+mac.gsub(/:/,"").upcase
["#{pxeconfig_dir}/"+mac.gsub(/:/,"").upcase]
end
end

def self.fetch_boot_file dst, src

filename = boot_filename(dst, src)
destination = Pathname.new(File.expand_path(filename, Proxy::TFTP::Plugin.settings.tftproot)).cleanpath
tftproot = Pathname.new(Proxy::TFTP::Plugin.settings.tftproot).cleanpath
Expand Down
2 changes: 0 additions & 2 deletions modules/tftp/tftp_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ def create_default variant
create_default "syslinux"
end

# Create a new TFTP reservation
post "/:mac" do |mac|
create "syslinux", mac
end

# Delete a record from a network
delete("/:mac") do |mac|
delete "syslinux", mac
end
Expand Down
89 changes: 78 additions & 11 deletions test/tftp/tftp_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,64 @@ def test_api_can_fetch_boot_file
end

def test_api_can_create_syslinux_tftp_reservation
mac = "aa:bb:cc:00:11:22"
mac_filename = "aa-bb-cc-dd-ee-ff-00-11-22"
mac = "aa:bb:cc:dd:ee:ff"
FileUtils.stubs(:mkdir_p).returns(true)
File.stubs(:open).with("/some/root/pxelinux.cfg/#{mac_filename}", 'w').returns(true)
Proxy::TFTP::Syslinux.any_instance.expects(:set).with(mac, args[:pxeconfig])
post "/#{mac}", args
file_handle = mock
file_handle.expects(:write).with(args[:pxeconfig]).once
File.expects(:open).with("/some/root/pxelinux.cfg/01-aa-bb-cc-dd-ee-ff", 'w').yields(file_handle).once
post "/syslinux/#{mac}", args
assert last_response.ok?
end

def test_api_can_create_pxegrub_tftp_reservation
mac = "aa:bb:cc:dd:ee:ff"
FileUtils.stubs(:mkdir_p).returns(true)
file_handle = mock
file_handle.stubs(:write).with(args[:pxeconfig])
File.expects(:open).with("/some/root/grub/menu.lst.01AABBCCDDEEFF", 'w').yields(file_handle).once
File.expects(:open).with("/some/root/grub/01-AA-BB-CC-DD-EE-FF", 'w').yields(file_handle).once
post "/pxegrub/#{mac}", args
assert last_response.ok?
end

def test_api_can_create_pxegrub2_tftp_reservation
mac = "aa:bb:cc:dd:ee:ff"
FileUtils.stubs(:mkdir_p).returns(true)
file_handle = mock
file_handle.expects(:write).with(args[:pxeconfig]).once
File.expects(:open).with("/some/root/grub2/grub.cfg-aa-bb-cc-dd-ee-ff", 'w').yields(file_handle).once
post "/pxegrub2/#{mac}", args
assert last_response.ok?
end

def test_api_can_create_ztp_tftp_reservation
mac = "aa:bb:cc:dd:ee:ff"
FileUtils.stubs(:mkdir_p).returns(true)
file_handle = mock
file_handle.expects(:write).with(args[:pxeconfig]).once
File.expects(:open).with("/some/root/ztp.cfg/AABBCCDDEEFF", 'w').yields(file_handle).once
post "/ztp/#{mac}", args
assert last_response.ok?
end

def test_api_can_create_poap_tftp_reservation
mac = "aa:bb:cc:dd:ee:ff"
FileUtils.stubs(:mkdir_p).returns(true)
file_handle = mock
file_handle.expects(:write).with(args[:pxeconfig]).once
File.expects(:open).with("/some/root/poap.cfg/AABBCCDDEEFF", 'w').yields(file_handle).once
post "/poap/#{mac}", args
assert last_response.ok?
end

def test_api_can_create_syslinux_tftp_reservation_for_64bit_mac
mac = "aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd"
mac_filename = "aa-bb-cc-dd-ee-ff-00-11-22-33-44-55-66-77-88-99-aa-bb-cc-dd"
FileUtils.stubs(:mkdir_p).returns(true)
File.stubs(:open).with("/some/root/pxelinux.cfg/#{mac_filename}", 'w').returns(true)
Proxy::TFTP::Syslinux.any_instance.expects(:set).with(mac, args[:pxeconfig])
post "/#{mac}", args
file_handle = mock
file_handle.expects(:write).with(args[:pxeconfig]).once
File.expects(:open).with("/some/root/pxelinux.cfg/01-#{mac_filename}", 'w').yields(file_handle).once
post "/syslinux/#{mac}", args
assert last_response.ok?
end

Expand All @@ -51,10 +93,35 @@ def test_api_returns_error_when_invalid_mac
assert_equal "Invalid MAC address: aa:bb:cc:00:11:zz", last_response.body
end

def test_api_can_create_default
def test_api_can_create_syslinux_default
params = { :menu => "foobar" }
FileUtils.stubs(:mkdir_p).returns(true)
file_handle = mock
file_handle.expects(:write).with(params[:menu]).once
File.expects(:open).with("/some/root/pxelinux.cfg/default", 'w').yields(file_handle).once
post "/syslinux/create_default", params
assert last_response.ok?
end

def test_api_can_create_pxegrub_default
params = { :menu => "foobar" }
Proxy::TFTP::Syslinux.any_instance.expects(:create_default).with(params[:menu])
post "/create_default", params
FileUtils.stubs(:mkdir_p).returns(true)
file_handle = mock
file_handle.stubs(:write).with(params[:menu])
File.expects(:open).with("/some/root/grub/menu.lst", 'w').yields(file_handle).once
File.expects(:open).with("/some/root/grub/efidefault", 'w').yields(file_handle).once
post "/pxegrub/create_default", params
assert last_response.ok?
end

def test_api_can_create_pxegrub2_default
params = { :menu => "foobar" }
FileUtils.stubs(:mkdir_p).returns(true)
file_handle = mock
file_handle.expects(:write).with(params[:menu]).once
File.expects(:open).with("/some/root/grub2/grub.cfg", 'w').yields(file_handle).once
post "/pxegrub2/create_default", params
assert last_response.ok?
end

private
Expand Down

0 comments on commit 15e9e59

Please sign in to comment.