forked from mtds/pxe_srv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pxesrv
executable file
·140 lines (114 loc) · 3.3 KB
/
pxesrv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env ruby
require 'erb'
require 'pathname'
require 'pp'
require 'sinatra'
require 'yaml'
configure do
# listen on all interfaces
set :bind, '0.0.0.0'
config_file = nil
config_env = 'PXESRV_CONF'
if ENV.has_key? config_env
config_file = ENV[config_env]
end
root_path = nil
root_env = 'PXESRV_ROOT'
if ENV.has_key? root_env
root_path = ENV[root_env]
end
# if the service root path has been defined
if root_path.nil? or not File.directory? root_path
$stderr.puts "Make sure to set $PXESRV_ROOT"
exit 1
end
default_response = "#{root_path}/default"
once_path = "#{root_path}/once"
static_path = "#{root_path}/static"
grub_path = "#{root_path}/grub"
# Sets the document root for serving static files:
set :root_path, root_path
set :public_folder, root_path
set :once_path, once_path
set :static_path, static_path
set :grub_path, grub_path
set :default_response, default_response
# Do not show exceptions
set :show_exceptions, false
# default location of the service log
log_file = '/var/log/pxesrv.log'
# an environment variable may define an alternative
log_env = 'PXESRV_LOG'
log_file = ENV[log_env] if ENV.has_key? log_env
# Log to file (ref: http://recipes.sinatrarb.com/p/middleware/rack_commonlogger)
log_handel = File.new(log_file, 'a+')
log_handel.sync = true
use Rack::CommonLogger, log_handel
$stdout.puts "pxesrv_root=#{root_path}\npxesrv_log=#{log_file}"
$stdout.puts "pxesrv_default_response=#{default_response}\npxesrv_once_path=#{once_path}\npxesrv_static_path=#{static_path}"
end
# Server will always return contents in text format:
before do
content_type :txt
end
#
# Installation with iPXE
#
# Basic route name as configured in the 'Filename' option on the DHCP server.
get '/redirect' do
ip = request.ip
response = settings.default_response
# check if the client should be redirected once
path = "#{settings.once_path}/#{ip}"
if File.symlink?(path)
response = File.readlink(Pathname.new(path).expand_path)
File.delete(path) # remove the symlink
# check if a static client redirect exists
else
logger.info "Missing #{path}"
# check if a static redirect exists
path = "#{settings.static_path}/#{ip}"
if File.exist?(path)
# send the client to a static boot configuration
response = path
else
logger.info "Missing #{path}"
end
end
# redirect node to target configuration
logger.info "Redirect #{ip} to #{response}"
send_file(response)
end
#
# Installation with pxelinux
#
# Default route:
get '/pxelinux.cfg/default' do
send_file(settings.public_dir + '/pxelinux.cfg/default')
end
#
# Remove a Grub configuration file from the server
#
get '/grub/clear/:version' do
response = settings.default_response
# convert client IP to hexadecimal notation
hex = '%02X%02X%02X%02X' % request.ip.split('.')
# construct the target Grub configuration file path
path = "#{settings.grub_path}/#{params[:version]}/grub.cfg-#{hex}"
logger.debug "grub/clear #{path}"
if File.exists?(path)
File.delete(path)
status 200
body "Delete #{path}"
else
status 400
body "Missing #{path}"
end
end
# ERB template installation:
get '/pxelinux.cfg/:name' do
end
# Gracefully handle 404 errors:
not_found do
"The requested route is not available"
end