Removed /usr/local from CDPATH
[clearscm.git] / cc / lockvobs
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         lockvobs,v
5 # Revision:     1.1.1.1
6 # Description:  [Un]locks all vobs in the current region, reports results
7 # Author:       Andrew@DeFaria.com
8 # Created:      Mon Mar 15 08:48:24 PST 2004
9 # Modified:     2007/05/17 07:45:48
10 # Language:     Perl
11 #
12 # (c) Copyright 2004, Andrew@DeFaria.com, all rights reserved.
13 #
14 ################################################################################
15 use strict;
16 use warnings;
17
18 use Net::SMTP;
19 use File::Spec;
20
21 # This will be set in the BEGIN block but by putting them here the become
22 # available for the whole script.
23 my (
24   $abs_path,
25   $me,
26   $bin_path,
27   $triggers_path,
28   $lib_path,
29   $log_path,
30   $etc_path,
31   $windows
32 );
33
34 BEGIN {
35   # Extract relative path and basename from script name.
36   $0 =~ /(.*)[\/\\](.*)/;
37
38   $abs_path     = (!defined $1) ? "." : File::Spec->rel2abs ($1);
39   $me           = (!defined $2) ? $0  : $2;
40
41   # Check to see if we are running on Windows
42   $windows      = ($^O =~ /MSWin/) ? "yes" : "no";
43
44   # Setup paths
45   $bin_path             = "$abs_path";
46   $triggers_path        = "$abs_path/../triggers";
47   $lib_path             = "$abs_path/../lib";
48   $log_path             = "$abs_path/../log";
49   $etc_path             = "$abs_path/../etc";
50
51   # Add the appropriate path to our modules to @INC array.
52   unshift (@INC, "$lib_path");
53 } # BEGIN
54
55 use Display;
56
57 # Store logfile in CM_TOOLS/logs
58 my $logfile = "$log_path/lockvobs.log";
59
60 # Production vob server
61 my $vob_server = defined $ENV {VOBSERVER} ? $ENV {VOBSERVER} : undef;
62
63 # Gotta be from somebody!
64 my $from = defined $ENV {FROM} ? $ENV {FROM} : undef;
65
66 # This should be changed to an email alias
67 my @to          = ();
68
69 # Who gets notified when there are errors
70 my @errors_to   = ();
71
72 my $unlock      = "no";
73 my $execute     = "yes";
74 my $smtphost    = "appsmtp";
75
76 # Exceptions file
77 my $exceptions_file = "$etc_path/vob_exceptions";
78
79 # Any errors?
80 my $errors = 0;
81
82 sub Usage {
83   my $me = $0;
84
85   $me =~ s/\.\///;
86
87   print "Usage $me:\t[-u] [-n] [-smtphost <smtphost>] [-to <email addresses]\n";
88   print "\t\t[-errors-to <email addresses>]\n";
89   print "\nWhere:\n";
90   print "\t-u\t\tUnlock vobs (default lock vobs)\n";
91   print "\t-smtphost\tSpecifies what SMTP host to use for mail (default\n";
92   print "\t\t\tnotesmail01)\n";
93   print "\t-to\t\tComma separated list (no spaces) of email addresses to\n";
94   print "\t\t\tsend output to (default: bsomisetty\@ameriquest.net,\n";
95   print "\t\t\tsgopavarapu\@ameriquest.net)\n";
96   print "\t-errors-to\tComma separated list (no spaces) of email addresses\n";
97   print "\t\t\tto send (only errors) to (default:\n";
98   print "\t\t\tadefaria\@ameriquest.net)\n";
99   exit 1;
100 } # Usage
101
102 sub logmsg {
103   my $msg = shift;
104
105   open LOGFILE, ">>$logfile"
106     or die "Unable to open logfile: $logfile - $!";
107
108   print LOGFILE $msg . "\n";
109
110   close LOGFILE;
111 } # logmsg
112
113 sub notify {
114   my $smtphost  = shift;
115   my $from      = shift;
116   my $errors    = shift;
117   my $unlock    = shift;
118
119   my $subject = $unlock eq "yes" ? "Unlock VOBs" : "Lock VOBs";
120
121   # Connect to mail server
122   my $smtp = Net::SMTP->new ($smtphost);
123
124   die "Unable to open connection to mail host: $smtphost\n" if !defined $smtp;
125
126   # Compose message
127   $smtp->mail ($from);
128
129   if ($errors ne 0) {
130     # Add @errors_to
131     foreach (@errors_to) {
132       push @to, $_;
133     } # foreach
134   } # if
135
136   # Add @to
137   foreach (@to) {
138     $smtp->to ($_);
139   } # foreach
140
141   # Start email data
142   $smtp->data ();
143
144   # Add From line
145   $smtp->datasend ("From: $from\n");
146
147   # Add @to and @errors_to
148   $smtp->datasend ("To: " . join (",", @to) . "\n");
149
150   # Add subject
151   $smtp->datasend ("Subject: $subject\n\n");
152
153   # Open logfile
154   open LOGFILE, $logfile
155     or die "Unable to open logfile $logfile - $!\n";
156
157   while (<LOGFILE>) {
158     $smtp->datasend ($_);
159   } # while
160
161   $smtp->dataend ();
162   $smtp->quit;
163
164   return 0;
165 } # notify
166
167 sub Error {
168   my $msg = shift;
169
170   logmsg $msg;
171
172   $errors++;
173
174   notify $smtphost, $from, $errors, $unlock;
175
176   exit $errors;
177 } # Error
178
179 sub IsAMember {
180   my $item      = shift;
181   my @list      = @_;
182
183   $item =~ s/\\//g;
184
185   foreach (@list) {
186     chomp;
187     s/\\//g;
188     return 1 if $item eq $_;
189   } # foreach
190
191   return 0;
192 } # IsAMember
193
194 # Get parms
195 while ($#ARGV >= 0) {
196   if ($ARGV [0] eq "-u") {
197     $unlock = "yes";
198     shift;
199     next;
200   } # if
201
202   if ($ARGV [0] eq "-n") {
203     $execute = "no";
204     shift;
205     next;
206   } # if
207
208   if ($ARGV [0] eq "-smtphost") {
209     shift;
210     $smtphost = $ARGV [0];
211     shift;
212     next;
213   } # if
214
215   if ($ARGV [0] eq "-to") {
216     shift;
217     @to = split /,/,$ARGV [0];
218     shift;
219     next;
220   } # if
221
222   if ($ARGV [0] eq "-errors-to") {
223     shift;
224     @errors_to = split /,/,$ARGV [0];
225     shift;
226     next;
227   } # if
228
229   Usage;
230 } # while
231
232 Usage "Vob server hasn't been defined"          if !defined $vob_server;
233 Usage "From has not been specified"             if !defined $from;
234 Usage "To has not been specified"               if @to;
235 Usage "Errors to has not been specififed"       if @errors_to;
236
237 open EXCEPTIONS, $exceptions_file
238   or error "Unable to open exceptions file ($exceptions_file)", 1;
239
240 my @exceptions = <EXCEPTIONS>;
241
242 # Remove logfile if present
243 unlink ($logfile) if (-e $logfile);
244
245 # Get list of vobs
246 open (VOBS, "cleartool lsvob -short -host $vob_server|")
247   or Error "Can't list vobs: $!";
248
249 # Process them
250 while (<VOBS>) {
251   chomp;
252   chop if /\r/; # any carriage return
253
254   next if $#exceptions ne 0 and IsAMember ($_, @exceptions);
255
256   $_ = "\\" . $_ if $windows ne "yes";
257
258   # [Un]lock the vob
259   if ($unlock eq "yes") {
260     if ($execute eq "no") {
261       print "[noexecute] cleartool unlock vob:$_\n";
262     } else {
263       system ("cleartool unlock vob:$_ >> $logfile 2>&1");
264     } # if
265   } else {
266     if ($execute eq "no") {
267       print "[noexecute] cleartool lock vob:$_\n";
268     } else {
269       system ("cleartool lock vob:$_ >> $logfile 2>&1");
270     } # if
271   } # if
272
273   # Convert the status
274   my $status = $? >> 8;
275
276   if ($status ne 0) {
277     $errors++;
278   } # if
279 } # while
280
281 my $status = $execute eq "yes" ? notify $smtphost, $from, $errors, $unlock : 0;
282
283 exit $status;