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

Last change on this file since 922 was 922, checked in by joergs, on Jul 29, 2010 at 6:46:20 PM

bugfix: error when user have not enough permission to update plugin result files

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 47.4 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 922 2010-07-29 16:46:20Z 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 922 2010-07-29 16:46:20Z 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 #print "executing command: " . $command . "\n";
699
700 open( RESULT, $command . ' 2>&1 |' );
701 my @result = <RESULT>;
702 close(RESULT);
703 my $retcode = $? >> 8;
704
705 #print @result;
706 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
707
708 return ( $retcode, @result );
709}
710
711sub run_interactive
712{
713
714 if ($verbose) {
715 print "run_interactive:" . join( " ", @_ ) . "\n";
716 }
717
718 system(@_);
719 if ( $? == -1 ) {
720 printf "failed to execute: $!\n";
721 } elsif ( $? & 127 ) {
722 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
723 ( $? & 128 ) ? 'with' : 'without';
724 } elsif ( $? >> 8 != 0 ) {
725 printf "child exited with value %d\n", $? >> 8;
726 }
727 return ( $? >> 8 );
728}
729
730#
731# en- or disable echo mode.
732# used for reading passwords from STDIN
733#
734sub setEchoMode( $ )
735{
736 my $mode = shift;
737 if ($mode) {
738 run_command("stty echo");
739 } else {
740 run_command("stty -echo");
741 }
742}
743
744sub write_array_to_file( $@ )
745{
746 my $filename = shift;
747 my @array = @_;
748
749 if ( -e $filename && !-w $filename ) {
750 warning( "failed to write to $filename:", "permission denied" );
751 return;
752 }
753
754 if ( !-w dirname($filename) ) {
755 warning( "failed to write to $filename:", "directory does not exist" );
756 return;
757 }
758
759 # directory exists => write
760 if ( !open( OUTFILE, ">$filename" ) ) {
761 warning("failed to open $filename: $!");
762 return;
763 }
764
765 foreach my $line (@array) {
766 print OUTFILE "$line";
767 }
768 close(OUTFILE);
769
770 # if group dasscm exists,
771 # create plugin results with group membership dasscm
772 if( $DASSCM_GID ) {
773 chown( -1, $DASSCM_GID, $filename );
774 }
775
776 return 1;
777}
778
779sub perform_plugins()
780{
781 check_env();
782
783 my @plugin_results = ();
784
785 # get all defined plugins.
786 # Plugin definitions starting with DASSCM_PLUGIN_
787 my @plugins = grep( /^DASSCM_PLUGIN_CMD_/, keys( %{$config} ) );
788
789 for my $plugin (@plugins) {
790 my $plugin_name = substr( $plugin, length("DASSCM_PLUGIN_CMD_") );
791 my $plugin_test = $config->{ 'DASSCM_PLUGIN_TEST_' . $plugin_name };
792 ( my $rc_test, my @result_test ) = run_command($plugin_test);
793 if ($verbose) { print "Plugin $plugin_name: "; }
794 if ( $rc_test != 0 ) {
795 if ($verbose) { print "skipped\n"; }
796 } else {
797 if ($verbose) { print "$config->{$plugin}\n"; }
798 ( my $rc, my @result ) = run_command( $config->{$plugin} );
799 if ( $rc != 0 ) {
800 warning("failed to run plugin $plugin");
801 } else {
802 my $plugin_result_file =
803 $DASSCM_PLUGIN_RESULTS_PATH . "/" . $plugin_name;
804 write_array_to_file( $plugin_result_file, @result );
805 push @plugin_results, $plugin_result_file;
806 }
807 }
808 }
809 return @plugin_results;
810}
811
812sub svn_check_credentials( $$;$$ )
813{
814 my $username = shift;
815 my $password = shift;
816
817 # check silently are allow user interaction?
818 my $interactive = shift || 0;
819
820 # default: exit program, if repository is not accessable
821 # (do not exit for 'init')
822 my $fatalerror = shift || 1;
823
824 print "checking credentials ";
825
826 if ( !$username ) {
827 fatalerror("no username given");
828 }
829
830 if ( !$password ) {
831 fatalerror("no password given");
832 }
833
834 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
835
836 # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
837 # therefore switching to "svn status"
838 # ( my $rc_update, my @result ) =
839 # run_command(
840 # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
841 # );
842 #print @result;
843
844 my $rc_update;
845 if ($interactive) {
846 $rc_update = run_interactive(
847 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
848 );
849 } else {
850 ( $rc_update, my @result ) = run_command(
851 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
852 );
853
854 if ( $rc_update != 0 ) {
855 print "\n", @result;
856 if ($fatalerror) {
857 fatalerror();
858 }
859 return;
860 }
861 }
862
863 # return success
864 return $rc_update == 0;
865}
866
867sub svn_update( ;$ )
868{
869 my $update_path = shift || "";
870
871 # return value
872 my $update_ok = 1;
873
874 # use this flag to do only one update per run
875 if ( !$svnRepositoryIsUptodate ) {
876 ( my $rc_update, my @result ) = run_command(
877 "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'"
878 );
879 print @result;
880 if ( $rc_update != 0 ) {
881 error("failed to update local repository ($update_path)");
882 $update_ok = 0;
883 } elsif ( not $update_path ) {
884
885 # set this flag if a full update is done
886 $svnRepositoryIsUptodate = 1;
887 }
888 }
889 return $update_ok;
890}
891
892sub svn_ls( ;@ )
893{
894 (
895 my $basename,
896 my $dirname_prod,
897 my $dirname_repo,
898 my $filename_prod,
899 my $filename_repo
900 ) = get_filenames( $_[0] );
901
902 # svn ls -R is better, but much, much slower
903 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
904
905 my @files = ();
906 my @links = ();
907 my @dirs = ();
908 my @others = ();
909
910 find(
911 {
912 wanted => sub {
913 my $name = normalize_path($File::Find::name);
914 $name =~ s|^$dirname_repo||;
915
916 #print "($name)\n";# . $File::Find::dir . "\n";
917 if ( not $name ) {
918
919 # name string is empty (top directory).
920 # do nothing
921 } elsif ( $name =~ m/\.svn/ ) {
922
923 # skip svn meta data
924 } elsif ( -l $_ ) {
925
926 # soft link
927 # important: check for links first
928 # to exclude them from further checks
929 push( @links, $name );
930 } elsif ( -d $_ ) {
931
932 # directories
933 push( @dirs, $name );
934 } elsif ( -f $_ ) {
935
936 # regular file
937 push( @files, $name );
938 } else {
939 push( @others, $name );
940 }
941 }
942 },
943 ($filename_repo)
944 );
945
946 return ( sort( @dirs, @files ) );
947}
948
949sub svn_revert( ;$ )
950{
951 my $path = shift || $DASSCM_REPO;
952
953 ( my $rc_update, my @result ) = run_command("$SVN revert -R '$path'");
954
955 if ( $rc_update != 0 ) {
956 print "\n", @result;
957 error("failed to revert subversion repository changes");
958 }
959}
960
961sub svn_remove_unknown_files( ;$ )
962{
963 my $path = shift || $DASSCM_REPO;
964
965 ( my $rc_update, my @result ) = run_command("$SVN status '$path'");
966
967 if ( $rc_update != 0 ) {
968 print "\n", @result;
969 error("failed to receive subversion repository information");
970 } else {
971 foreach (@result) {
972 if (s/^\? +//) {
973 chomp;
974
975 # if file is unknown to subversion (line starts with "?")
976 # remove it
977 print "removing $_\n";
978
979 # unlink doesn't work recursive, there "rm -rf" is used
980 #unlink($_);
981 system("rm -rf $_");
982 }
983 }
984 }
985}
986
987sub getModifiedFiles( ;$ )
988{
989 (
990 my $basename,
991 my $dirname_prod,
992 my $dirname_repo,
993 my $filename_prod,
994 my $filename_repo
995 ) = get_filenames( $_[0] );
996
997 my @files = svn_ls($filename_prod);
998
999 # stores result from status (cvscheck)
1000 my %removedfiles = ();
1001 my %changedfiles = ();
1002 my %unknownfiles = ();
1003
1004 # create list of modified files
1005 if (@files) {
1006
1007 foreach my $file (@files) {
1008
1009 my $realfile = $dirname_prod . $file;
1010 my $cvsworkfile = $dirname_repo . $file;
1011
1012 if ( -d $realfile ) {
1013
1014 # directory
1015 if ( !-d "$cvsworkfile" ) {
1016
1017 # real is directory, repository is not. This is a problem
1018 $changedfiles{"$realfile"} = $cvsworkfile;
1019 }
1020 } elsif ( !-e $realfile ) {
1021 $removedfiles{"$realfile"} = $cvsworkfile;
1022 } elsif ( !-r $realfile ) {
1023
1024 # don't have permission to read the file,
1025 # can't check it
1026 $unknownfiles{"$realfile"} = $cvsworkfile;
1027 } else {
1028 ( -r "$cvsworkfile" )
1029 || fatalerror("failed to read $cvsworkfile");
1030 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
1031 $changedfiles{"$realfile"} = $cvsworkfile;
1032 }
1033 }
1034 }
1035 }
1036
1037 return ( \%changedfiles, \%removedfiles, \%unknownfiles );
1038}
1039
1040#
1041# from an array of files/dirs,
1042# generates list of files
1043# sorted by type
1044#
1045sub get_files( @ )
1046{
1047 my @files = ();
1048 my @links = ();
1049 my @dirs = ();
1050 my @others = ();
1051
1052 if (@_) {
1053 find(
1054 {
1055 wanted => sub {
1056 my $fullname = cwd() . "/" . $_;
1057 if ( -l $_ ) {
1058
1059 # soft link
1060 # important: check for links first
1061 # to exclude them from further checks
1062 push( @links, $fullname );
1063 } elsif ( -d $_ ) {
1064
1065 # directories
1066 push( @dirs, $fullname );
1067 } elsif ( -f $_ ) {
1068
1069 # regular file
1070 push( @files, $fullname );
1071 } else {
1072 push( @others, $fullname );
1073 }
1074 }
1075 },
1076 @_
1077 );
1078 }
1079
1080 # don't rely on others.
1081 # If more specific file types are needed,
1082 # they will be added
1083 return {
1084 files => \@files,
1085 links => \@links,
1086 dirs => \@dirs,
1087 others => \@others
1088 };
1089}
1090
1091sub print_files_hash( $ )
1092{
1093 my $href_files = shift;
1094
1095 my @files = @{ $href_files->{files} };
1096 my @links = @{ $href_files->{links} };
1097
1098 if (@files) {
1099 my $number = $#files + 1;
1100 print "files to check-in ($number): \n";
1101 print join( "\n", @files );
1102 print "\n";
1103 }
1104
1105 # TODO: check in links and also link target? At least warn about link target
1106 if (@links) {
1107 my $number = $#links + 1;
1108 print "\n";
1109 print "ignoring links ($number):\n";
1110 print join( "\n", @links );
1111 print "\n";
1112 }
1113
1114}
1115
1116#
1117# use globbing to get lsit of files
1118# that matches the given prefix
1119# used for bash completion
1120#
1121sub get_complete_path_globbing( $ )
1122{
1123 my $path = shift;
1124
1125 # add globbing
1126 $path .= "*";
1127
1128 # get files
1129 my @files = glob($path);
1130
1131 if ( $#files == 0 ) {
1132
1133 # if only one result is available
1134 # and this result is a directory,
1135 # add another result entry
1136 # (directory with and withour trainling /),
1137 # otherwise complete will stop here and continue with the next parameter
1138 my $path = normalize_path( $files[0] );
1139 if ( -d $path ) {
1140 @files = ( substr( $path, 0, -1 ), $path );
1141 }
1142 } else {
1143
1144 # add "/" to all directories
1145 @files = map( { normalize_path($_) } @files );
1146 }
1147
1148 return @files;
1149}
1150
1151#####################################################################
1152#
1153# functions
1154sub help(;@)
1155{
1156 if ( not @_ ) {
1157 usage();
1158 } else {
1159 print "help for ", join( " ", @_ ), ": ...\n";
1160 usage();
1161 }
1162}
1163
1164sub login(@)
1165{
1166 check_parameter( @_, 1 );
1167 check_env();
1168
1169 my $input_username = $_[0];
1170
1171 if ( not $input_username ) {
1172 my $output_username = "";
1173 if ($DASSCM_USERNAME) {
1174 $output_username = " ($DASSCM_USERNAME)";
1175 }
1176
1177 print "Enter DASSCM user name", $output_username, ": ";
1178 $input_username = <STDIN>;
1179 chomp($input_username);
1180
1181 $input_username = $input_username || $DASSCM_USERNAME;
1182 }
1183
1184 # hidden password input
1185 print "Enter password for $input_username: ";
1186 setEchoMode(0);
1187 my $input_password = <STDIN>;
1188 setEchoMode(1);
1189 chomp($input_password);
1190 print "\n";
1191
1192 # checking checkout username/password
1193 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
1194 $DASSCM_CHECKOUT_PASSWORD );
1195 print "checkout access okay\n";
1196
1197 svn_check_credentials( $input_username, $input_password );
1198
1199 #
1200 # set environment variables
1201 #
1202 $ENV{'DASSCM_USERNAME'} = "$input_username";
1203 $ENV{'DASSCM_PASSWORD'} = "$input_password";
1204
1205 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
1206 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
1207 "DASSCM_REPO: $DASSCM_REPO\n",
1208 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
1209
1210 status();
1211
1212 print "\n[dasscm shell]\n\n";
1213 my $shell = $SHELL || "bash";
1214 exec($shell) or die "failed to start new shell";
1215}
1216
1217#
1218# initialize local checkout directory (initial checkout)
1219#
1220sub init(@)
1221{
1222 check_parameter( @_, 1 );
1223 check_env();
1224
1225 # don't do repository creation (svn mkdir) here,
1226 # because then their must be a lot of prior checks
1227
1228 # update complete repository
1229 my $retcode = run_interactive(
1230 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY"
1231 );
1232}
1233
1234sub ls(@)
1235{
1236 check_parameter( @_, 1 );
1237 check_env();
1238
1239 my @files = svn_ls(@_);
1240
1241 if (@files) {
1242 print join( "\n", @files );
1243 print "\n";
1244 }
1245}
1246
1247sub update(@)
1248{
1249 check_parameter( @_, 1 );
1250 check_env();
1251
1252 #
1253 # update local repository
1254 #
1255 svn_update();
1256}
1257
1258#
1259# helper function for "add" command
1260#
1261sub add_helper(@)
1262{
1263 (
1264 my $basename,
1265 my $dirname_prod,
1266 my $dirname_repo,
1267 my $filename_prod,
1268 my $filename_repo
1269 ) = get_filenames( $_[0] );
1270
1271 mkpath($dirname_repo);
1272 copy_file_to_repository($filename_prod);
1273
1274 # already checked in?
1275 chdir $DASSCM_REPO;
1276
1277 # also add the path to filename.
1278 for my $dir ( split( '/', $dirname_prod ) ) {
1279 if ($dir) {
1280 my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
1281 if ( $rc > 0 ) {
1282 print join( "\n", @out );
1283 }
1284 chdir $dir;
1285 }
1286 }
1287 my ( $rc, @out ) = run_command("$SVN add '$basename'");
1288 if ( $rc > 0 ) {
1289 print join( "\n", @out );
1290 }
1291 chdir $StartDirectory;
1292
1293}
1294
1295sub add_helper_multi(@)
1296{
1297
1298 # get all regular files and links
1299 my $href_files = get_files(@_);
1300
1301 #print Dumper( $href_files );
1302
1303 my @files = @{ $href_files->{files} };
1304 my @links = @{ $href_files->{links} };
1305
1306 # copy files one by one to local repository
1307 for my $file (@files) {
1308
1309 # add file
1310 add_helper($file);
1311 }
1312
1313 return $href_files;
1314}
1315
1316#
1317# adding new files (or directories)
1318#
1319sub add(@)
1320{
1321 check_parameter( @_, 1 );
1322 check_env();
1323
1324 #
1325 # update local repository
1326 #
1327 svn_update();
1328
1329 # add files to repository, print information about added files
1330 print_files_hash( add_helper_multi(@_) );
1331
1332 # perform plugins and add additional files, like plugin results
1333 perform_plugins();
1334 add_helper_multi(@DASSCM_ADDITIONAL_FILES);
1335
1336 if ( $options{'message'} ) {
1337 $svnOptions .= " --message \"$options{'message'}\" ";
1338 }
1339
1340 # commit calls $EDITOR.
1341 # use "interactive" here, to display output
1342 my $retcode = run_interactive(
1343 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1344 );
1345
1346 # svn commit does not deliever an error return code, if commit is canceld,
1347 # so a revert is performed in any case
1348 svn_revert();
1349}
1350
1351#
1352# checks in all modified files
1353#
1354sub commit(@)
1355{
1356 check_parameter( @_, 1 );
1357 check_env();
1358
1359 (
1360 my $basename,
1361 my $dirname_prod,
1362 my $dirname_repo,
1363 my $filename_prod,
1364 my $filename_repo
1365 ) = get_filenames( $_[0] );
1366
1367 #
1368 # update local repository
1369 #
1370 svn_update();
1371
1372 ( my $refChangedFiles, my $refRemovedFiles ) =
1373 getModifiedFiles($filename_prod);
1374 my %changedfiles = %{$refChangedFiles};
1375 my %removedfiles = %{$refRemovedFiles};
1376
1377 if (%removedfiles) {
1378 my $removedFilesString =
1379 '"' . join( '" "', values(%removedfiles) ) . '"';
1380 my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
1381 if ( $rc > 0 ) {
1382 print join( "\n", @out );
1383 }
1384 }
1385
1386 # copy files one by one to local repository
1387 for my $file ( keys(%changedfiles) ) {
1388 copy_file_to_repository($file);
1389 }
1390
1391 perform_plugins();
1392 add_helper_multi(@DASSCM_ADDITIONAL_FILES);
1393
1394 if ( $options{'message'} ) {
1395 $svnOptions .= " --message \"$options{'message'}\" ";
1396 }
1397
1398 # commit calls $EDITOR.
1399 # use "interactive" here, to display output
1400 my $retcode = run_interactive(
1401 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1402 );
1403
1404 # svn commit does not deliever an error return code, if commit is canceld,
1405 # so a revert is performed in any case
1406 svn_revert();
1407}
1408
1409#
1410# revert: copies files back from repository to system
1411#
1412sub revert(@)
1413{
1414 check_parameter( @_, 1 );
1415 check_env();
1416
1417 (
1418 my $basename,
1419 my $dirname_prod,
1420 my $dirname_repo,
1421 my $filename_prod,
1422 my $filename_repo
1423 ) = get_filenames( $_[0] );
1424
1425 # return code for the shell
1426 # default: error
1427 my $return_code = $RETURN_OK;
1428
1429 # cleanup repository
1430 cleanup();
1431
1432 #svn_update();
1433
1434 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1435 getModifiedFiles($filename_prod);
1436 my %changedfiles = %{$refChangedFiles};
1437 my %removedfiles = %{$refRemovedFiles};
1438 my %unknownfiles = %{$refUnknownFiles};
1439
1440 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1441
1442 if (%removedfiles) {
1443 print "DELETED files and directories. Recreated from repository:\n";
1444 my @removedPaths =
1445 ( sort { length $a > length $b } keys %removedfiles );
1446 print join( "\n", @removedPaths ) . "\n\n";
1447
1448 # copy files one by one from local repository to system
1449 # and also create directories
1450 # paths are sorted, so that directories are created first
1451 for my $real_path (@removedPaths) {
1452 if ( -d $removedfiles{"$real_path"} ) {
1453 mkpath("$real_path");
1454 } else {
1455 copy_file_from_repository_to_system($real_path);
1456 }
1457 }
1458 }
1459
1460 if (%changedfiles) {
1461 print "MODIFIED files. Copied from repository to the system:\n";
1462 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1463
1464 # copy files one by one from local repository to system
1465 for my $real_file ( keys(%changedfiles) ) {
1466 copy_file_from_repository_to_system($real_file);
1467 }
1468
1469 }
1470
1471 if (%unknownfiles) {
1472 print "UNKNOWN: insufficient permission to check files:\n";
1473 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1474
1475 $return_code = $RETURN_NOK;
1476 }
1477
1478 } else {
1479 print "no modified files found in $dirname_repo\n";
1480 }
1481
1482 return $return_code;
1483}
1484
1485sub blame(@)
1486{
1487 check_parameter( @_, 1 );
1488 check_env();
1489
1490 (
1491 my $basename,
1492 my $dirname_prod,
1493 my $dirname_repo,
1494 my $filename_prod,
1495 my $filename_repo
1496 ) = get_filenames( $_[0] );
1497
1498 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
1499}
1500
1501sub diff(@)
1502{
1503 check_parameter( @_, 1 );
1504 check_env();
1505
1506 (
1507 my $basename,
1508 my $dirname_prod,
1509 my $dirname_repo,
1510 my $filename_prod,
1511 my $filename_repo
1512 ) = get_filenames( $_[0] );
1513
1514 #print "$basename,$dirname_prod,$dirname_repo\n";
1515
1516 svn_update();
1517
1518 ( my $rc_diff, my @diff_result ) =
1519 run_command( $diff . " $filename_repo $filename_prod" );
1520
1521 print @diff_result;
1522}
1523
1524sub status(@)
1525{
1526 check_parameter( @_, 1 );
1527 check_env();
1528
1529 (
1530 my $basename,
1531 my $dirname_prod,
1532 my $dirname_repo,
1533 my $filename_prod,
1534 my $filename_repo
1535 ) = get_filenames( $_[0] || "/" );
1536
1537 # return code for the shell
1538 # default: error
1539 my $return_code = $RETURN_NOK;
1540
1541 #
1542 # update local repository
1543 #
1544 #svn_update( $filename_prod );
1545
1546 # perform plugins (required to see changes in plugin results)
1547 perform_plugins();
1548
1549 # get modified files
1550 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1551 getModifiedFiles($dirname_prod);
1552 my %changedfiles = %{$refChangedFiles};
1553 my %removedfiles = %{$refRemovedFiles};
1554 my %unknownfiles = %{$refUnknownFiles};
1555
1556 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1557
1558 if (%removedfiles) {
1559 print "DELETED: files found in repository, but not in system:\n";
1560 print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
1561 }
1562
1563 if (%changedfiles) {
1564 print "MODIFIED: files differs between repository and system:\n";
1565 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1566 }
1567
1568 if (%unknownfiles) {
1569 print "UNKNOWN: insufficient permission to check files:\n";
1570 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1571 }
1572
1573 } else {
1574 print "no modified files found in $dirname_repo\n";
1575 $return_code = $RETURN_OK;
1576 }
1577
1578 return $return_code;
1579}
1580
1581#
1582# return short status in Nagios plugin conform way
1583#
1584sub check()
1585{
1586 check_env();
1587
1588 # return code for the shell
1589 my $return_code = $RETURN_OK;
1590 my $return_string = "OK: no modified files";
1591
1592 # perform plugins (required to see changes in plugin results)
1593 perform_plugins();
1594
1595 # get modified files
1596 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1597 getModifiedFiles("/");
1598 my %changedfiles = %{$refChangedFiles};
1599 my %removedfiles = %{$refRemovedFiles};
1600 my %unknownfiles = %{$refUnknownFiles};
1601
1602 if ( %removedfiles or %changedfiles ) {
1603 $return_string = "Warning: ";
1604 if (%changedfiles) {
1605 $return_string .=
1606 "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
1607 }
1608 if (%removedfiles) {
1609 $return_string .=
1610 "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
1611 }
1612 if (%unknownfiles) {
1613 $return_string .=
1614 "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
1615 }
1616 $return_code = $RETURN_WARN;
1617 }
1618
1619 # addition nagios Service Status
1620 #Critical
1621 #Unknown
1622
1623 print "$return_string\n";
1624 return $return_code;
1625}
1626
1627sub permissions()
1628{
1629 check_env();
1630
1631 my $return_code = $RETURN_OK;
1632
1633 #
1634 # update local repository
1635 #
1636 #svn_update();
1637
1638 my $dir = $DASSCM_REPO;
1639 my @files = svn_ls("/");
1640
1641 if (@files) {
1642
1643 print "#\n";
1644 print "# created by dasscm permissions\n";
1645 print "# It is intended to be used for restoring permissions\n";
1646 print "#\n";
1647
1648 # generate and print permissions
1649 foreach my $line ( generatePermissionList(@files) ) {
1650 print "$line\n";
1651 }
1652
1653 }
1654
1655 return $return_code;
1656}
1657
1658#
1659# remove all uncommited changes in the repository
1660#
1661sub cleanup()
1662{
1663 check_env();
1664
1665 svn_revert($DASSCM_REPO);
1666 svn_remove_unknown_files($DASSCM_REPO);
1667}
1668
1669#
1670# used for bash completion
1671# prints the next possible command line parameters
1672#
1673sub complete(@)
1674{
1675 my @input = @_;
1676 my %options_complete = ();
1677
1678 # check and remove global options. if options are wrong, nothing to do
1679 @ARGV = @input;
1680 if ( GetOptions( \%options_complete, @OPTIONS_GLOBAL ) ) {
1681 my $number_arguments = @input;
1682 if ( $number_arguments <= 1 ) {
1683
1684 # complete dasscm commands
1685 my $input = $input[0] || "";
1686 map { m/^$input/ && print $_, "\n" } ( keys %COMMANDS );
1687 } else {
1688
1689 # complete dasscm parameter
1690 my $command = get_command_uniform_name( $input[0] );
1691 if ($command) {
1692
1693 # remove command
1694 shift @input;
1695
1696 # check and remove options
1697 my @options = get_command_possible_options($command);
1698 @ARGV = @input;
1699 if ( ( not @options )
1700 || ( GetOptions( \%options_complete, @options ) ) )
1701 {
1702
1703 my @params = get_command_possible_params($command);
1704 if ($verbose) { print "params: ", Dumper(@params); }
1705
1706 my $number_arguments = @input;
1707
1708 #print "input: ", join( ",", @input ), " (", $number_arguments, ")\n";
1709
1710 if ( $number_arguments > 0 ) {
1711 my $parameter_number = $number_arguments - 1;
1712 if ( defined( $params[$parameter_number] )
1713 && $params[$parameter_number] )
1714 {
1715 my $param = $params[$parameter_number];
1716 if ($verbose) {
1717 print "param used: ", $param, "\n";
1718 }
1719 if ( $param eq "PATH_PROD" ) {
1720 complete_path(
1721 $input[ $number_arguments - 1 ] );
1722 } elsif ( $param eq "PATH_REPO" ) {
1723 complete_repopath(
1724 $input[ $number_arguments - 1 ] );
1725 }
1726 }
1727 }
1728 }
1729 }
1730 }
1731 }
1732}
1733
1734sub complete_path(@)
1735{
1736 check_parameter( @_, 1 );
1737 check_env();
1738
1739 (
1740 my $basename,
1741 my $dirname_prod,
1742 my $dirname_repo,
1743 my $filename_prod,
1744 my $filename_repo
1745 ) = get_filenames( $_[0] );
1746
1747 my @files = get_complete_path_globbing($filename_prod);
1748
1749 if (@files) {
1750 print join( "\n", @files );
1751 print "\n";
1752 }
1753}
1754
1755sub complete_repopath(@)
1756{
1757 check_parameter( @_, 1 );
1758 check_env();
1759
1760 (
1761 my $basename,
1762 my $dirname_prod,
1763 my $dirname_repo,
1764 my $filename_prod,
1765 my $filename_repo
1766 ) = get_filenames( $_[0] );
1767
1768 my @files = get_complete_path_globbing($filename_repo);
1769
1770 if (@files) {
1771
1772 # remove DASSCM_REPO path again
1773 print join(
1774 "\n",
1775 map( {
1776 s|^${DASSCM_REPO}|/|;
1777 $_
1778 } @files )
1779 );
1780 print "\n";
1781 }
1782
1783}
1784
1785#####################################################################
1786#
1787# main
1788#
1789
1790my $return_code = $RETURN_OK;
1791my $number_arguments = @ARGV;
1792
1793# global options
1794# stops at first non-option
1795Getopt::Long::Configure('require_order');
1796if ( not GetOptions( \%options, @OPTIONS_GLOBAL ) ) {
1797 usage();
1798 exit $RETURN_NOK;
1799}
1800
1801# set verbose to command line option
1802$verbose = $options{'verbose'};
1803
1804if ( $options{'help'} ) {
1805 help(@ARGV);
1806 exit;
1807}
1808
1809# get subcommand and remove it from @ARGV
1810if ( defined( $ARGV[0] ) ) {
1811 $command = get_command_uniform_name( $ARGV[0] );
1812 shift @ARGV;
1813}
1814
1815if ( not defined($command) ) {
1816 usage();
1817 exit $RETURN_NOK;
1818}
1819
1820$DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1821$DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
1822
1823$DASSCM_PLUGIN_RESULTS_PATH =
1824 $config->{'DASSCM_LOCAL_REPOSITORY_BASE'} . "/" . "plugin-results/";
1825
1826# get list of additional directories and files, seperated by blank (" ")
1827# these files are always stored in subversion
1828if ( $config->{'DASSCM_ADDITIONAL_FILES'} ) {
1829 @DASSCM_ADDITIONAL_FILES = split / /, $config->{'DASSCM_ADDITIONAL_FILES'};
1830} else {
1831 @DASSCM_ADDITIONAL_FILES = ( $DASSCM_PLUGIN_RESULTS_PATH );
1832}
1833
1834# TODO: check variables
1835$DASSCM_SVN_REPOSITORY =
1836 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1837
1838$DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1839$DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
1840
1841#
1842# if a user is given by dasscm configuration file, we use it.
1843# Otherwise we expect that read-only account is configured
1844# as local subversion configuration.
1845# If this is also not the case,
1846# user is required to type username and password.
1847# This will be stored as local subversion configuration thereafter.
1848#
1849if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
1850 $svnCheckoutCredentials =
1851 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
1852}
1853
1854
1855# check for command options
1856my @cmd_options = get_command_possible_options($command);
1857if (@cmd_options) {
1858
1859 # get command line options and store them in options hash
1860 my $result = GetOptions( \%options, @cmd_options );
1861
1862 # print options
1863 foreach my $option ( keys %options ) {
1864 print "${option}: $options{$option}\n";
1865 }
1866}
1867
1868#
1869# action accordinly to command are taken
1870#
1871&{ get_command_function($command) }(@ARGV);
1872
1873exit $return_code;
Note: See TracBrowser for help on using the repository browser.