File: AddClients.pm

package info (click to toggle)
systeminstaller 1.04-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 748 kB
  • ctags: 261
  • sloc: perl: 5,769; makefile: 70
file content (214 lines) | stat: -rw-r--r-- 7,851 bytes parent folder | download | duplicates (2)
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
package SystemInstaller::Tk::AddClients;

#   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 of the License, 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

use base qw(Exporter);
use vars qw(@EXPORT %oldvars);
use Data::Dumper;
use AppConfig;
use POSIX;
use Carp;
use Tk;
use Tk::FileSelect;
use SystemInstaller::Tk::Common;
use SystemInstaller::Tk::Help;
use SIS::Image;
use SIS::DB;
use strict;

@EXPORT = qw(addclients_window);


sub addclients_window {
    my $config = init_si_config();

    my $window = shift;
    my %vars = (
                imgname => "",
                basename => "",
                domainname => "",
                startinghostnum => "",
                startip => "",
                netmask => "",
                gateway => "",
                padding => "0",
                numhosts => "",
                noshow => {},
                # This is the dummy postinstall.  Postinstalls MUST return true, lest the sky falls
                postinstall => sub {return 1},
                @_,
               );

    my %defaults = %vars;
    my %noshow = %{$vars{noshow}};

    my $addclient_window = $window->Toplevel();
    $addclient_window->title("Add Clients to a SIS Image");

    #
    #  First line:  What is your image name?
    # 

    # Get the list of images and remove the default selection
    my @allimages= list_image();
    my @mostimages;
    my $defimage;
    foreach my $img (@allimages) {
            if ($img->name eq $vars{imgname}) {
                    $defimage=$vars{imgname};
            } else {
                push(@mostimages,$img->name);
            }
    }
    # If the default selection wasn't found, just use 
    # the first defined image.
    unless ($defimage) {
        $defimage=shift(@mostimages);
    }

    my $imagebox=label_listbox_line($addclient_window, "Image Name", $defimage, \@mostimages,
                     helpbutton($addclient_window, 'Image Name Addclients')) unless $noshow{imgname};
    #
    #  Second line: What is your domain name?
    #

    label_entry_line($addclient_window, "Domain Name", \$vars{domainname},"",
                     helpbutton($addclient_window, 'Domain Name')) unless $noshow{domainname};

    #
    #  Third line: What is the base name?
    #

    label_entry_line($addclient_window, "Base Name", \$vars{basename},"",
                     helpbutton($addclient_window, 'Base Name')) unless $noshow{basename};

    label_entry_line($addclient_window, "Number of Hosts", \$vars{numhosts},"",
                     helpbutton($addclient_window, 'Number of Hosts')) unless $noshow{numhosts};

    label_entry_line($addclient_window, "Starting Number", \$vars{startinghostnum},"",
                     helpbutton($addclient_window,'Starting Number')) unless $noshow{startinghostnum};
    # Number padding
    label_entry_line($addclient_window, "Padding", \$vars{padding},"",
                     helpbutton($addclient_window,'Padding')) unless $noshow{padding};


    #
    #  More lines: Starting IP Addr?
    #



    label_entry_line($addclient_window, "Starting IP", \$vars{startip},"",
                     helpbutton($addclient_window, 'Starting IP')) unless $noshow{startip};
    label_entry_line($addclient_window, "Subnet Mask", \$vars{netmask},"",
                     helpbutton($addclient_window, 'Subnet Mask')) unless $noshow{netmask};
    label_entry_line($addclient_window, "Default Gateway", \$vars{gateway},"",
                     helpbutton($addclient_window, 'Default Gateway')) unless $noshow{gateway};

#    label_entry_line($addclient_window, "Ending IP", \$vars{endip}, [\&compute_hosts, \%vars]) unless $noshow{endip};

    # Then a whole bunch of control buttons

    my $reset_button = $addclient_window->Button(
                                             -text=>"Reset",
                                             -command=> [\&reset_window, $addclient_window, 
                                                         \%vars, \%defaults, {}], # imgname => $imgoption}],
                                             -pady => 8,
                                             -padx => 8,
                                            );

    my $activate_button = $addclient_window->Button(
                                                -text => "Addclients",
                                                -command => [\&run_addclients, $addclient_window, $imagebox, \%vars],
                                                -pady => 8,
                                                -padx => 8,
                                               );

    $reset_button->grid($activate_button, quit_button($addclient_window) , -sticky => "nesw");

}

sub what_changed {
    my $vars = shift;

    foreach my $key (qw(startinghostnum endinghostnum startip endip numhosts)) {
        if($oldvars{$key} ne $$vars{$key}) {
            %oldvars = %$vars;
            return $key;
        }
    }
    return undef;
}

sub reset_window {
    my ($window, $curvars, $defvars, $optiondefaults) = @_;
    resethash($curvars, $defvars);
    foreach my $key (keys %$optiondefaults) {
        if($$optiondefaults{$key} and $$curvars{$key}) {
            $$optiondefaults{$key}->setOption($$curvars{$key});
        }
    }
    return 1;
}

sub run_addclients {
    my ($window, $imagebox, $vars) = @_;
    $window->Busy(-recurse => 1);
    my %hashkeys = (
                    numhosts => 'count',
                    basename => 'basename',
                    startinghostnum => 'start',
                    startip => 'ipstart',
                    gateway => 'gateway',
                    netmask => 'netmask',
                    domainname => 'domain',
                    padding => 'pad',
                   );

    my @imagesel=$imagebox->curselection;
    my $imagename=$imagebox->get($imagesel[0]);

    my $cmd = "mksirange --image $imagename ";
    foreach my $key (keys %hashkeys) {
        if($$vars{$key}) {
            $cmd .= " --$hashkeys{$key}=$$vars{$key}";
        }
    }

    !system($cmd) or (carp("Couldn't run mksirange: $!"),
                      error_window($window,"Couldn't run mksirange: $!"),
                      $window->Unbusy(),
                      return undef);

    if(ref($$vars{postinstall}) eq "CODE") {
        &{$$vars{postinstall}}($vars)  or (carp("Couldn't run postinstall"), 
                                          error_window($window,"There was an error running the post addclients script, please check your logs for more info"),
                                          $window->Unbusy(),
                                          return 0);
    }
    if(ref($$vars{postinstall}) eq "ARRAY") {
        my $sub = shift(@{$$vars{postinstall}});
        &$sub($vars, @{$$vars{postinstall}}) or (carp("Couldn't run postinstall"),
                                                 error_window($window,"There was an error running the post addclients script, please check your logs for more info"),
                                                 $window->Unbusy(),
                                                 return 0);
    }

    done_window($window, "Successfully created clients for image $$vars{imgname}");
    $window->Unbusy();
    return 1;
}

1;