From 6af19b09bbe795917ec77bed06a71970bd957e3f Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 4 Aug 2009 15:25:09 +0000 Subject: [PATCH] quite huge refactoring - config is now place to define client configuration - upstream provide mirroring - pxelinux creates per-client specific configurations - various tweaks and cleanups --- lib/PXElator/config.pm | 53 +++++++-------------------------------- lib/PXElator/pxelinux.pm | 38 ++++++++++++++++++++++++++++ lib/PXElator/server.pm | 21 ++++++++++++---- lib/PXElator/t/config.t | 8 +++--- lib/PXElator/t/server.t | 15 +++++++++++ lib/PXElator/t/upstream.t | 18 +++++++++++++ lib/PXElator/tftpd.pm | 4 ++- lib/PXElator/upstream.pm | 46 +++++++++++++++++++++++++++++++++ 8 files changed, 148 insertions(+), 55 deletions(-) create mode 100755 lib/PXElator/t/server.t create mode 100755 lib/PXElator/t/upstream.t create mode 100644 lib/PXElator/upstream.pm diff --git a/lib/PXElator/config.pm b/lib/PXElator/config.pm index 0c0c816..3bcb53d 100644 --- a/lib/PXElator/config.pm +++ b/lib/PXElator/config.pm @@ -8,69 +8,34 @@ use server; use pxelinux; use File::Slurp; -sub shared { - my ($name, $value) = @_; - - my $path ="$server::base_dir/conf/$server::ip/$name"; - if ( defined $value ) { - write_file $path, $value; - } else { - $value = read_file $path if -e $path; - } - return $value; -} - sub debian_live { $dhcpd::file = "pxelinux.0"; $pxelinux::path_prefix = 'live-helper/tftpboot/'; $pxelinux::config_file = 'pxelinux.cfg/default'; } -use LWP::Simple qw/mirror RC_NOT_MODIFIED/; -sub webconverger { - $dhcpd::file = "pxelinux.0"; - $pxelinux::path_prefix = 'webconverger/'; - - my $dir = "$server::base_dir/tftp/$pxelinux::path_prefix"; - - mkdir $dir unless -d $dir; - - my $cfg = "$dir/pxelinux.cfg"; - return 1 if -e $cfg; +use upstream; - mkdir $cfg; - - my $iso = '/srv/pxelator/iso/webc-5.2.iso'; - my $url = 'http://download.webconverger.com/webc-5.2.iso'; - - print STDERR "mirror $url"; - mirror( $url, $iso) - == RC_NOT_MODIFIED - && warn(" not modified\n") - || warn(" done ", -s $iso, " bytes\n") - ; - - mkdir "$dir/iso" unless -e "$dir/iso"; - - system "mount -t iso9660 | grep webc-5.2 || sudo mount $iso $dir/iso/ -o loop -t iso9660 -v"; +sub webconverger { + my $ip = shift; - symlink "/usr/lib/syslinux/pxelinux.0", "$dir/$dhcpd::file"; + upstream::iso( 'http://download.webconverger.com/webc-5.2.iso' ); - $cfg .= '/default'; + pxelinux::config_for_ip( $ip, qq{ - write_file $cfg, qq{ +default webconverger label webconverger kernel iso/live/vmlinuz-2.6.30-backports.1-486 append initrd=iso/live/initrd.img-2.6.30-backports.1-486 fetch=http://172.16.10.1/pxelator/webconverger/iso/live/filesystem.squashfs boot=live quiet homepage=http://172.16.10.1:7777/ nonetworking nosudo splash video=vesa:ywrap,mtrr vga=788 nopersistent username=webc hostname=webconverger union=aufs - }; -# $pxelinux::config_file = $cfg; + }); + } sub for_ip { my $ip = shift; # debian_live(); - webconverger(); + webconverger($ip); # $tftp::dir = "$server::base_dir/tftp/$pxelinux::path_prefix"; } diff --git a/lib/PXElator/pxelinux.pm b/lib/PXElator/pxelinux.pm index ebfa4a7..2837db2 100644 --- a/lib/PXElator/pxelinux.pm +++ b/lib/PXElator/pxelinux.pm @@ -1,9 +1,47 @@ package pxelinux; +use warnings; +use strict; +use autodie; + our $magic = 0xF100747E; our $config_file; #= 'pxelinux.cfg/default'; our $path_prefix; our $reboot_time; # 300 s +use server; +use File::Slurp; +use Carp qw/confess/; + +sub config_for_ip { + my ($ip,$config) = @_; + confess "$ip not IP" unless $ip =~ m{^\d+\.\d+\.\d+\.\d+$}; + + $path_prefix = (caller(1))[3]; + $path_prefix =~ s{config::}{} || die "caller isn't package config !"; + $path_prefix .= '/'; + + warn "# $ip $path_prefix"; + + my $dir = "$server::base_dir/tftp"; + $dhcpd::file = 'pxelinux.0'; + symlink "/usr/lib/syslinux/pxelinux.0", "$dir/$dhcpd::file" unless -e "$dir/$dhcpd::file"; + + $dir .= "/$path_prefix"; + mkdir $dir unless -d $dir; + + mkdir "$dir/pxelinux.cfg" unless -e "$dir/pxelinux.cfg"; + + $config_file = 'pxelinux.cfg/' . uc sprintf "%02x%02x%02x%02x", split(/\./, $ip, 4); + warn "$ip config_file $config_file"; + + my $path = "$dir/$config_file"; + write_file $path, $config; + + warn "# config: $path ", -s $path; + + +} + 1; diff --git a/lib/PXElator/server.pm b/lib/PXElator/server.pm index 02ea204..be053cf 100644 --- a/lib/PXElator/server.pm +++ b/lib/PXElator/server.pm @@ -3,8 +3,6 @@ package server; use warnings; use strict; -use Module::Refresh qw//; -sub refresh { Module::Refresh->refresh }; our $ip = '172.16.10.1'; our $netmask = '255.255.255.0'; @@ -13,10 +11,23 @@ our ( $ip_from, $ip_to ) = ( 10, 100 ); our $base_dir = '/srv/pxelator'; -use config; +use Module::Refresh qw//; +sub refresh { Module::Refresh->refresh }; + +use File::Slurp; +sub shared { + my ($name, $value) = @_; + + my $path ="$base_dir/conf/$server::ip/$name"; + if ( defined $value ) { + write_file $path, $value; + } else { + $value = read_file $path if -e $path; + } + return $value; +} -our $debug; -sub debug { $debug = config::shared('debug', @_) || 0 } +sub debug { shared('debug', @_) || 0 } warn "loaded"; diff --git a/lib/PXElator/t/config.t b/lib/PXElator/t/config.t index 0d0ed97..547596e 100755 --- a/lib/PXElator/t/config.t +++ b/lib/PXElator/t/config.t @@ -4,13 +4,11 @@ use warnings; use strict; use autodie; -use Test::More tests => 4; +use Test::More tests => 2; use Data::Dump qw/dump/; use_ok 'config'; -ok( my $test = config::shared( 'test', 42 ), 'set shared' ); -diag $test; -cmp_ok( $test, '==', config::shared( 'test' ), 'get shared' ); +sub test { config::webconverger( '172.0.0.1' ) }; -ok( config::webconverger(), 'webconverger' ); +ok( test(), 'webconverger' ); diff --git a/lib/PXElator/t/server.t b/lib/PXElator/t/server.t new file mode 100755 index 0000000..c4faf94 --- /dev/null +++ b/lib/PXElator/t/server.t @@ -0,0 +1,15 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use autodie; + +use Test::More tests => 3; +use Data::Dump qw/dump/; + +use_ok 'server'; + +ok( my $test = server::shared( 'test', 42 ), 'set shared' ); +diag $test; +cmp_ok( $test, '==', server::shared( 'test' ), 'get shared' ); + diff --git a/lib/PXElator/t/upstream.t b/lib/PXElator/t/upstream.t new file mode 100755 index 0000000..4d14908 --- /dev/null +++ b/lib/PXElator/t/upstream.t @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use autodie; + +use Test::More tests => 3; +use Data::Dump qw/dump/; + +use_ok 'upstream'; + +ok( config::test(), 'iso' ); +ok( system("sudo umount -t iso9660 -a") == 0, 'umount' ); + +package config; +sub test { upstream::iso( 'http://download.webconverger.com/webc-5.2.iso' ) }; + +1; diff --git a/lib/PXElator/tftpd.pm b/lib/PXElator/tftpd.pm index 54bf5dd..7292389 100644 --- a/lib/PXElator/tftpd.pm +++ b/lib/PXElator/tftpd.pm @@ -36,6 +36,8 @@ sub transfer_status { } } +use config; + sub tftp_request { my $request = shift; @@ -43,7 +45,7 @@ sub tftp_request { warn 'request: ', dump( $request ) if $debug; - config::for_ip(); + config::for_ip( $request->{_REQUEST_}->{PeerAddr} ); if ( $request->{RootDir} ne $dir ) { $request->{RootDir} = $dir; diff --git a/lib/PXElator/upstream.pm b/lib/PXElator/upstream.pm new file mode 100644 index 0000000..764d3ce --- /dev/null +++ b/lib/PXElator/upstream.pm @@ -0,0 +1,46 @@ +package upstream; + +use warnings; +use strict; +use autodie; + +use LWP::Simple qw/mirror RC_NOT_MODIFIED/; +use server; + +our $just_once; + +sub iso { + my $url = shift; + + return if $just_once->{$url}++; + + my $name = (caller(1))[3]; + $name =~ s{config::}{} || die "caller isn't package config !"; + + warn "$name $url"; + + my $dir = "$server::base_dir/iso"; + mkdir $dir unless -e $dir; + + my $file = $1 if $url =~ m{/([^/]+\.iso$)}i; + die "can't find iso file in $url" unless $file; + + my $iso = "$dir/$file"; + + print STDERR "$name mirror $url"; + mirror( $url, $iso ) + == RC_NOT_MODIFIED + && warn(" not modified\n") + || warn(" done ", -s $iso, " bytes\n") + ; + + $file =~ s{\.iso$}{}i; + my $mnt = "$server::base_dir/tftp/$name"; + mkdir $mnt unless -d $mnt; + $mnt .= '/iso'; + mkdir $mnt unless -d $mnt; + + system("mount -t iso9660 | grep $mnt || sudo mount $iso $mnt -o loop -t iso9660 -v") == 0; +} + +1; -- 2.20.1