Project

General

Profile

Download (3.3 KB) Statistics
| Branch: | Tag: | Revision:
ba083eee Ohad Levy
require 'fileutils'
470630be Ohad Levy
require 'pathname'
ba083eee Ohad Levy
e09de9d9 Ohad Levy
module Proxy::TFTP
038fa6cf Dmitri Dolguikh
class Server
b0e03d34 Paul Kelly
include Proxy::Log
# Creates TFTP pxeconfig file
def set mac, config
25691fff Dmitri Dolguikh
raise "Invalid parameters received" if mac.nil? || config.nil?
e09de9d9 Ohad Levy
b0e03d34 Paul Kelly
FileUtils.mkdir_p pxeconfig_dir
e09de9d9 Ohad Levy
b0e03d34 Paul Kelly
File.open(pxeconfig_file(mac), 'w') {|f| f.write(config) }
logger.info "TFTP: entry for #{mac} created successfully"
2be40fe2 Justin Sherrill
end

b0e03d34 Paul Kelly
# Removes pxeconfig files
def del mac
file = pxeconfig_file(mac)
25691fff Dmitri Dolguikh
if File.exist?(file)
e09de9d9 Ohad Levy
FileUtils.rm_f file
b0e03d34 Paul Kelly
logger.debug "TFTP: entry for #{mac} removed successfully"
e09de9d9 Ohad Levy
else
1867338d Ohad Levy
logger.info "TFTP: Skipping a request to delete a file which doesn't exists"
e09de9d9 Ohad Levy
end
end

b0e03d34 Paul Kelly
# Gets the contents of a pxeconfig file
def get mac
file = pxeconfig_file(mac)
25691fff Dmitri Dolguikh
if File.exist?(file)
b0e03d34 Paul Kelly
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"
end
config
end
470630be Ohad Levy
b0e03d34 Paul Kelly
# Creates a default menu file
def create_default config
raise "Default config not supplied" if config.nil?
470630be Ohad Levy
b0e03d34 Paul Kelly
FileUtils.mkdir_p File.dirname pxe_default
File.open(pxe_default, 'w') {|f| f.write(config) }
logger.info "TFTP: #{pxe_default} entry created successfully"
e09de9d9 Ohad Levy
end

b0e03d34 Paul Kelly
protected
e09de9d9 Ohad Levy
# returns the absolute path
038fa6cf Dmitri Dolguikh
# TODO:
e09de9d9 Ohad Levy
def path(p = nil)
038fa6cf Dmitri Dolguikh
p ||= Proxy::TFTP::Plugin.settings.tftproot
return (p =~ /^\//) ? p : Pathname.new(File.expand_path(File.dirname(__FILE__))).join(p).to_s
e09de9d9 Ohad Levy
end
b0e03d34 Paul Kelly
end
e09de9d9 Ohad Levy
038fa6cf Dmitri Dolguikh
class Syslinux < Server
b0e03d34 Paul Kelly
def pxeconfig_dir
"#{path}/pxelinux.cfg"
9115e420 Ohad Levy
end
b0e03d34 Paul Kelly
def pxe_default
"#{pxeconfig_dir}/default"
end
def pxeconfig_file mac
"#{pxeconfig_dir}/01-"+mac.gsub(/:/,"-").downcase
2be40fe2 Justin Sherrill
end
b0e03d34 Paul Kelly
end
2be40fe2 Justin Sherrill
038fa6cf Dmitri Dolguikh
class Pxegrub < Server
b0e03d34 Paul Kelly
def pxeconfig_dir
"#{path}"
e09de9d9 Ohad Levy
end
b0e03d34 Paul Kelly
def pxe_default
"#{pxeconfig_dir}/boot/grub/menu.lst"
end
def pxeconfig_file mac
"#{pxeconfig_dir}/menu.lst.01"+mac.gsub(/:/,"").upcase
end
end

038fa6cf Dmitri Dolguikh
class Ztp < Server
d1c1f81d Frank Wall
def pxeconfig_dir
"#{path}/ztp.cfg"
end
def pxe_default
pxeconfig_dir
end
def pxeconfig_file mac
"#{pxeconfig_dir}/"+mac.gsub(/:/,"").upcase
end
end

17d3726e Fernando Carolo
class Poap < Server
def pxeconfig_dir
"#{path}/poap.cfg"
end
def pxe_default
pxeconfig_dir
end
def pxeconfig_file mac
"#{pxeconfig_dir}/"+mac.gsub(/:/,"").upcase
end
end

d44cdd95 Dmitri Dolguikh
def self.fetch_boot_file dst, src
a7297466 Dmitri Dolguikh
filename = boot_filename(dst, src)
d44cdd95 Dmitri Dolguikh
destination = Pathname.new(File.expand_path(filename, Proxy::TFTP::Plugin.settings.tftproot)).cleanpath
tftproot = Pathname.new(Proxy::TFTP::Plugin.settings.tftproot).cleanpath
raise "TFTP destination outside of tftproot" unless destination.to_s.start_with?(tftproot.to_s)
e09de9d9 Ohad Levy
d44cdd95 Dmitri Dolguikh
# Ensure that our image directory exists
# as the dst might contain another sub directory
FileUtils.mkdir_p destination.parent
b0e03d34 Paul Kelly
5f7fd34c Lukas Zapletal
::Proxy::HttpDownload.new(src.to_s, destination.to_s).start
e09de9d9 Ohad Levy
end
a7297466 Dmitri Dolguikh
def self.boot_filename(dst, src)
# Do not append a '-' if the dst is a directory path
dst.end_with?('/') ? dst + src.split("/")[-1] : dst + '-' + src.split("/")[-1]
end
e09de9d9 Ohad Levy
end