Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | 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;
45 rodolico 21
 
129 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
16 rodolico 33
 
45 rodolico 34
my $config;
16 rodolico 35
 
12 rodolico 36
sub findSendEmail {
37
   my $currentLocation = shift;
53 rodolico 38
   my @possibles = ( 
39
            '/usr/local/opt/sendEmail/sendEmail',
40
            '/usr/local/opt/sendEmail/sendEmail.pl',
41
            '/opt/sendEmail/sendEmail', 
42
            '/opt/sendEmail/sendEmail.pl' 
43
            );
12 rodolico 44
   return $currentLocation if ( $currentLocation && -x $currentLocation );
45
   # well, we didn't find it, so look around some more
104 rodolico 46
   # Look through the possibles
12 rodolico 47
   foreach my $current ( @possibles ) {
48
      return $current if -x $current;
49
   }
50
   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 51
      my $path = `perl getSendEmail.pl`;
12 rodolico 52
      chomp $path;
53
      return $path;
54
   }
55
   return '';
56
}
57
 
11 rodolico 58
sub userConfirmation {
45 rodolico 59
   my $config = shift;
11 rodolico 60
 
61
}
62
 
16 rodolico 63
sub setUpTransport {
64
   my ($priority, $transport, $fields, $type ) = @_;
65
   $priority = getAnswer( 'Priority', $priority );
66
   $$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
108 rodolico 67
   $$transport{'name'} = $type unless $$transport{'name'};
16 rodolico 68
   my $allKeys = $$fields{'keys'};
69
   foreach my $key ( @$allKeys ) {
70
      if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
28 rodolico 71
         my $temp = &findSendEmail( $$transport{$key} );
16 rodolico 72
         $$transport{$key} = $temp if $temp;
73
      }
74
      $$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
75
   }
76
   return ( $priority, $transport );
77
}
78
 
79
sub showAllTransports {
80
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
81
      print '='x40 . "\n";
82
      &showTransports( $priority, $$transports{ $priority } );
83
      print '='x40 . "\n";
84
   }
85
}   
86
 
87
sub showTransports {
88
   my ( $priority,$thisOne )  = @_;
108 rodolico 89
   print $$thisOne{'name'} . " has priority $priority\n";
16 rodolico 90
   foreach my $key ( sort keys %$thisOne ) {
91
      next if $key =~ m/^-.*-$/;
92
      print "$key = $$thisOne{$key}\n";
93
   }
94
}
95
 
96
sub doTransports {
97
   my ( $transports ) = @_;
98
 
99
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
108 rodolico 100
      if ( &yesno( $$transports{$priority}{'name'} . " has a priority of $priority, edit it?") ) {
101
         #print Dumper( $sendTypes{$$transports{$priority}{'name'}} );
16 rodolico 102
         #die;
108 rodolico 103
         my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'name'}} );
16 rodolico 104
         if ( $newpriority != $priority ) {
105
            delete $$transports{$priority};
106
            $priority = $newpriority;
107
         }
108
         $$transports{$priority} = $temp;
109
      } # if
110
   }
111
 
112
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
113
      print '='x40 . "\n";
114
      &showTransports( $priority, $$transports{ $priority } );
115
      print '='x40 . "\n";
116
   }
117
 
118
   while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
28 rodolico 119
      my $newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
16 rodolico 120
      my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
121
      $$transports{$priority} = $temp;
122
   }
123
 
124
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
125
      print '='x40 . "\n";
126
      &showTransports( $priority, $$transports{ $priority } );
127
      print '='x40 . "\n";
128
   }
129
}
130
 
129 rodolico 131
sub showEntry {
132
   my ( $lineno, $prompt, $value ) = @_;
133
   $value = '' unless $value;
134
   return "$lineno. $prompt: $value\n";
135
}
16 rodolico 136
 
129 rodolico 137
sub displayEditScreen {
138
   my $config = shift;
139
   #print "\033[2J";    #clear the screen
140
   #print "\033[0;0H"; #jump to 0,0   
141
   print &showEntry( 1, 'Client Name', $config->{'clientName'} );
142
   print &showEntry( 2, 'Host Name', $config->{'hostname'} );
143
   print &showEntry( 3, 'Serial Number', $config->{'serialNumber'} );
144
   print &showEntry( 4, 'UUID', $config->{'UUID'} );
145
   print &showEntry( 5, 'Module Dirs', join( ',', $config->{'moduleDirs'} ?  @{ $config->{'moduleDirs'} } : () ) );
146
   print &showEntry( 6, 'Script Dirs', join( ',', $config->{'scriptDirs'} ?  @{ $config->{'scriptDirs'} } : () ) );
147
   print &showEntry( 7, 'Transports', '' );
148
   foreach my $key ( sort { $a <=> $b } keys %{ $config->{'transports'} } ) {
149
      print "\t$key\t" . $config->{'transports'}->{$key}->{'name'} . "\n";
150
   }
151
   print "Select Option to Edit, or 'Done' if finished [Done]: ";
152
   my $selection = <>;
153
   chomp $selection;
154
   $selection = 'd' unless $selection;
155
   return lc substr( $selection, 0, 1 );
156
}
45 rodolico 157
 
129 rodolico 158
sub getValue {
159
   my ( $prompt, $value ) = @_;
138 rodolico 160
   $value = '' unless $value;
129 rodolico 161
   print "$prompt [$value]: ";
162
   my $t = <>;
163
   chomp $t;
164
   return $t ? $t : $value;
165
}
104 rodolico 166
 
129 rodolico 167
sub editSelection {
168
   my $selection = shift;
169
   if ( $selection == 1 ) {
170
      $config->{'clientName'} = &getValue( 'Client Name', $config->{'clientName'} );
171
   } elsif ( $selection == 2 ) {
172
      $config->{'hostname'} = &getValue( 'Host Name', $config->{'hostname'} );
173
   } elsif ( $selection == 3 ) {
174
      $config->{'serialNumber'} = &getValue( 'Serial Number', $config->{'serialNumber'} );
175
   } elsif ( $selection == 4 ) {
176
      $config->{'UUID'} = &getValue( 'UUID', $config->{'UUID'} );
177
   } elsif ( $selection == 5 ) {
178
      my $t = join( ',', $config->{'moduleDirs'} ?  @{ $config->{'moduleDirs'} } : () );
179
      $t = &getValue( "Comma separated list of Module Directories\n\t", $t );
180
      my @t = split( ',', $t );
181
      $config->{'moduleDirs'} =  \@t;
182
   } elsif ( $selection == 6 ) {
183
      my $t = join( ',', $config->{'scriptDirs'} ?  @{ $config->{'scriptDirs'} } : () );
184
      $t = &getValue( "Comma separated list of Script Directories\n\t", $t );
185
      my @t = split( ',', $t );
186
      $config->{'scriptDirs'} =  \@t;
187
   } elsif ( $selection == 7 ) {
188
      print "Can not automatically do this at this time\nManually edit the file (press enter to continue)";
189
      my $t = <>;
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";
223
   print "$0 --verbose --file=/full/paht/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 is the file which will be written to\n\n";
227
   print "All parameters are optional\n";
228
   print "Will search common locations for sysinfo-client.yaml and, if found, append this to the end\n";
229
   print "of the input file list.\n";
230
   print "If no --output is passed, will write to the last --file passed or the file which was found\n";
231
   print "in the search\n";
232
   print "All parameters accept the short flag, ie -v, -f, -o and -h\n";
233
   exit;
234
}
235
 
236
 
129 rodolico 237
my @confFileName;
132 rodolico 238
my $outputFile;
129 rodolico 239
my $verbose;
240
my $help;
241
my $version;
211 rodolico 242
my $test = 0;
243
my $dryRun = 0;
244
my $quiet = 0;
129 rodolico 245
 
246
# handle any command line parameters that may have been passed in
247
# verbose is incremental, so for example, -vv would give more info than -v
248
# there can be multiple files passed, and an entry can have a comma
249
# separated list. The files will be processed in order, with the last
250
# one passed in being the one that overrides everything.
251
GetOptions (
132 rodolico 252
            "verbose|v+"  => \$verbose, # verbosity level, 0-9
253
            'file|f=s'    => \@confFileName, # array of files to read
254
            'output|o=s'  => \$outputFile, # file to save to
211 rodolico 255
            'test|t'      => \$test, # test the config file
256
            'dryrun|n'    => \$dryRun, # do not save result
257
            'quiet|q'     => \$quiet, # do not ask any questions
129 rodolico 258
            'help|h'      => \$help
259
            ) or die "Error parsing command line\n";
260
 
261
if ( $help ) { &help() ; exit; }
262
 
263
# if they passed something like -f file1,file2,file3, parse it out
264
@confFileName = split(/,/,join(',',@confFileName));
265
 
266
# look for the standard configuration file in the standard place
103 rodolico 267
( $confDir, $sysinfo3 ) = &findConf( $sysinfo3 );
211 rodolico 268
$outputFile = $confDir . '/' . $sysinfo3 unless $outputFile;
129 rodolico 269
push @confFileName, $confDir . "/" . $sysinfo3 if ( $confDir );
53 rodolico 270
 
129 rodolico 271
print "Loading defaults from existing configs\n";
211 rodolico 272
$config = &makeConfig( $outputFile, @confFileName  );
104 rodolico 273
 
129 rodolico 274
my $selection;
211 rodolico 275
while ( ! $quiet && ( $selection = &displayEditScreen( $config ) ) ne 'd' ) {
129 rodolico 276
   &editSelection( $selection );
11 rodolico 277
}
278
 
211 rodolico 279
my $testResults = &testConfig($config);
280
print "Error found in configuration file, manually run\n$0\n" if ( $testResults ne 'Ok' );
281
print $testResults if $test || $testResults ne 'Ok';
282
 
283
if ( $quiet || &yesno( "Ready to write configuration to $outputFile, ok?" ) ) {
284
   print "Config File written to $outputFile\n";
285
   &writeConfig( $outputFile, &showConf( $config ) ) unless $dryRun;
132 rodolico 286
}
20 rodolico 287
 
45 rodolico 288
#use Data::Dumper;
289
#print Dumper( $config ); die;
16 rodolico 290
 
132 rodolico 291
#&doTransports( $$config{'transports'} );
11 rodolico 292
 
293
 
294
1;