Subversion Repositories camp_sysinfo_client_3

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
 
6
our $VERSION = '1.1.3';
7
 
8
# find our location and use it for searching for libraries
9
BEGIN {
10
   use FindBin;
11
   use File::Spec;
12
   use lib File::Spec->catdir($FindBin::Bin);
13
}
14
 
15
use sysinfoconf;
16
use File::Basename;
17
 
18
use Data::Dumper;
19
 
20
# $TESTING can have the following values
21
# 0 - do everything
22
# 1 - Do everything except the install, display what would be done
23
# 2 - Be verbose to STDERR
24
# 3 - Be very verbose
25
my $TESTING = 1; # if test mode, simply show what would be done
26
 
27
my $status; # exit status of the processes
28
my $sourceDir = File::Spec->catdir($FindBin::Bin);
29
 
30
 
31
my %install = (  'bindir' => '/opt/camp/sysinfo-client',
32
                 'confdir' => '/etc/camp/sysinfo-client',
33
                 'files' => {
34
                           'configure.pl' => { 
35
                                 'type' => 'file',
36
                                 'permission' => '700', 
37
                                 'owner' => 'root:root', 
38
                                 'target' =>  '<bindir>'
39
                              },
40
                           'sysinfo-client' => { 
41
                                 'type' => 'file',
42
                                 'permission' => '700',
43
                                 'owner' => 'root:root',
44
                                 'target' =>  '<bindir>'
45
                              },
46
                           'sysinfoconf.pm' => {
47
                                 'type' => 'file',
48
                                 'permission' => '600',
49
                                 'owner' => 'root:root',
50
                                 'target' =>  '<bindir>'
51
                              },
52
                           'debian.pm' => {
53
                                 'type' => 'file',
54
                                 'permission' => '600',
55
                                 'owner' => 'root:root',
56
                                 'target' =>  '<bindir>' 
57
                              },
58
                           'ipfire.pm' => { 
59
                                 'type' => 'file',
60
                                 'permission' => '600',
61
                                 'owner' => 'root:root',
62
                                 'target' =>  '<bindir>'
63
                              },
64
                           'notes' => { 
65
                                 'type' => 'file',
66
                                 'permission' => '600', 
67
                                 'owner' => 'root:root', 
68
                                 'target' =>  '<bindir>'
69
                              },
70
                           'sysinfo-client.conf.template' => { 
71
                                 'type' => 'file',
72
                                 'permission' => '600', 
73
                                 'owner' => 'root:root', 
74
                                 'target' =>  '<bindir>' 
75
                              },
76
                           'uninstall.pl' => { 
77
                                 'type' => 'file',
78
                                 'permission' => '700', 
79
                                 'owner' => 'root:root', 
80
                                 'target' =>  '<bindir>' 
81
                              },
82
                           'getSendEmail.pl' => { 
83
                                 'type' => 'file',
84
                                 'permission' => '700', 
85
                                 'owner' => 'root:root', 
86
                                 'target' =>  '<bindir>' 
87
                              },
88
                           'upgrade.pl' => { 
89
                                 'type' => 'file',
90
                                 'permission' => '700', 
91
                                 'owner' => 'root:root', 
92
                                 'target' =>  '<bindir>' 
93
                              },
94
                           'install.pl' => {
95
                                 'type' => 'file',
96
                                 'permission' => '700', 
97
                                 'owner' => 'root:root', 
98
                                 'target' =>  '<bindir>' 
99
                              },
100
                           'MANIFEST' => {
101
                                 'type' => 'file',
102
                                 'permission' => '600', 
103
                                 'owner' => 'root:root', 
104
                                 'target' =>  '<bindir>' 
105
                              },
106
                           'sysinfo-client.seed.example' => { 
107
                                 'type' => 'file',
108
                                 'permission' => '600', 
109
                                 'owner' => 'root:root', 
110
                                 'target' =>  '<bindir>' 
111
                              },
112
                           'VERSION' => { 
113
                                 'type' => 'file',
114
                                 'permission' => '600', 
115
                                 'owner' => 'root:root', 
116
                                 'target' =>  '<bindir>' 
117
                              },
118
                              'modules' => {
119
                                 'type' => 'directory',
120
                                 'permission' => '700', 
121
                                 'owner' => 'root:root', 
122
                                 'target' =>  '<bindir>',
123
                                 'action' => 'chmod 700 *'
124
                              },
125
                              'scripts' => {
126
                                 'type' => 'directory',
127
                                 'permission' => '700', 
128
                                 'owner' => 'root:root', 
129
                                 'target' =>  '<bindir>',
130
                                 'action' => 'chmod 700 *'
131
                              },
132
                              'sysinfo-client.conf' => {
133
                                 'type' => 'file',
134
                                 'create' => '<bindir>/configure.pl',
135
                                 'permission' => '700',
136
                                 'owner'      => 'root',
137
                                 'target'     => '<confdir>'
138
                              }
139
                     }
140
                  );
141
 
142
# hash to set up os specific rules                  
143
my %operatingSystems = (
144
                  'debian' => {
145
                     'bindir' => '/opt/camp/sysinfo-client',
146
                     'confdir' => '/etc/camp/sysinfo-client',
147
                     'crontab' => '/etc/cron.daily/sysinfo-client',
148
                     'modules' => '/^((dpkg)|(unix)|(ipmi))/',
149
                  },
150
                  'ipfire' => {
151
                     'bindir' => '/opt/camp/sysinfo-client',
152
                     'confdir' => '/etc/camp/sysinfo-client',
153
                     'crontab' => '/etc/fcron.daily/sysinfo-client.fcron',
154
                     'modules' => '/^((ipfire)|(unix))/',
155
                  }
156
                  );
157
 
158
 
159
# attempt to locate the operating system.
160
# if found, will set some defaults for it.
161
sub getOS {
162
   my ( $installs, $operatingSystems ) = @_;
163
   my $osString = `uname -a`;
164
   foreach my $osType ( keys %$operatingSystems ) {
165
      next unless $osString =~ m/$osType/i;
166
      # We found the OS, set up some defaults
167
      $$installs{'os'} = $osType;
168
      for my $key ( keys $$operatingSystems{ $osType } ) {
169
         $$installs{$key} = $operatingSystems{ $osType }{$key};
170
      } # if it is a known OS
171
      return 1;
172
   }
173
   return 0;
174
} # getOperatingSystem
175
 
176
 
177
sub populateSourceDir {
178
   my ( $install, $sourceDir ) = @_;
179
   my $bindir = $$install{'bindir'};
180
   my $confdir = $$install{'confdir'};
181
 
182
   my $allFiles = $$install{'files'};
183
 
184
   foreach my $dir ( keys %$allFiles ) {
185
      if ( defined( $$allFiles{$dir}{'type'} ) && $$allFiles{$dir}{'type'} eq 'directory' ) {
186
         print "Found directory $dir\n" if $TESTING > 2;
187
         if ( opendir( my $dh, "$sourceDir/$dir" ) ) {
188
            my @files = map{ $dir . '/' . $_ } grep { ! /^\./ && -f "$sourceDir/$dir/$_" } readdir( $dh );
189
            print "\tFound files " . join( ' ', @files ) . "\n" if $TESTING > 2;
190
            foreach my $file ( @files ) {
191
               $$allFiles{ $file }{'type'} = 'file';
192
               $$allFiles{ $file }{'permission'} = $$allFiles{ $dir }{'permission'};
193
               $$allFiles{ $file }{'owner'} = $$allFiles{ $dir }{'owner'};
194
               $$allFiles{ $file }{'target'} = $$allFiles{ $dir }{'target'};
195
            } # foreach
196
            closedir $dh;
197
         } # if opendir
198
      } # if it is a directory
199
   } # foreach
200
   print Dumper( $install ) if $TESTING > 2;
201
   foreach my $file ( keys %$allFiles ) {
202
      print STDERR "$file\t$bindir\t$$allFiles{$file}{'target'}\n" if $TESTING > 1;
203
      $$allFiles{$file}{'source'} = "$sourceDir/$file";
204
      $$allFiles{$file}{'target'} =~ s/<bindir>/$bindir/gi;
205
      $$allFiles{$file}{'target'} =~ s/<confdir>/$confdir/gi;
206
      $$allFiles{$file}{'target'} .= "/$file";
207
      if ( $$allFiles{$file}{'create'} ) {
208
         $$allFiles{$file}{'create'} =~ s/<bindir>/$bindir/gi;
209
         $$allFiles{$file}{'create'} =~ s/<confdir>/$confdir/gi;
210
         $$allFiles{$file}{'create'} .= "/file";
211
      }
212
 
213
   } # foreach
214
 
215
   return 1;
216
} # populateSourceDir
217
 
218
# find versions of all files so we can do an upgrade vs a full installation
219
sub getVersions {
220
   my $install = shift;
221
   my $currentVersionFile = $$install{'files'}{'VERSION'}{'target'};
222
   my $newVersionFile = $$install{'files'}{'VERSION'}{'source'};
223
   if ( open FILE,"<$currentVersionFile" ) {
224
      while ( my $line = <FILE> ) {
225
         chomp $line;
226
         my ( $filename, $version, $checksum ) = split( "\t", $line );
227
         $$install{'files'}{$filename}{'installed version'} = $version ? $version : '';
228
#         $$install{'files'}{$filename}{'installed checksum'} = $checksum ? $checksum : '';
229
      }
230
      close FILE;
231
   }
232
   if ( open FILE,"<$newVersionFile" ) {
233
      while ( my $line = <FILE> ) {
234
         chomp $line;
235
         my ( $filename, $version, $checksum ) = split( "\t", $line );
236
         $$install{'files'}{$filename}{'new version'} = $version ? $version : '';
237
#         $$install{'files'}{$filename}{'new checksum'} = $checksum ? $checksum : '';
238
      }
239
      close FILE;
240
   }
241
   return 1;
242
} # getVersions
243
 
244
sub runCommand {
245
   while ( my $command = shift ) {
246
      if ( $TESTING ) {
247
         print "$command\n";
248
      } else {
249
         `$command`;
250
      }
251
   }
252
   return 1;
253
} # runCommand
254
 
255
my %directories; # holds list of directories already created
256
 
257
sub checkDirectoryExists {
258
   my ( $filename,$mod,$owner ) = @_;
259
   $mod = "0700" unless $mod;
260
   $owner = "root:root" unless $owner;
261
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
262
   my ($fn, $dirname) = fileparse( $filename );
263
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
264
   return '' if exists $directories{$dirname};
265
   if ( -d $dirname ) {
266
      $directories{$dirname} = 1;
267
      return '';
268
   }
269
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
270
      $directories{$dirname} = 1;
271
   }
272
   return '';   
273
}
274
 
275
 
276
sub doInstall {
277
   my $install = shift;
278
   my $fileList = $$install{'files'};
279
 
280
   &checkDirectoryExists( $$install{'bindir'} . '/' );
281
   foreach my $file ( keys %$fileList ) {
282
      next unless ( $$fileList{$file}{'type'} && $$fileList{$file}{'type'} eq 'file' );
283
      next if $$fileList{$file}{'installed version'} &&
284
              $$fileList{$file}{'new version'} eq $$fileList{$file}{'installed version'};
285
      &checkDirectoryExists( $$fileList{$file}{'target'} );
286
      &runCommand( 
287
            "cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
288
            "chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
289
            "chown  $$fileList{$file}{'owner'} $$fileList{$file}{'target'}"
290
            );
291
   } # foreach file
292
   return 1;
293
}
294
 
295
# figure out if we know our operating system
296
$status = &getOS( \%install, \%operatingSystems );
297
 
298
# based on the defaults, flesh out the install hash
299
$status = &populateSourceDir( \%install, $sourceDir );
300
 
301
$status = &getVersions( \%install );
302
 
303
$status = &doInstall( \%install );
304
 
305
print Dumper( \%install ) if $TESTING > 2;