blib/lib/VUser/Install.pm untested

filter: Statementstmt 0.0% 0 / 373 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 575 CC 42 SCAR 41.2
CC
42
Cov
0%
install_diskless
240.0
update_dhcp
56.0
upgrade_diskless
42.0
CRAP 61.7
1 package VUser::Install;
2 use warnings;
3 use strict;
4  
5 # Copyright 2005 Randy Smith <perlstalker@vuser.org>
6 # $Id: Install.pm,v 1.6 2005/10/13 17:04:38 perlstalker Exp $
7  
8 use vars ('@ISA');
9  
10 use VUser::ExtLib qw(strip_ws check_bool);
11 use VUser::Meta;
12 use VUser::ResultSet;
13 use VUser::Extension;
14 push @ISA, 'VUser::Extension';
15  
16 our $REVISION = (split (' ', '$Revision: 1.6 $'))[1];
17 our $VERSION = '0.1.0';
18  
19 my $c_sec = 'Extension_Install';
20  
21 my %meta = (
22 'service' => VUser::Meta->new('name' => 'service',
23 'type' => 'string',
24 'description' => ''),
25 'ip' => VUser::Meta->new('name' => 'ip',
26 'type' => 'string', # IPv4
27 'description' => 'IP Address'),
28 'mac' => VUser::Meta->new('name' => 'mac',
29 'type' => 'string', # MAC addr
30 'description' => 'MAC address'),
31 'hostname' => VUser::Meta->new('name' => 'hostname',
32 'type' => 'string',
33 'description' => 'Host name'),
34 'disk' => VUser::Meta->new('name' => 'disk',
35 'type' => 'string',
36 'description' => 'Disk to partition'),
37 'kernel' => VUser::Meta->new('name' => 'kernel',
38 'type' => 'string',
39 'description' => 'Default kernel')
40 );
41  
42 my %lcfg; # Local config
43  
44 my $debug = $main::DEBUG;
45 my $verbose = $debug;
46  
47 sub version { return $VERSION; }
48 sub revision { return $REVISION; }
49  
50 sub unload { return; }
51  
52 sub init
53 {
54 my $eh = shift;
55 my %cfg = @_;
56  
57 my $lconfig = strip_ws($cfg{$c_sec}{'config dir'}).'/VUser-Install.ini';
58 if (-e $lconfig) {
59 tie %lcfg, 'Config::IniFiles', (-file => $lconfig);
60 } else {
61 die "Config file $lconfig doesn't exist\n";
62 }
63  
64 # install
65 $eh->register_keyword('install', 'Installation tools');
66  
67 # install-diskless
68 $eh->register_action('install', 'diskless', 'Manage diskless installs');
69 $eh->register_option('install', 'diskless', $meta{'service'}, 'req');
70 $eh->register_option('install', 'diskless', $meta{'ip'}, 'req');
71 $eh->register_option('install', 'diskless', $meta{'mac'}, 'req');
72 $eh->register_option('install', 'diskless', $meta{'hostname'}, 'req');
73 $eh->register_option('install', 'diskless', $meta{'disk'});
74 $eh->register_option('install', 'diskless', $meta{'kernel'});
75 # Local scripts are usually only run on the server that is being installed
76 # and a disk is required. The 'local-scripts' option tells VUser::Install
77 # to run the scripts in the diskless root space. This can 'almost' be
78 # used to force a diskless install when a disk is required. I say
79 # 'almost' because certain things, e.g. /etc/fstab, expect to be mounting
80 # those disks.
81 $eh->register_option('install', 'diskless', 'local-scripts', '', 0,
82 'Run local commands');
83 $eh->register_task('install', 'diskless', \&install_diskless);
84  
85 # install-standalone
86 $eh->register_action('install', 'standalone', 'Not implemented');
87 $eh->register_option('install', 'standalone', $meta{'service'}, 'req');
88 $eh->register_option('install', 'standalone', $meta{'ip'}, 'req');
89 $eh->register_option('install', 'standalone', $meta{'mac'});
90 $eh->register_option('install', 'standalone', $meta{'hostname'}, 'req');
91 $eh->register_option('install', 'standalone', $meta{'disk'}, 'req');
92 $eh->register_option('install', 'standalone', $meta{'kernel'});
93 #$eh->register_task('install', 'standalone', \&install_standalone);
94  
95 # install-tarball
96 $eh->register_action('install', 'tarball', 'Get the location of the install tarball');
97 $eh->register_option('install', 'tarball', $meta{'service'}, 'req');
98 $eh->register_option('install', 'tarball', $meta{'ip'}, 'req');
99 $eh->register_task('install', 'tarball', \&install_tarball);
100  
101 # install-diskrequired
102 $eh->register_action('install', 'diskrequired', 'See if a local disk is required for the given service');
103 $eh->register_option('install', 'diskrequired', $meta{'service'}, 'req');
104 $eh->register_task('install', 'diskrequired', \&install_diskrequired);
105  
106 # install-diskinfo
107 $eh->register_action('install', 'diskinfo', 'Get partition info for disk');
108 $eh->register_option('install', 'diskinfo', $meta{'service'}, 'req');
109 $eh->register_task('install', 'diskinfo', \&install_diskinfo);
110  
111 # install-partinfo
112 $eh->register_action('install', 'partinfo', 'Get partition info for disk');
113 $eh->register_option('install', 'partinfo', $meta{'service'}, 'req');
114 $eh->register_task('install', 'partinfo', \&install_partinfo);
115  
116 # install-fsinfo
117 $eh->register_action('install', 'fsinfo', 'Get filesystem info for disks');
118 $eh->register_option('install', 'fsinfo', $meta{'service'}, 'req');
119 $eh->register_task('install', 'fsinfo', \&install_fsinfo);
120  
121 # install-mountinfo
122 $eh->register_action('install', 'mountinfo', 'Get mount point info for disks');
123 $eh->register_option('install', 'mountinfo', $meta{'service'}, 'req');
124 $eh->register_task('install', 'mountinfo', \&install_mountinfo);
125  
126 # install-services
127 $eh->register_action('install', 'services', 'Get the list of available services');
128 $eh->register_task('install', 'services', \&install_services);
129  
130 # update
131 $eh->register_keyword('update', 'Update tools');
132  
133 # update-dhcp
134 $eh->register_action('update', 'dhcp', 'Update dhcp configuration');
135 $eh->register_task('update', 'dhcp', \&update_dhcp);
136  
137 # upgrade
138 $eh->register_keyword('upgrade', 'Upgrade systems');
139  
140 # upgrade-diskless
141 $eh->register_action('upgrade', 'diskless', 'Upgrade diskless installs');
142 $eh->register_option('upgrade', 'diskless', $meta{'service'});
143 $eh->register_option('upgrade', 'diskless', $meta{'ip'});
144 $eh->register_option('upgrade', 'diskless', 'local-scripts', '', 0,
145 'Run local commands');
146 $eh->register_task('upgrade', 'diskless', \&upgrade_diskless);
147  
148 # uninstall
149 $eh->register_keyword('uninstall', 'Uninstall management');
150  
151 # uninstall-diskless
152 $eh->register_action('uninstall', 'diskless', 'Remove a node from diskless setup');
153 $eh->register_option('uninstall', 'diskless', $meta{'ip'}, 'req');
154 $eh->register_task('uninstall', 'diskless', \&uninstall_diskless);
155  
156 # uninstall-standalone?
157  
158 # Optionally update dhcp on a install (or uninstall) diskless.
159 if (check_bool($cfg{$c_sec}{'auto-update dhcp'})) {
160 $eh->register_task('install', 'diskless', \&update_dhcp, '+ 1');
161 $eh->register_task('uninstall', 'diskless', \&update_dhcp, '+ 1');
162 }
163 }
164  
165 sub install_diskless
166 {
167 my ($cfg, $opts, $action, $eh) = @_;
168  
169 my $prototypes = strip_ws($cfg->{$c_sec}{'prototype dir'});
170 my $diskless = strip_ws($cfg->{$c_sec}{'diskless dir'});
171 my $data_dir = strip_ws($cfg->{$c_sec}{'data dir'});
172 my $config_dir = strip_ws($cfg->{$c_sec}{'config dir'});
173  
174 my $service = $opts->{'service'};
175 if (not $lcfg{$service} or not -d $prototypes) {
176 die "No such service '$service'\n";
177 }
178  
179 my $kernel = $opts->{'kernel'} || 'bzImage';
180 my $nfs_server = strip_ws($lcfg{$service}{'nfs server'});
181 die "No NFS server defined\n" unless defined $nfs_server;
182  
183 my $ip = $opts->{'ip'};
184  
185 if (not -e "$diskless/$service" or not -d "$diskless/$service") {
186 die "$diskless/$service does not exist or is not a directory\n";
187 }
188  
189 my $disk = $opts->{'disk'} || '';
190
191 # Add the new server to the config file that we'll use to write
192 # dhcp.conf.
193 my $file = "$data_dir/servers.dat";
194 add_server_info($file,
195 $opts->{ip},
196 $opts->{mac},
197 $opts->{service},
198 $opts->{hostname},
199 $opts->{disk},
200 $opts->{kernel}
201 );
202  
203 # Now, it's time to build the root system for this node
204 if (-e "$diskless/$service/$ip" or -d "$diskless/$service/$ip") {
205 #die "$diskless/$service/$ip already exists\n";
206 }
207
208 System("mkdir", "$diskless/$service") unless -e "$diskless/$service";
209 System("mkdir", "$diskless/$service/$ip") unless -e "$diskless/$service/$ip";
210  
211 # TODO: This should be more configurable
212 my @dirs = qw(home dev proc tmp mnt mnt/.initd root
213 var var/lib var/empty var/lock var/log var/log/news
214 var/run var/spool
215 usr opt mfs
216 );
217 foreach my $dir (@dirs) {
218 #run_dangerous("mkdir '$diskless/$service/$ip/$dir'");
219 if (not -e "$diskless/$service/$ip/$dir") {
220 System('mkdir', "$diskless/$service/$ip/$dir");
221 }
222 }
223 System('chmod', '777', "$diskless/$service/$ip/tmp");
224 # Make console device
225 # WARNING: Linux-centric
226 unless (-e "$diskless/$service/$ip/dev/console") {
227 System('mknod', "$diskless/$service/$ip/dev/console", "c", "5", "1");
228 }
229  
230 # Dirs that must be in the root file system to successfully boot.
231 # TODO: This should be more configurable
232 my @root_dirs = qw(etc bin sbin lib);
233 # Sync the new root with the prototype
234 foreach my $dir (@root_dirs) {
235 System('rsync', '-avz',
236 "$prototypes/$service/$dir",
237 "$diskless/$service/$ip/");
238 }
239 my $host = $opts->{'hostname'};
240 System("echo $host >$diskless/$service/$ip/etc/hostname");
241  
242 # WARNING: More Linux-centric stuff
243 # PXELINUX will look for a file with the IP (in HEX) to determine the
244 # boot flags. It it can't find it, It will strip off the right-most
245 # letter of the address and repete until it finds the file or there
246 # are no letters left. If it runs out of files, it will use a file
247 # named 'default'
248  
249 # Convert IP to HEX
250 use Socket;
251 my $ip_hex = sprintf('%04X', unpack('N', inet_aton($ip)));
252  
253 # Create the pxelinux config for this IP
254 System ("echo 'DEFAULT kernels/$kernel' >$diskless/$service/pxelinux.cfg/$ip_hex");
255 System ("echo 'APPEND ip=dhcp root=/dev/nfs nfsroot=$nfs_server:$diskless/$service/$ip' >>$diskless/$service/pxelinux.cfg/$ip_hex");
256  
257 # Run init scripts
258 if (-d "$diskless/$service/init") {
259 if (opendir (INIT, "$diskless/$service/init")) {
260 my @scripts = grep { ! /^\./
261 && /\.(?:pl|sh)$/
262 } readdir INIT;
263 closedir INIT;
264  
265 print "Running scripts in $diskless/$service/init/\n";
266 use Cwd;
267 my $cwd = cwd();
268 chdir ("$diskless/$service/init");
269 foreach my $script (sort @scripts) {
270 if (-x "$diskless/$service/init/$script") {
271 System ("$diskless/$service/init/$script",
272 "$diskless/$service/$ip",
273 $service,
274 $host,
275 $ip,
276 $nfs_server,
277 "$prototypes/$service",
278 $disk
279 );
280 }
281 }
282 chdir($cwd);
283 }
284 }
285  
286 if ($opts->{'local-scripts'} and -d "$diskless/$service/init/local") {
287 if (opendir (INIT, "$diskless/$service/init/local")) {
288 my @scripts = grep { ! /^\./
289 && /\.(?:pl|sh)$/
290 } readdir INIT;
291 closedir INIT;
292  
293 print "Running scripts in $diskless/$service/init/local/\n";
294 use Cwd;
295 my $cwd = cwd();
296 chdir ("$diskless/$service/init");
297 foreach my $script (sort @scripts) {
298 if (-x "$diskless/$service/init/$script") {
299 System ("$diskless/$service/init/$script",
300 "$diskless/$service/$ip",
301 $service,
302 $host,
303 $ip,
304 $nfs_server,
305 "$prototypes/$service",
306 $disk);
307 }
308 }
309 chdir($cwd);
310 }
311 }
312 }
313  
314 sub install_diskrequired
315 {
316 my ($cfg, $opts, $action, $eh) = @_;
317  
318 my $req = check_bool($lcfg{$opts->{'service'}}{'disk required'});
319 my $rs = VUser::ResultSet->new();
320 $rs->add_meta(VUser::Meta->new('name' => 'diskrequired',
321 'type' => 'boolean',
322 'description' => 'Is a local disk required')
323 );
324 $rs->add_data([$req]) if defined $req; # $req should always be defined
325  
326 return $rs;
327 }
328  
329 sub install_diskinfo
330 {
331 my ($cfg, $opts, $action, $eh) = @_;
332  
333 my $rs = VUser::ResultSet->new();
334 $rs->add_meta(VUser::Meta->new('name' => 'disk',
335 'type' => 'string',
336 'description' => 'Disk name')
337 );
338 $rs->add_meta(VUser::Meta->new('name' => 'partinfo',
339 'type' => 'string',
340 'description' => 'Partion Info')
341 );
342 $rs->add_meta(VUser::Meta->new('name' => 'fsinfo',
343 'type' => 'string',
344 'description' => 'Filesystem Info')
345 );
346 $rs->add_meta(VUser::Meta->new('name' => 'mountinfo',
347 'type' => 'string',
348 'description' => 'Mount point Info')
349 );
350  
351 my %disks = get_disk_config($cfg, $opts);
352 foreach my $disk (sort keys %disks) {
353 $rs->add_data([$disk,
354 $disks{$disk}{'part'},
355 $disks{$disk}{'fs'},
356 $disks{$disk}{'mount'},
357 ]);
358 }
359  
360 return $rs;
361 }
362  
363 sub install_partinfo
364 {
365 my ($cfg, $opts, $action, $eh) = @_;
366  
367 my $rs = VUser::ResultSet->new();
368 $rs->add_meta(VUser::Meta->new('name' => 'partinfo',
369 'type' => 'string',
370 'description' => 'Partion Info')
371 );
372  
373 my %disks = get_disk_config($cfg, $opts);
374 foreach my $disk (sort keys %disks) {
375 $rs->add_data([$disks{$disk}{'part'}]);
376 }
377  
378 return $rs;
379 }
380  
381 sub install_fsinfo
382 {
383 my ($cfg, $opts, $action, $eh) = @_;
384  
385 my $rs = VUser::ResultSet->new();
386 $rs->add_meta(VUser::Meta->new('name' => 'fsinfo',
387 'type' => 'string',
388 'description' => 'Filesystem Info')
389 );
390  
391 my %disks = get_disk_config($cfg, $opts);
392 foreach my $disk (sort keys %disks) {
393 $rs->add_data([$disks{$disk}{'fs'}]);
394 }
395  
396 return $rs;
397 }
398  
399 sub install_mountinfo
400 {
401 my ($cfg, $opts, $action, $eh) = @_;
402  
403 my $rs = VUser::ResultSet->new();
404 $rs->add_meta(VUser::Meta->new('name' => 'mountinfo',
405 'type' => 'string',
406 'description' => 'Mount point Info')
407 );
408  
409 my %disks = get_disk_config($cfg, $opts);
410 foreach my $disk (sort keys %disks) {
411 $rs->add_data([$disks{$disk}{'mount'}]);
412 }
413  
414 return $rs;
415 }
416  
417 sub install_services
418 {
419 my ($cfg, $opts, $action, $eh) = @_;
420
421 my $dir = strip_ws($cfg->{$c_sec}{'prototype dir'});
422 my @services = get_services($cfg);
423  
424 my $rs = VUser::ResultSet->new();
425 $rs->add_meta(VUser::Meta->new('name' => 'service',
426 'type' => 'string',
427 'description' => 'Available services')
428 );
429  
430 foreach my $service (@services) {
431 $rs->add_data([$service]);
432 }
433  
434 return $rs;
435 }
436  
437 sub install_tarball
438 {
439 my ($cfg, $opts, $action, $eh) = @_;
440  
441 my $service = $opts->{'service'};
442 my $ip = $opts->{'ip'};
443  
444 my $base = strip_ws($lcfg{$service}{'tarball base url'});
445 my $file = strip_ws($lcfg{$service}{'tarball file'});
446 $base =~ s!/$!!; # Strip off any trailing /s.
447  
448 $base = eval qq("$base");
449 $file = eval qq("$file");
450  
451 my $url = $base.'/'.$file;
452 #eval $url; # Replace $service, $ip in URL
453  
454 my $rs = VUser::ResultSet->new();
455 $rs->add_meta(VUser::Meta->new('name' => 'url',
456 'type' => 'string', # URL
457 'description' => 'Tarball URL')
458 );
459 $rs->add_data([$url]);
460  
461 print "Tarball URL: $url\n" if $debug;
462  
463 return $rs;
464 }
465  
466 sub update_dhcp
467 {
468 my ($cfg, $opts, $action, $eh) = @_;
469  
470 my $data_dir = strip_ws($cfg->{$c_sec}{'config dir'});
471 my $file = "$data_dir/servers.dat";
472 my %servers = read_server_info($file);
473  
474 use Text::Template;
475  
476 my @entries = ();
477 my $diskless = strip_ws($cfg->{$c_sec}{'diskless dir'});
478  
479 # $server keys are: ip mac service hostname disk kernel
480 foreach my $server (values %servers) {
481 # use Data::Dumper; print Dumper $server;
482 my $service = $server->{'service'};
483 my $mac = $server->{'mac'};
484 my $ip = $server->{'ip'};
485 my $template = '';
486 if (-e "$data_dir/$service-$mac.dhcp.tmpl") {
487 $template = "$service-$mac.dhcp.tmpl";
488 } elsif (-e "$data_dir/$service-$ip.dhcp.tmpl") {
489 $template = "$service-$ip.dhcp.tmpl";
490 } else {
491 $template = "$service.dhcp.tmpl";
492 }
493  
494 my $tt = Text::Template->new (TYPE => 'FILE',
495 SOURCE => "$data_dir/$template");
496
497 if (not $tt) {
498 warn "Can't build template ($template): $Text::Template::ERROR\n";
499 next;
500 }
501  
502 my $text = $tt->fill_in(HASH => { server => $server,
503 diskless => $diskless
504 },
505 DELIMITERS => ['<<', '>>']
506 );
507 if (not defined $text) {
508 warn "Can't fill in template ($template): $Text::Template::ERROR\n";
509 next;
510 }
511 push @entries, $text;
512 $server->{'entry'} = $text;
513 }
514  
515 my $main = "$data_dir/dhcp.tmpl";
516 my $tt = Text::Template->new (TYPE => 'FILE', SOURCE => $main);
517 if (not $tt) {
518 die "Can't build template ($main): $Text::Template::ERROR\n";
519 }
520  
521 my $dhcp = strip_ws($cfg->{$c_sec}{'dhcp.conf'});
522 open (DHCP, ">$dhcp.tmp") or die "Can't write $dhcp.tmp: $!\n";
523 my $warning = VUser::ExtLib::edit_warning();
524 my $ok = $tt->fill_in(HASH => { entries => \@entries,
525 diskless => $diskless,
526 servers => \%servers,
527 warning => $warning
528 },
529 OUTPUT => \*DHCP,
530 DELIMITERS => ['<<', '>>']
531 );
532 close DHCP;
533  
534 if ($ok) {
535 run_dangerous("rename '$dhcp.tmp', '$dhcp'");
536 System (strip_ws($cfg->{$c_sec}{'dhcp restart'}));
537 }
538 }
539  
540 sub upgrade_diskless
541 {
542 my ($cfg, $opts, $action, $eh) = @_;
543  
544 my $service = $opts->{'service'};
545 my $ip = $opts->{'ip'};
546  
547 if (not defined $ip and not defined $service) {
548 die "At least one of 'ip' or 'service' must be used.\n";
549 }
550  
551 my $data_dir = strip_ws($cfg->{$c_sec}{'config dir'});
552 my $file = "$data_dir/servers.dat";
553  
554 my %servers = read_server_info($file);
555  
556 my %to_upgrade = ();
557 if (defined $service) {
558 foreach my $ip (keys %servers) {
559 if ($servers{$ip}{'service'} eq $service) {
560 $to_upgrade{$ip} = $servers{$ip};
561 }
562 }
563 } else {
564 %to_upgrade = %servers;
565 }
566  
567 my @keys;
568 if ($ip) {
569 @keys = ($ip);
570 } else {
571 @keys = keys %to_upgrade;
572 }
573  
574 foreach my $host (@keys) {
575 my %inst_opts = ();
576 $inst_opts{'service'} = $to_upgrade{$host}{'service'};
577 $inst_opts{'ip'} = $to_upgrade{$host}{'ip'};
578 $inst_opts{'mac'} = $to_upgrade{$host}{'mac'};
579 $inst_opts{'hostname'} = $to_upgrade{$host}{'hostname'};
580 $inst_opts{'disk'} = $to_upgrade{$host}{'disk'} if $to_upgrade{$host}{'disk'};
581 $inst_opts{'kernel'} = $to_upgrade{$host}{'kernel'};
582 $inst_opts{'local-scripts'} = $opts->{'local-scripts'};
583 $eh->run_tasks('install', 'diskless', $cfg, %inst_opts);
584 }
585 }
586  
587 sub uninstall_diskless
588 {
589 my ($cfg, $opts, $action, $eh) = @_;
590  
591 my $ip = $opts->{'ip'};
592  
593 my $diskless = strip_ws($cfg->{$c_sec}{'diskless dir'});
594  
595 my $data_dir = strip_ws($cfg->{$c_sec}{'config dir'});
596 my $file = "$data_dir/servers.dat";
597  
598 my %servers = read_server_info($file);
599  
600 if (not exists $servers{$ip}) {
601 warn "Unknown server: $ip\n";
602 return;
603 }
604  
605 my $service = $servers{$ip}{'service'};
606  
607 del_server_info($file, $ip);
608  
609 if (-d "$diskless/$service/$ip") {
610 System('rm', '-r', "$diskless/$service/$ip");
611 } else {
612 warn "Unable to delete server directory: $diskless/$service/$ip doesn't exist\n";
613 }
614 }
615  
616 # Return a list of services names for use in various places
617 sub get_services
618 {
619 my $cfg = shift;
620  
621 my @services = ();
622  
623 @services = sort keys %lcfg;
624  
625 # if (opendir (PROTOS, $dir)) {
626 # @services = grep { $_ !~ /^\./ && -d "$dir/$_" } readdir PROTOS;
627 # closedir PROTOS;
628 # }
629  
630 return @services;
631 };
632  
633 sub System
634 {
635 print(join(" ", @_), "\n") if $verbose;
636  
637 return 0 if $debug;
638  
639 my $rc = system (@_);
640 return $rc;
641 }
642  
643 sub run_dangerous
644 {
645 my $cmd = shift;
646 print("$cmd\n") if $verbose;
647  
648 return 0 if $debug;
649  
650 my @rv = eval $cmd;
651 die $@ if $@;
652 return wantarray? @rv : $rv[0];
653 }
654  
655 # Get server info used to write dhcp.conf from the given file.
656 # Format: | delimited
657 # Fields (In order):
658 # IP
659 # MAC
660 # Service
661 # Hostname
662 # Kernel
663 sub read_server_info
664 {
665 my $config_file = shift;
666  
667 my %servers = ();
668  
669 open (CONF, $config_file) or die "Can't read config file ($config_file): $!\n";
670  
671 while (my $line = <CONF>) {
672 chomp $line;
673 my $server = {};
674 @{$server}{qw(ip mac service hostname disk kernel)} = split('\|', $line);
675 $servers{$server->{ip}} = $server;
676 }
677  
678 close CONF;
679 return %servers;
680 }
681  
682 sub write_server_info
683 {
684 my $config_file = shift;
685 my %servers = @_;
686  
687 open (CONF, ">$config_file.tmp") or die "Can't write config file ($config_file.tmp): $!\n";
688  
689 foreach my $server (values %servers) {
690 print CONF join('|', map { defined $_? $_ : ''} @{$server}{qw(ip mac service hostname disk kernel)});
691 print CONF "\n";
692 }
693  
694 close CONF;
695  
696 unless ($debug) {
697 rename "$config_file.tmp", $config_file
698 or die "Enable to rename to $config_file: $!\n";
699 }
700 }
701  
702 sub add_server_info
703 {
704 my ($file, $ip, $mac, $service, $hostname, $disk, $kernel) = @_;
705  
706 $kernel = 'bzImage' if not defined $kernel or $kernel eq '';
707  
708 my %servers = read_server_info($file);
709 $servers{$ip} = {ip => $ip,
710 mac => $mac,
711 service => $service,
712 hostname => $hostname,
713 disk => $disk,
714 kernel => $kernel};
715  
716 write_server_info($file, %servers);
717 }
718  
719 sub del_server_info
720 {
721 my ($file, $ip) = @_;
722 my %servers = read_server_info($file);
723 delete $servers{$ip};
724 write_server_info($file, %servers);
725 }
726  
727 sub get_disk_config
728 {
729 my ($cfg, $opts) = @_;
730 my %disks = ();
731 foreach my $disk (sort grep { /^disk\d+/ } keys %{ $lcfg{$opts->{'service'}} }) {
732 my ($part, $fs, $mount) = split ('\|', $lcfg{$opts->{'service'}}{$disk});
733  
734 $mount =~ s!/$!! if defined $mount and $mount ne '/';
735  
736 $disks{$disk} = {'part' => $part,
737 'fs' => $fs,
738 'mount' => $mount};
739 }
740  
741 return %disks;
742 }
743  
744 1;
745  
746 =head1 NAME
747  
748 VUser::Install - vuser extension to manage netboot installs
749  
750 =head1 REQUIRES
751  
752 Text::Template
753  
754 =head1 DESCRIPTION
755  
756 Handles installs of new systems from netboot installs.
757  
758 =head2 Diskless
759  
760 Configures the DHCP server to give the new box a static IP address
761 and sets up the root path and kernel options. B<Note:> VUser::Install
762 assumes that it is running on the DHCP server that the diskless box will
763 boot off of.
764  
765 If you need to partition a disk for scratch space or other things on the
766 node, run the install-local script from the node.
767  
768 =head2 Stand-alone
769  
770 Packages a root tarball that can be unpackaged in / to install/upgrade
771 a system. The included 'install-local' script can be run from the new
772 system with the diskless setup.
773  
774 The assumption here, is that you have a system root installed somewhere
775 that can easily be packaged up after running a few scripts to change
776 things based on IP address or host name.
777  
778 =head2 install-local
779  
780 This is an interactive script which is designed to run on a new server
781 to make local changes to the server that cannot be done from the install
782 server, e.g. partitioning disks.
783  
784 B<Note:> install-local currently tries to use F<sfdisk> to partition drives.
785 At some point, I hope to add F<disklabel> support for systems that use
786 that instead.
787  
788 install-local requires that vsoapd is running on the installation server.
789  
790 =head1 CONFIGURATION
791  
792 [Extension_Install]
793 # Prototype roots are ${prototype dir}/$service
794 prototype dir = /prototypes
795
796 # Diskless roots are ${diskless dir}/$service/$ip
797 diskless dir = /diskless
798
799 # Directory to store server info used to write dhcp.conf
800 # The server file is called 'servers.dat'
801 data dir = /etc/vuser/
802
803 # Directory for VUser::Install service configs.
804 # Also, in this directory, templates for dhcp entries will be stored
805 # dhcp templates for a new host are used in this order:
806 # (Where $service, $mac and $ip are replaced with the values for this node)
807 # $service-$mac.dhcp.tmpl
808 # $service-$ip.dhcp.tmpl
809 # $service.dhcp.tmpl
810 #
811 # The main dhcp.conf is dhcp.tmpl
812 config dir = /etc/vuser/install/
813
814 # DHCP settings
815 # dhcp config file
816 dhcp.conf = /etc/dhcp/dhcp.conf
817
818 # Command to run to restart dhcp
819 dhcp restart = /etc/init.d/dhcp restart
820  
821 =head1 diskless node data file
822  
823 IP|MAC|service|hostname|disk|kernel
824  
825 Example data file:
826  
827 192.168.1.100|00:0D:87:6A:4E:22|mail|mail1|hda|bzImage
828 192.168.1.101|00:0D:87:6A:4E:24|mail|mail2|hda|bzImage
829 192.168.2.100|00:0D:87:6A:4F:32|www|web1|sda|bzImage-smp
830 192.168.3.147|00:0D:87:6A:3F:27|desktop|desk47||bzImage
831  
832 =head1 dhcp Templates
833  
834 VUser::Install uses Text::Template to generate these files. Variables
835 are inserted like so: C<E<lt>E<lt> $foo E<gt>E<gt>>.
836  
837 See perldoc Text::Template for full details on the format of templates.
838 The short version is that you can include any perl code between
839 'E<lt>E<lt>' and 'E<gt>E<gt>'.
840 The return value (or the value of the I<$OUT> variable) will replace the
841 E<lt>E<lt> E<gt>E<gt> block.
842  
843 =head2 Node Template
844  
845 These templates are used for idividual nodes. The following variables are
846 available to the template:
847  
848 =over 4
849  
850 =item $diskless
851  
852 The path to the diskless tree (From Extention_Install::diskless dir
853  
854 =item %server
855  
856 The server information. See entry in L<Global Template> for a list of keys.
857 B<Note:> The I<entry> key does not exist here since this template is what's
858 used to create that.
859  
860 =back
861  
862 Below is a sample template.
863  
864 host << $server{hostname} >> {
865 hardware ethernet << $server{mac} >>;
866 fixed-address << $server{ip} >>;
867 filename "<< $server{service} >>/pxelinux.0";
868 option root-path "<< $diskless >>/<< $server{service} >>/<< $server{ip} >>";
869 }
870  
871 =head2 Global Template
872  
873 The global template gives you these variables:
874  
875 =over 4
876  
877 =item @entries
878  
879 These are the already created templates for each node. For simple
880 networks, this is the easiest way of put the get the entries into the
881 config file.
882  
883 << join("\n", @entries); >>
884  
885 =item $diskless
886  
887 Path to diskless (from Extention_Install::diskless dir
888  
889 =item %servers
890  
891 If you have a more complex setup, you can use I<%servers> to do all
892 sorts of fun stuff. The keys of I<%servers> are the IP addresses of the
893 servers. The values are hash refs with the following keys:
894  
895 =over 8
896  
897 =item ip
898  
899 The host's IP address
900  
901 =item mac
902  
903 The host's MAC address.
904  
905 =item hostname
906  
907 The host's hostname.
908  
909 =item service
910  
911 The service for this host.
912  
913 =item disk
914  
915 =item kernel
916  
917 The prefered kernel for this host.
918  
919 =item entry
920  
921 The filled out template for this host. This matches the entry for the
922 host in I<@entries>.
923  
924 =back
925  
926 =item $warning
927  
928 A simple warning message that says the file was generated by vuser and
929 the time it was created. See the example below.
930  
931 # This file was written by vuser on Mon Sep 12 16:44:22 2005
932 # DO NOT EDIT THIS FILE. Manual changes to this file will be lost.
933  
934 =back
935  
936 Below is a sample template.
937  
938 # option definitions common to all supported networks...
939 option domain-name "vuser.org";
940 option domain-name-servers 192.168.100.1, 192.168.100.2;
941
942 default-lease-time 600;
943 max-lease-time 7200;
944
945 # If this DHCP server is the official DHCP server for the local
946 # network, the authoritative directive should be uncommented.
947 authoritative;
948
949 # Use this to send dhcp log messages to a different log file (you also
950 # have to hack syslog.conf to complete the redirection).
951 log-facility local7;
952
953 ddns-update-style none;
954
955 subnet 192.168.1.0 netmask 255.255.255.0 {
956 option subnet-mask 255.255.255.0;
957 option broadcast-address 192.168.1.255;
958 option routers 192.168.1.1;
959
960 range 192.168.1.125 192.168.1.127;
961 next-server 192.168.1.120;
962  
963 # Server entries here
964 << join("\n", @entries); >>
965 }
966  
967 =head1 Service configuration file
968  
969 B<Note:> I<service> below, is the name of the service.
970  
971 [service]
972 # Is a local disk required. (yes|no)
973 disk required = yes
974
975 # Partition instructions.
976 # The values are pipe delimited with these fields:
977 # partition instructions
978 # file system.
979 # mount point
980 #
981 # ex:
982 # disk1=,4096,S|swap
983 # disk2=;|reiserfs
984 #
985 # disk1 creates a 4GB partion at the start of the disk and will be
986 # use for swap space.
987 # disk2 will use the rest of the disk for reiserfs
988 #
989 # The partion instructions look like sfdisk commands
990 # for a reason. (See sfdisk(8) dir details) Size units are MBs.
991 #
992 # If the file system is not specified, no file system will be created
993 # on the partition.
994 # Filesystem types are:
995 # (Note: You must have the appropriate tools in your install
996 # setup or that filesystem will fail.)
997 # swap
998 # reiserfs
999 # ext2
1000 # ext3
1001 # jfs
1002 # xfs
1003 #
1004 # The mount point is specified here so that it can be used as a reference
1005 # point for the setup scripts to initialize them. For example, if you are
1006 # installing an MTA (such as postfix) you may want to have a local disk
1007 # for the spool. That spool will need to have certain directories created
1008 # after the disk is partitioned and the filesystem is created. In the case
1009 # of postfix, the directory /var/spool/postfix would need to be created.
1010 # You may also want to use the local disk for scratch space for a virus
1011 # scanner or other things and so those directories will need to be created
1012 # as well.
1013 #
1014 # The mount point may be left empty for swap disks.
1015 disk1=,4096,S|swap|
1016 disk2=;|reiserfs|/var
1017
1018 # Location to put tarballs of standalone systems for download
1019 tarball dir=/tarballs
1020
1021 # Given $service and $ip, what is the base URL of the tarball
1022 #tarball base url=http://download.example.com/tarballs/$service/
1023 tarball base url=http://download.example.com/tarballs/
1024
1025 # Given $service and $ip, what is the name of the tarball
1026 #tarball file=$service-default.tgz
1027 tarball file=$service-$ip.tgz
1028  
1029 =head1 FILE LAYOUTS
1030  
1031 =head2 Prototypes
1032  
1033 /prototypes
1034  
1035 F</prototypes/service> is the root directory for the service prototype.
1036  
1037 /prototypes/service
1038  
1039 =head2 Diskless
1040  
1041 F</diskless> here is set by Extension_Install::diskless. I<service> and
1042 I<ip> are the service and node IP address, respectively.
1043  
1044 /diskless
1045 /diskless/service/
1046  
1047 The pxelinux bootstrap is pxelinux.0 and the config files for it are in
1048 F<pxelinux.cfg>.
1049  
1050 /diskless/service/pxelinux.0
1051 /diskless/service/pxelinux.cfg/
1052  
1053 The available kernels for this service are saved in:
1054  
1055 /diskless/service/kernels
1056  
1057 F<init> holds scripts and various other files for configuring a node.
1058  
1059 /diskless/service/init
1060  
1061 Each node's root directory is in:
1062  
1063 /diskless/service/ip
1064  
1065 =head1 INIT SCRIPTS
1066  
1067 Executable files in F</diskless/service/init> are called when a new service
1068 is installed. These scripts B<must> be able to be run again even if the
1069 service is already install. (This is primarily for upgrade functionality.)
1070  
1071 VUser::Install will cd to F</diskless/service/init> before running any
1072 init scripts. Each script will be passed the following command line
1073 parameters:
1074  
1075 =over 4
1076  
1077 =item diskless root
1078  
1079 This is the root directory for the new install (/diskless/service/ip).
1080  
1081 =item service
1082  
1083 The name of the service (from the --service option)
1084  
1085 =item hostname
1086  
1087 The hostname for this install (from --hostname)
1088  
1089 =item ip address
1090  
1091 This install's IP (from --ip)
1092  
1093 =item NFS server
1094  
1095 The NFS server this host will use. (From VUser-Install.ini service::nfs server)
1096  
1097 =item Prototype Root
1098  
1099 The path to the prototype root (from Extension_Install::prototype dir
1100 and --service; e.g /prototypes/service)
1101  
1102 =back
1103  
1104 =head1 ADDING USERS
1105  
1106 If you use F<install-local>, you will need to create users to allow
1107 it to connect. These users will are user by vuser's SOAP daemon F<vsoapd>
1108 to allow access.
1109  
1110 1) Enable the I<ACL> extension in F<vuser.conf>:
1111  
1112 extensions = Install ACL
1113
1114 [ACL]
1115 use internal auth=yes
1116 auth modules=SQLite
1117 # ALLOW or DENY
1118 acl default=ALLOW
1119
1120 [ACL SQLite]
1121 file=/etc/vuser/install/acl.db
1122  
1123 B<Note:> This setup requires that DBD::SQLite be installed on the master
1124 server, i.e. the box running F<vsoapd>.
1125  
1126 =head1 AUTHOR
1127  
1128 Randy Smith <perlstalker@vuser.org>
1129  
1130 =head1 LICENSE
1131
1132 This file is part of vuser.
1133
1134 vuser is free software; you can redistribute it and/or modify
1135 it under the terms of the GNU General Public License as published by
1136 the Free Software Foundation; either version 2 of the License, or
1137 (at your option) any later version.
1138
1139 vuser is distributed in the hope that it will be useful,
1140 but WITHOUT ANY WARRANTY; without even the implied warranty of
1141 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1142 GNU General Public License for more details.
1143
1144 You should have received a copy of the GNU General Public License
1145 along with vuser; if not, write to the Free Software
1146 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1147  
1148 =cut