Subversion Repositories camp_sysinfo_client_3

Rev

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

Rev 92 Rev 94
Line 7... Line 7...
7
# added UUID and set defaults for config file
7
# added UUID and set defaults for config file
8
#
8
#
9
# v1.4.0 20190204 RWR
9
# v1.4.0 20190204 RWR
10
# added autoconvert from v1.2 configuration file to v1.3
10
# added autoconvert from v1.2 configuration file to v1.3
11
# using dumper to set configuration file
11
# using dumper to set configuration file
-
 
12
#
-
 
13
# version 2.0 20190330 RWR
-
 
14
# changed it so all configs are YAML
12
 
15
 
13
package sysinfoconf;
16
package sysinfoconf;
14
 
17
 
15
 
18
 
16
our $VERSION = '1.4.0';
19
our $VERSION = '2.0';
17
use warnings;
20
use warnings;
18
use strict;  
21
use strict;  
19
 
22
 
20
#use Data::Dumper;
23
#use Data::Dumper;
21
use YAML::Tiny;
24
use YAML::Tiny;
Line 148... Line 151...
148
      } # foreach
151
      } # foreach
149
   } # foreach
152
   } # foreach
150
   return $config;
153
   return $config;
151
} # transportsToConfig
154
} # transportsToConfig
152
 
155
 
-
 
156
 
-
 
157
# read the configuration file passed in and return a reference to it
-
 
158
# to the calling routine
-
 
159
sub readConfig {
-
 
160
   my $filename = shift;
-
 
161
   my $config;
-
 
162
   return $config unless -e $filename;
-
 
163
   if ( open CONF,"<$filename" ) {
-
 
164
      my $contents = join( '', <CONF> );
-
 
165
      close CONF;
-
 
166
      # now, look to see what kind of file this is.
-
 
167
      if ( $contents =~ m/^---(\s*#.*)?$/m ) {
-
 
168
         print "Reading $filename as YAML\n";
-
 
169
         #print "Contents are:\n\n=====================$contents\n=====================\n";
-
 
170
         # this is a yaml file
-
 
171
         #$contents = YAML::Tiny->read( $config );
-
 
172
         $config = Load( $contents );
-
 
173
      } elsif ( $contents =~ m/\$clientName/ ) {
-
 
174
         # this is old style
-
 
175
         print "Reading $filename as old school file\n";
-
 
176
         my $clientName = '';
-
 
177
         my $serialNumber = '';
-
 
178
         my $hostname = '';
-
 
179
         my @moduleDirs;
-
 
180
         my @scriptDirs;
-
 
181
         my $UUID = '';
-
 
182
         my $transports;
-
 
183
         # now, eval the information we just read.
-
 
184
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
-
 
185
         no strict "vars";
-
 
186
         eval( $contents );
-
 
187
         use strict "vars";
-
 
188
         die "Error during eval: $@\n" if $@;
-
 
189
         $config->{'clientName'} = $clientName if $clientName;
-
 
190
         $config->{'serialNumber'} = $serialNumber if $serialNumber;
-
 
191
         $config->{'UUID'} = $UUID unless $config->{'UUID'};
-
 
192
         $config->{'hostname'} = $hostname if $hostname;
-
 
193
         $config->{'moduleDirs'} = [ @moduleDirs ] if @moduleDirs;
-
 
194
         $config->{'scriptDirs'} = [ @scriptDirs ] if @scriptDirs;
-
 
195
         $config->{'transports'} = $transports if $transports;
-
 
196
      }
-
 
197
   } else {
-
 
198
         warn "Could not read config file $filename, skipped: $!\n";
-
 
199
   }
-
 
200
   return $config;
-
 
201
}
-
 
202
 
153
sub makeConfig {
203
sub makeConfig {
154
   my @configFileNames = @_;
204
   my @configFileNames = @_;
155
   my %config;
205
   my %config;
156
   my $clientName = '';
-
 
157
   my $serialNumber = '';
-
 
158
   my $hostname = '';
-
 
159
   my @moduleDirs;
-
 
160
   my @scriptDirs;
-
 
161
   my $UUID = '';
-
 
162
   my $transports;
-
 
163
 
206
 
164
   foreach my $config ( @configFileNames ) {
207
   foreach my $config ( @configFileNames ) {
165
      $contents = YAML::Tiny->read( $config );
-
 
166
      %config = Load( $contents );
208
      my $thisConfig = readConfig( $config );
167
      Dumper( \%config ); die;
-
 
168
      #open CONF,"<$config" or die "could not open $config: $!\n";
-
 
169
      #my $contents = join( '', <CONF> );
-
 
170
      #close CONF;
-
 
171
      # now, eval the information we just read.
-
 
172
      # NOTE: we must turn off strict while doing this, and we die if something breaks.
209
      # add the new config to %config, overwriting any existing keys which are duplicated
173
      #no strict "vars"; eval( $contents ); use strict "vars"; die "Error during eval: $@\n" if $@;
-
 
174
      # if this is a v2 config, we will update it
-
 
175
      $config{'clientName'} = $clientName unless $config{'clientName'};
-
 
176
      $config{'serialNumber'} = $serialNumber unless $config{'serialNumber'};
-
 
177
      $config{'UUID'} = $UUID unless $config{'UUID'};
-
 
178
      $config{'hostname'} = $hostname unless $config{'hostname'};
210
      @config{keys %$thisConfig} = values %$thisConfig;
179
      $config{'moduleDirs'} = [ @moduleDirs ] unless $config{'moduleDirs'};
-
 
180
      $config{'scriptDirs'} = [ @scriptDirs ] unless $config{'scriptDirs'};
-
 
181
      $config{'transports'} = $transports unless $config{'transports'};
-
 
182
   }
211
   }
183
 
-
 
184
 
-
 
-
 
212
   # now, ensure the correct values are loaded in some areas
185
   unless ( $config{'hostname'} ) {
213
   unless ( $config{'hostname'} ) {
186
      $hostname = `hostname -f`;
214
      $hostname = `hostname -f`;
187
      chomp $hostname;
215
      chomp $hostname;
188
      $config{'hostname'} = $hostname;
216
      $config{'hostname'} = $hostname;
189
   }
217
   }
Line 192... Line 220...
192
      chomp $serialNumber;
220
      chomp $serialNumber;
193
      $serialNumber =~ s/\s//gi;
221
      $serialNumber =~ s/\s//gi;
194
      $config{'serialNumber'} = $serialNumber;
222
      $config{'serialNumber'} = $serialNumber;
195
   }
223
   }
196
   unless ( $config{'UUID'} ) {
224
   unless ( $config{'UUID'} ) {
197
      $UUID = `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` if `which dmidecode`;
225
      my $UUID = `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` if `which dmidecode`;
198
      $UUID =~ s/\s//gi;
226
      $UUID =~ s/\s//gi;
199
      $config{'UUID'} = $UUID;
227
      $config{'UUID'} = $UUID;
200
   }
228
   }
201
 
229
 
202
   return \%config;
230
   return \%config;