Subversion Repositories camp_sysinfo_client_3

Rev

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

Rev Author Line No. Line
223 rodolico 1
#!/usr/bin/env perl
2
 
3
package sysinfosetup;
4
 
5
our $VERSION = '1.0.0';
6
use warnings;
7
use strict;  
8
 
9
use Exporter;
10
 
11
our @ISA = qw( Exporter );
12
our @EXPORT = qw( 
13
                  &logIt
14
                  &getAnswer
15
                  &yesno
16
                  &runCommand
17
                  &slurp
18
                  $logFile
19
                  $DEBUG
20
                  $dryRun
21
                );
22
 
23
our $DEBUG = 0;
24
our $logFile = '/tmp/sysinfo.log';
25
our $dryRun = 0;
26
 
27
#&logIt( "Operating System is $install{'os'}" );
28
 
29
sub runCommand {
30
   my $command = shift;
31
   if ( $command =~ m/cd (.*)/ ) { # cd is not a command but a built in, so we can not just execute it
32
      chdir $1;
33
      print "In directory `pwd`" if $DEBUG;
34
      return 1;
35
   } else { # just a command, so run it
36
      print "Running [$command]\n" if $DEBUG;
37
      return 1 if $dryRun;
38
      system $command;
39
      if ($? == -1) {
40
          die "failed to execute $command: $!\n";
41
      } elsif ($? & 127) {
42
          die sprintf( "[%s] died with signal %d\n", $command, ($? & 127) );
43
      } else {
44
          die sprintf( "%s returned %d\n", $command, $? >> 8 ) if $? >> 8;
45
      }
46
   }
47
   return 1;
48
}
49
 
50
sub slurp {
51
    my $file = shift;
52
    open my $fh, '<', $file or die;
53
    local $/ = undef;
54
    my $cont = <$fh>;
55
    close $fh;
56
    return $cont;
57
}
58
 
59
 
60
#######################################################
61
# function to simply log things
62
# first parameter is the priority, if <= $logDef->{'log level'} will print
63
# all subsequent parameters assumed to be strings to sent to the log
64
# returns 0 on failure
65
#         1 on success
66
#         2 if priority > log level
67
#        -1 if $logDef is unset
68
# currently, only logs to a file
69
#######################################################
70
sub logIt {
71
   open LOG, ">>$logFile" or die "Could not append to $logFile: $!\n";
72
   while ( my $t = shift ) {
73
      print LOG &timeStamp() . "\t$t\n";
74
   }
75
   close LOG;
76
   return 1;
77
}
78
 
79
#######################################################
80
#
81
# timeStamp
82
#
83
# return current system date as YYYY-MM-DD HH:MM:SS
84
#
85
#######################################################
86
sub timeStamp {
87
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
88
   return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
89
}
90
 
91
sub yesno {
92
   my ( $prompt, $default ) = @_;
93
   $default = 'yes' unless $default;
94
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
95
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
96
}
97
 
98
# prompt the user for a response, then allow them to enter it
99
# the first response is considered the default and is printed
100
# in all caps if more than one exists
101
# first answer is used if they simply press the Enter
102
# key. The response is returned all lower case if more than one
103
# exists.
104
# it is assumed 
105
sub getAnswer {
106
   my ( $prompt, @answers ) = @_;
107
   $answers[0] = '' unless defined( $answers[0] );
108
   my $default = $answers[0];
109
   my $onlyOneAnswer = scalar( @answers ) == 1;
110
   print $prompt . '[ ';
111
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
112
   print join( ' | ', @answers ) . ' ]: ';
113
   my $thisAnswer = <>;
114
   chomp $thisAnswer;
115
   $thisAnswer = $default unless $thisAnswer;
116
   return $thisAnswer;
117
}
118
 
119
1;