added server::as_hash_for $ip to get server configuration
[pxelator] / lib / PXElator / config.pm
1 package config;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 use server;
8 use pxelinux;
9 use client;
10 use file;
11 use ssh;
12
13 use File::Slurp;
14
15 our $mounted;
16
17 sub available { qw/katalog debian_live webconverger debirf tinycore nfsroot openvz printer wrt/ };
18
19 sub debian_live {
20         my ($ip) = @_;
21
22         upstream::files( qw{
23                 http://cdimage.debian.org/cdimage/release/current-live/i386/web/
24                 vmlinuz1
25                 initrd1.img
26                 debian-live-501-i386-standard.squashfs
27         });
28
29         my $hostname = client::conf( $ip => 'hostname' ) || 'debian-live';
30
31         pxelinux::config_for_ip( $ip, qq{
32
33 default debian_live
34 label debian_live
35         kernel vmlinuz1
36         append initrd=initrd1.img fetch=http://${server::ip}:7777/debian_live/debian-live-501-i386-standard.squashfs boot=live nopersistent hostname=$hostname union=aufs noprompt
37         });
38
39 }
40
41 use upstream;
42
43 =head1 webconverger
44
45 Webconverger - the opensource Web Kiosk
46
47 L<http://webconverger.org/>
48
49 =cut
50
51 sub webconverger {
52         my ($ip) = @_;
53
54         $mounted->{"webconverger/$ip"} ||= upstream::iso( 'http://download.webconverger.com/webc-5.5.iso' );
55
56         my $hostname = client::conf( $ip => 'hostname' ) || 'webconverger';
57
58         my $homepage = client::conf( $ip => 'webconverger/homepage'
59                 , default => "http://${server::ip}:7777/client" );
60         my $fetch    = client::conf( $ip => 'webconverger/fetch'
61                 , default => "http://${server::ip}:7777/webconverger/iso/live/filesystem.squashfs" );
62
63         pxelinux::config_for_ip( $ip, qq{
64
65 default webconverger
66 label webconverger
67         kernel iso/live/vmlinuz-2.6.30-backports.1-486
68         append initrd=iso/live/initrd.img-2.6.30-backports.1-486 fetch=$fetch boot=live quiet nosudo splash video=vesa:ywrap,mtrr vga=788 nopersistent username=webc hostname=$hostname union=aufs homepage=$homepage locale=hr noprompt
69
70         });
71
72 }
73
74 sub katalog {
75         my ($ip) = @_;
76         $mounted->{"webconverger/$ip"} ||= upstream::iso( 'http://download.webconverger.com/webc-5.5.iso' );
77         my $hostname = client::conf( $ip => 'hostname' ) || 'katalog';
78         my $homepage = client::conf( $ip => 'webconverger/homepage' => "http://koha.ffzg.hr" );
79         my $fetch    = client::conf( $ip => 'webconverger/fetch' => "http://${server::ip}:7777/webconverger/custom.squashfs" );
80
81         pxelinux::config_for_ip( $ip, qq{
82
83 default katalog
84 label katalog
85         kernel iso/live/vmlinuz-2.6.30-backports.1-486
86         append initrd=iso/live/initrd.img-2.6.30-backports.1-486 fetch=$fetch boot=live quiet nosudo splash video=vesa:ywrap,mtrr vga=788 nopersistent username=webc hostname=$hostname union=aufs homepage=$homepage locale=hr noprompt
87
88         });
89
90 }
91 =head1 debirf
92
93 debirf is a system that will create diskless, all-in-ram images (kernel and initramfs) that boot entirely into ram and leave the user in a fully functional Debian system. 
94
95 L<http://cmrg.fifthhorseman.net/wiki/debirf>
96
97 =cut
98
99 sub debirf {
100         my $ip = shift;
101
102         upstream::iso( 'http://cmrg.mayfirst.org/debirf/debirf-rescue_lenny_2.6.26-1-686.iso' );
103
104         pxelinux::config_for_ip( $ip, qq{
105
106 default linux
107 label linux
108         kernel iso/vmlinuz-2.6.26-1-686
109         append initrd=iso//debirf-rescue_lenny_2.6.26-1-686.cgz
110
111         });
112 }
113
114 sub tinycore {
115         my $ip = shift;
116         upstream::iso( 'http://distro.ibiblio.org/pub/linux/distributions/tinycorelinux/2.x/release/tinycore_2.2.iso' );
117         pxelinux::config_for_ip( $ip, qq{
118
119 default linux
120 label linux
121         kernel iso/boot/bzImage
122         append initrd=iso/boot/tinycore.gz
123
124         });
125 }
126
127 sub in_chroot {
128         my ( $dir, $command ) = @_;
129         write_file "$dir/tmp/inside.sh", $command;
130         system "sudo chroot $dir sh -x /tmp/inside.sh";
131 }
132
133 sub nfsroot {
134         my $ip = shift || die "no ip?";
135
136         my $nfsroot = "$server::base_dir/tftp/nfsroot";
137         my $debian_mirror = server::conf_default( 'debian_mirror', 'http://ftp.debian.org/debian' );
138
139         if ( ! -e $nfsroot ) {
140                 system "sudo apt-get install nfs-kernel-server debootstrap aufs-modules-`uname -r` aufs-tools";
141                 mkdir $nfsroot;
142         }
143
144         my $debootstrap = "$nfsroot/debootstrap";
145
146         if ( ! -e $debootstrap ) {
147
148                 system "sudo debootstrap --arch i386 lenny $debootstrap $debian_mirror";
149
150                 file::append "$debootstrap/etc/apt/sources.list.d/non-free.list", "deb $debian_mirror lenny non-free\n";
151                 in_chroot     $debootstrap => 'apt-get update';
152
153                 file::append "$debootstrap/etc/kernel-img.conf", "do_initrd = Yes\n";
154                 in_chroot     $debootstrap => 'apt-get install -y --force-yes atl2-modules-2.6-686 firmware-bnx2';
155                 in_chroot     $debootstrap => 'apt-get -f install -y --force-yes locales';      # linux-image-2.6-686
156
157                 file::append "$debootstrap/etc/initramfs-tools/modules", "atl2\n";
158                 file::change("$debootstrap/etc/initramfs-tools/initramfs.conf",  'BOOT=local' => 'BOOT=nfs' ) &&
159                 in_chroot     $debootstrap => 'update-initramfs -u';
160
161         }
162
163         my $export = "$nfsroot/$ip";
164         my $br     = "$nfsroot/br/$ip";
165
166         $mounted->{"nfsroot/$ip"} ||= `mount | grep $export`;
167
168         if ( ! $mounted->{"nfsroot/$ip"} ) {
169                 warn "mounting $export";
170
171                 mkdir "$nfsroot/br"     unless -e "$nfsroot/br";
172                 mkdir $br                       unless -e $br;
173                 mkdir $export           unless -e $export;
174
175                 system "sudo mount -t aufs -o br:$br:$debootstrap none $export";
176                 system "sudo exportfs -i -o rw,async,no_root_squash,no_subtree_check,fsid=999 $ip:$export";
177         
178         }
179
180         my $hostname = client::conf( $ip => 'hostname' ) || 'nfsroot';
181         file::replace "$export/etc/hostname", $hostname;
182         file::replace "$export/etc/hosts", "127.0.0.1\tlocalhost $hostname\n";
183         file::replace "$export/etc/resolv.conf", "domain $server::domain_name\nnameserver $server::ip\n";
184         file::replace "$export/etc/rsyslog.d/pxelator.conf", "*.*\t\@$server::ip\n";
185
186         file::append "$debootstrap/etc/network/interfaces", qq{
187
188 auto lo
189 iface lo inet loopback
190
191 allow-hotplug eth0
192 iface eth0 inet dhcp
193
194         };
195
196         map {
197                 file::copy_once $_ => "$export/$_";
198                 file::append "$export/etc/rc.local", $_;
199         } ( '/srv/sysadmin-cookbook/recepies/amt/serial-console.sh' );
200
201         pxelinux::config_for_ip( $ip, qq{
202
203 default nfsroot
204 label nfsroot
205         kernel debootstrap/vmlinuz
206         append initrd=debootstrap/initrd.img root=/dev/nfs nfsroot=$server::ip:$export ro ip=dhcp
207
208         });
209 }
210
211 sub openvz {
212         my $ip = shift;
213         ssh::shell( $ip, 'cat /proc/vz/veinfo', 'vzlist -a', 'uptime', 'free', 'df', 'zfs list' );
214 }
215 sub printer     {}
216 sub wrt         {}
217
218 sub for_ip {
219         my $ip = shift || return;
220         my $config = client::conf( $ip => 'config' ) || return;
221         eval $config . '($ip)';
222 #       $tftp::dir = "$server::base_dir/tftp/$pxelinux::path_prefix";
223 }
224
225 warn 'loaded';
226
227 1;