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
 
211 rodolico 193
sub testConfig {
194
   my $config = shift;
195
   my @return;
196
   push @return, 'No client name given' unless $config->{'clientName'};
197
   push @return, 'No host name given' unless $config->{'hostname'};
198
   push @return, 'No serial number given' unless $config->{'serialNumber'};
199
   push @return, 'No UUID given' unless $config->{'UUID'};
200
   push @return, 'No Module Directories given' unless $config->{'moduleDirs'};
201
   push @return, 'No Script Directories given' unless $config->{'scriptDirs'};
202
 
203
   return @return ? join ( "\n", @return ) . "\n" : 'Ok';
204
}
205
 
132 rodolico 206
sub help {
207
   print "$0 version $VERSION\n";
208
   print "Reads one or more sysinfo configuration file and\nallows user to edit the values\n\n";
209
   print "$0 --verbose --file=/full/paht/to/input -output=/full/path/to/sysinfo-client.yaml\n";
210
   print "   --verbose may be entered more than one time to increase verbosity\n";
211
   print "   --file may be entered multiple times and may contain a comma delimited group of files\n";
212
   print "   --output is the file which will be written to\n\n";
213
   print "All parameters are optional\n";
214
   print "Will search common locations for sysinfo-client.yaml and, if found, append this to the end\n";
215
   print "of the input file list.\n";
216
   print "If no --output is passed, will write to the last --file passed or the file which was found\n";
217
   print "in the search\n";
218
   print "All parameters accept the short flag, ie -v, -f, -o and -h\n";
219
   exit;
220
}
221
 
222
 
129 rodolico 223
my @confFileName;
132 rodolico 224
my $outputFile;
129 rodolico 225
my $verbose;
226
my $help;
227
my $version;
211 rodolico 228
my $test = 0;
229
my $dryRun = 0;
230
my $quiet = 0;
129 rodolico 231
 
232
# handle any command line parameters that may have been passed in
233
# verbose is incremental, so for example, -vv would give more info than -v
234
# there can be multiple files passed, and an entry can have a comma
235
# separated list. The files will be processed in order, with the last
236
# one passed in being the one that overrides everything.
237
GetOptions (
132 rodolico 238
            "verbose|v+"  => \$verbose, # verbosity level, 0-9
239
            'file|f=s'    => \@confFileName, # array of files to read
240
            'output|o=s'  => \$outputFile, # file to save to
211 rodolico 241
            'test|t'      => \$test, # test the config file
242
            'dryrun|n'    => \$dryRun, # do not save result
243
            'quiet|q'     => \$quiet, # do not ask any questions
129 rodolico 244
            'help|h'      => \$help
245
            ) or die "Error parsing command line\n";
246
 
247
if ( $help ) { &help() ; exit; }
248
 
249
# if they passed something like -f file1,file2,file3, parse it out
250
@confFileName = split(/,/,join(',',@confFileName));
251
 
252
# look for the standard configuration file in the standard place
103 rodolico 253
( $confDir, $sysinfo3 ) = &findConf( $sysinfo3 );
211 rodolico 254
$outputFile = $confDir . '/' . $sysinfo3 unless $outputFile;
129 rodolico 255
push @confFileName, $confDir . "/" . $sysinfo3 if ( $confDir );
53 rodolico 256
 
129 rodolico 257
print "Loading defaults from existing configs\n";
211 rodolico 258
$config = &makeConfig( $outputFile, @confFileName  );
104 rodolico 259
 
129 rodolico 260
my $selection;
211 rodolico 261
while ( ! $quiet && ( $selection = &displayEditScreen( $config ) ) ne 'd' ) {
129 rodolico 262
   &editSelection( $selection );
11 rodolico 263
}
264
 
211 rodolico 265
my $testResults = &testConfig($config);
266
print "Error found in configuration file, manually run\n$0\n" if ( $testResults ne 'Ok' );
267
print $testResults if $test || $testResults ne 'Ok';
268
 
269
if ( $quiet || &yesno( "Ready to write configuration to $outputFile, ok?" ) ) {
270
   print "Config File written to $outputFile\n";
271
   &writeConfig( $outputFile, &showConf( $config ) ) unless $dryRun;
132 rodolico 272
}
20 rodolico 273
 
45 rodolico 274
#use Data::Dumper;
275
#print Dumper( $config ); die;
16 rodolico 276
 
132 rodolico 277
#&doTransports( $$config{'transports'} );
11 rodolico 278
 
279
 
280
1;