File Coverage

blib/lib/Wizard/Examples/Apache/Host.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   1123 use strict;
  1         2  
  1         37  
4              
5 1     1   5 use Wizard::State ();
  1         2  
  1         14  
6 1     1   38 use Wizard::SaveAble ();
  0            
  0            
7             use Wizard::Examples::Apache::Server ();
8             use File::Spec ();
9             use File::Path ();
10             use Socket ();
11             use Symbol ();
12              
13              
14             package Wizard::Examples::Apache::Host;
15              
16             @Wizard::Examples::Apache::Host::ISA = qw(Wizard::Examples::Apache);
17             $Wizard::Examples::Apache::Host::VERSION = '0.01';
18              
19             sub init {
20             my $self = shift;
21             return $self->SUPER::init(1) unless shift;
22             my $item = $self->{'host'} || die "Missing host";
23             ($self->SUPER::init(1), $item);
24             }
25              
26             sub GetKey { 'host';};
27              
28             sub getFileDir {
29             my($self, $name, $class)= @_;
30             $class ||= (ref($self) || $self); my $meth = $class . '::_superFileDir';
31             my($ofile, $odir) = $self->$meth('');
32             my $key = $class->GetKey();
33             $name ||= $self->{$key}->{'apache-' . $key . '-name'} if ref($self);
34             my $file = File::Spec->catfile($odir, "$name.cfg");
35             my $dir = File::Spec->catdir($odir, $name);
36             wantarray ? ($file, $dir, $odir) : $file;
37             }
38              
39             sub _superFileDir {
40             Wizard::Examples::Apache::getFileDir(shift, shift, 'Wizard::Example::Apache');
41             }
42              
43             sub Load {
44             my ($self, $wiz, $class) = @_;
45             $class ||= (ref($self) || $self);
46             my $key = $class->GetKey();
47             my $meth = $class . '::init'; $self->$meth();
48             my $item = $wiz->param('apache-' . $key) or die "Missing $key specification";
49             $meth = $class . '::getFileDir';
50             my $file = $self->$meth($item);
51             $item = $self->{$key} = Wizard::SaveAble->new($file);
52             $self->Store($wiz);
53             }
54              
55             sub ShowMe {
56             my($self, $wiz, $host) = @_;
57             (['Wizard::Elem::Title',
58             'value' => $host->CreateMe() ?
59             'Apache Wizard: Create a new host' :
60             'Apache Wizard: Edit an existing host'],
61             ['Wizard::Elem::Link'],
62             ['Wizard::Elem::Text', 'name' => 'apache-host-name',
63             'value' => $host->{'apache-host-name'},
64             'descr' => 'Hosts short, descriptive name'],
65             ['Wizard::Elem::Text', 'name' => 'apache-host-descr',
66             'value' => $host->{'apache-host-descr'},
67             'descr' => 'Hosts long, informative description'],
68             ['Wizard::Elem::Text', 'name' => 'apache-host-arch',
69             'value' => $host->{'apache-host-arch'},
70             'descr' => 'Hosts architecture'],
71             ['Wizard::Elem::Text', 'name' => 'apache-host-ip',
72             'value' => $host->{'apache-host-ip'},
73             'descr' => 'Hosts DNS name or IP address'],
74             ['Wizard::Elem::Submit', 'name' => 'Action_HostSave',
75             'value' => 'Save these settings', 'id' => 1],
76             ['Wizard::Elem::Submit', 'name' => 'Action_Reset',
77             'value' => 'Return to host menu', 'id' => 98],
78             ['Wizard::Elem::Submit', 'name' => 'Wizard::Examples::Apache::Action_Reset',
79             'value' => 'Return to top menu', 'id' => 99]);
80             }
81              
82             *Action_CreateHost = \&Action_CreateItem;
83              
84             sub Action_CreateItem {
85             my($self, $wiz, $notshow) = @_;
86             $self->init(); my $key = $self->GetKey();
87              
88             $self->{$key} = Wizard::SaveAble->new();
89             $self->{$key}->CreateMe(1);
90             $self->Store($wiz);
91             $self->ShowMe($wiz, $self->{$key}) unless $notshow;
92             }
93              
94             sub Action_HostSave {
95             my($self, $wiz) = @_;
96             my ($prefs, $basedir, $host) = $self->init(1);
97            
98             my $old_name = $host->{'apache-host-name'};
99             foreach my $opt (qw(apache-host-name apache-host-descr apache-host-arch
100             apache-host-ip)) {
101             $host->{$opt} = $wiz->param($opt) if defined($wiz->param($opt));
102             }
103              
104             # Verify the new settings
105             my $errors = '';
106             my $name = $host->{'apache-host-name'} || '';
107             $errors .= "Missing host name.\n" unless $name;
108             my $ip = $host->{'apache-host-ip'}
109             or ($errors .= "Missing host ip.\n");
110             my $arch = $host->{'apache-host-arch'}
111             or ($errors .= "Missing host architecture.\n");
112              
113             my($file, $dir, $odir) = $self->getFileDir();
114             unless ($name) {
115             if ($host->CreateMe() or $name ne $old_name) {
116             $errors .= "A host $name already exists: $file.\n" if -e $file;
117             $errors .= "A host $name already exists: $dir.\n" if -e $dir;
118             }
119             }
120             if ($ip) {
121             $errors .= "Cannot resolve IP address $ip.\n"
122             unless Socket::inet_aton($ip);
123             }
124             die $errors if $errors;
125              
126             if (!$host->CreateMe() and ($name ne $old_name)) {
127             my $old_file = File::Spec->catfile($odir, "$old_name.cfg");
128             my $old_dir = File::Spec->catdir($odir, "$old_name");
129             rename($old_file, $file)
130             or die "Failed to rename $old_file to $file: $!";
131             rename($old_dir, $dir)
132             or die "Failed to rename $old_dir to $dir: $!";
133             }
134             $host->File($file);
135             if ($host->CreateMe()) {
136             File::Path::mkpath([$dir],0, 0777);
137             die "Couldn't create directory $dir: $!" unless -d $dir;
138             }
139             $host->Modified(1);
140             $self->Store($wiz, 1);
141             $self->Action_Reset($wiz);
142             }
143              
144             sub Action_Reset {
145             my($self, $wiz) = @_;
146             my ($prefs, $basedir) = $self->init();
147              
148             delete $self->{'host'};
149             $self->Store($wiz);
150              
151             # Return the initial menu.
152             (['Wizard::Elem::Title', 'value' => 'Apache Wizard Host Menu'],
153             ['Wizard::Elem::Submit', 'value' => 'Create a new host',
154             'name' => 'Action_CreateHost',
155             'id' => 1],
156             ['Wizard::Elem::Submit', 'value' => 'Modify an existing host',
157             'name' => 'Action_ModifyHost',
158             'id' => 2],
159             ['Wizard::Elem::Submit', 'value' => 'Server Menu',
160             'name' => 'Action_ServerMenu', 'id' => 3],
161             ['Wizard::Elem::Submit', 'value' => 'Delete an existing host',
162             'name' => 'Action_DeleteHost',
163             'id' => 4],
164             ['Wizard::Elem::BR'],
165             ['Wizard::Elem::Submit', 'value' => 'Return to Top Menu',
166             'name' => 'Wizard::Examples::Apache::Action_Reset',
167             'id' => 98],
168             ['Wizard::Elem::Submit', 'value' => 'Exit Apache Wizard',
169             'id' => 99]);
170             }
171              
172              
173             sub Action_EditItem {
174             my $self = shift; my $wiz = shift;
175             my $key = $self->GetKey();
176             $self->Load($wiz);
177             $self->ShowMe($wiz, $self->{$key});
178             }
179              
180             *Action_EditHost = \&Action_EditItem;
181              
182             sub ItemList {
183             my $self = shift; my $basedir = shift;
184             my $fh = Symbol::gensym();
185             opendir($fh, $basedir) || die "Failed to open directory $basedir: $!";
186             my @items = map { (/^(.*)\.cfg$/ and -f File::Spec->catfile($basedir, $_))
187             ? $1 : () } readdir($fh);
188             closedir($fh);
189             @items;
190             }
191              
192             sub Action_ModifyItem {
193             my $self = shift; my $wiz = shift; my $button = shift;
194             my $action = shift; my $descr = shift; my $key = shift;
195             $key ||= $self->GetKey();
196             $self->init();
197             my @ret = $self->getFileDir();
198              
199             my @items = $self->ItemList($ret[2]);
200             return $self->Action_Reset($wiz) unless @items;
201             # Return the initial menu.
202             (['Wizard::Elem::Title', 'value' => "Apache $descr Selection"],
203             ['Wizard::Elem::Select', 'options' => \@items, 'name' => 'apache-' . $key,
204             'descr' => 'Select a directory'],
205             ['Wizard::Elem::Submit', 'value' => $button, 'name' => $action,
206             'id' => 1]);
207            
208             }
209              
210             sub Action_ModifyHost {
211             my $self = shift;
212             $self->Action_ModifyItem(shift, shift || 'Modify host',
213             shift || 'Action_EditHost', 'Host');
214             }
215              
216             sub Action_DeleteHost {
217             my($self, $wiz) = @_;
218             $self->Action_ModifyHost($wiz, 'Delete this host', 'Action_DeleteHost2');
219             }
220              
221             sub Action_ServerMenu {
222             my($self, $wiz) = @_;
223             $self->Action_ModifyHost($wiz, 'Manage servers on this host',
224             'Wizard::Examples::Apache::Server::Action_Enter');
225             }
226              
227             sub Action_DeleteHost2 {
228             my $self = shift; my $wiz = shift;
229             my ($prefs, $basedir) = $self->init();
230             $self->Load($wiz);
231             my $host = $self->{'host'};
232             my ($file, $dir, $odir) = $self->getFileDir();
233              
234             (['Wizard::Elem::Title', 'value' => 'Deleting an Apache host'],
235             ['Wizard::Elem::Link', 'action'=> 'DeleteHost'],
236             ['Wizard::Elem::Data', 'descr' => 'Host name',
237             'value' => $host->{'apache-host-name'}],
238             ['Wizard::Elem::Data', 'descr' => 'Host description',
239             'value' => $host->{'apache-host-descr'}],
240             ['Wizard::Elem::Data', 'descr' => 'Host architecture',
241             'value' => $host->{'apache-host-arch'}],
242             ['Wizard::Elem::Data', 'descr' => 'Hosts DNS name',
243             'value' => $host->{'apache-host-ip'}],
244             ['Wizard::Elem::Submit', 'value' => 'Yes, delete it',
245             'id' => 1, 'name' => 'Action_DeleteHost3'],
246             (-d $dir ? ['Wizard::Elem::Submit',
247             'value' => 'Yes, delete it, including data directory',
248             'id' => 2, 'name' => 'Action_DeleteHost4'] : ()),
249             ['Wizard::Elem::Submit', 'value' => 'Return to Host Menu',
250             'id' => 98, 'name' => 'Action_Reset'],
251             ['Wizard::Elem::Submit', 'value' => 'Return to Top Menu',
252             'id' => 99, 'name' => 'Wizard::Examples::Apache::Action_Reset']);
253             }
254              
255             sub DeleteItem {
256             my $self = shift; my $wiz = shift; my $dirs = shift;
257             $self->init(1); my $key = $self->GetKey();
258             my($file, $dir) = $self->getFileDir();
259             my $item = delete $self->{$key} or die "Missing $key";
260             unlink $file or die "Failed to remove $file: $!";
261             if (-d $dir and $dirs) {
262             require File::Path;
263             File::Path::rmtree([$dir]);
264             die "Failed to remove directory $dir: $!" if -d $dir;
265             }
266             $self->Store($wiz);
267             }
268              
269             sub Action_DeleteHost3 {
270             my($self, $wiz) = @_;
271             $self->DeleteItem($wiz);
272             $self->Action_Reset($wiz);
273             }
274              
275             sub Action_DeleteHost4 {
276             my($self, $wiz) = @_;
277             $self->DeleteItem($wiz, 1);
278             $self->Action_Reset($wiz);
279             }
280