Subversion Repositories camp_sysinfo_client_3

Rev

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
20 rodolico 16
 
129 rodolico 17
use Getopt::Long;
45 rodolico 18
 
129 rodolico 19
 
20
our $VERSION = '1.3.0';
21
 
21 rodolico 22
# find our location and use it for searching for libraries
23
BEGIN {
24
   use FindBin;
25
   use File::Spec;
26
   use lib File::Spec->catdir($FindBin::Bin);
11 rodolico 27
}
28
 
21 rodolico 29
use sysinfoconf; # a library of shared routines with install.pl
16 rodolico 30
 
45 rodolico 31
my $config;
16 rodolico 32
 
12 rodolico 33
sub findSendEmail {
34
   my $currentLocation = shift;
53 rodolico 35
   my @possibles = ( 
36
            '/usr/local/opt/sendEmail/sendEmail',
37
            '/usr/local/opt/sendEmail/sendEmail.pl',
38
            '/opt/sendEmail/sendEmail', 
39
            '/opt/sendEmail/sendEmail.pl' 
40
            );
12 rodolico 41
   return $currentLocation if ( $currentLocation && -x $currentLocation );
42
   # well, we didn't find it, so look around some more
104 rodolico 43
   # Look through the possibles
12 rodolico 44
   foreach my $current ( @possibles ) {
45
      return $current if -x $current;
46
   }
47
   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 48
      my $path = `perl getSendEmail.pl`;
12 rodolico 49
      chomp $path;
50
      return $path;
51
   }
52
   return '';
53
}
54
 
11 rodolico 55
sub userConfirmation {
45 rodolico 56
   my $config = shift;
11 rodolico 57
   my $temp;
58
 
45 rodolico 59
   $$config{'clientName'} = &getAnswer( "Client Name ", ($$config{'clientName'}) );
60
   $$config{'serialNumber'} = &getAnswer( "Serial Number ", ($$config{'serialNumber'}) );
77 rodolico 61
   $$config{'UUID'} = &getAnswer( "UUID ", ($$config{'UUID'}) );
45 rodolico 62
   $$config{'hostname'} = &getAnswer( "Host Name ", ($$config{'hostname'}) );
11 rodolico 63
 
103 rodolico 64
   $temp = join( ",", @{ $$config{ 'moduleDirs' } } ) if $config->{ 'moduleDirs' };
11 rodolico 65
   $temp = &getAnswer( "Locations to search for modules (comma separated) ", ($temp) );
45 rodolico 66
   $$config{ 'moduleDirs' } = [ split( ',', $temp ) ];
11 rodolico 67
 
103 rodolico 68
   $temp = join( ",", @{ $$config{ 'scriptDirs' } } ) if $config->{'scriptDirs'};
11 rodolico 69
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
45 rodolico 70
   $$config{ 'scriptDirs' } = [ split( ',', $temp ) ];
11 rodolico 71
 
72
}
73
 
16 rodolico 74
sub setUpTransport {
75
   my ($priority, $transport, $fields, $type ) = @_;
76
   $priority = getAnswer( 'Priority', $priority );
77
   $$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
108 rodolico 78
   $$transport{'name'} = $type unless $$transport{'name'};
16 rodolico 79
   my $allKeys = $$fields{'keys'};
80
   foreach my $key ( @$allKeys ) {
81
      if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
28 rodolico 82
         my $temp = &findSendEmail( $$transport{$key} );
16 rodolico 83
         $$transport{$key} = $temp if $temp;
84
      }
85
      $$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
86
   }
87
   return ( $priority, $transport );
88
}
89
 
90
sub showAllTransports {
91
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
92
      print '='x40 . "\n";
93
      &showTransports( $priority, $$transports{ $priority } );
94
      print '='x40 . "\n";
95
   }
96
}   
97
 
98
sub showTransports {
99
   my ( $priority,$thisOne )  = @_;
108 rodolico 100
   print $$thisOne{'name'} . " has priority $priority\n";
16 rodolico 101
   foreach my $key ( sort keys %$thisOne ) {
102
      next if $key =~ m/^-.*-$/;
103
      print "$key = $$thisOne{$key}\n";
104
   }
105
}
106
 
107
sub doTransports {
108
   my ( $transports ) = @_;
109
 
110
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
108 rodolico 111
      if ( &yesno( $$transports{$priority}{'name'} . " has a priority of $priority, edit it?") ) {
112
         #print Dumper( $sendTypes{$$transports{$priority}{'name'}} );
16 rodolico 113
         #die;
108 rodolico 114
         my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'name'}} );
16 rodolico 115
         if ( $newpriority != $priority ) {
116
            delete $$transports{$priority};
117
            $priority = $newpriority;
118
         }
119
         $$transports{$priority} = $temp;
120
      } # if
121
   }
122
 
123
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
124
      print '='x40 . "\n";
125
      &showTransports( $priority, $$transports{ $priority } );
126
      print '='x40 . "\n";
127
   }
128
 
129
   while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
28 rodolico 130
      my $newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
16 rodolico 131
      my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
132
      $$transports{$priority} = $temp;
133
   }
134
 
135
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
136
      print '='x40 . "\n";
137
      &showTransports( $priority, $$transports{ $priority } );
138
      print '='x40 . "\n";
139
   }
140
}
141
 
129 rodolico 142
sub showEntry {
143
   my ( $lineno, $prompt, $value ) = @_;
144
   $value = '' unless $value;
145
   return "$lineno. $prompt: $value\n";
146
}
16 rodolico 147
 
129 rodolico 148
sub displayEditScreen {
149
   my $config = shift;
150
   #print "\033[2J";    #clear the screen
151
   #print "\033[0;0H"; #jump to 0,0   
152
   print &showEntry( 1, 'Client Name', $config->{'clientName'} );
153
   print &showEntry( 2, 'Host Name', $config->{'hostname'} );
154
   print &showEntry( 3, 'Serial Number', $config->{'serialNumber'} );
155
   print &showEntry( 4, 'UUID', $config->{'UUID'} );
156
   print &showEntry( 5, 'Module Dirs', join( ',', $config->{'moduleDirs'} ?  @{ $config->{'moduleDirs'} } : () ) );
157
   print &showEntry( 6, 'Script Dirs', join( ',', $config->{'scriptDirs'} ?  @{ $config->{'scriptDirs'} } : () ) );
158
   print &showEntry( 7, 'Transports', '' );
159
   foreach my $key ( sort { $a <=> $b } keys %{ $config->{'transports'} } ) {
160
      print "\t$key\t" . $config->{'transports'}->{$key}->{'name'} . "\n";
161
   }
162
   print "Select Option to Edit, or 'Done' if finished [Done]: ";
163
   my $selection = <>;
164
   chomp $selection;
165
   $selection = 'd' unless $selection;
166
   return lc substr( $selection, 0, 1 );
167
}
45 rodolico 168
 
129 rodolico 169
sub getValue {
170
   my ( $prompt, $value ) = @_;
171
   print "$prompt [$value]: ";
172
   my $t = <>;
173
   chomp $t;
174
   return $t ? $t : $value;
175
}
104 rodolico 176
 
129 rodolico 177
sub editSelection {
178
   my $selection = shift;
179
   if ( $selection == 1 ) {
180
      $config->{'clientName'} = &getValue( 'Client Name', $config->{'clientName'} );
181
   } elsif ( $selection == 2 ) {
182
      $config->{'hostname'} = &getValue( 'Host Name', $config->{'hostname'} );
183
   } elsif ( $selection == 3 ) {
184
      $config->{'serialNumber'} = &getValue( 'Serial Number', $config->{'serialNumber'} );
185
   } elsif ( $selection == 4 ) {
186
      $config->{'UUID'} = &getValue( 'UUID', $config->{'UUID'} );
187
   } elsif ( $selection == 5 ) {
188
      my $t = join( ',', $config->{'moduleDirs'} ?  @{ $config->{'moduleDirs'} } : () );
189
      $t = &getValue( "Comma separated list of Module Directories\n\t", $t );
190
      my @t = split( ',', $t );
191
      $config->{'moduleDirs'} =  \@t;
192
   } elsif ( $selection == 6 ) {
193
      my $t = join( ',', $config->{'scriptDirs'} ?  @{ $config->{'scriptDirs'} } : () );
194
      $t = &getValue( "Comma separated list of Script Directories\n\t", $t );
195
      my @t = split( ',', $t );
196
      $config->{'scriptDirs'} =  \@t;
197
   } elsif ( $selection == 7 ) {
198
      print "Can not automatically do this at this time\nManually edit the file (press enter to continue)";
199
      my $t = <>;
200
   }
201
}
202
 
203
my @confFileName;
204
my $verbose;
205
my $help;
206
my $version;
207
 
208
# handle any command line parameters that may have been passed in
209
# verbose is incremental, so for example, -vv would give more info than -v
210
# there can be multiple files passed, and an entry can have a comma
211
# separated list. The files will be processed in order, with the last
212
# one passed in being the one that overrides everything.
213
GetOptions (
214
            "verbose|v+" => \$verbose, # verbosity level, 0-9
215
            'file|f=s'      => \@confFileName,
216
            'help|h'      => \$help
217
            ) or die "Error parsing command line\n";
218
 
219
if ( $help ) { &help() ; exit; }
220
if ( $version ) { print "$0 version $VERSION\n"; exit; }
221
 
222
# if they passed something like -f file1,file2,file3, parse it out
223
@confFileName = split(/,/,join(',',@confFileName));
224
 
225
# look for the standard configuration file in the standard place
103 rodolico 226
( $confDir, $sysinfo3 ) = &findConf( $sysinfo3 );
129 rodolico 227
# add it at the tail end of the list
228
push @confFileName, $confDir . "/" . $sysinfo3 if ( $confDir );
53 rodolico 229
 
129 rodolico 230
print "Loading defaults from existing configs\n";
231
$config = &makeConfig( @confFileName  );
104 rodolico 232
 
129 rodolico 233
my $selection;
234
while ( ( $selection = &displayEditScreen( $config ) ) ne 'd' ) {
235
   &editSelection( $selection );
11 rodolico 236
}
237
 
129 rodolico 238
die;
239
#&userConfirmation( $config );
20 rodolico 240
 
45 rodolico 241
#use Data::Dumper;
242
#print Dumper( $config ); die;
16 rodolico 243
 
45 rodolico 244
&doTransports( $$config{'transports'} );
11 rodolico 245
 
246
print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
45 rodolico 247
print &showConf( $config );
11 rodolico 248
print '-'x40 . "\n";
55 rodolico 249
$confName = $confDir . '/' . $confName;
250
print &writeConfig( $confName, &showConf( $config ) ) if &yesno( "Write this configuration to $confName" );
11 rodolico 251
 
12 rodolico 252
 
55 rodolico 253
print "sysinfo-client has been installed configured. You can return to\nthis screen at any time by running config.pl\n";
11 rodolico 254
 
255
1;