source: dasscm/trunk/usr/bin/dasscm@ 926

Last change on this file since 926 was 926, checked in by joergs, on Jul 30, 2010 at 6:45:47 PM

supress plugin test outputs, also when combinied commands by &&, add increase output

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 47.6 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 926 2010-07-30 16:45:47Z joergs $
4
5use strict;
6
7use Env
8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD $SHELL);
9use Cwd;
10use Getopt::Long;
11use File::Basename;
12use File::Compare;
13## used system("cp -a"), because File::Copy does not keep permissions
14##use File::Copy;
15use File::Find;
16use File::stat;
17use File::Path;
18use Term::ReadKey;
19
20use Data::Dumper;
21
22#####################################################################
23#
24# global
25#
26
27# shell exit codes
28my $RETURN_OK = 0;
29my $RETURN_NOK = 1;
30
31# Nagios return codes
32my $RETURN_WARN = 1;
33my $RETURN_CRIT = 2;
34my $RETURN_UNKNOWN = 3;
35
36# documentation file (for usage)
37my $doc_file = "/usr/share/doc/packages/dasscm/dasscm_howto.txt";
38
39my $config_file = "/etc/dasscm.conf";
40my $config = get_config($config_file);
41
42my @OPTIONS_GLOBAL = ( 'help', 'verbose' );
43
44my %COMMANDS = (
45 'help' => 'help',
46 'login' => 'login',
47 'init' => 'init',
48 'ls' => 'ls',
49 'update' => 'update',
50 'up' => 'update',
51 'add' => 'add',
52 'commit' => 'commit',
53 'checkin' => 'commit',
54 'ci' => 'commit',
55 'revert' => 'revert',
56 'blame' => 'blame',
57 'diff' => 'diff',
58 'status' => 'status',
59 'st' => 'status',
60 'check' => 'check',
61 'permissions' => 'permissions',
62 'cleanup' => 'cleanup',
63 'complete' => 'complete',
64 'complete_path' => 'complete_path',
65 'complete_repopath' => 'complete_repopath',
66 'plugins' => 'plugins',
67);
68
69# desc: description (eg. for usage)
70# params: parameters
71# CMD
72# USER
73# PATH_PROD
74# PATH_REPO
75# require:
76# WRITE commands that require write access (and therefore a login)
77my %COMMAND_DEFINITIONS = (
78 'help' => {
79 'desc' => ["print help and usage information"],
80 'params' => ["CMD"],
81 'function' => \&help,
82 },
83 'login' => {
84 'desc' => ["user login to Subversion repositoty"],
85 'params' => ["USER"],
86 'function' => \&login
87 },
88 'init' => {
89 'desc' => [
90 "initialize local subversion checkout.",
91 "This is the first thing to do (after configuring $config_file)"
92 ],
93 'params' => [],
94 'function' => \&init
95 },
96 'ls' => {
97 'desc' => ["list file from repository"],
98 'params' => ["PATH_REPO"],
99 'function' => \&ls
100 },
101 'update' => {
102 'desc' => [
103 "update local repository checkout",
104 "Normally, this is done automatically"
105 ],
106 'params' => ["PATH_REPO"],
107 'function' => \&update
108 },
109 'add' => {
110 'desc' => [
111 "add a file to the subversion repository",
112 "Unlike the native svn command,",
113 "dasscm adds and immediatly submits a file to the subversion repository"
114 ],
115 'params' => ["PATH_PROD"],
116 'options' => [ 'verbose', 'message=s' ],
117 'require' => ["WRITE"],
118 'function' => \&add
119 },
120 'commit' => {
121 'desc' => ["commit a changed file to the subversion repository"],
122 ## TODO: only modified files
123 'params' => ["PATH_REPO"],
124 'options' => [ 'verbose', 'message=s' ],
125 'require' => ["WRITE"],
126 'function' => \&commit
127 },
128 'revert' => {
129 'desc' => [
130 "revert local changes back to version from the repository (see diff)"
131 ],
132 'params' => ["PATH_REPO"],
133 'function' => \&revert
134 },
135 'blame' => {
136 'desc' => ['like "svn blame"'],
137 ## TODO: only files from PATH_REPO
138 'params' => ["PATH_REPO"],
139 'function' => \&blame
140 },
141 'diff' => {
142 'desc' => [
143 'display the differences between files on the system and the repository'
144 ],
145 'params' => ["PATH_REPO"],
146 'function' => \&diff
147 },
148 'status' => {
149 'desc' => [
150 'display status information about modified and deleted files.',
151 'If no path is given "/" is assumed',
152 '(in contract to "svn" with assumes ".")'
153 ],
154 'params' => ["PATH_REPO"],
155 'function' => \&status
156 },
157 'check' => {
158 'desc' => ["perform Nagios NRPE conform check"],
159 'params' => [],
160 'function' => \&check
161 },
162 'permissions' => {
163 'desc' =>
164 ["internal, print permissions for all files in the repository"],
165 'params' => [],
166 'function' => \&permissions
167 },
168 'cleanup' => {
169 'desc' => ["internal, used to clean repository checkout"],
170 'params' => [],
171 'function' => \&cleanup
172 },
173 'complete' => {
174 'desc' => ["internal, used for bash completion"],
175 'params' => ["CMD"],
176 'function' => \&complete
177 },
178 'complete_path' => {
179 'desc' => ["internal, used for bash completion"],
180 'params' => [],
181 'function' => \&complete_path
182 },
183 'complete_repopath' => {
184 'desc' => ["internal, used for bash completion"],
185 'params' => [],
186 'function' => \&complete_repopath
187 },
188 'plugins' => {
189 'desc' => ["internal, perform plugins"],
190 'params' => [],
191 'function' => \&perform_plugins
192 },
193
194);
195
196# configuration file
197my $DASSCM_LOCAL_REPOSITORY_BASE;
198my $DASSCM_REPOSITORY_NAME;
199my $DASSCM_PLUGIN_RESULTS_PATH;
200my $DASSCM_SVN_REPOSITORY;
201my $DASSCM_CHECKOUT_USERNAME;
202my $DASSCM_CHECKOUT_PASSWORD;
203my $DASSCM_GID;
204my @DASSCM_ADDITIONAL_FILES;
205
206# current directory at program start
207my $StartDirectory = cwd();
208
209my $diff = "diff --exclude .svn ";
210my $SVN = "svn ";
211my $svnOptions = "";
212my $svnCheckoutCredentials = "";
213my $svnPasswordCredentials = "";
214
215# flag. Set to true by svn_update
216# This prevents, that svn_update is called multiple times
217my $svnRepositoryIsUptodate = 0;
218
219# command line options get stored in options hash
220my %options = ();
221
222# subcommand, that gets executed (add, commit, ...)
223my $command;
224
225my $verbose = 0;
226
227#####################################################################
228#
229# util functions
230#
231sub usage()
232{
233 print '$Id: dasscm 926 2010-07-30 16:45:47Z joergs $';
234 print "\n\n";
235 print "usage: dasscm <subcommand> [options] [args]\n";
236 print "\n";
237 print "dasscm is intended to help versioning configuration files\n";
238 print "\n";
239 print "Available subcommands:\n";
240 foreach my $i ( sort keys(%COMMAND_DEFINITIONS) ) {
241 print " ", $i, " ", join( " ", get_command_possible_params($i) ),
242 "\n";
243 foreach my $line ( get_command_desc($i) ) {
244 print " " x 20, $line, "\n";
245 }
246 }
247 print "\n";
248 print "If dasscm is not yet configured, read $doc_file\n";
249}
250
251sub warning(@)
252{
253 print "Warning: " . join( "\n ", @_ ) . "\n";
254}
255
256sub error(@)
257{
258 print "Error: " . join( "\n ", @_ ) . "\n";
259}
260
261sub fatalerror(@)
262{
263 error(@_);
264
265 #print "Exiting\n";
266 exit 1;
267}
268
269#
270# reading config file and return key/value pairs as hash
271#
272sub get_config
273{
274 my $file = $_[0];
275
276 if ( !$file ) {
277 fatalerror( "failed to open config file" . $file );
278 }
279
280 my $data = {};
281
282 # try to open config file
283 if ( !open( FH, $file ) ) {
284 fatalerror( "failed to open config file" . $file );
285 } else {
286 while (<FH>) {
287 chomp;
288 if (/^#/) {
289 next;
290 }
291 if ( $_ =~ /=/g ) {
292
293 # splitting in 2 fields at maximum
294 my ( $option, $value ) = split( /=/, $_, 2 );
295 $option =~ s/^\s+//g;
296 $option =~ s/\s+$//g;
297 $option =~ s/\"+//g;
298 $value =~ s/^\s+//g;
299 $value =~ s/\s+$//g;
300 $value =~ s/\"+//g;
301
302 if ( length($option) ) {
303 $data->{$option} = $value;
304 }
305 }
306 }
307 }
308 close(FH);
309
310 return $data;
311}
312
313#
314# check and evaluate environment variables
315#
316sub check_env()
317{
318
319 # DASSCM_PROD
320 if ( !$DASSCM_PROD ) {
321 $DASSCM_PROD = "/";
322 }
323
324 if ( !-d $DASSCM_PROD ) {
325 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
326 }
327 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
328
329 # DASSCM_REPOSITORY_NAME
330 if ( !$DASSCM_REPOSITORY_NAME ) {
331 die
332 "Variable DASSCM_REPOSITORY_NAME is not defined.\nIt needs to be a unique name.\nNormally the full qualified host name is used.\nUse file $config_file to configure it.\n";
333 }
334
335 # DASSCM_REPO
336 if ( !$DASSCM_REPO ) {
337 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
338 $DASSCM_REPO =
339 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
340 } else {
341 die
342 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
343 }
344 }
345 $DASSCM_REPO = normalize_path($DASSCM_REPO);
346 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
347
348 #
349 # subversion checkout user
350 #
351 if ( !$DASSCM_CHECKOUT_USERNAME ) {
352 fatalerror(
353 "variable DASSCM_CHECKOUT_USERNAME is not defined.",
354 "Use file $config_file to configure it."
355 );
356 }
357
358 if ( !$DASSCM_CHECKOUT_PASSWORD ) {
359 fatalerror(
360 "variable DASSCM_CHECKOUT_PASSWORD is not defined.",
361 "Use file $config_file to configure it."
362 );
363 }
364
365 #
366 # check if local repository directory exist
367 # (if not creating by init)
368 #
369 if ( $command ne "init" ) {
370 if ( not -d $DASSCM_REPO ) {
371 fatalerror(
372 "Can't access local repository DASSCM_REPO",
373 "($DASSCM_REPO)",
374 "Check configuration and execute",
375 "dasscm init"
376 );
377 }
378
379 #
380 # user settings
381 #
382
383 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
384 if ( !$DASSCM_USERNAME ) {
385 $DASSCM_USERNAME = $DASSCM_USER;
386 }
387
388 # user root is not allowed for checkins.
389 # if user is root, DASSCM_USER has to be set,
390 # otherwise USER can be used
391 if ( "$USER" eq "root" ) {
392 if ( ( not $DASSCM_USERNAME )
393 and ( get_command_requires_write($command) ) )
394 {
395
396 #( $command ne "login" ) and ( $command ne "status" ) ) {
397 fatalerror(
398 "Envirnonment variable DASSCM_USERNAME not set.",
399 "Set DASSCM_USERNAME to your subversion user account or",
400 "use 'dasscm login'"
401 );
402 }
403 $svnOptions .= " --no-auth-cache ";
404 } elsif ( !$DASSCM_USERNAME ) {
405 $DASSCM_USERNAME = $USER;
406 }
407
408 #
409 # password
410 #
411 if ($DASSCM_PASSWORD) {
412 $svnPasswordCredentials = " --password '$DASSCM_PASSWORD' ";
413 }
414 }
415
416 #$svnOptions .= " --username $DASSCM_USERNAME "
417
418 #
419 # prepare file permissions
420 # (read-write access for group "dasscm",
421 # if this group exists)
422 #
423 (my $gname, my $gpw, $DASSCM_GID, my $members) = getgrnam( "dasscm" );
424 if( $DASSCM_GID ) {
425 umask 0007
426 }
427}
428
429#
430# has been intendend,
431# to check addtitional parameters.
432# Currently not used.
433#
434sub check_parameter(@)
435{
436}
437
438sub get_command_uniform_name( $ )
439{
440 my $command_abbrivation = $_[0];
441 if ( defined( $COMMANDS{$command_abbrivation} ) ) {
442 return $COMMANDS{$command_abbrivation};
443 }
444 return;
445}
446
447sub get_command_desc( $ )
448{
449 my $command = get_command_uniform_name( $_[0] );
450 my @desc = ();
451 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'desc'} ) ) {
452 @desc = @{ $COMMAND_DEFINITIONS{$command}{'desc'} };
453 }
454 return @desc;
455}
456
457sub get_command_function( $ )
458{
459 my $command = get_command_uniform_name( $_[0] );
460 my $func;
461 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'function'} ) ) {
462 $func = $COMMAND_DEFINITIONS{$command}{'function'};
463 }
464 return $func;
465}
466
467sub get_command_possible_params( $ )
468{
469 my $command = get_command_uniform_name( $_[0] );
470 my @params = ();
471 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'params'} ) ) {
472 @params = @{ $COMMAND_DEFINITIONS{$command}{'params'} };
473 }
474 return @params;
475}
476
477sub get_command_possible_options( $ )
478{
479 my $command = get_command_uniform_name( $_[0] );
480 my @params = ();
481 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'options'} ) ) {
482 @params = @{ $COMMAND_DEFINITIONS{$command}{'options'} };
483 }
484 return @params;
485}
486
487sub get_command_requirements( $ )
488{
489 my $command = get_command_uniform_name( $_[0] );
490 my @requirements = ();
491 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'require'} ) ) {
492 @requirements = @{ $COMMAND_DEFINITIONS{$command}{'require'} };
493 }
494 return @requirements;
495}
496
497sub get_command_requires_write( $ )
498{
499 return grep( /^WRITE$/, get_command_requirements( $_[0] ) );
500}
501
502#
503# normalize path namens:
504# - directories should end with "/"
505# - use only single "/"
506#
507sub normalize_path($)
508{
509 my $path = shift || "";
510
511 if ( $path =~ m|^/| ) {
512
513 # full path
514 if ( -d $path ) {
515
516 # ensure, a directory ends with '/'
517 $path .= '/';
518 }
519 } elsif ( -d cwd() . '/' . $path ) {
520
521 # ensure, a directory ends with '/'
522 $path .= '/';
523 }
524
525 # remove double (triple) slashes (/)
526 $path =~ s|/[/]*|/|g;
527
528 # remove self reference path
529 $path =~ s|/./|/|g;
530
531 return $path;
532}
533
534#
535# generate from (relative) filename
536# all required file and directory names:
537# $basename, $dirname_prod, $dirname_repo,
538# $filename_prod, $filename_repo
539#
540sub get_filenames(@)
541{
542 my $filename_prod = $_[0] || ".";
543
544 # make filename absolut
545 if ( !( $filename_prod =~ m/^\// ) ) {
546 $filename_prod = cwd() . '/' . $filename_prod;
547 }
548
549 # file must be readable.
550 # The only exceptions are,
551 # - if the file parameter is to be completed or
552 # - if a file should be reverted
553 if ( $command ne "revert" && $command !~ m/^complete/ ) {
554 if ( not -r $filename_prod ) {
555 fatalerror( $filename_prod . " is not accessable" );
556 }
557 }
558
559 # dirname buggy: eg. "/etc/" is reduced to "/",
560 # "/etc" is used as filename
561 # herefore make sure, that if filename is a directory,
562 # it will end by "/"
563 $filename_prod = normalize_path($filename_prod);
564
565 ( my $basename, my $dirname_prod ) = fileparse($filename_prod);
566
567 # normalize path.
568 # not done for reverting, because in this case, the directory may not exist
569 # and the correct path should already be stored in the repository
570 if ( $command ne "revert" ) {
571
572 # uses chdir to determine real directory in a unique way
573 chdir $dirname_prod
574 or fatalerror( "failed to access directory $dirname_prod: " . $! );
575 $dirname_prod = normalize_path( cwd() );
576 chdir $StartDirectory;
577 }
578
579 my $dirname_repo = normalize_path( $DASSCM_REPO . "/" . $dirname_prod );
580 my $filename_repo = normalize_path("$dirname_repo/$basename");
581
582 if ($verbose) {
583 print "filename_repo: " . $filename_repo . "\n";
584 print "dirname_repo: " . $dirname_repo . "\n";
585 print "filename_prod: " . $filename_prod . "\n";
586 print "dirname_prod: " . $dirname_prod . "\n";
587 print "basename: " . $basename . "\n";
588 }
589
590 return (
591 $basename, $dirname_prod, $dirname_repo,
592 $filename_prod, $filename_repo
593 );
594}
595
596sub copy_file_to_repository( $ )
597{
598 my $filename = shift;
599
600 (
601 my $basename,
602 my $dirname_prod,
603 my $dirname_repo,
604 my $filename_prod,
605 my $filename_repo
606 ) = get_filenames($filename);
607
608 #copy( $filename_prod, $filename_repo )
609 ( my $rc, my @result ) =
610 run_command("cp -a \"$filename_prod\" \"$filename_repo\"");
611 if ( $rc != 0 ) {
612 error( "failed to copy $filename_prod to repository: ", @result );
613 }
614
615 # return success
616 return $rc == 0;
617}
618
619sub copy_file_from_repository_to_system( $ )
620{
621 my $filename = shift;
622
623 (
624 my $basename,
625 my $dirname_prod,
626 my $dirname_repo,
627 my $filename_prod,
628 my $filename_repo
629 ) = get_filenames($filename);
630
631 ( my $rc, my @result ) =
632 run_command("cp -a \"$filename_repo\" \"$filename_prod\"");
633 if ( $rc != 0 ) {
634 error( "failed to copy $filename_repo to $filename_prod: ", @result );
635 }
636
637 # return success
638 return $rc == 0;
639}
640
641#
642# creates a file with permissions
643#
644sub generatePermissionList
645{
646
647 # generieren der Zeilen für Permission-Savefile
648 my @files = @_;
649 my @permlist = ();
650 foreach my $file (@files) {
651 $file = "/" . $file;
652 if ( -e $file ) {
653 my $info = stat($file) || die "failed to stat $file: aborting";
654 my $mode = get_type( $info->mode ) & 07777;
655 my $modestring = sprintf( "%04o", $mode );
656 my $uidnumber = $info->uid;
657 my $uid = getpwuid($uidnumber) || $uidnumber;
658 my $gidnumber = $info->gid;
659 my $gid = getgrgid($gidnumber) || $gidnumber;
660 push(
661 @permlist,
662 sprintf( "%-55s %-17s %4d",
663 $file, "${uid}:${gid}", $modestring )
664 );
665 }
666 }
667 return @permlist;
668}
669
670sub get_type
671{
672
673 # Funktion übernommen aus /usr/bin/chkstat
674 my $S_IFLNK = 0120000; # symbolic link
675 my $S_IFREG = 0100000; # regular file
676 my $S_IFDIR = 0040000; # directory
677 my $S_IFCHAR = 0020000; # character device
678 my $S_IFBLK = 0060000; # block device
679 my $S_IFFIFO = 0010000; # fifo
680 my $S_IFSOCK = 0140000; # socket
681 my $S_IFMT = 0170000; # type of file
682
683 my $S_m;
684 if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
685 elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
686 elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
687 elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
688 elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
689 elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
690 elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
691 $S_m;
692}
693
694sub run_command
695{
696 my $command = shift;
697
698 if ($verbose) {
699 print "executing command: " . $command . "\n";
700 }
701
702 my @result;
703 if( open( RESULT, $command . ' 2>&1 |' ) ) {
704 @result = <RESULT>;
705 close(RESULT);
706 }
707 my $retcode = $? >> 8;
708
709 if ($verbose) {
710 print @result;
711 if( $retcode ) { print "return code: " . $retcode . "\n"; }
712 }
713
714 return ( $retcode, @result );
715}
716
717sub run_interactive
718{
719
720 if ($verbose) {
721 print "run_interactive:" . join( " ", @_ ) . "\n";
722 }
723
724 system(@_);
725 if ( $? == -1 ) {
726 printf "failed to execute: $!\n";
727 } elsif ( $? & 127 ) {
728 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
729 ( $? & 128 ) ? 'with' : 'without';
730 } elsif ( $? >> 8 != 0 ) {
731 printf "child exited with value %d\n", $? >> 8;
732 }
733 return ( $? >> 8 );
734}
735
736#
737# en- or disable echo mode.
738# used for reading passwords from STDIN
739#
740sub setEchoMode( $ )
741{
742 my $mode = shift;
743 if ($mode) {
744 run_command("stty echo");
745 } else {
746 run_command("stty -echo");
747 }
748}
749
750sub write_array_to_file( $@ )
751{
752 my $filename = shift;
753 my @array = @_;
754
755 if ( -e $filename && !-w $filename ) {
756 warning( "failed to write to $filename:", "permission denied" );
757 return;
758 }
759
760 if ( !-w dirname($filename) ) {
761 warning( "failed to write to $filename:", "directory does not exist" );
762 return;
763 }
764
765 # directory exists => write
766 if ( !open( OUTFILE, ">$filename" ) ) {
767 warning("failed to open $filename: $!");
768 return;
769 }
770
771 foreach my $line (@array) {
772 print OUTFILE "$line";
773 }
774 close(OUTFILE);
775
776 # if group dasscm exists,
777 # create plugin results with group membership dasscm
778 if( $DASSCM_GID ) {
779 chown( -1, $DASSCM_GID, $filename );
780 }
781
782 return 1;
783}
784
785sub perform_plugins()
786{
787 check_env();
788
789 my @plugin_results = ();
790
791 # get all defined plugins.
792 # Plugin definitions starting with DASSCM_PLUGIN_
793 my @plugins = grep( /^DASSCM_PLUGIN_CMD_/, keys( %{$config} ) );
794
795 for my $plugin (@plugins) {
796 my $plugin_name = substr( $plugin, length("DASSCM_PLUGIN_CMD_") );
797 my $plugin_test = $config->{ 'DASSCM_PLUGIN_TEST_' . $plugin_name };
798 if ($verbose) { print "Plugin $plugin_name: "; }
799 # all plugins are executed with LANG settings C
800 # bash -c is used, to supress all output
801 # (otherwise there are problem with && commands)
802 ( my $rc_test, my @result_test ) = run_command( "LANG=C bash -c " . $plugin_test);
803 if ( $rc_test != 0 ) {
804 if ($verbose) { print "skipped\n"; }
805 } else {
806 if ($verbose) { print "$config->{$plugin}\n"; }
807 ( my $rc, my @result ) = run_command( $config->{$plugin} );
808 if ( $rc != 0 ) {
809 warning("failed to run plugin $plugin");
810 } else {
811 my $plugin_result_file =
812 $DASSCM_PLUGIN_RESULTS_PATH . "/" . $plugin_name;
813 write_array_to_file( $plugin_result_file, @result );
814 push @plugin_results, $plugin_result_file;
815 }
816 }
817 }
818 return @plugin_results;
819}
820
821sub svn_check_credentials( $$;$$ )
822{
823 my $username = shift;
824 my $password = shift;
825
826 # check silently are allow user interaction?
827 my $interactive = shift || 0;
828
829 # default: exit program, if repository is not accessable
830 # (do not exit for 'init')
831 my $fatalerror = shift || 1;
832
833 print "checking credentials ";
834
835 if ( !$username ) {
836 fatalerror("no username given");
837 }
838
839 if ( !$password ) {
840 fatalerror("no password given");
841 }
842
843 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
844
845 # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
846 # therefore switching to "svn status"
847 # ( my $rc_update, my @result ) =
848 # run_command(
849 # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
850 # );
851 #print @result;
852
853 my $rc_update;
854 if ($interactive) {
855 $rc_update = run_interactive(
856 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
857 );
858 } else {
859 ( $rc_update, my @result ) = run_command(
860 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
861 );
862
863 if ( $rc_update != 0 ) {
864 print "\n", @result;
865 if ($fatalerror) {
866 fatalerror();
867 }
868 return;
869 }
870 }
871
872 # return success
873 return $rc_update == 0;
874}
875
876sub svn_update( ;$ )
877{
878 my $update_path = shift || "";
879
880 # return value
881 my $update_ok = 1;
882
883 # use this flag to do only one update per run
884 if ( !$svnRepositoryIsUptodate ) {
885 ( my $rc_update, my @result ) = run_command(
886 "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'"
887 );
888 print @result;
889 if ( $rc_update != 0 ) {
890 error("failed to update local repository ($update_path)");
891 $update_ok = 0;
892 } elsif ( not $update_path ) {
893
894 # set this flag if a full update is done
895 $svnRepositoryIsUptodate = 1;
896 }
897 }
898 return $update_ok;
899}
900
901sub svn_ls( ;@ )
902{
903 (
904 my $basename,
905 my $dirname_prod,
906 my $dirname_repo,
907 my $filename_prod,
908 my $filename_repo
909 ) = get_filenames( $_[0] );
910
911 # svn ls -R is better, but much, much slower
912 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
913
914 my @files = ();
915 my @links = ();
916 my @dirs = ();
917 my @others = ();
918
919 find(
920 {
921 wanted => sub {
922 my $name = normalize_path($File::Find::name);
923 $name =~ s|^$dirname_repo||;
924
925 #print "($name)\n";# . $File::Find::dir . "\n";
926 if ( not $name ) {
927
928 # name string is empty (top directory).
929 # do nothing
930 } elsif ( $name =~ m/\.svn/ ) {
931
932 # skip svn meta data
933 } elsif ( -l $_ ) {
934
935 # soft link
936 # important: check for links first
937 # to exclude them from further checks
938 push( @links, $name );
939 } elsif ( -d $_ ) {
940
941 # directories
942 push( @dirs, $name );
943 } elsif ( -f $_ ) {
944
945 # regular file
946 push( @files, $name );
947 } else {
948 push( @others, $name );
949 }
950 }
951 },
952 ($filename_repo)
953 );
954
955 return ( sort( @dirs, @files ) );
956}
957
958sub svn_revert( ;$ )
959{
960 my $path = shift || $DASSCM_REPO;
961
962 ( my $rc_update, my @result ) = run_command("$SVN revert -R '$path'");
963
964 if ( $rc_update != 0 ) {
965 print "\n", @result;
966 error("failed to revert subversion repository changes");
967 }
968}
969
970sub svn_remove_unknown_files( ;$ )
971{
972 my $path = shift || $DASSCM_REPO;
973
974 ( my $rc_update, my @result ) = run_command("$SVN status '$path'");
975
976 if ( $rc_update != 0 ) {
977 print "\n", @result;
978 error("failed to receive subversion repository information");
979 } else {
980 foreach (@result) {
981 if (s/^\? +//) {
982 chomp;
983
984 # if file is unknown to subversion (line starts with "?")
985 # remove it
986 print "removing $_\n";
987
988 # unlink doesn't work recursive, there "rm -rf" is used
989 #unlink($_);
990 system("rm -rf $_");
991 }
992 }
993 }
994}
995
996sub getModifiedFiles( ;$ )
997{
998 (
999 my $basename,
1000 my $dirname_prod,
1001 my $dirname_repo,
1002 my $filename_prod,
1003 my $filename_repo
1004 ) = get_filenames( $_[0] );
1005
1006 my @files = svn_ls($filename_prod);
1007
1008 # stores result from status (cvscheck)
1009 my %removedfiles = ();
1010 my %changedfiles = ();
1011 my %unknownfiles = ();
1012
1013 # create list of modified files
1014 if (@files) {
1015
1016 foreach my $file (@files) {
1017
1018 my $realfile = $dirname_prod . $file;
1019 my $cvsworkfile = $dirname_repo . $file;
1020
1021 if ( -d $realfile ) {
1022
1023 # directory
1024 if ( !-d "$cvsworkfile" ) {
1025
1026 # real is directory, repository is not. This is a problem
1027 $changedfiles{"$realfile"} = $cvsworkfile;
1028 }
1029 } elsif ( !-e $realfile ) {
1030 $removedfiles{"$realfile"} = $cvsworkfile;
1031 } elsif ( !-r $realfile ) {
1032
1033 # don't have permission to read the file,
1034 # can't check it
1035 $unknownfiles{"$realfile"} = $cvsworkfile;
1036 } else {
1037 ( -r "$cvsworkfile" )
1038 || fatalerror("failed to read $cvsworkfile");
1039 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
1040 $changedfiles{"$realfile"} = $cvsworkfile;
1041 }
1042 }
1043 }
1044 }
1045
1046 return ( \%changedfiles, \%removedfiles, \%unknownfiles );
1047}
1048
1049#
1050# from an array of files/dirs,
1051# generates list of files
1052# sorted by type
1053#
1054sub get_files( @ )
1055{
1056 my @files = ();
1057 my @links = ();
1058 my @dirs = ();
1059 my @others = ();
1060
1061 if (@_) {
1062 find(
1063 {
1064 wanted => sub {
1065 my $fullname = cwd() . "/" . $_;
1066 if ( -l $_ ) {
1067
1068 # soft link
1069 # important: check for links first
1070 # to exclude them from further checks
1071 push( @links, $fullname );
1072 } elsif ( -d $_ ) {
1073
1074 # directories
1075 push( @dirs, $fullname );
1076 } elsif ( -f $_ ) {
1077
1078 # regular file
1079 push( @files, $fullname );
1080 } else {
1081 push( @others, $fullname );
1082 }
1083 }
1084 },
1085 @_
1086 );
1087 }
1088
1089 # don't rely on others.
1090 # If more specific file types are needed,
1091 # they will be added
1092 return {
1093 files => \@files,
1094 links => \@links,
1095 dirs => \@dirs,
1096 others => \@others
1097 };
1098}
1099
1100sub print_files_hash( $ )
1101{
1102 my $href_files = shift;
1103
1104 my @files = @{ $href_files->{files} };
1105 my @links = @{ $href_files->{links} };
1106
1107 if (@files) {
1108 my $number = $#files + 1;
1109 print "files to check-in ($number): \n";
1110 print join( "\n", @files );
1111 print "\n";
1112 }
1113
1114 # TODO: check in links and also link target? At least warn about link target
1115 if (@links) {
1116 my $number = $#links + 1;
1117 print "\n";
1118 print "ignoring links ($number):\n";
1119 print join( "\n", @links );
1120 print "\n";
1121 }
1122
1123}
1124
1125#
1126# use globbing to get lsit of files
1127# that matches the given prefix
1128# used for bash completion
1129#
1130sub get_complete_path_globbing( $ )
1131{
1132 my $path = shift;
1133
1134 # add globbing
1135 $path .= "*";
1136
1137 # get files
1138 my @files = glob($path);
1139
1140 if ( $#files == 0 ) {
1141
1142 # if only one result is available
1143 # and this result is a directory,
1144 # add another result entry
1145 # (directory with and withour trainling /),
1146 # otherwise complete will stop here and continue with the next parameter
1147 my $path = normalize_path( $files[0] );
1148 if ( -d $path ) {
1149 @files = ( substr( $path, 0, -1 ), $path );
1150 }
1151 } else {
1152
1153 # add "/" to all directories
1154 @files = map( { normalize_path($_) } @files );
1155 }
1156
1157 return @files;
1158}
1159
1160#####################################################################
1161#
1162# functions
1163sub help(;@)
1164{
1165 if ( not @_ ) {
1166 usage();
1167 } else {
1168 print "help for ", join( " ", @_ ), ": ...\n";
1169 usage();
1170 }
1171}
1172
1173sub login(@)
1174{
1175 check_parameter( @_, 1 );
1176 check_env();
1177
1178 my $input_username = $_[0];
1179
1180 if ( not $input_username ) {
1181 my $output_username = "";
1182 if ($DASSCM_USERNAME) {
1183 $output_username = " ($DASSCM_USERNAME)";
1184 }
1185
1186 print "Enter DASSCM user name", $output_username, ": ";
1187 $input_username = <STDIN>;
1188 chomp($input_username);
1189
1190 $input_username = $input_username || $DASSCM_USERNAME;
1191 }
1192
1193 # hidden password input
1194 print "Enter password for $input_username: ";
1195 setEchoMode(0);
1196 my $input_password = <STDIN>;
1197 setEchoMode(1);
1198 chomp($input_password);
1199 print "\n";
1200
1201 # checking checkout username/password
1202 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
1203 $DASSCM_CHECKOUT_PASSWORD );
1204 print "checkout access okay\n";
1205
1206 svn_check_credentials( $input_username, $input_password );
1207
1208 #
1209 # set environment variables
1210 #
1211 $ENV{'DASSCM_USERNAME'} = "$input_username";
1212 $ENV{'DASSCM_PASSWORD'} = "$input_password";
1213
1214 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
1215 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
1216 "DASSCM_REPO: $DASSCM_REPO\n",
1217 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
1218
1219 status();
1220
1221 print "\n[dasscm shell]\n\n";
1222 my $shell = $SHELL || "bash";
1223 exec($shell) or die "failed to start new shell";
1224}
1225
1226#
1227# initialize local checkout directory (initial checkout)
1228#
1229sub init(@)
1230{
1231 check_parameter( @_, 1 );
1232 check_env();
1233
1234 # don't do repository creation (svn mkdir) here,
1235 # because then their must be a lot of prior checks
1236
1237 # update complete repository
1238 my $retcode = run_interactive(
1239 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY"
1240 );
1241}
1242
1243sub ls(@)
1244{
1245 check_parameter( @_, 1 );
1246 check_env();
1247
1248 my @files = svn_ls(@_);
1249
1250 if (@files) {
1251 print join( "\n", @files );
1252 print "\n";
1253 }
1254}
1255
1256sub update(@)
1257{
1258 check_parameter( @_, 1 );
1259 check_env();
1260
1261 #
1262 # update local repository
1263 #
1264 svn_update();
1265}
1266
1267#
1268# helper function for "add" command
1269#
1270sub add_helper(@)
1271{
1272 (
1273 my $basename,
1274 my $dirname_prod,
1275 my $dirname_repo,
1276 my $filename_prod,
1277 my $filename_repo
1278 ) = get_filenames( $_[0] );
1279
1280 mkpath($dirname_repo);
1281 copy_file_to_repository($filename_prod);
1282
1283 # already checked in?
1284 chdir $DASSCM_REPO;
1285
1286 # also add the path to filename.
1287 for my $dir ( split( '/', $dirname_prod ) ) {
1288 if ($dir) {
1289 my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
1290 if ( $rc > 0 ) {
1291 print join( "\n", @out );
1292 }
1293 chdir $dir;
1294 }
1295 }
1296 my ( $rc, @out ) = run_command("$SVN add '$basename'");
1297 if ( $rc > 0 ) {
1298 print join( "\n", @out );
1299 }
1300 chdir $StartDirectory;
1301
1302}
1303
1304sub add_helper_multi(@)
1305{
1306
1307 # get all regular files and links
1308 my $href_files = get_files(@_);
1309
1310 #print Dumper( $href_files );
1311
1312 my @files = @{ $href_files->{files} };
1313 my @links = @{ $href_files->{links} };
1314
1315 # copy files one by one to local repository
1316 for my $file (@files) {
1317
1318 # add file
1319 add_helper($file);
1320 }
1321
1322 return $href_files;
1323}
1324
1325#
1326# adding new files (or directories)
1327#
1328sub add(@)
1329{
1330 check_parameter( @_, 1 );
1331 check_env();
1332
1333 #
1334 # update local repository
1335 #
1336 svn_update();
1337
1338 # add files to repository, print information about added files
1339 print_files_hash( add_helper_multi(@_) );
1340
1341 # perform plugins and add additional files, like plugin results
1342 perform_plugins();
1343 add_helper_multi(@DASSCM_ADDITIONAL_FILES);
1344
1345 if ( $options{'message'} ) {
1346 $svnOptions .= " --message \"$options{'message'}\" ";
1347 }
1348
1349 # commit calls $EDITOR.
1350 # use "interactive" here, to display output
1351 my $retcode = run_interactive(
1352 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1353 );
1354
1355 # svn commit does not deliever an error return code, if commit is canceld,
1356 # so a revert is performed in any case
1357 svn_revert();
1358}
1359
1360#
1361# checks in all modified files
1362#
1363sub commit(@)
1364{
1365 check_parameter( @_, 1 );
1366 check_env();
1367
1368 (
1369 my $basename,
1370 my $dirname_prod,
1371 my $dirname_repo,
1372 my $filename_prod,
1373 my $filename_repo
1374 ) = get_filenames( $_[0] );
1375
1376 #
1377 # update local repository
1378 #
1379 svn_update();
1380
1381 ( my $refChangedFiles, my $refRemovedFiles ) =
1382 getModifiedFiles($filename_prod);
1383 my %changedfiles = %{$refChangedFiles};
1384 my %removedfiles = %{$refRemovedFiles};
1385
1386 if (%removedfiles) {
1387 my $removedFilesString =
1388 '"' . join( '" "', values(%removedfiles) ) . '"';
1389 my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
1390 if ( $rc > 0 ) {
1391 print join( "\n", @out );
1392 }
1393 }
1394
1395 # copy files one by one to local repository
1396 for my $file ( keys(%changedfiles) ) {
1397 copy_file_to_repository($file);
1398 }
1399
1400 perform_plugins();
1401 add_helper_multi(@DASSCM_ADDITIONAL_FILES);
1402
1403 if ( $options{'message'} ) {
1404 $svnOptions .= " --message \"$options{'message'}\" ";
1405 }
1406
1407 # commit calls $EDITOR.
1408 # use "interactive" here, to display output
1409 my $retcode = run_interactive(
1410 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1411 );
1412
1413 # svn commit does not deliever an error return code, if commit is canceld,
1414 # so a revert is performed in any case
1415 svn_revert();
1416}
1417
1418#
1419# revert: copies files back from repository to system
1420#
1421sub revert(@)
1422{
1423 check_parameter( @_, 1 );
1424 check_env();
1425
1426 (
1427 my $basename,
1428 my $dirname_prod,
1429 my $dirname_repo,
1430 my $filename_prod,
1431 my $filename_repo
1432 ) = get_filenames( $_[0] );
1433
1434 # return code for the shell
1435 # default: error
1436 my $return_code = $RETURN_OK;
1437
1438 # cleanup repository
1439 cleanup();
1440
1441 #svn_update();
1442
1443 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1444 getModifiedFiles($filename_prod);
1445 my %changedfiles = %{$refChangedFiles};
1446 my %removedfiles = %{$refRemovedFiles};
1447 my %unknownfiles = %{$refUnknownFiles};
1448
1449 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1450
1451 if (%removedfiles) {
1452 print "DELETED files and directories. Recreated from repository:\n";
1453 my @removedPaths =
1454 ( sort { length $a > length $b } keys %removedfiles );
1455 print join( "\n", @removedPaths ) . "\n\n";
1456
1457 # copy files one by one from local repository to system
1458 # and also create directories
1459 # paths are sorted, so that directories are created first
1460 for my $real_path (@removedPaths) {
1461 if ( -d $removedfiles{"$real_path"} ) {
1462 mkpath("$real_path");
1463 } else {
1464 copy_file_from_repository_to_system($real_path);
1465 }
1466 }
1467 }
1468
1469 if (%changedfiles) {
1470 print "MODIFIED files. Copied from repository to the system:\n";
1471 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1472
1473 # copy files one by one from local repository to system
1474 for my $real_file ( keys(%changedfiles) ) {
1475 copy_file_from_repository_to_system($real_file);
1476 }
1477
1478 }
1479
1480 if (%unknownfiles) {
1481 print "UNKNOWN: insufficient permission to check files:\n";
1482 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1483
1484 $return_code = $RETURN_NOK;
1485 }
1486
1487 } else {
1488 print "no modified files found in $dirname_repo\n";
1489 }
1490
1491 return $return_code;
1492}
1493
1494sub blame(@)
1495{
1496 check_parameter( @_, 1 );
1497 check_env();
1498
1499 (
1500 my $basename,
1501 my $dirname_prod,
1502 my $dirname_repo,
1503 my $filename_prod,
1504 my $filename_repo
1505 ) = get_filenames( $_[0] );
1506
1507 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
1508}
1509
1510sub diff(@)
1511{
1512 check_parameter( @_, 1 );
1513 check_env();
1514
1515 (
1516 my $basename,
1517 my $dirname_prod,
1518 my $dirname_repo,
1519 my $filename_prod,
1520 my $filename_repo
1521 ) = get_filenames( $_[0] );
1522
1523 #print "$basename,$dirname_prod,$dirname_repo\n";
1524
1525 svn_update();
1526
1527 ( my $rc_diff, my @diff_result ) =
1528 run_command( $diff . " $filename_repo $filename_prod" );
1529
1530 print @diff_result;
1531}
1532
1533sub status(@)
1534{
1535 check_parameter( @_, 1 );
1536 check_env();
1537
1538 (
1539 my $basename,
1540 my $dirname_prod,
1541 my $dirname_repo,
1542 my $filename_prod,
1543 my $filename_repo
1544 ) = get_filenames( $_[0] || "/" );
1545
1546 # return code for the shell
1547 # default: error
1548 my $return_code = $RETURN_NOK;
1549
1550 #
1551 # update local repository
1552 #
1553 #svn_update( $filename_prod );
1554
1555 # perform plugins (required to see changes in plugin results)
1556 perform_plugins();
1557
1558 # get modified files
1559 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1560 getModifiedFiles($dirname_prod);
1561 my %changedfiles = %{$refChangedFiles};
1562 my %removedfiles = %{$refRemovedFiles};
1563 my %unknownfiles = %{$refUnknownFiles};
1564
1565 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1566
1567 if (%removedfiles) {
1568 print "DELETED: files found in repository, but not in system:\n";
1569 print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
1570 }
1571
1572 if (%changedfiles) {
1573 print "MODIFIED: files differs between repository and system:\n";
1574 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1575 }
1576
1577 if (%unknownfiles) {
1578 print "UNKNOWN: insufficient permission to check files:\n";
1579 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1580 }
1581
1582 } else {
1583 print "no modified files found in $dirname_repo\n";
1584 $return_code = $RETURN_OK;
1585 }
1586
1587 return $return_code;
1588}
1589
1590#
1591# return short status in Nagios plugin conform way
1592#
1593sub check()
1594{
1595 check_env();
1596
1597 # return code for the shell
1598 my $return_code = $RETURN_OK;
1599 my $return_string = "OK: no modified files";
1600
1601 # perform plugins (required to see changes in plugin results)
1602 perform_plugins();
1603
1604 # get modified files
1605 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1606 getModifiedFiles("/");
1607 my %changedfiles = %{$refChangedFiles};
1608 my %removedfiles = %{$refRemovedFiles};
1609 my %unknownfiles = %{$refUnknownFiles};
1610
1611 if ( %removedfiles or %changedfiles ) {
1612 $return_string = "Warning: ";
1613 if (%changedfiles) {
1614 $return_string .=
1615 "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
1616 }
1617 if (%removedfiles) {
1618 $return_string .=
1619 "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
1620 }
1621 if (%unknownfiles) {
1622 $return_string .=
1623 "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
1624 }
1625 $return_code = $RETURN_WARN;
1626 }
1627
1628 # addition nagios Service Status
1629 #Critical
1630 #Unknown
1631
1632 print "$return_string\n";
1633 return $return_code;
1634}
1635
1636sub permissions()
1637{
1638 check_env();
1639
1640 my $return_code = $RETURN_OK;
1641
1642 #
1643 # update local repository
1644 #
1645 #svn_update();
1646
1647 my $dir = $DASSCM_REPO;
1648 my @files = svn_ls("/");
1649
1650 if (@files) {
1651
1652 print "#\n";
1653 print "# created by dasscm permissions\n";
1654 print "# It is intended to be used for restoring permissions\n";
1655 print "#\n";
1656
1657 # generate and print permissions
1658 foreach my $line ( generatePermissionList(@files) ) {
1659 print "$line\n";
1660 }
1661
1662 }
1663
1664 return $return_code;
1665}
1666
1667#
1668# remove all uncommited changes in the repository
1669#
1670sub cleanup()
1671{
1672 check_env();
1673
1674 svn_revert($DASSCM_REPO);
1675 svn_remove_unknown_files($DASSCM_REPO);
1676}
1677
1678#
1679# used for bash completion
1680# prints the next possible command line parameters
1681#
1682sub complete(@)
1683{
1684 my @input = @_;
1685 my %options_complete = ();
1686
1687 # check and remove global options. if options are wrong, nothing to do
1688 @ARGV = @input;
1689 if ( GetOptions( \%options_complete, @OPTIONS_GLOBAL ) ) {
1690 my $number_arguments = @input;
1691 if ( $number_arguments <= 1 ) {
1692
1693 # complete dasscm commands
1694 my $input = $input[0] || "";
1695 map { m/^$input/ && print $_, "\n" } ( keys %COMMANDS );
1696 } else {
1697
1698 # complete dasscm parameter
1699 my $command = get_command_uniform_name( $input[0] );
1700 if ($command) {
1701
1702 # remove command
1703 shift @input;
1704
1705 # check and remove options
1706 my @options = get_command_possible_options($command);
1707 @ARGV = @input;
1708 if ( ( not @options )
1709 || ( GetOptions( \%options_complete, @options ) ) )
1710 {
1711
1712 my @params = get_command_possible_params($command);
1713 if ($verbose) { print "params: ", Dumper(@params); }
1714
1715 my $number_arguments = @input;
1716
1717 #print "input: ", join( ",", @input ), " (", $number_arguments, ")\n";
1718
1719 if ( $number_arguments > 0 ) {
1720 my $parameter_number = $number_arguments - 1;
1721 if ( defined( $params[$parameter_number] )
1722 && $params[$parameter_number] )
1723 {
1724 my $param = $params[$parameter_number];
1725 if ($verbose) {
1726 print "param used: ", $param, "\n";
1727 }
1728 if ( $param eq "PATH_PROD" ) {
1729 complete_path(
1730 $input[ $number_arguments - 1 ] );
1731 } elsif ( $param eq "PATH_REPO" ) {
1732 complete_repopath(
1733 $input[ $number_arguments - 1 ] );
1734 }
1735 }
1736 }
1737 }
1738 }
1739 }
1740 }
1741}
1742
1743sub complete_path(@)
1744{
1745 check_parameter( @_, 1 );
1746 check_env();
1747
1748 (
1749 my $basename,
1750 my $dirname_prod,
1751 my $dirname_repo,
1752 my $filename_prod,
1753 my $filename_repo
1754 ) = get_filenames( $_[0] );
1755
1756 my @files = get_complete_path_globbing($filename_prod);
1757
1758 if (@files) {
1759 print join( "\n", @files );
1760 print "\n";
1761 }
1762}
1763
1764sub complete_repopath(@)
1765{
1766 check_parameter( @_, 1 );
1767 check_env();
1768
1769 (
1770 my $basename,
1771 my $dirname_prod,
1772 my $dirname_repo,
1773 my $filename_prod,
1774 my $filename_repo
1775 ) = get_filenames( $_[0] );
1776
1777 my @files = get_complete_path_globbing($filename_repo);
1778
1779 if (@files) {
1780
1781 # remove DASSCM_REPO path again
1782 print join(
1783 "\n",
1784 map( {
1785 s|^${DASSCM_REPO}|/|;
1786 $_
1787 } @files )
1788 );
1789 print "\n";
1790 }
1791
1792}
1793
1794#####################################################################
1795#
1796# main
1797#
1798
1799my $return_code = $RETURN_OK;
1800my $number_arguments = @ARGV;
1801
1802# global options
1803# stops at first non-option
1804Getopt::Long::Configure('require_order');
1805if ( not GetOptions( \%options, @OPTIONS_GLOBAL ) ) {
1806 usage();
1807 exit $RETURN_NOK;
1808}
1809
1810# set verbose to command line option
1811$verbose = $options{'verbose'};
1812
1813if ( $options{'help'} ) {
1814 help(@ARGV);
1815 exit;
1816}
1817
1818# get subcommand and remove it from @ARGV
1819if ( defined( $ARGV[0] ) ) {
1820 $command = get_command_uniform_name( $ARGV[0] );
1821 shift @ARGV;
1822}
1823
1824if ( not defined($command) ) {
1825 usage();
1826 exit $RETURN_NOK;
1827}
1828
1829$DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1830$DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
1831
1832$DASSCM_PLUGIN_RESULTS_PATH =
1833 $config->{'DASSCM_LOCAL_REPOSITORY_BASE'} . "/" . "plugin-results/";
1834
1835# get list of additional directories and files, seperated by blank (" ")
1836# these files are always stored in subversion
1837if ( $config->{'DASSCM_ADDITIONAL_FILES'} ) {
1838 @DASSCM_ADDITIONAL_FILES = split / /, $config->{'DASSCM_ADDITIONAL_FILES'};
1839} else {
1840 @DASSCM_ADDITIONAL_FILES = ( $DASSCM_PLUGIN_RESULTS_PATH );
1841}
1842
1843# TODO: check variables
1844$DASSCM_SVN_REPOSITORY =
1845 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1846
1847$DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1848$DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
1849
1850#
1851# if a user is given by dasscm configuration file, we use it.
1852# Otherwise we expect that read-only account is configured
1853# as local subversion configuration.
1854# If this is also not the case,
1855# user is required to type username and password.
1856# This will be stored as local subversion configuration thereafter.
1857#
1858if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
1859 $svnCheckoutCredentials =
1860 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
1861}
1862
1863
1864# check for command options
1865my @cmd_options = get_command_possible_options($command);
1866if (@cmd_options) {
1867
1868 # get command line options and store them in options hash
1869 my $result = GetOptions( \%options, @cmd_options );
1870
1871 # print options
1872 foreach my $option ( keys %options ) {
1873 print "${option}: $options{$option}\n";
1874 }
1875}
1876
1877#
1878# action accordinly to command are taken
1879#
1880&{ get_command_function($command) }(@ARGV);
1881
1882exit $return_code;
Note: See TracBrowser for help on using the repository browser.