Subversion Repositories camp_sysinfo_client_3

Rev

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

Rev Author Line No. Line
8 rodolico 1
#! /usr/bin/perl -w
2
 
9 rodolico 3
my $TESTING = 1;
4
 
8 rodolico 5
my %installationTypes = (
6
                           'ipfire' => {
7
                               'post process' => \&ipfire
8
                            }
9
                      );
10
 
11
my %installation = (
12
                     '1. Configuration Directory' => '/etc/camp',
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
                  },
9 rodolico 42
                  'sysinfo_client' => {
8 rodolico 43
                     'type'       => 'file',
44
                     'target'     => '4. Executable Directory',
45
                     'owner'      => 'root:root',
46
                     'dir perms'  => '0755',
9 rodolico 47
                     'file perms' => '0755',
48
                     'cron'       => 1
8 rodolico 49
                  },
10 rodolico 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
                  },
8 rodolico 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
               );
66
my $installationType;
67
my $cronLocation; 
10 rodolico 68
my $installMode;
8 rodolico 69
 
10 rodolico 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
}
8 rodolico 88
 
10 rodolico 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 {
8 rodolico 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 '';
10 rodolico 113
} # getOperatingSystem
8 rodolico 114
 
10 rodolico 115
# see if we can find a cron of some kind that will run the process
116
# daily
8 rodolico 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 '2. Modules Directory' or $answer eq '3. Scripts Directory' ) {
129
         $$installation{$answer} = $$installation{'1. Configuration Directory'} . '/' . $$installation{$answer};
130
      }
10 rodolico 131
      $$installation{$answer} = &getAnswer( $answer, ( $$installation{$answer} ) );
8 rodolico 132
   }
10 rodolico 133
   $$cronLocation = &getAnswer( 'Link executable into which location', ( $$cronLocation ) );
8 rodolico 134
}   
135
 
136
sub makeDirectory {
137
   my ( $directory, $owner, $permissions ) = @_;
9 rodolico 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";
10 rodolico 156
         print "make_path( $directory, { mode => $permissions, owner=>$owner } )\n";
9 rodolico 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
   }
8 rodolico 162
}
163
 
164
sub doInstall {
165
   my ( $installation, $cronLocation, $packageActions ) = @_;
166
   foreach my $item ( keys %$packageActions ) {
9 rodolico 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
      }
193
   } # foreach
8 rodolico 194
}
195
 
196
sub readConfiguration {
197
   my ( $configuration,$confFileName ) = @_;
198
}   
199
 
200
sub ipFire {
201
   my @BACKUP_DIRS = ('/opt/sysinfo', '/etc/sysinfo');
202
   my @INSTALL_COMMANDS = (
203
              'cp -av opt /',
204
              'cp -av etc /',
205
              'ln -s /opt/sysinfo/sysinfo /etc/fcron.daily/sysinfo.cron'
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
 
10 rodolico 240
$installMode = &checkInstall();
8 rodolico 241
# if installationType or cronLocation not set, find them
10 rodolico 242
$installationType = &getOperatingSystem() unless $installationType;
8 rodolico 243
$cronLocation = &findCron( '/etc/cron.daily', '/etc/fcron.daily' ) unless $cronLocation;
244
# allow use to change installation paths
245
&customizeInstall( \%installation,\$cronLocation, \%packageActions );
246
 
10 rodolico 247
print "Install mode is: $installMode\n";
248
print "Cron job will be run from: $cronLocation\n";
8 rodolico 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
 
9 rodolico 260
&doInstall( \%installation, $cronLocation, \%packageActions );
8 rodolico 261
 
262
1;