Subversion Repositories camp_sysinfo_client_3

Rev

Rev 32 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 rodolico 1
#! /usr/bin/env perl
12 rodolico 2
 
26 rodolico 3
use warnings;
4
use strict;
5
 
6
our $VERSION = '1.0';
12 rodolico 7
my $TESTING = 1;
8
 
9
my %installationTypes = (
10
                           'ipfire' => {
11
                               'post process' => \&ipfire
12
                            }
13
                      );
14
 
15
my %installation = (
16
                     'Configuration Directory' => '/etc/camp',
17
                     'Modules Directory' => 'modules',
18
                     'Scripts Directory' => 'scripts',
19
                     'Executable Directory' => '/opt/camp'
20
                   );
21
 
22
my %configuration;
23
 
24
my %packageActions = (
25
                  'modules' => {
26
                     'type'       => 'directory',
27
                     'target'     => 'Modules Directory',
28
                     'owner'      => 'root:root',
29
                     'dir perms'  => '0755',
30
                     'file perms' => '0755'
31
                  },
32
                  'scripts' => {
33
                     'type'       => 'directory',
34
                     'target'     => 'Scripts Directory',
35
                     'owner'      => 'root:root',
36
                     'dir perms'  => '0755',
37
                     'file perms' => '0755'
38
                  },
39
                  'notes' => {
40
                     'type'       => 'file',
41
                     'target'     => 'Configuration Directory',
42
                     'owner'      => 'root:root',
43
                     'dir perms'  => '0755',
44
                     'file perms' => '0644'
45
                  },
46
                  'sysinfo_client' => {
47
                     'type'       => 'file',
48
                     'target'     => 'Executable Directory',
49
                     'owner'      => 'root:root',
50
                     'dir perms'  => '0755',
51
                     'file perms' => '0755',
52
                     'cron'       => &findCron(),
53
                     'link'       => '/usr/bin'
54
                  },
55
                  'install.pl' => {
56
                     'type'       => 'file',
57
                     'target'     => 'Configuration Directory',
58
                     'owner'      => 'root:root',
59
                     'dir perms'  => '0755',
60
                     'file perms' => '0755'
61
                  },
62
                  'sysinfo.conf.template' => {
63
                     'type'       => 'file',
64
                     'target'     => 'Configuration Directory',
65
                     'owner'      => 'root:root',
66
                     'dir perms'  => '0755',
67
                     'file perms' => '0644'
68
                  }
69
               );
70
my $installationType;
71
my $cronLocation; 
72
my $installMode;
73
 
74
# prompt the user for a response, then allow them to enter it
75
# the first response is considered the default and is printed
76
# in all caps if more than one exists
77
# first answer is used if they simply press the Enter
78
# key. The response is returned all lower case if more than one
79
# exists.
80
# it is assumed 
81
sub getAnswer {
82
   my ( $prompt, @answers ) = @_;
83
   my $onlyOneAnswer = scalar( @answers ) == 1;
84
   print $prompt . '[';
85
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
86
   print join( '/', @answers ) . ']: ';
87
   $thisAnswer = <>;
88
   chomp $thisAnswer;
89
   $thisAnswer = $answers[0] unless $thisAnswer;
90
   return $onlyOneAnswer ? lc $thisAnswer : $thisAnswer;
91
}
92
 
93
sub checkInstall {
94
   my $installed = `which sysinfo-client`;
95
   my $action;
96
   chomp $installed;
97
   my $oldconf = (-f '/etc/sysinfo/sysinfo.conf' ) ? '/etc/sysinfo/sysinfo.conf' : '';
98
   if ( $installed ) {
99
      $action = &getAnswer( 'Application appears to already be installed\nWhat do you want to do', ('configure','remove') );
100
   } else {
101
      my $useOldConf = &getAnswer( 'Doing a fresh install, and old sysinfo configuration exists, use it for defaults?',('y','n') ) if $oldconf;
102
      $action = 'install';
103
      $action .= ':' . $oldconf if $useOldConf eq 'y';
104
   }
105
   return $action;
106
}
107
 
108
# simply attempts to detect the operating system so we can do OS
109
# specific actions at the end.   
110
sub getOperatingSystem {
111
   my @OSTypes = ( 'ipfire','debian' );
112
   my $OS = `uname -a`;
113
   foreach $osType ( @OSTypes ) {
114
      return $osType if $OS =~ m/$osType/i;
115
   }
116
   return '';
117
} # getOperatingSystem
118
 
119
# see if we can find a cron of some kind that will run the process
120
# daily
121
sub findCron {
122
   while ( $location = shift ) {
123
      return $location if -d $location;
124
   }
125
   return '';
126
}
127
 
128
sub customizeInstall {
129
   my ( $installation, $cronLocation ) = @_;
130
   # Allow user to customize the installation paths
131
   foreach my $answer ( sort keys %$installation ) {
132
      if ( $answer eq 'Modules Directory' or $answer eq 'Scripts Directory' ) {
133
         $$installation{$answer} = $$installation{'Configuration Directory'} . '/' . $$installation{$answer};
134
      }
135
      $$installation{$answer} = &getAnswer( $answer, ( $$installation{$answer} ) );
136
   }
137
   $$cronLocation = &getAnswer( 'Link executable into which location', ( $$cronLocation ) );
138
}   
139
 
140
sub makeDirectory {
141
   my ( $directory, $owner, $permissions ) = @_;
142
   return if -d $directory;
143
   # make_path is very good and efficient, if File is installed, so we try it
144
   eval { use File::Path 'make_path'; };
145
   if ( $@ ) {
146
      if ( $TESTING ) {
147
         print "Using OS to create $directory\n";
148
         print "mkdir -p $directory\n";
149
         print "chown $owner $directory\n";
150
         print "chmod $permissions $directory\n";
151
         return;
152
      }
153
      `mkdir -p $directory`;
154
      `chown $owner $directory`;
155
      `chmod $permissions $directory`;
156
      die "Could not create directory [$directory]\n" unless -d $directory;
157
   } else { # we could not load library, so do it via the OS
158
      if ( $TESTING ) {
159
         print "Using make_path to create $directory\n";
160
         print "make_path( $directory, { mode => $permissions, owner=>$owner } )\n";
161
         return;
162
      }
163
      make_path( $directory, { mode => $permissions, owner=>$owner, error => \my $err } );
164
      die "Could not create directory [$directory]\n" if @$err;
165
   }
166
}
167
 
168
sub doInstall {
169
   my ( $installation, $cronLocation, $packageActions ) = @_;
170
   foreach my $item ( keys %$packageActions ) {
171
      &makeDirectory( 
172
         $$installation{$$packageActions{$item}{'target'}}, 
173
         $$packageActions{$item}{'owner'}, 
174
         $$packageActions{$item}{'dir perms'} 
175
         );
176
      my $targetDir = $$installation{$$packageActions{$item}{'target'}};
177
      my $perms = $$packageActions{$item}{'file perms'};
178
      my $owner = $$packageActions{$item}{'owner'};
179
      $item .= '/*' if  $$packageActions{$item}{'type'} eq 'directory';
180
      if ( $TESTING ) {
181
         print "mv $item $targetDir\n";
182
         print "chmod $perms \"$targetDir/$item\"\n";
183
         print "chown $owner \"$targetDir/$item\"\n";
184
         # is this one supposed to be run as cron?
185
         if ( $$packageActions{$item}{'cron'} && $cronLocation ) {
186
            print "ln -s \"$targetDir/$item\" \"$cronLocation/$item\"\n";
187
         }
188
      } else {
189
         `mv $item $targetDir`;
190
         `chmod $perms "$targetDir/$item"`;
191
         `chown $owner "$targetDir/$item"`;
192
         # is this one supposed to be run as cron?
193
         if ( $$packageActions{$item}{'cron'} && $cronLocation ) {
194
            `ln -s "$targetDir/$item" "$cronLocation/$item"`;
195
         }
196
         if ( $$packageActions{$item}{'link'} ) {
197
            my $linkTo = $$packageActions{$item}{'link'} . "/$item";
198
            `ln -s "$targetDir/$item" "$linkTo"`;
199
         }
200
      }
201
   } # foreach
202
}
203
 
204
sub readConfiguration {
205
   my ( $configuration,$confFileName ) = @_;
206
}   
207
 
208
sub ipFire {
209
   my @BACKUP_DIRS = ('/opt/sysinfo', '/etc/sysinfo');
210
   my @INSTALL_COMMANDS = (
211
              'cp -av opt /',
212
              'cp -av etc /',
213
              'ln -s /opt/sysinfo/sysinfo /etc/fcron.daily/sysinfo.cron'
214
              );
215
   # now, check for backup directories not in include.user
216
   open BACKUP, '</var/ipfire/backup/include.user';
217
   while ( $line = <BACKUP> ) {
218
      chomp $line;
219
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
220
         if ( $BACKUP_DIRS[$i] eq $line ) {
221
            $BACKUP_DIRS[$i] = '';
222
            last;
223
         } # if
224
      }# for
225
   } # while
226
   close BACKUP;
227
 
228
   # if any remain, append them to include.user
229
   open BACKUP, '>>/var/ipfire/backup/include.user';
230
   foreach my $backupDir ( @BACKUP_DIRS ) {
231
      print BACKUP "$backupDir\n" if $backupDir;
232
   }
233
   close BACKUP;
234
 
235
   # set all modules with ipfire in the name to run
236
   opendir $moduleDir, $MODULES_DIR;
237
   my @modules = grep { /ipfire/ } readdir $moduleDir;
238
   closedir $moduleDir;
239
   foreach my $module (@modules) {
240
      `chmod 755 $MODULES_DIR/$module`;
241
   }
242
}
243
 
244
#####################################################################
245
# Main Program
246
#####################################################################
247
 
248
$installMode = &checkInstall();
249
# if installationType or cronLocation not set, find them
250
$installationType = &getOperatingSystem() unless $installationType;
251
$cronLocation = &findCron( '/etc/cron.daily', '/etc/fcron.daily' ) unless $cronLocation;
252
# allow use to change installation paths
253
&customizeInstall( \%installation,\$cronLocation, \%packageActions );
254
 
255
print "Install mode is: $installMode\n";
256
print "Cron job will be run from: $cronLocation\n";
257
print "The operating system is: $installationType\n";
258
foreach my $answer ( sort keys %installation ) {
259
   print "$answer = $installation{$answer}\n";
260
}
261
print 'Continue? [Y/n]: ';
262
$answer = <>;
263
chomp $answer;
264
$answer = $answer ? uc $answer : 'Y';
265
exit unless $answer eq 'Y';
266
print "Continuing the installation\n";
267
 
268
&doInstall( \%installation, $cronLocation, \%packageActions );
269
 
270
1;