Subversion Repositories camp_sysinfo_client_3

Rev

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