5 =head2 NAME $RCSfile: mktriggers.pl,v $
7 Enforce the application of triggers to vobs
15 Andrew DeFaria <Andrew@ClearSCM.com>
23 Sat Apr 3 09:06:11 PDT 2003
27 $Date: 2011/03/24 22:22:00 $
31 Usage: mktriggers.pl [-u|sage] [-[no]e|xec] [-[no]a|dd] [-[no]r|eplace]
32 [-[no]p|rivate] [ -vobs ] [-ve|rbose] [-d|ebug]
36 -u|sage: Displays usage
37 -[no]e|exec: Execute mode (Default: Do not execute)
38 -[no]a|dd: Add any missing triggers (Default: Don't add)
39 -[no]r|eplace: Replace triggers even if already present (Default:
42 Note: If neither -add nor -replace is specified then
43 both -add and -replace are performed.
45 -triggers: Name of triggers.dat file (Default:
46 $FindBin::Bin/../etc/triggers.dat)
47 -[no]p|rivate: Process private vobs (Default: Don't process private
51 -d|ebug: Output debug messages
53 -vob List of vob tags to apply triggers to (default all vobs)
55 Note: You can specify -vob /vobs/vob1,/vobs/vob2 -vob /vobs/vob3 which will
56 result in processing all of /vobs/vobs1, /vobs/vob2 and /vobs/vob3.
60 This script parses triggers.dat and created trigger types in vobs. It is
61 designed to be run periodically (cron(1)) and will add/replace triggers on
62 all vobs by default. It can also operate on individual vobs if required. The
63 script is driven by a data file, triggers.dat, which describes which triggers
64 are to be enforced one which vobs.
68 File format: Lines beginning with "#" are treated as comments Blank lines are
69 skipped. Spaces and tabs can be used for whitespace.
72 WinTriggerPath: \\<NAS device>\clearscm\triggers
73 LinuxTriggerPath: /net/<NAS device>/clearscm/triggers
75 # All vobs get the evil twin trigger
77 Description: Evil Twin Prevention Trigger
79 Opkinds: -preop lnname
84 # Only these vobs get this trigger to enforce a naming policy
85 # Note the trigger script gets a parameter
87 Description: Enforce standard naming policies
89 Opkinds: -preop lnname
91 Script: stdnames.pl -lowercase
92 Vobs: \dbengine, \backend
95 # All vobs get rmelen trigger except ours!
97 Description: Disable RMELEM
99 Opkinds: -preop lnname
107 If the environment variable VEBOSE or DEBUG are set then it's as if -verbose
108 or -debug was specified.
112 Copyright (c) 2004 Andrew DeFaria , ClearSCM, Inc.
121 use lib "$FindBin::Bin/../lib";
126 # Where is the trigger source code kept?
127 my ($windows_trig_path, $linux_trig_path);
129 # Where is the trigger definition file?
130 my $etc_path = "$FindBin::Bin/../etc";
131 my $triggerData = "$etc_path/triggers.dat";
139 system "perldoc $FindBin::Script";
144 sub ParseTriggerData {
145 open my $triggerData, '<', $triggerData
146 or error "Unable to open $triggerData - $!", 1;
149 my ($name, $desc, $type, $opkinds, $engine, $script, $vobs, $novobs);
151 while (<$triggerData>) {
152 chomp; chop if /\r$/;
154 next if /^$/; # Skip blank lines
155 next if /^\#/; # and comments
160 if (/^\s*WinTriggerPath:\s*(.*)/i) {
161 $windows_trig_path = $1;
165 if (/^\s*LinuxTriggerPath:\s*(.)/i) {
166 $linux_trig_path = $1;
170 if (/^\s*Trigger:\s*(.*)/i) {
175 if (/^\s*Description:\s*(.*)/i) {
180 if (/^\s*Type:\s*(.*)/i) {
185 if (/^\s*Opkinds:\s*(.*)/i) {
190 if (/^\s*ScriptEngine:\s*(.*)/i) {
195 if (/^\s*Script:\s*(.*)/i) {
200 if (/^\s*Vobs:\s*(.*)/i) {
205 if (/^\s*Novobs:\s*(.*)/i) {
213 $trigger{name} = $name;
214 $trigger{desc} = $desc;
215 $trigger{type} = $type;
216 $trigger{opkinds} = $opkinds;
217 $trigger{engine} = $engine;
218 $trigger{script} = $script;
219 $trigger{vobs} = !$vobs ? 'all' : $vobs;
220 $trigger{novobs} = $novobs ? $novobs : '';
222 push (@triggers, \%trigger);
224 $name = $desc = $type = $opkinds = $engine = $script = $vobs = $novobs = "";
230 error 'You must define WindowsTriggerPath, LinuxTriggerPath or both', 1
231 unless ($windows_trig_path or $linux_trig_path);
236 sub RemoveVobPrefix ($) {
239 if ($ARCH =~ /windows/ or $ARCH =~ /cygwin/) {
242 $vob =~ s/^\/vobs\///;
248 sub MkTriggerType ($$$$%) {
249 my ($vob, $exec, $add, $replace, %trigger) = @_;
253 # Need an extra set of "\\" for non Windows systems such as Cygwin
254 # since apparently the shell if envoked, collapsing a set of "\\".
255 my $vobtag = $ARCH =~ /cygwin/i ? "\\$vob" : $vob;
256 my $status = system ("cleartool lstype trtype:$trigger{name}\@$vobtag > $NULL 2>&1");
259 debug "Found pre-existing trigger $trigger{name}";
261 # If we are not replacing then skip by returning
265 $replaceOpt = '-replace';
267 debug "No pre-existing trigger $trigger{name}";
269 # We need to add the trigger. However, if we are not adding then skip by
275 error "Sorry I only support ScriptEngines of Perl!" if $trigger{engine} ne "Perl";
277 my $win_engine = 'ccperl';
278 my $linux_engine = 'Perl';
280 my ($script, $parm) = split / /, $trigger{script};
284 my ($win_script, $linux_script, $execwin, $execlinux);
286 $execwin = $execlinux = '';
288 if ($windows_trig_path) {
289 $win_script = $ARCH =~ /cygwin/i ? "\\\\$windows_trig_path\\$script"
290 : "$windows_trig_path\\$script";
292 warning "Unable to find trigger script $win_script ($!)"
293 if ($ARCH =~ /windows/i and $ARCH =~ /cygwin/) and not -e $win_script;
295 $execwin = "-execwin \"$win_engine $win_script $parm\" ";
296 } elsif ($linux_trig_path) {
297 $linux_script = "$linux_trig_path/$script";
299 warning "Unable to find trigger script $linux_script ($!)"
300 if ($ARCH !~ /windows/i and $ARCH !~ /cygwin/) and not -e $linux_script;
302 $execlinux = "-execwin \"$win_engine $win_script $parm\" ";
306 'cleartool mktrtype ' .
309 "$trigger{opkinds} " .
310 "-comment \"$trigger{desc}\" " .
313 "$trigger{name}\@$vobtag " .
316 debug "Command: $command";
321 $status = system $command
325 error "Unable to add trigger! Status = $status\nCommand: $command";
332 display "Replaced trigger $trigger{name} in $vob";
334 display "[noexecute] Would have replaced trigger $trigger{name} in $vob";
340 display "Added trigger $trigger{name} to $vob";
342 display "[noexecute] Would have added trigger $trigger{name} to $vob";
353 # Need an extra set of "\\" for non Windows systems such as Cygwin
354 # since apparently the shell if envoked, collapsing a set of "\\".
355 $vob = "\\" . $vob if $ARCH =~ /cygwin/;
357 my @lines = `cleartool describe vob:$vob`;
359 chomp @lines; chop @lines if $lines[0] =~ /\r$/;
369 sub MkTriggers ($$$$@) {
370 my ($vob, $exec, $add, $replace, @triggers) = @_;
372 TRIGGER: foreach (@triggers) {
375 my $vobname = RemoveVobPrefix $vob;
377 # Skip vobs on the novobs list
378 foreach (split /[\s+|,]/, $trigger{novobs}) {
379 my $vobtag = RemoveVobPrefix $_;
381 if ($vobname eq RemoveVobPrefix $_) {
382 debug "Skipping $vob (on novobs list)";
387 # For triggers whose vob type is "all" or unspecified make the trigger
388 if ($trigger{vobs} eq 'all' || $trigger{vobs} eq '') {
389 MkTriggerType $vob, $exec, $add, $replace, %trigger;
390 } elsif ($trigger{vobs} eq 'base' || $trigger{vobs} eq 'ucm') {
391 # If vob type is "base" or "ucm" make sure the vob is of correct type
392 my $vob_type = VobType ($vob);
394 if ($vob_type eq $trigger{vobs}) {
395 MkTriggerType $vob, $exec, $add, $replace, %trigger;
397 verbose "Trigger $trigger{name} is for $trigger{vobs} vobs but $vob is a $vob_type vob - Skipping...";
400 my @Vobs = split /[\s+|,]/, $trigger{vobs};
402 # Otherwise we expect the strings in $triggers{vobs} to be space or comma
403 # separated vob tags so we make sure it matches this $vob.
405 if ($vobname eq RemoveVobPrefix $_) {
406 MkTriggerType $vob, $exec, $add, $replace, %trigger;
416 my ($exec, $add, $replace, $private, @vobs) = (0, 0, 0, 0);
419 usage => sub { Usage },
420 verbose => sub { set_verbose },
421 debug => sub { set_debug },
422 'triggers=s', \$triggerData,
425 'replace!', \$replace,
426 'private!', \$private,
428 ) or Usage "Invalid parameter";
430 # This allows comma separated parms like -vob vob1,vob2,etc.
431 @vobs = split /,/, join (',', @vobs);
433 # If the user didn't specify -add or -replace then toggle both on
435 unless $add or $replace;
437 # If the user didn't specify any -vobs then that means all vobs
438 @vobs = `cleartool lsvob -short`
441 chomp @vobs; chop @vobs if $vobs[0] =~ /\r/;
443 # Parse the triggers.dat file
444 debug "Parsing trigger data ($triggerData)";
446 my @triggers = ParseTriggerData;
448 # Iterrate through the list of vobs
449 debug 'Processing ' . scalar @vobs . ' vobs';
451 foreach (sort @vobs) {
452 # Need an extra set of "\\" for non Windows systems such as Cygwin
453 # since apparently the shell if envoked, collapsing a set of "\\".
454 my $vob = $ARCH =~ /cygwin/i ? "\\$_" : $_;
455 my $line = `cleartool lsvob $vob`;
459 if ($line =~ / private/) {
460 verbose "Skipping private vob $vob...";
467 debug "Applying triggers to $vob...";
469 MkTriggers $_, $exec, $add, $replace, @triggers;
472 debug 'All triggers applied';