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
54 changes: 40 additions & 14 deletions modules/tftp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ def set(mac, config)
pxeconfig_file(mac).each do |file|
write_file file, config
end

host_pxe_dir = File.join(path, 'host-config', dashed_mac(mac).downcase, host_pxe_dir_name)
FileUtils.mkdir_p(host_pxe_dir)

pxeconfig_file(mac).each do |path|
ln_from = File.join(host_pxe_dir, path.split('/').last)
ln_to = Pathname.new(path).relative_path_from(host_pxe_dir)

FileUtils.ln_s ln_to, ln_from, force: true
end

true
end

Expand All @@ -20,6 +31,9 @@ def del(mac)
pxeconfig_file(mac).each do |file|
delete_file file
end

delete_host_dir mac

true
end

Expand Down Expand Up @@ -66,6 +80,8 @@ def delete_file(file)

def delete_host_dir(mac)
host_dir = File.join(path, 'host-config', dashed_mac(mac).downcase)
return unless Dir.exist?(host_dir)

logger.debug "TFTP: Removing directory '#{host_dir}'."
FileUtils.rm_rf host_dir
end
Expand All @@ -80,7 +96,7 @@ def dashed_mac(mac)

class Syslinux < Server
def pxeconfig_dir
"#{path}/pxelinux.cfg"
File.join(path, 'pxelinux.cfg')
end

def pxe_default
Expand All @@ -90,6 +106,10 @@ def pxe_default
def pxeconfig_file(mac)
["#{pxeconfig_dir}/01-" + dashed_mac(mac).downcase]
end

def host_pxe_dir_name
'pxe'
end
end
class Pxelinux < Syslinux; end
class Pxegrub2 < Server
Expand All @@ -116,8 +136,6 @@ def bootloader_universe_symlinks(bootloader_path, pxeconfig_dir_mac)
end

def default_symlinks(bootfile_suffix, pxeconfig_dir_mac)
pxeconfig_dir = pxeconfig_dir()

grub_source = "grub#{bootfile_suffix}.efi"
shim_source = "shim#{bootfile_suffix}.efi"

Expand Down Expand Up @@ -167,11 +185,6 @@ def setup_bootloader(mac:, os:, release:, arch:, bootfile_suffix:)
create_symlinks(symlinks)
end

def del(mac)
super mac
delete_host_dir mac
end

def pxeconfig_dir(mac = nil)
if mac
File.join(path, 'host-config', dashed_mac(mac).downcase, 'grub2')
Expand All @@ -185,15 +198,15 @@ def pxe_default
end

def pxeconfig_file(mac)
pxeconfig_dir_mac = pxeconfig_dir(mac)
[
"#{pxeconfig_dir_mac}/grub.cfg",
"#{pxeconfig_dir_mac}/grub.cfg-01-#{dashed_mac(mac).downcase}",
"#{pxeconfig_dir_mac}/grub.cfg-#{mac.downcase}",
"#{pxeconfig_dir}/grub.cfg-01-" + dashed_mac(mac).downcase,
"#{pxeconfig_dir}/grub.cfg-#{mac.downcase}",
]
end

def host_pxe_dir_name
'grub2'
end
end

class Ztp < Server
Expand All @@ -208,6 +221,10 @@ def pxe_default
def pxeconfig_file(mac)
["#{pxeconfig_dir}/" + mac.delete(':').upcase, "#{pxeconfig_dir}/" + mac.delete(':').upcase + ".cfg"]
end

def host_pxe_dir_name
'ztp'
end
end

class Poap < Server
Expand All @@ -222,19 +239,28 @@ def pxe_default
def pxeconfig_file(mac)
["#{pxeconfig_dir}/" + mac.delete(':').upcase]
end

def host_pxe_dir_name
'poap'
end
end

class Ipxe < Server
def pxeconfig_dir
"#{path}/pxelinux.cfg"
File.join(path, 'pxelinux.cfg')
end

def pxe_default
["#{pxeconfig_dir}/default.ipxe"]
end

def pxeconfig_file(mac)
["#{pxeconfig_dir}/01-" + dashed_mac(mac).downcase + ".ipxe"]
file = "01-" + dashed_mac(mac).downcase + ".ipxe"
[File.join(pxeconfig_dir, file)]
end

def host_pxe_dir_name
'ipxe'
end
end

Expand Down
24 changes: 19 additions & 5 deletions test/tftp/tftp_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module TftpGenericServerSuite
def setup
@rootdir = "/some/root"
@rootdir = Dir.mktmpdir
@mac = "aa:bb:cc:dd:ee:ff"
@content = "file content"
Proxy::TFTP::Plugin.settings.stubs(:tftproot).returns(@rootdir)
Expand Down Expand Up @@ -100,6 +100,11 @@ def setup_paths
@pxe_config_files = ["pxelinux.cfg/01-aa-bb-cc-dd-ee-ff"]
@pxe_default_files = ["pxelinux.cfg/default"]
end

def test_symlinks_in_host_config_dir
@subject.set(@mac, @content)
assert_equal @content, File.read(File.join(@subject.path, 'host-config', @subject.dashed_mac(@mac).downcase, 'pxe', '01-aa-bb-cc-dd-ee-ff'))
end
end

class TftpPxegrub2ServerTest < Test::Unit::TestCase
Expand All @@ -116,9 +121,6 @@ def setup
def setup_paths
@subject = Proxy::TFTP::Pxegrub2.new
@pxe_config_files = [
"host-config/aa-bb-cc-dd-ee-ff/grub2/grub.cfg",
"host-config/aa-bb-cc-dd-ee-ff/grub2/grub.cfg-01-aa-bb-cc-dd-ee-ff",
"host-config/aa-bb-cc-dd-ee-ff/grub2/grub.cfg-aa:bb:cc:dd:ee:ff",
"grub2/grub.cfg-01-aa-bb-cc-dd-ee-ff",
"grub2/grub.cfg-aa:bb:cc:dd:ee:ff",
]
Expand All @@ -127,7 +129,7 @@ def setup_paths

def test_pxeconfig_dir
assert_equal File.join(@subject.path, "host-config", @subject.dashed_mac(@mac).downcase, "grub2"), @subject.pxeconfig_dir(@mac)
assert_equal File.join(@subject.path, "grub2"), @subject.pxeconfig_dir()
assert_equal File.join(@subject.path, "grub2"), @subject.pxeconfig_dir
end

def test_release_specific_bootloader_path
Expand Down Expand Up @@ -189,6 +191,13 @@ def test_del
@subject.expects(:delete_host_dir).with(@mac).once
@subject.del @mac
end

def test_symlinks_in_host_config_dir
@subject.set(@mac, @content)
['grub.cfg-01-aa-bb-cc-dd-ee-ff', 'grub.cfg-aa:bb:cc:dd:ee:ff'].each do |file|
assert_equal @content, File.read(File.join(@subject.path, 'host-config', @subject.dashed_mac(@mac).downcase, 'grub2', file))
end
end
end

class TftpPoapServerTest < Test::Unit::TestCase
Expand Down Expand Up @@ -225,4 +234,9 @@ def setup_paths
@pxe_config_files = ["pxelinux.cfg/01-aa-bb-cc-dd-ee-ff.ipxe"]
@pxe_default_files = ["pxelinux.cfg/default.ipxe"]
end

def test_symlinks_in_host_config_dir
@subject.set(@mac, @content)
assert_equal @content, File.read(File.join(@subject.path, 'host-config', @subject.dashed_mac(@mac).downcase, 'ipxe', '01-aa-bb-cc-dd-ee-ff.ipxe'))
end
end