Subversion Repositories camp_sysinfo_client_3

Rev

Rev 10 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 10 Rev 11
Line 1... Line 1...
1
#! /usr/bin/perl -w
1
#! /usr/bin/perl -w
2
 
2
 
3
my $TESTING = 1;
3
# an extremely basic installer for sysinfo-client
4
 
4
 
5
my %installationTypes = (
-
 
6
                           'ipfire' => {
-
 
7
                               'post process' => \&ipfire
-
 
8
                            }
-
 
9
                      );
-
 
10
                      
-
 
11
my %installation = (
-
 
12
                     '1. Configuration Directory' => '/etc/camp',
5
for $dir ( '/opt/camp/sysinfo', '/opt/camp/sysinfo/modules', '/opt/camp/sysinfo/scripts', '/etc/camp', '/etc/camp/usermodules' ) {
13
                     '2. Modules Directory' => 'modules',
-
 
14
                     '3. Scripts Directory' => 'scripts',
-
 
15
                     '4. Executable Directory' => '/usr/bin'
-
 
16
                   );
-
 
17
 
-
 
18
my %configuration;
-
 
19
 
-
 
20
my %packageActions = (
-
 
21
                  'modules' => {
-
 
22
                     'type'       => 'directory',
-
 
23
                     'target'     => '2. Modules Directory',
-
 
24
                     'owner'      => 'root:root',
-
 
25
                     'dir perms'  => '0755',
-
 
26
                     'file perms' => '0755'
-
 
27
                  },
-
 
28
                  'scripts' => {
-
 
29
                     'type'       => 'directory',
-
 
30
                     'target'     => '3. Scripts Directory',
-
 
31
                     'owner'      => 'root:root',
-
 
32
                     'dir perms'  => '0755',
-
 
33
                     'file perms' => '0755'
-
 
34
                  },
-
 
35
                  'notes' => {
-
 
36
                     'type'       => 'file',
-
 
37
                     'target'     => '1. Configuration Directory',
-
 
38
                     'owner'      => 'root:root',
-
 
39
                     'dir perms'  => '0755',
-
 
40
                     'file perms' => '0644'
-
 
41
                  },
-
 
42
                  'sysinfo_client' => {
-
 
43
                     'type'       => 'file',
-
 
44
                     'target'     => '4. Executable Directory',
-
 
45
                     'owner'      => 'root:root',
-
 
46
                     'dir perms'  => '0755',
-
 
47
                     'file perms' => '0755',
-
 
48
                     'cron'       => 1
-
 
49
                  },
-
 
50
                  'install.pl' => {
-
 
51
                     'type'       => 'file',
-
 
52
                     'target'     => '1. Configuration Directory',
-
 
53
                     'owner'      => 'root:root',
-
 
54
                     'dir perms'  => '0755',
-
 
55
                     'file perms' => '0755',
-
 
56
                     'cron'       => 1
-
 
57
                  },
-
 
58
                  'sysinfo.conf.template' => {
-
 
59
                     'type'       => 'file',
-
 
60
                     'target'     => '1. Configuration Directory',
-
 
61
                     'owner'      => 'root:root',
-
 
62
                     'dir perms'  => '0755',
-
 
63
                     'file perms' => '0644'
-
 
64
                  }
-
 
65
               );
6
   `mkdir -p $dir`;
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 = <>;
7
   `chmod 0700 $dir`;
84
   chomp $thisAnswer;
8
   `chown root:root $dir`;
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
}
9
}
103
 
10
 
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 ) {
11
for $dir ( 'modules', 'scripts' ) {
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 '2. Modules Directory' or $answer eq '3. Scripts Directory' ) {
-
 
129
         $$installation{$answer} = $$installation{'1. 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 ) = @_;
12
   `cp -av $dir/* /opt/camp/sysinfo/$dir/`;
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";
13
   `chmod 0700 /opt/camp/sysinfo/$dir/*` if $dir eq 'scripts';
144
         print "mkdir -p $directory\n";
-
 
145
         print "chown $owner $directory\n";
-
 
146
         print "chmod $permissions $directory\n";
14
   `chown root:root /opt/camp/sysinfo/$dir/*`;
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
}
15
}
163
 
16
 
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 ) {
17
for $file ( 'sysinfo-client','notes', 'sysinfo-client.conf.template' ) {
182
            print "ln -s \"$targetDir/$item\" \"$cronLocation/$item\"\n";
-
 
183
         }
-
 
184
      } else {
-
 
185
         `mv $item $targetDir`;
18
   `cp $file /opt/camp/sysinfo/`;
186
         `chmod $perms "$targetDir/$item"`;
19
   `chmod 0700 /opt/camp/sysinfo/$file`;
187
         `chown $owner "$targetDir/$item"`;
20
   `chown root:root /opt/camp/sysinfo/$file`;
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
      }
-
 
193
   } # foreach
-
 
194
}
21
}
195
 
22
 
196
sub readConfiguration {
-
 
197
   my ( $configuration,$confFileName ) = @_;
23
# Create a link into /usr/local/bin
198
}   
-
 
199
 
-
 
200
sub ipFire {
-
 
201
   my @BACKUP_DIRS = ('/opt/sysinfo', '/etc/sysinfo');
24
`chmod 755 /opt/camp/sysinfo/sysinfo-client`;
202
   my @INSTALL_COMMANDS = (
-
 
203
              'cp -av opt /',
-
 
204
              'cp -av etc /',
-
 
205
              'ln -s /opt/sysinfo/sysinfo /etc/fcron.daily/sysinfo.cron'
25
`ln -s /opt/camp/sysinfo/sysinfo-client /usr/local/bin/sysinfo-client`;
206
              );
-
 
207
   # now, check for backup directories not in include.user
-
 
208
   open BACKUP, '</var/ipfire/backup/include.user';
-
 
209
   while ( $line = <BACKUP> ) {
-
 
210
      chomp $line;
-
 
211
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
-
 
212
         if ( $BACKUP_DIRS[$i] eq $line ) {
-
 
213
            $BACKUP_DIRS[$i] = '';
-
 
214
            last;
-
 
215
         } # if
-
 
216
      }# for
-
 
217
   } # while
-
 
218
   close BACKUP;
-
 
219
 
-
 
220
   # if any remain, append them to include.user
-
 
221
   open BACKUP, '>>/var/ipfire/backup/include.user';
-
 
222
   foreach my $backupDir ( @BACKUP_DIRS ) {
-
 
223
      print BACKUP "$backupDir\n" if $backupDir;
-
 
224
   }
-
 
225
   close BACKUP;
-
 
226
 
-
 
227
   # set all modules with ipfire in the name to run
-
 
228
   opendir $moduleDir, $MODULES_DIR;
-
 
229
   my @modules = grep { /ipfire/ } readdir $moduleDir;
-
 
230
   closedir $moduleDir;
-
 
231
   foreach my $module (@modules) {
-
 
232
      `chmod 755 $MODULES_DIR/$module`;
-
 
233
   }
-
 
234
}
-
 
235
 
-
 
236
#####################################################################
-
 
237
# Main Program
-
 
238
#####################################################################
-
 
239
 
-
 
240
$installMode = &checkInstall();
-
 
241
# if installationType or cronLocation not set, find them
-
 
242
$installationType = &getOperatingSystem() unless $installationType;
-
 
243
$cronLocation = &findCron( '/etc/cron.daily', '/etc/fcron.daily' ) unless $cronLocation;
-
 
244
# allow use to change installation paths
-
 
245
&customizeInstall( \%installation,\$cronLocation, \%packageActions );
-
 
246
 
-
 
247
print "Install mode is: $installMode\n";
-
 
248
print "Cron job will be run from: $cronLocation\n";
-
 
249
print "The operaitng system is: $installationType\n";
-
 
250
foreach my $answer ( sort keys %installation ) {
-
 
251
   print "$answer = $installation{$answer}\n";
-
 
252
}
-
 
253
print 'Continue? [Y/n]: ';
-
 
254
$answer = <>;
-
 
255
chomp $answer;
-
 
256
$answer = $answer ? uc $answer : 'Y';
-
 
257
exit unless $answer eq 'Y';
-
 
258
print "Continuing the installation\n";
-
 
259
 
26
 
260
&doInstall( \%installation, $cronLocation, \%packageActions );
27
`configure.pl`;
261
 
28
 
262
1;
29
1;