Subversion Repositories camp_sysinfo_client_3

Rev

Rev 244 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 rodolico 1
#!/usr/bin/env perl
2
 
16 rodolico 3
use warnings;
26 rodolico 4
use strict;
16 rodolico 5
 
45 rodolico 6
# v1.1.0 20161022 RWR
7
# use library sysinfoconf.pm for loading, editing and displaying config
77 rodolico 8
#
9
# v1.2.0 20190108 RWR
10
# added UUID
129 rodolico 11
#
12
# v1.3.0 20191107 RWR
13
# changed the screen to allow the user to see most of the values and decide what to edit
14
# then write the results.
15
# also, parameters are passed that allow the import of a file in old conf format
138 rodolico 16
#
17
# v1.3.1 20191112 RWR
18
# Fixed showEntry() to set value to empty string if it is null 
20 rodolico 19
 
129 rodolico 20
use Getopt::Long;
227 rodolico 21
use Data::Dumper;
45 rodolico 22
 
138 rodolico 23
our $VERSION = '1.3.1';
129 rodolico 24
 
21 rodolico 25
# find our location and use it for searching for libraries
26
BEGIN {
27
   use FindBin;
28
   use File::Spec;
29
   use lib File::Spec->catdir($FindBin::Bin);
11 rodolico 30
}
31
 
229 rodolico 32
use Cwd qw(abs_path);
33
 
34
 
21 rodolico 35
use sysinfoconf; # a library of shared routines with install.pl
228 rodolico 36
our %displayOrder;
16 rodolico 37
 
229 rodolico 38
$binDir = abs_path(File::Spec->catdir($FindBin::Bin) );
39
 
40
 
45 rodolico 41
my $config;
16 rodolico 42
 
12 rodolico 43
sub findSendEmail {
44
   my $currentLocation = shift;
53 rodolico 45
   my @possibles = ( 
46
            '/usr/local/opt/sendEmail/sendEmail',
47
            '/usr/local/opt/sendEmail/sendEmail.pl',
48
            '/opt/sendEmail/sendEmail', 
49
            '/opt/sendEmail/sendEmail.pl' 
50
            );
12 rodolico 51
   return $currentLocation if ( $currentLocation && -x $currentLocation );
52
   # well, we didn't find it, so look around some more
104 rodolico 53
   # Look through the possibles
12 rodolico 54
   foreach my $current ( @possibles ) {
55
      return $current if -x $current;
56
   }
57
   if ( &yesno( "You are asking for sendEmail, but I don't see it on the system\nWould you like me to automatically download and install" ) ) {
28 rodolico 58
      my $path = `perl getSendEmail.pl`;
12 rodolico 59
      chomp $path;
60
      return $path;
61
   }
62
   return '';
63
}
64
 
11 rodolico 65
sub userConfirmation {
45 rodolico 66
   my $config = shift;
11 rodolico 67
 
68
}
69
 
16 rodolico 70
sub setUpTransport {
71
   my ($priority, $transport, $fields, $type ) = @_;
72
   $priority = getAnswer( 'Priority', $priority );
73
   $$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
108 rodolico 74
   $$transport{'name'} = $type unless $$transport{'name'};
16 rodolico 75
   my $allKeys = $$fields{'keys'};
76
   foreach my $key ( @$allKeys ) {
77
      if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
28 rodolico 78
         my $temp = &findSendEmail( $$transport{$key} );
16 rodolico 79
         $$transport{$key} = $temp if $temp;
80
      }
81
      $$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
82
   }
83
   return ( $priority, $transport );
84
}
85
 
86
sub showAllTransports {
87
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
88
      print '='x40 . "\n";
89
      &showTransports( $priority, $$transports{ $priority } );
90
      print '='x40 . "\n";
91
   }
92
}   
93
 
94
sub showTransports {
95
   my ( $priority,$thisOne )  = @_;
108 rodolico 96
   print $$thisOne{'name'} . " has priority $priority\n";
16 rodolico 97
   foreach my $key ( sort keys %$thisOne ) {
98
      next if $key =~ m/^-.*-$/;
99
      print "$key = $$thisOne{$key}\n";
100
   }
101
}
102
 
103
sub doTransports {
104
   my ( $transports ) = @_;
105
 
106
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
108 rodolico 107
      if ( &yesno( $$transports{$priority}{'name'} . " has a priority of $priority, edit it?") ) {
108
         #print Dumper( $sendTypes{$$transports{$priority}{'name'}} );
16 rodolico 109
         #die;
108 rodolico 110
         my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'name'}} );
16 rodolico 111
         if ( $newpriority != $priority ) {
112
            delete $$transports{$priority};
113
            $priority = $newpriority;
114
         }
115
         $$transports{$priority} = $temp;
116
      } # if
117
   }
118
 
119
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
120
      print '='x40 . "\n";
121
      &showTransports( $priority, $$transports{ $priority } );
122
      print '='x40 . "\n";
123
   }
124
 
125
   while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
28 rodolico 126
      my $newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
16 rodolico 127
      my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
128
      $$transports{$priority} = $temp;
129
   }
130
 
131
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
132
      print '='x40 . "\n";
133
      &showTransports( $priority, $$transports{ $priority } );
134
      print '='x40 . "\n";
135
   }
136
}
137
 
129 rodolico 138
sub showEntry {
139
   my ( $lineno, $prompt, $value ) = @_;
140
   $value = '' unless $value;
228 rodolico 141
   my $valueType = ref( $value );
142
   if ( $valueType eq 'ARRAY' ) {
143
      $value = join( ', ', @$value );
144
   } elsif ( $valueType eq  'HASH' ) {
145
      $value = ( scalar keys %$value ) . " Entries";
146
   }
129 rodolico 147
   return "$lineno. $prompt: $value\n";
148
}
16 rodolico 149
 
129 rodolico 150
sub displayEditScreen {
151
   my $config = shift;
228 rodolico 152
#   print "\033[2J";    #clear the screen
153
#   print "\033[0;0H"; #jump to 0,0  
154
   foreach my $entry ( sort keys %displayOrder ) {
155
      print &showEntry( 
156
         $entry, 
157
         $displayOrder{$entry}{'display'}, 
158
         $config->{$displayOrder{$entry}{'fieldname'}} 
159
      );
129 rodolico 160
   }
228 rodolico 161
   print "\nSelect Option to Edit, or 'Done' if finished [Done]: ";
129 rodolico 162
   my $selection = <>;
163
   chomp $selection;
164
   $selection = 'd' unless $selection;
165
   return lc substr( $selection, 0, 1 );
166
}
45 rodolico 167
 
129 rodolico 168
sub getValue {
169
   my ( $prompt, $value ) = @_;
138 rodolico 170
   $value = '' unless $value;
129 rodolico 171
   print "$prompt [$value]: ";
172
   my $t = <>;
173
   chomp $t;
174
   return $t ? $t : $value;
175
}
104 rodolico 176
 
129 rodolico 177
sub editSelection {
228 rodolico 178
   my $entry = shift;
179
   my $type = ref( $config->{$displayOrder{$entry}{'fieldname'}} );
180
   print "Type is $type\n";
181
   if ( $type eq 'ARRAY' ) {
182
      print "Don't know how to edit arrays yet\n";
183
   } elsif ( $type eq 'HASH' ) {
184
      print "Don't know how to edit transports yet\n";
185
   } else { # it is a scalar
186
      $config->{$displayOrder{$entry}{'fieldname'}} = &getValue( 
187
         $displayOrder{$entry}{'display'}, 
188
         $config->{$displayOrder{$entry}{'fieldname'}}
189
         );
129 rodolico 190
   }
191
}
192
 
219 rodolico 193
 
194
# will validate the config, adding modules, scripts and transports if necessary
211 rodolico 195
sub testConfig {
196
   my $config = shift;
197
   my @return;
198
   push @return, 'No client name given' unless $config->{'clientName'};
199
   push @return, 'No host name given' unless $config->{'hostname'};
219 rodolico 200
   $config->{'serialNumber'} = $config->{'UUID'} if $config->{'serialNumber'} eq 'NotSpecified';
211 rodolico 201
   push @return, 'No serial number given' unless $config->{'serialNumber'};
202
   push @return, 'No UUID given' unless $config->{'UUID'};
219 rodolico 203
   # if scriptDirs not defined, use installdir/scripts
223 rodolico 204
   $config->{'scriptDirs'} = [ $binDir . '/scripts' ] unless @{$config->{'scriptDirs'}};
219 rodolico 205
   # if moduleDirs not defined, use installdir/modules
223 rodolico 206
   $config->{'moduleDirs'} = [ $binDir . '/modules' ] unless defined $config->{'moduleDirs'};
219 rodolico 207
 
208
   # ensure we have the default SaveLocal transport defined
209
   unless ( defined $config->{'transports'}{'99'} ) {
210
      $config->{'transports'}{'99'} = {
211
                         'name'=> 'SaveLocal',
212
                         'output directory' => '/tmp',
213
                         'sendScript' => 'save_local'
214
                        };
215
   }
211 rodolico 216
 
217
   return @return ? join ( "\n", @return ) . "\n" : 'Ok';
218
}
219
 
132 rodolico 220
sub help {
221
   print "$0 version $VERSION\n";
222
   print "Reads one or more sysinfo configuration file and\nallows user to edit the values\n\n";
236 rodolico 223
   print "$0 --verbose --file=/full/path/to/input -output=/full/path/to/sysinfo-client.yaml\n";
224
   print "   --verbose - may be entered more than one time to increase verbosity\n";
225
   print "   --file     - may be entered multiple times and may contain a comma delimited group of files\n";
226
   print "   --output   - file which will be written to\n\n";
227
   print "   --test     - test the config file and report errors\n";
228
   print "   --dryrun   - do not save result\n";
229
   print "   --quiet    - do not ask any questions, automatically check, repair and write to output\n";
230
   print "   --help     -  display this help screen\n\n";
231
 
132 rodolico 232
   print "All parameters are optional\n";
233
   print "Will search common locations for sysinfo-client.yaml and, if found, append this to the end\n";
234
   print "of the input file list.\n";
235
   print "If no --output is passed, will write to the last --file passed or the file which was found\n";
236
   print "in the search\n";
237
   print "All parameters accept the short flag, ie -v, -f, -o and -h\n";
238
   exit;
239
}
240
 
241
 
227 rodolico 242
my $confFileName;
132 rodolico 243
my $outputFile;
129 rodolico 244
my $verbose;
245
my $help;
246
my $version;
211 rodolico 247
my $test = 0;
248
my $dryRun = 0;
249
my $quiet = 0;
129 rodolico 250
 
251
# handle any command line parameters that may have been passed in
252
# verbose is incremental, so for example, -vv would give more info than -v
253
# there can be multiple files passed, and an entry can have a comma
254
# separated list. The files will be processed in order, with the last
255
# one passed in being the one that overrides everything.
256
GetOptions (
132 rodolico 257
            "verbose|v+"  => \$verbose, # verbosity level, 0-9
227 rodolico 258
            'file|f=s'    => \$confFileName, # file name to use
132 rodolico 259
            'output|o=s'  => \$outputFile, # file to save to
211 rodolico 260
            'test|t'      => \$test, # test the config file
261
            'dryrun|n'    => \$dryRun, # do not save result
262
            'quiet|q'     => \$quiet, # do not ask any questions
129 rodolico 263
            'help|h'      => \$help
264
            ) or die "Error parsing command line\n";
265
 
266
if ( $help ) { &help() ; exit; }
267
 
268
# look for the standard configuration file in the standard place
227 rodolico 269
unless ( $confFileName ) {
270
   ( $confDir, $sysinfo3 ) = &findConf( $sysinfo3 );
271
   $outputFile = $confDir . '/' . $sysinfo3 unless $outputFile;
272
   $confFileName =  $confDir . "/" . $sysinfo3 if ( $confDir );
273
}
228 rodolico 274
print "Loading defaults from existing config\n";
227 rodolico 275
$config = &makeConfig( $outputFile, $confFileName  );
104 rodolico 276
 
227 rodolico 277
 
228 rodolico 278
 
129 rodolico 279
my $selection;
211 rodolico 280
while ( ! $quiet && ( $selection = &displayEditScreen( $config ) ) ne 'd' ) {
129 rodolico 281
   &editSelection( $selection );
11 rodolico 282
}
283
 
229 rodolico 284
#die Dumper($config) . "\n";
228 rodolico 285
 
211 rodolico 286
my $testResults = &testConfig($config);
287
print "Error found in configuration file, manually run\n$0\n" if ( $testResults ne 'Ok' );
288
print $testResults if $test || $testResults ne 'Ok';
289
 
290
if ( $quiet || &yesno( "Ready to write configuration to $outputFile, ok?" ) ) {
291
   print "Config File written to $outputFile\n";
292
   &writeConfig( $outputFile, &showConf( $config ) ) unless $dryRun;
132 rodolico 293
}
20 rodolico 294
 
45 rodolico 295
#use Data::Dumper;
296
#print Dumper( $config ); die;
16 rodolico 297
 
132 rodolico 298
#&doTransports( $$config{'transports'} );
11 rodolico 299
 
300
 
301
1;