-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetboot.rb
More file actions
398 lines (341 loc) · 9.32 KB
/
Copy pathnetboot.rb
File metadata and controls
398 lines (341 loc) · 9.32 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Copyright (c) by INRIA, Luc Sarzyniec - 2011-2013
# CECILL License V2 - http://www.cecill.info
# For details on use and redistribution please refer to License.txt
# in Kadeploy3 sources: http://kadeploy3.gforge.inria.fr/
require 'pathname'
module NetBoot
class NetBoot::Error < StandardError; end
def self.custom_prefix(user, id)
"#{user}-#{id.to_s}"
end
def self.Factory(kind, binary, export_kind, export_server, repository_dir,
custom_dir, profiles_dir, profiles_kind, chain=nil
)
begin
c = NetBoot.class_eval(kind)
rescue NameError
raise 'Invalid kind of PXE configuration'
end
c.new(
binary,
c::Export.new(export_kind, export_server),
repository_dir,
custom_dir,
profiles_dir,
profiles_kind,
chain,
)
end
class PXE
class Export
def initialize(kind, server)
raise NetBoot::Error, "#{self.class.name} do not support "\
+ "'#{kind.to_s}' (supported values: #{kinds().join(', ')})"\
unless kinds().include?(kind)
@kind = kind
@server = server
end
def kinds
raise 'Should be reimplemented'
end
def path(_path)
raise 'Should be reimplemented'
end
end
attr_reader :binary
attr_reader :export
attr_reader :chain
attr_reader :repository_dir
attr_reader :custom_dir
def initialize(binary, export, repository_dir, custom_dir, profiles_dir,
profiles_kind, chain=nil
)
@binary = binary
@export = export
@repository_dir = repository_dir
@custom_dir = custom_dir
if !profiles_dir || profiles_dir.empty?
@profiles_dir = @repository_dir
elsif Pathname.new(profiles_dir).absolute?
@profiles_dir = profiles_dir
else
@profiles_dir = File.join(@repository_dir, profiles_dir)
end
@profiles_kind = "profilename_#{profiles_kind}".to_sym
@chain = chain
end
def boot(kind, nodes, headers, *args)
if @chain && (@chain.class != self.class || @chain.binary != @binary)
@chain.boot(:chain, nodes, headers, @binary)
end
profile, meth = __send__("boot_#{kind.to_s}".to_sym, *args)
unless kind == :custom
header = headers[kind].to_s
header += "\n" if header != '' && header[-1] != "\n"
profile = labelize(header, kind.to_s, profile, args)
end
write_profile(nodes, profile, meth)
end
protected
def labelize(_header, _kind, _profile, _args)
raise 'Should be reimplemented'
end
def boot_chain(_pxebin)
raise 'Should be reimplemented'
end
def boot_local(_env, _diskname, _device_id, _partition_id, _default_pars='')
raise 'Should be reimplemented'
end
def boot_network(_kernel, _initrd, _params)
raise 'Should be reimplemented'
end
def boot_custom(profile, user, id, singularities=nil)
[
profile,
lambda do |prof, node|
#prof.gsub!("PXE_EXPORT",export_path(@custom_dir))
prof.gsub!("NODE_SINGULARITY", singularities[node[:hostname]].to_s) \
if singularities
prof.gsub!("FILES_PREFIX",
File.join(
export_path(@custom_dir),
NetBoot.custom_prefix(user, id),
),
)
prof
end,
]
end
def export_path(path)
@export.path(path)
end
private
def profilename_hostname(node)
node[:hostname]
end
def profilename_hostname_short(node)
node[:hostname].split('.')[0]
end
def profilename_ip(node)
node[:ip].strip
end
def profilename_ip_hex(node)
node[:ip].split(".").collect{ |n| sprintf("%02X", n) }.join('')
end
def profile_dir(node)
File.join(@profiles_dir, __send__(@profiles_kind, node))
end
def write_profile(nodes, profile, meth=nil)
prof = profile
nodes.each do |node|
file = profile_dir(node)
File.delete(file) if File.exist?(file)
begin
f = File.new(file, File::CREAT | File::RDWR, 0644)
prof = meth.call(profile.dup, node) if meth
f.write(prof)
f.close
rescue
return false
end
end
true
end
end
class PXElinux < PXE
class Export < PXE::Export
def kinds
[
:tftp
]
end
def path(path)
if path
File.join('/', path)
else
''
end
end
end
def labelize(header, kind, profile, *_args)
header +
"DEFAULT #{kind}\n"\
"LABEL #{kind}\n"\
+ profile.collect{|line| "\t#{line}" }.join("\n")
end
def boot_chain(pxebin)
[[
"KERNEL #{export_path(pxebin)}",
"APPEND keeppxe"
]]
end
def boot_local(_env, _diskname, device_id, partition_id, _default_pars='')
[[
"COM32 #{export_path('chain.c32')}",
"APPEND hd#{device_id} #{partition_id}",
]]
end
def boot_network(kernel, initrd, params)
profile = []
if initrd
initrd = "initrd=#{export_path(initrd)} "
else
initrd = ''
end
profile << "KERNEL #{export_path(kernel)}"
profile << "APPEND #{initrd}#{params}"
[ profile ]
end
end
class GPXElinux < PXElinux
class Export < PXE::Export
def kinds
[
:tftp,
:http,
:ftp,
]
end
def path(path)
case @kind
when :tftp
if path
File.join('/', path)
else
''
end
when :http, :ftp
if path
File.join("#{@kind.to_s}://", @server, path)
else
File.join("#{@kind.to_s}://", @server)
end
end
end
end
end
class IPXE < PXE
class Export < PXE::Export
def kinds
[
:tftp,
:http,
:ftp,
]
end
def path(path)
case @kind
when :tftp
if path
File.join('/', path)
else
''
end
when :http, :ftp
if path
File.join("#{@kind.to_s}://", @server, path)
else
File.join("#{@kind.to_s}://", @server)
end
end
end
end
def labelize(header, _kind, profile, *_args)
"#!ipxe\n#{header}#{profile.join("\n")}"
end
def boot_chain(pxebin)
[[
"chain #{export_path(pxebin)}"
]]
end
def boot_local(_env, _diskname, device_id, partition_id, _default_pars='')
[[
"chain #{export_path('chain.c32')} hd#{device_id} #{partition_id}"
]]
end
def boot_network(kernel, initrd, params)
profile = []
profile << "initrd #{export_path(initrd)}" if initrd
profile << "chain #{export_path(kernel)} #{params}"
[ profile ]
end
end
class GrubPXE < PXE
class Export < PXE::Export
def kinds
[
:tftp,
:http,
]
end
def path(path)
case @kind
when :tftp
# Compatibility for old GRUB disks versions
if path
File.join('(pxe)', path)
else
'(pxe)'
end
when :http
if path
File.join("(#{@kind.to_s},#{@server})", path)
else
"(#{@kind.to_s},#{@server})"
end
end
end
end
def labelize(header, kind, profile, *_args)
header += "\ntimeout=0\n" unless header.include?("timeout")
header +
"default=0\n"\
"menuentry #{kind} {\n"\
"#{profile.collect{|line| "\t#{line}" }.join("\n")}\n"\
"}"
end
def boot_chain(pxebin)
[[
"pxechainloader #{export_path(pxebin)}",
"boot",
]]
end
def boot_local(env, diskname, device_id, partition_id, default_params='')
# The search command should be used to find the device, several options:
# --fs-uuid + one microstep to gather the uuid on each machine
# --label + write a label on the disk when partitioning
# --file + create a unique file (deploy ID# ?) on the deployed fs
profile = [ "set root=(hd#{device_id},#{partition_id})" ]
partname = "#{diskname}#{partition_id}"
kernel_params = (env.kernel_params.empty? \
? default_params : env.kernel_params)
case env.environment_kind
when 'linux'
profile << "linux #{env.kernel} #{kernel_params} root=#{partname}"
profile << "initrd #{env.initrd}" if env.initrd && !env.initrd.empty?
when 'xen'
profile << "multiboot #{env.hypervisor} #{env.hypervisor_params}"
profile << "module #{env.kernel} #{kernel_params} root=#{partname}"
profile << "module #{env.initrd}" if env.initrd && !env.initrd.empty?
when 'bsd'
profile << "insmod ufs1"
profile << "insmod ufs2"
profile << "insmod zfs"
profile << "chainloader +1"
when 'windows'
profile << "insmod fat"
profile << "insmod ntfs"
profile << "ntldr /bootmgr"
when 'other'
profile << "chainloader +1"
end
[ profile ]
end
def boot_network(kernel, initrd, params, kind='linux')
profile = []
profile << "#{kind} #{export_path(kernel)} #{params}"
profile << "initrd #{export_path(initrd)}" if initrd
[ profile ]
end
end
end