summaryrefslogtreecommitdiffstats
path: root/pxe_wizard/Pxe.pm
blob: 67ad1bdf865827b079f17d5983cb0da89b9a6462 (plain)
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
#!/usr/bin/perl -w

# Drakwizard

# Copyright (C) 2003 Mandrakesoft
#
# Author: Antoine Ginies <aginies _ateuh _ mandrakesoft.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

package MDK::Wizard::Pxe;
use lib qw(/usr/lib/libDrakX);
use strict;

use common;
use services;
use MDK::Wizard::Wizcommon;

my $wiz = new MDK::Wizard::Wizcommon;

#my $INSTALLDIR = "/var/install/pxe";
#my $IA64PATH = '/IA64PC';

my $TMPDIR = '/tmp';
my $HOSTNAME = chomp_(`hostname`);
my $TFTPDIR = "/var/lib/tftpboot";
my $CLIENTPATH = '/X86PC/linux';
my $X86 = $TFTPDIR . $CLIENTPATH;
my $IMGPATH = $X86 . '/images';
my $PXEHELP = $X86 . '/help.txt';
my $PXEMESSAGE = $X86 . '/messages';
my $PXEDEFAULT = $X86 . '/pxelinux.cfg/default';

my $CONF = '/etc/pxe.conf';
my $SYSLINUXPATH = '/usr/lib/syslinux/';
my $MEMDISK = $SYSLINUXPATH . '/memdisk';
my $XINETDDIR = "/etc/xinetd.d";

my $o = {
	 name => N("PXE Wizard (devel)"),
	 var => {
		 IMG => '/tmp/t.img',
		 PXENAME => '92',
		 DESCR => 'install 9.2',
		 IMGTOREMOVE => '',
		},
	 needed_rpm => [ 'pxe', 'tftp-server', 'xinetd' ],
	};

my %level = (
             1 => N("PXE - Set PXE server"),
             2 => N("add - Add image in PXE"),
	     3 => N("remove - remove image in PXE"),
#	     4 => N("Modify - Modify image in PXE"),
	    );
# to get menu entry
list_menu();

$o->{pages} = {
	       welcome => {
			   name => N("PXE wizard") . "\n\n" . N("Set a PXE server.") . "\n\n" . N("This wizard will help you configuring the PXE server. This configuration will provide a pxe service, and ability to add/remove/modify boot images."),
			   no_back => 1,
			   pre => sub {
			     $o->{var}{wiz_level} ||= 1;
			   },
			   post => sub {
			     if ($o->{var}{wiz_level} == 2) {
			       return 'addimg' }
			     elsif ($o->{var}{wiz_level} == 3) {
			       return 'removeimg' }
			     elsif ($o->{var}{wiz_level} == 4) {
			       return 'modifyimg' }
			   },
			   data => [
				    { label => N("Which operation:"), val => \$o->{var}{wiz_level}, list => [ keys %level ], format => sub { $level{$_[0]} } },
				   ],
			   no_back => 1,
			   next => 'pxeserver',
			  },
	       addimg => {
			  name => N("Add a boot Image") . "\n\n" . N("Path to image: Full path to image") . "\n\n" . N("PXE Name: Name in PXE menu (one word/number, no space)"),
			  data => [
				   { label => "PXE Name:", val => \$o->{var}{PXENAME} },
				   { label => "PXE Description:", val => \$o->{var}{DESCR} },
				   { label => "Full Path to image:", val => \$o->{var}{IMG} },
				  ],
			  post => \&test_data,
			  next => 'summaryadd',
			 },
	       removeimg => {
			     name => N("Remove a boot Image") . "\n\n" . N("Which one ?"),
			     data => [
				      { label => N("Which one:"), val => \$o->{var}{IMGTOREMOVE} },
				     ],
			     next => 'summaryremove',
			    },
	       modifyimg => {
			     name => N("Add Option to boot image") . "\n\n" . N("on which image ?"),
			     data => [
				      { label => "Wich one ?", val => \$o->{var}{PXENAME} },
				     ],
			     next => 'summarymodify',
			    },
	       pxeserver => {
			     name => N("Set PXE server") . "\n\n" . N("We will use a special dhcpd.conf"),
			     next => 'summaryserver',
			    },
	       error_dir => {
			     name => N('Error Should be a directory'),
			     next => '',
			    },
	       error_img => {
			     name => N('Please provide a Bootable image...'),
			     next => 'addimg',
			    },
	       error_name => {
			      name => N('Please provide a correct name in PXE entry (one word)'),
			      next => 'addimg',
			     },
	       simmilar_entry => {
				  name => N('Similar Name is already used in PXE menu entry') . "\n\n" . N("Please provide another Pxe Menu Name"),
				  next => 'addimg',
				 },
	       summaryserver => {
				 name => N('Now will prepapre all default files to get PXE server'),
				 data => [
					  { label => N("Tftp directory"), fixed_val => \$TFTPDIR },
					  { label => N("Image path"), fixed_val => \$IMGPATH },
					  { label => N("PXE \'default\' file "), fixed_val => \$PXEDEFAULT },
					  { label => N("PXE \'help\' file"), fixed_val => \$PXEHELP },
					 ],
				 post => \&do_it_pxe,
				 next => 'endserver',
				},
	       summarymodify => {
				 name => N('Now will Modify boot options in image'),
				 post => \&do_it_modify,
				 next => 'endmodify',
				},
	       summaryremove => {
				 name => N('Now will remove your PXE boot image'),
				 data => [
					  { label => N("PXE entry to remove:"), fixed_val => \$o->{var}{IMGTOREMOVE} },
					 ],
				 post => \&do_it_remove,
				 next => 'endremove',
				},
	       summaryadd => {
			      name => N('Now will add your PXe boot image'),
				 data => [
					  { label => "PXE Name:", fixed_val => \$o->{var}{PXENAME} },
					  { label => "PXE Description:", fixed_val => \$o->{var}{DESCR} },
					  { label => "Path to image:", fixed_val => \$o->{var}{IMG} },
					 ],
			      post => \&do_it_add,
			      next => 'endadd',
			     },
	       endadd => {
			     name => N('Congratulations'),
			     data => [ { label => N('The wizard successfully add a PXE image.') } ],
                             no_back => 1,
                             end => 1,
                             next => 0
			    },
	       endremove => {
			     name => N('Congratulations'),
			     data => [ { label => N('The wizard successfully remove a PXE image.') } ],
                             no_back => 1,
                             end => 1,
                             next => 0
			    },
	       endmodify => {
			     name => N('Congratulations'),
			     data => [ { label => N('The wizard successfully modify image(s).') } ],
                             no_back => 1,
                             end => 1,
                             next => 0
			    },
	       endserver => {
			     name => N('Congratulations'),
			     data => [ { label => N('The wizard successfully configured Your PXE server.') } ],
                             no_back => 1,
                             end => 1,
                             next => 0
			    },
	      };

sub memlinux_prep {
  if (!-f "$X86/memdisk") {
    cp_af($MEMDISK, $X86);
  }
}

sub test_data {
  $o->{var}{PXENAME} =~ /^\w+$/ or return 'error_name';
  if (!-f $o->{var}{IMG}) {
    return 'error_img'
  }
  if (any { /^$o->{var}{PXENAME}/ } cat_($PXEHELP)) {
    return 'simmilar_entry'
  }
}

sub enable_tftps {
  substInFile { s/disable.*/disable = no/ } "$XINETDDIR/tftp";
}

sub tftp_blksize {
  # $o should be with W or not N
  my ($o) = @_;
  if ($o =~ /W/) {
    substInFile { s/server_args.*/server_args = -r blksize -s $TFTPDIR/ } "$XINETDDIR/tftp";
  } else {
    substInFile { s/server_args.*/server_args = -s $TFTPDIR/ } "$XINETDDIR/tftp";
  }
}


sub crea_wdir {
  if (-e $TMPDIR) { rm_rf($TMPDIR);
	     }
  mkdir_p($TMPDIR);
}

sub add_in_default {
  my ($NAME) = @_;
  if (!any { /${NAME}_BEGIN/ } cat_($PXEDEFAULT)) {
    append_to_file($PXEDEFAULT ,<<EOF);
# TAG: ${NAME}_BEGIN
label $NAME
      KERNEL memdisk
      APPEND initrd=images/$NAME.img
# TAG: ${NAME}_END
EOF
  }
}

sub add_in_help {
  my ($NAME, $DESCR) = @_;
  append_to_file($PXEHELP ,<<EOF);
$NAME : $DESCR
EOF
}

sub add_img {
  my ($SIMG, $PXENAME) = @_;
  cp_af($SIMG, $IMGPATH . "/" . $PXENAME . '.img');
}

sub get_menu_entry {
  my ($NAME) = @_;
  my $conf = cat_($PXEDEFAULT) . "#EndOfFile";
  my ($NAME_bloc) = $conf =~ /(# TAG: ${NAME}_BEGIN.*?# TAG: ${NAME}_END)#EndOfFile/s or die "unable to find Menu entry section";
  print "$NAME_bloc";
}

sub remove_in_menu {
  my ($NAME) = @_;
  my $conf = cat_($PXEDEFAULT) . "#EndOfFile";
  my ($begin, $NAME_bloc, $end) = $conf =~ /(.*?)(# TAG: ${NAME}_BEGIN.*?# TAG: ${NAME}_END)\n(.*)#EndOfFile/s or die "unable to find Menu entry section";
  output($PXEDEFAULT, $begin, $end);
}

sub list_menu {
  foreach (cat_($PXEDEFAULT)) {
    my ($ent) = /\# TAG: (\w+)_BEGIN/;
    ! $ent or print "$ent\n";
  }
}

sub remove_in_help {
  my ($NAME) = @_;
  substInFile {
    s/^$NAME :.*//x;
  } $PXEHELP;
}

sub remove_img {
  my ($NAME) = @_;
  rm_rf($IMGPATH . "/" . $NAME . '.img');
}

sub default_pxe_menu {
  output($PXEHELP, <<EOF);
Available images are:
---------------------
local: local boot
EOF

  output($PXEDEFAULT, <<EOF);
PROMPT 1
DEFAULT local
DISPLAY messages
TIMEOUT 50
F1 help.txt
label local
      LOCALBOOT 0

EOF
}

sub default_pxe_messages {
  output($PXEMESSAGE, <<EOF);

                   Welcome to Mandrake Linux PXE Server
                                                       Pxelinux
              .              .-----------------------------------.
             /|\\            /    Press F1 for available images    \\
            /_|_\\           \\    Hosted by  $HOSTNAME
            \\ | /   _       /'-----------------------------------'
             \\|/   (')     /
              '.    U     /                                (O__
                 .   '.  /                 (o_  (o_  (0_   //\\
                 {o_   (o_  (o_  (o_  (o_  //\\  //\\  //\\  //  )
                 (')_  (`)_ (/)_ (/)_ (/)_ V_/_ V_/_ V_/_ V__/_
            ---------------------------------------------------------

 press F1 for help
EOF

}

sub do_it_add {
  return if $::testing;
  add_in_help($o->{var}{PXENAME}, $o->{var}{DESCR});
  add_in_default($o->{var}{PXENAME});
  add_img($o->{var}{IMG}, $o->{var}{PXENAME});
}

sub do_it_remove {
  return if $::testing;
  remove_in_menu($o->{var}{IMGTOREMOVE});
  remove_in_help($o->{var}{IMGTOREMOVE});
  remove_img($o->{var}{IMGTOREMOVE});
}

sub do_it_modify {
  return if $::testing;
}

sub do_it_pxe {
  return if $::testing;
  my $in = 'interactive'->vnew('su', 'PXE server');
  my $w = $in->wait_message(N("PXE server"), N("Configuring PXE server on your system..."));
  memlinux_prep();
  default_pxe_menu();
  default_pxe_messages();
  enable_tftps();
  tftp_blksize('N');
  map { system("service $_ restart"); $_ } qw(xinetd pxe);
  undef $w;
}


sub new {
    my ($class, $conf) = @_;
    bless {
            o   => $o,
	   }, $class;
}

1;