File Coverage

blib/lib/Wizard/Examples/Apache/Server.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             # -*- perl -*-
2              
3 1     1   661 use strict;
  1         3  
  1         30  
4              
5 1     1   6 use Wizard::State ();
  1         2  
  1         16  
6 1     1   35 use Wizard::SaveAble ();
  0            
  0            
7             use Wizard::Examples::Apache::Host ();
8             use Wizard::Examples::Apache::VirtualServer ();
9             use Wizard::Examples::Apache::Directory ();
10             use File::Spec ();
11             use File::Path ();
12             use Socket ();
13             use Symbol ();
14              
15              
16             package Wizard::Examples::Apache::Server;
17              
18             @Wizard::Examples::Apache::Server::ISA = qw(Wizard::Examples::Apache::Host);
19             $Wizard::Examples::Apache::Server::VERSION = '0.01';
20              
21              
22             sub init {
23             my $self = shift;
24             return $self->SUPER::init(1) unless shift;
25             my $item = $self->{'server'} || die "Missing server";
26             ($self->SUPER::init(1), $item);
27             }
28              
29             sub GetKey { 'server';};
30              
31              
32             sub _superFileDir {
33             Wizard::Examples::Apache::Host::getFileDir(shift, shift,
34             'Wizard::Examples::Apache::Host');
35             }
36              
37             sub ShowMe {
38             my($self, $wiz, $server) = @_;
39             my $hname = $self->{'host'}->{'apache-host-name'};
40             my $uwdir = File::Spec->catdir(File::Spec->rootdir(), 'usr','local', 'www');
41             (['Wizard::Elem::Title',
42             'value' => $server->CreateMe() ?
43             'Apache Wizard: Create new server' :
44             'Apache Wizard: Edit an existing server'],
45             ['Wizard::Elem::Text', 'name' => 'apache-server-name',
46             'value' => $server->{'apache-server-name'},
47             'descr' => 'Server descriptive name'],
48             ['Wizard::Elem::Text', 'name' => 'apache-server-ip',
49             'value' => $server->{'apache-server-ip'} || $hname,
50             'descr' => 'Server DNS name or ip adress'],
51             ['Wizard::Elem::Text', 'name' => 'apache-server-vserver-root',
52             'value' => $server->{'apache-server-vserver-root'} ||
53             $uwdir,
54             'descr' => 'Default directory for the VirtualServer root dirs'],
55             # ['Wizard::Elem::Text', 'name' => 'apache-server-root',
56             # 'value' => $server->{'apache-server-root'} ||
57             # File::Spec->catfile($uwdir, $hname),
58             # 'descr' => 'Server root directory'],
59             ['Wizard::Elem::Text', 'name' => 'apache-server-admin',
60             'value' => $server->{'apache-server-admin'} ||
61             "webmaster\@$hname",
62             'descr' => 'Server administrator'],
63             ['Wizard::Elem::Text', 'name' => 'apache-server-http-port',
64             'value' => $server->{'apache-server-http-port'} || '80',
65             'descr' => 'Server HTTP port'],
66             ['Wizard::Elem::Text', 'name' => 'apache-server-https-port',
67             'value' => $server->{'apache-server-https-port'} || '443',
68             'descr' => 'Server HTTPS port'],
69             # ['Wizard::Elem::Text', 'name' => 'apache-server-index',
70             # 'value' => $server->{'apache-server-index'},
71             # 'descr' => 'Server DirectoryIndex'],
72             # ['Wizard::Elem::Text', 'name' => 'apache-server-options',
73             # 'value' => $server->{'apache-server-options'},
74             # 'descr' => 'Server Options'],
75             ['Wizard::Elem::Submit', 'name' => 'Action_ServerSave',
76             'value' => 'Save these settings',
77             'id' => '1'],
78             ['Wizard::Elem::BR'],
79             ['Wizard::Elem::Submit', 'name' => 'Action_Reset',
80             'value' => 'Return to Server menu ',
81             'id' => '97'],
82             ['Wizard::Elem::Submit', 'name' => 'Wizard::Examples::Apache::Host::Action_Reset',
83             'value' => 'Return to Host menu ',
84             'id' => '97'],
85             ['Wizard::Elem::Submit', 'value' => 'Return to Top Menu',
86             'name' => 'Wizard::Examples::Apache::Action_Reset',
87             'id' => 98],
88             ['Wizard::Elem::Submit', 'value' => 'Exit Apache Wizard',
89             'id' => 99]);
90             }
91              
92             sub Action_Enter {
93             my($self, $wiz) = @_;
94             $self->Load($wiz, 'Wizard::Examples::Apache::Host');
95             $self->Action_Reset($wiz);
96             }
97              
98             sub Action_Reset {
99             my $self = shift; my $wiz=shift;
100             my($prefs, $basedir, $host) = $self->init();
101             delete $self->{'server'};
102             $self->Store($wiz);
103              
104             # Return the initial menu.
105             (['Wizard::Elem::Title', 'value' => 'Apache Wizard Server Menu'],
106             ['Wizard::Elem::Submit', 'value' => 'Create a new server',
107             'name' => 'Action_CreateServer', 'id' => 1],
108             ['Wizard::Elem::Submit', 'value' => 'Modify an existing server',
109             'name' => 'Action_ModifyServer', 'id' => 2],
110             ['Wizard::Elem::Submit', 'value' => 'Virtual Server Menu',
111             'name' => 'Action_VServerMenu', 'id' => 3],
112             ['Wizard::Elem::Submit', 'value' => 'Delete an existing server',
113             'name' => 'Action_DeleteServer',
114             'id' => 4],
115             ['Wizard::Elem::BR'],
116             ['Wizard::Elem::Submit', 'value' => 'Return to Top Menu',
117             'name' => 'Wizard::Examples::Apache::Action_Reset',
118             'id' => 97],
119             ['Wizard::Elem::Submit', 'value' => 'Return to Host Menu',
120             'name' => 'Wizard::Examples::Apache::Host::Action_Reset',
121             'id' => 98],
122             ['Wizard::Elem::Submit', 'value' => 'Exit Apache Wizard',
123             'id' => 99]);
124             }
125              
126             *Action_CreateServer = \&Wizard::Examples::Apache::Host::Action_CreateItem;
127              
128             sub Action_ServerSave {
129             my($self, $wiz) = @_;
130             my($prefs, $basedir, $host, $server) = $self->init(1);
131             $self->init(1);
132             my $hname = $host->{'apache-host-name'};
133            
134             my $old_name = $server->{'apache-server-name'};
135             foreach my $opt ($wiz->param()) {
136             $server->{$opt} = $wiz->param($opt)
137             if (($opt =~ /^apache\-server/) && (defined($wiz->param($opt))));
138             }
139            
140             # Verify the new settings
141             my $errors = '';
142             my $name = $server->{'apache-server-name'}
143             or ($errors .= "Missing server name.\n");
144             my $ip = $server->{'apache-server-ip'}
145             or ($errors .= "Missing server ip\n");
146             my $http = $server->{'apache-server-http-port'}
147             or ($errors .= "Missing http port\n");
148             my $https = $server->{'apache-server-https-port'}
149             or ($errors .= "Missing https port\n");
150             # my $sroot = $server->{'apache-server-root'}
151             # or ($errors .= "Missing server root dir\n");
152             my $vsroot = $server->{'apache-server-vserver-root'}
153             or ($errors .= "Missing default root dir for the virtual servers\n");
154             my $admin = $server->{'apache-server-admin'}
155             or ($errors .= "Missing admin\n");
156              
157             my($file, $dir, $odir) = $self->getFileDir();
158             unless ($name) {
159             if($server->CreateMe() or $name ne $old_name) {
160             $errors .= "A server $name already exists: $file.\n" if -e $file;
161             $errors .= "A server $name already exists: $dir.\n" if -e $dir;
162             }
163             }
164             if ($ip) {
165             $errors .= "Cannot resolve IP address or DNS name $ip.\n"
166             unless Socket::inet_aton($ip);
167             }
168             die $errors if $errors;
169             if(!$server->CreateMe() and ($name ne $old_name)) {
170             my $old_file = File::Spec->catfile($odir, "$old_name.cfg");
171             my $old_dir = File::Spec->catdir($odir, $old_name);
172             rename($old_file, $file)
173             or die "Failed to rename $old_file to $file: $!";
174             rename($old_dir, $dir)
175             or die "Failed to rename $old_dir to $dir: $!";
176             }
177             $server->File($file);
178             if ($server->CreateMe()) {
179             File::Path::mkpath([$dir], 0, 0777);
180             die "Couldn't create directory $dir" unless -d $dir;
181             Wizard::Examples::Apache::VirtualServer::createDefault($self, $name, $vsroot, $dir);
182             }
183             $server->Modified(1);
184             $self->Store($wiz, 1);
185             $self->Action_Reset($wiz);
186             }
187              
188             *Action_EditServer = \&Wizard::Examples::Apache::Host::Action_EditItem;
189              
190             sub Action_ModifyServer {
191             my $self = shift;
192             $self->Action_ModifyItem(shift, shift || 'Modify server',
193             shift || 'Action_EditServer', 'Server');
194             }
195              
196             sub Action_DeleteServer {
197             shift->Action_ModifyServer(shift, 'Delete this server',
198             'Action_DeleteServer2');
199             }
200              
201             sub Action_VServerMenu {
202             shift->Action_ModifyServer(shift, 'Manage virtual servers on this server',
203             'Wizard::Examples::Apache::VirtualServer::Action_Enter');
204             }
205              
206             sub Action_DeleteServer2 {
207             my $self = shift; my $wiz = shift;
208             my ($prefs, $basedir, $host) = $self->init();
209             $self->Load($wiz);
210             my $server = $self->{'server'};
211             my ($file, $dir) = $self->getFileDir();
212              
213             (['Wizard::Elem::Title', 'value' => 'Deleting an Apache server'],
214             ['Wizard::Elem::Data', 'descr' => 'Server name',
215             'value' => $server->{'apache-server-name'}],
216             ['Wizard::Elem::Data', 'descr' => 'Server DNS name',
217             'value' => $server->{'apache-server-ip'}],
218             ['Wizard::Elem::Data', 'descr' => 'Server root',
219             'value' => $server->{'apache-server-root'}],
220             ['Wizard::Elem::Data', 'descr' => 'Server admin',
221             'value' => $server->{'apache-server-admin'}],
222             ['Wizard::Elem::Data', 'descr' => 'Server HTTP port',
223             'value' => $server->{'apache-server-http-port'}],
224             ['Wizard::Elem::Data', 'descr' => 'Server HTTPS port',
225             'value' => $server->{'apache-server-https-port'}],
226             ['Wizard::Elem::Submit', 'value' => 'Yes, delete it',
227             'id' => 1, 'name' => 'Action_DeleteServer3'],
228             (-d $dir ? ['Wizard::Elem::Submit',
229             'value' => 'Yes, delete it, including data directory',
230             'id' => 2, 'name' => 'Action_DeleteServer4'] : ()),
231             ['Wizard::Elem::Submit', 'value' => 'Return to Host Menu',
232             'id' => 97, 'name' => 'Action_Reset'],
233             ['Wizard::Elem::Submit', 'value' => 'Return to Top Menu',
234             'id' => 98, 'name' => 'Wizard::Examples::Apache::Host::Action_Reset'],
235             ['Wizard::Elem::Submit', 'value' => 'Exit Apache Wizard',
236             'id' => 99]);
237            
238             }
239              
240              
241             sub Action_DeleteServer3 {
242             my($self, $wiz) = @_;
243             $self->DeleteItem($wiz);
244             $self->Action_Reset($wiz);
245             }
246              
247             sub Action_DeleteServer4 {
248             my($self, $wiz) = @_;
249             $self->DeleteItem($wiz,1);
250             $self->Action_Reset($wiz);
251             }