Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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