Removed /usr/local from CDPATH
[clearscm.git] / clearadm / clearexec.pl
1 #!/usr/bin/env perl
2
3 =pod
4
5 =head1 NAME $RCSfile: clearexec.pl,v $
6
7 Execute commands on the remote system
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@ClearSCM.com>
16
17 =item Revision
18
19 $Revision: 1.11 $
20
21 =item Created:
22
23 Mon Dec 13 09:13:27 EST 2010
24
25 =item Modified:
26
27 $Date: 2012/04/27 14:47:22 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage clearexec.pl: [-u|sage] [-ve|rbose] [-deb|ug]
34                      [-h|ost <host>] [-p|ort <port>] [<cmd>]
35
36  Where:
37    -u|sage:       Displays usage
38  
39    -ve|rbose:     Be verbose
40    -deb|ug:       Output debug messages
41  
42    -h|ost <host>: Host to contact (Default: localhost)
43    -p|ort <port>: Port to connect to (Default: 25327) 
44    <cmd>          Command to perform
45      
46 =head1 DESCRIPTION
47
48 This script exercises the clearserver.pl daemon by executing a command on the
49 remote host:port that the clearserver.pl daemon is running on
50
51 =cut
52
53 use strict;
54 use warnings;
55
56 use Getopt::Long;
57 use FindBin;
58 use Term::ANSIColor qw(color);
59
60 use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
61
62 use Clearexec;
63 use CmdLine;
64 use Display;
65 use Utils;
66
67 my $VERSION  = '$Revision: 1.11 $';
68   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
69
70 my $me = $FindBin::Script;
71    $me =~ s/\.pl$//;
72    
73 local $0 = $me;
74
75 my $host = $Clearexec::CLEAROPTS{CLEAREXEC_HOST};
76 my $port = $Clearexec::CLEAROPTS{CLEAREXEC_PORT};
77
78 my $clearexec;
79
80 sub CmdLoop() {
81   my ($line, $result);
82
83   my $prompt = color('BOLD YELLOW') . "$me->$host:" . color('RESET');
84   
85   $CmdLine::cmdline->set_prompt($prompt);
86     
87   while (($line, $result) = $CmdLine::cmdline->get()) {
88     last unless defined $line;
89     last if $line =~ /exit|quit/i;
90     
91     my ($status, @output) = $clearexec->execute($line);
92     
93     last if $line =~ /stopserver/i;
94     
95     if ($status) {
96       error "Non zero status returned from $line ($status)\n" . join "\n", @output;
97     } else {
98       display join "\n", @output;
99       display "Status: $status"
100         if $status;
101     } # if
102   } # while
103   
104   return; 
105 } # CmdLoop
106
107 # Main
108 GetOptions(
109   'usage'   => sub { Usage },
110   'verbose' => sub { set_verbose },
111   'debug'   => sub { set_debug },
112   'host=s'  => \$host,
113   'port=s'  => \$port,
114 ) or Usage "Invalid parameter";
115
116 my $cmd = join ' ', @ARGV;
117
118 verbose "$FindBin::Script V$VERSION";
119
120 $clearexec = Clearexec->new;
121
122 my ($status, @output);
123
124 $status = $clearexec->connectToServer($host, $port);
125
126 error "Unable to connect to $host:$port", 1 unless $status;
127
128 if ($cmd ne '') {
129   ($status, @output) = $clearexec->execute($cmd);
130
131   if ($status) {
132     error "Unable to execute $cmd (Status: $status)\n" . join("\n", @output), 1;
133   } else {
134     display join "\n", @output;
135     display "Status: $status";
136   } # if
137 } else {
138   CmdLoop;
139 } # if
140
141 =pod
142
143 =head1 CONFIGURATION AND ENVIRONMENT
144
145 DEBUG: If set then $debug is set to this level.
146
147 VERBOSE: If set then $verbose is set to this level.
148
149 TRACE: If set then $trace is set to this level.
150
151 =head1 DEPENDENCIES
152
153 =head2 Perl Modules
154
155 L<FindBin>
156
157 L<Getopt::Long|Getopt::Long>
158
159 =head2 ClearSCM Perl Modules
160
161 =begin man 
162
163  Clearexec
164  Display
165  Utils
166
167 =end man
168
169 =begin html
170
171 <blockquote>
172 <a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearexec.pm">Clearexec</a><br>
173 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
174 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
175 </blockquote>
176
177 =end html
178
179 =head1 BUGS AND LIMITATIONS
180
181 There are no known bugs in this script
182
183 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
184
185 =head1 LICENSE AND COPYRIGHT
186
187 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
188
189 =cut