Subversion Repositories camp_sysinfo_client_3

Rev

Rev 201 | Rev 244 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 201 Rev 222
Line 180... Line 180...
180
# to the calling routine
180
# to the calling routine
181
sub readConfig {
181
sub readConfig {
182
   my $filename = shift;
182
   my $filename = shift;
183
   my $config;
183
   my $config;
184
   return $config unless -e $filename;
184
   return $config unless -e $filename;
185
   if ( open CONF,"<$filename" ) {
-
 
186
      my $contents = join( '', <CONF> );
-
 
187
      close CONF;
-
 
188
      # now, look to see what kind of file this is.
-
 
189
      if ( $contents =~ m/^---(\s*#.*)?$/m ) {
-
 
190
         print "Reading $filename as YAML\n";
-
 
191
         #print "Contents are:\n\n=====================$contents\n=====================\n";
-
 
192
         # this is a yaml file
-
 
193
         #$contents = YAML::Tiny->read( $config );
185
   my $yaml = YAML::Tiny->read( $filename );
194
         # try to load the contents into $config, and warn if there is a problem.
-
 
195
         eval( $config = Load( $contents ) ); warn $@ if $@;
-
 
196
      } elsif ( $contents =~ m/\$clientName/ ) {
-
 
197
         # this is old style
-
 
198
         print "Reading $filename as old school file\n";
-
 
199
         my $clientName = '';
-
 
200
         my $serialNumber = '';
-
 
201
         my $hostname = '';
-
 
202
         my @moduleDirs;
-
 
203
         my @scriptDirs;
-
 
204
         my $UUID = '';
-
 
205
         my $transports;
-
 
206
         # now, eval the information we just read.
-
 
207
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
-
 
208
         no strict "vars";
-
 
209
         eval( $contents );
-
 
210
         use strict "vars";
-
 
211
         die "Error during eval: $@\n" if $@;
-
 
212
         $config->{'clientName'} = $clientName if $clientName;
-
 
213
         $config->{'serialNumber'} = $serialNumber if $serialNumber;
-
 
214
         $config->{'UUID'} = $UUID unless $config->{'UUID'};
-
 
215
         $config->{'hostname'} = $hostname if $hostname;
-
 
216
         $config->{'moduleDirs'} = [ @moduleDirs ] if @moduleDirs;
-
 
217
         $config->{'scriptDirs'} = [ @scriptDirs ] if @scriptDirs;
-
 
218
         $config->{'transports'} = $transports if $transports;
-
 
219
         foreach my $trans ( keys %{$config->{'transports'}} ) {
-
 
220
            if ( exists ( $config->{'transports'}->{$trans}->{'-name-'} ) ) {
-
 
221
               $config->{'transports'}->{$trans}->{'name'} = $config->{'transports'}->{$trans}->{'-name-'};
-
 
222
               delete( $config->{'transports'}->{$trans}->{'-name-'} );
-
 
223
            }
-
 
224
         }
-
 
225
      }
-
 
226
   } else {
-
 
227
         warn "Could not read config file $filename, skipped: $!\n";
-
 
228
   }
-
 
229
   return $config;
186
   return $yaml->[0];
230
}
187
}
231
 
188
 
232
sub makeConfig {
189
sub makeConfig {
233
   my @configFileNames = @_;
190
   my @configFileNames = @_;
234
   my %config;
191
   my $config;
235
 
192
 
-
 
193
   use Hash::Merge;
-
 
194
   my $merge = Hash::Merge->new('RIGHT_PRECEDENT');
236
   foreach my $config ( @configFileNames ) {
195
   foreach my $configFile ( @configFileNames ) {
-
 
196
      next unless $configFile && -e $configFile;
-
 
197
      print "Processing config file $configFile\n";
237
      my $thisConfig = &readConfig( $config ) if $config && -e $config;
198
      my $thisConfig = &readConfig( $configFile );
238
      # add the new config to %config, overwriting any existing keys which are duplicated
199
      # add the new config to %config, overwriting any existing keys which are duplicated
-
 
200
      $config = $merge->merge( $config, $thisConfig );
239
      @config{keys %$thisConfig} = values %$thisConfig;
201
      #@config{keys %$thisConfig} = values %$thisConfig;
240
   }
202
   }
241
   # now, ensure the correct values are loaded in some areas
203
   # now, ensure the correct values are loaded in some areas
242
   unless ( $config{'hostname'} ) {
204
   unless ( $config->{'hostname'} ) {
243
      $hostname = `hostname -f`;
205
      $hostname = `hostname -f`;
244
      chomp $hostname;
206
      chomp $hostname;
245
      $config{'hostname'} = $hostname;
207
      $config->{'hostname'} = $hostname;
246
   }
208
   }
247
   unless ( $config{'serialNumber'} ) {
209
   unless ( $config->{'serialNumber'} ) {
248
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
210
      $serialNumber = `dmidecode -s system-serial-number` if `which dmidecode`;
249
      chomp $serialNumber;
211
      chomp $serialNumber;
250
      $serialNumber =~ s/\s//gi;
212
      $serialNumber =~ s/\s//gi;
251
      $config{'serialNumber'} = $serialNumber;
213
      $config->{'serialNumber'} = $serialNumber;
252
   }
214
   }
253
   unless ( $config{'UUID'} ) {
215
   unless ( $config->{'UUID'} ) {
254
      my $UUID = `which dmidecode` ?  `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '';
216
      my $UUID = `which dmidecode` ?  `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '';
255
      $UUID =~ s/\s//gi;
217
      $UUID =~ s/\s//gi;
256
      $config{'UUID'} = $UUID;
218
      $config->{'UUID'} = $UUID;
257
   }
219
   }
258
 
-
 
259
   # ensure we have the default SaveLocal transport defined
-
 
260
   unless ( defined $config{'transports'}{'99'} ) {
-
 
261
      $config{'transports'}{'99'} = {
-
 
262
                         'name'=> 'SaveLocal',
-
 
263
                         'output directory' => '/tmp',
-
 
264
                         'sendScript' => 'save_local'
-
 
265
                        };
-
 
266
   }
220
   
267
 
-
 
268
   return \%config;
221
   return $config;
269
}
222
}
270
 
223
 
271
# prompt the user for a response, then allow them to enter it
224
# prompt the user for a response, then allow them to enter it
272
# the first response is considered the default and is printed
225
# the first response is considered the default and is printed
273
# in all caps if more than one exists
226
# in all caps if more than one exists