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

Last change on this file since 929 was 929, checked in by joergs, on Sep 23, 2010 at 2:45:44 PM

add missing return codes

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 48.2 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 929 2010-09-23 12:45:44Z 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 929 2010-09-23 12:45:44Z 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 return $RETURN_OK;
1173}
1174
1175sub login(@)
1176{
1177 check_parameter( @_, 1 );
1178 check_env();
1179
1180 my $input_username = $_[0];
1181
1182 if ( not $input_username ) {
1183 my $output_username = "";
1184 if ($DASSCM_USERNAME) {
1185 $output_username = " ($DASSCM_USERNAME)";
1186 }
1187
1188 print "Enter DASSCM user name", $output_username, ": ";
1189 $input_username = <STDIN>;
1190 chomp($input_username);
1191
1192 $input_username = $input_username || $DASSCM_USERNAME;
1193 }
1194
1195 # hidden password input
1196 print "Enter password for $input_username: ";
1197 setEchoMode(0);
1198 my $input_password = <STDIN>;
1199 setEchoMode(1);
1200 chomp($input_password);
1201 print "\n";
1202
1203 # checking checkout username/password
1204 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
1205 $DASSCM_CHECKOUT_PASSWORD );
1206 print "checkout access okay\n";
1207
1208 svn_check_credentials( $input_username, $input_password );
1209
1210 #
1211 # set environment variables
1212 #
1213 $ENV{'DASSCM_USERNAME'} = "$input_username";
1214 $ENV{'DASSCM_PASSWORD'} = "$input_password";
1215
1216 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
1217 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
1218 "DASSCM_REPO: $DASSCM_REPO\n",
1219 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
1220
1221 status();
1222
1223 print "\n[dasscm shell]\n\n";
1224 my $shell = $SHELL || "bash";
1225 exec($shell) or die "failed to start new shell";
1226}
1227
1228#
1229# initialize local checkout directory (initial checkout)
1230#
1231sub init(@)
1232{
1233 check_parameter( @_, 1 );
1234 check_env();
1235
1236 # don't do repository creation (svn mkdir) here,
1237 # because then their must be a lot of prior checks
1238
1239 # update complete repository
1240 my $retcode = run_interactive(
1241 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY"
1242 );
1243
1244 return $retcode;
1245}
1246
1247sub ls(@)
1248{
1249 my $return_code = $RETURN_OK;
1250 check_parameter( @_, 1 );
1251 check_env();
1252
1253 my @files = svn_ls(@_);
1254
1255 if (@files) {
1256 print join( "\n", @files );
1257 print "\n";
1258 }
1259 return $return_code;
1260}
1261
1262sub update(@)
1263{
1264 my $return_code = $RETURN_OK;
1265 check_parameter( @_, 1 );
1266 check_env();
1267
1268 #
1269 # update local repository
1270 #
1271 if( ! svn_update() ) {
1272 $return_code = $RETURN_NOK;
1273 }
1274 return $return_code;
1275}
1276
1277#
1278# helper function for "add" command
1279#
1280sub add_helper(@)
1281{
1282 (
1283 my $basename,
1284 my $dirname_prod,
1285 my $dirname_repo,
1286 my $filename_prod,
1287 my $filename_repo
1288 ) = get_filenames( $_[0] );
1289
1290 mkpath($dirname_repo);
1291 copy_file_to_repository($filename_prod);
1292
1293 # already checked in?
1294 chdir $DASSCM_REPO;
1295
1296 # also add the path to filename.
1297 for my $dir ( split( '/', $dirname_prod ) ) {
1298 if ($dir) {
1299 my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
1300 if ( $rc > 0 ) {
1301 print join( "\n", @out );
1302 }
1303 chdir $dir;
1304 }
1305 }
1306 my ( $rc, @out ) = run_command("$SVN add '$basename'");
1307 if ( $rc > 0 ) {
1308 print join( "\n", @out );
1309 }
1310 chdir $StartDirectory;
1311
1312}
1313
1314sub add_helper_multi(@)
1315{
1316
1317 # get all regular files and links
1318 my $href_files = get_files(@_);
1319
1320 #print Dumper( $href_files );
1321
1322 my @files = @{ $href_files->{files} };
1323 my @links = @{ $href_files->{links} };
1324
1325 # copy files one by one to local repository
1326 for my $file (@files) {
1327
1328 # add file
1329 add_helper($file);
1330 }
1331
1332 return $href_files;
1333}
1334
1335#
1336# adding new files (or directories)
1337#
1338sub add(@)
1339{
1340 check_parameter( @_, 1 );
1341 check_env();
1342
1343 #
1344 # update local repository
1345 #
1346 svn_update();
1347
1348 # add files to repository, print information about added files
1349 print_files_hash( add_helper_multi(@_) );
1350
1351 # perform plugins and add additional files, like plugin results
1352 perform_plugins();
1353 add_helper_multi(@DASSCM_ADDITIONAL_FILES);
1354
1355 if ( $options{'message'} ) {
1356 $svnOptions .= " --message \"$options{'message'}\" ";
1357 }
1358
1359 # commit calls $EDITOR.
1360 # use "interactive" here, to display output
1361 my $retcode = run_interactive(
1362 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1363 );
1364
1365 # svn commit does not deliever an error return code, if commit is canceld,
1366 # so a revert is performed in any case
1367 svn_revert();
1368 return $retcode;
1369}
1370
1371#
1372# checks in all modified files
1373#
1374sub commit(@)
1375{
1376 check_parameter( @_, 1 );
1377 check_env();
1378
1379 (
1380 my $basename,
1381 my $dirname_prod,
1382 my $dirname_repo,
1383 my $filename_prod,
1384 my $filename_repo
1385 ) = get_filenames( $_[0] );
1386
1387 #
1388 # update local repository
1389 #
1390 svn_update();
1391
1392 ( my $refChangedFiles, my $refRemovedFiles ) =
1393 getModifiedFiles($filename_prod);
1394 my %changedfiles = %{$refChangedFiles};
1395 my %removedfiles = %{$refRemovedFiles};
1396
1397 if (%removedfiles) {
1398 my $removedFilesString =
1399 '"' . join( '" "', values(%removedfiles) ) . '"';
1400 my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
1401 if ( $rc > 0 ) {
1402 print join( "\n", @out );
1403 }
1404 }
1405
1406 # copy files one by one to local repository
1407 for my $file ( keys(%changedfiles) ) {
1408 copy_file_to_repository($file);
1409 }
1410
1411 perform_plugins();
1412 add_helper_multi(@DASSCM_ADDITIONAL_FILES);
1413
1414 if ( $options{'message'} ) {
1415 $svnOptions .= " --message \"$options{'message'}\" ";
1416 }
1417
1418 # commit calls $EDITOR.
1419 # use "interactive" here, to display output
1420 my $retcode = run_interactive(
1421 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1422 );
1423
1424 # svn commit does not deliever an error return code, if commit is canceld,
1425 # so a revert is performed in any case
1426 svn_revert();
1427 return $retcode;
1428}
1429
1430#
1431# revert: copies files back from repository to system
1432#
1433sub revert(@)
1434{
1435 check_parameter( @_, 1 );
1436 check_env();
1437
1438 (
1439 my $basename,
1440 my $dirname_prod,
1441 my $dirname_repo,
1442 my $filename_prod,
1443 my $filename_repo
1444 ) = get_filenames( $_[0] );
1445
1446 # return code for the shell
1447 # default: error
1448 my $return_code = $RETURN_OK;
1449
1450 # cleanup repository
1451 cleanup();
1452
1453 #svn_update();
1454
1455 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1456 getModifiedFiles($filename_prod);
1457 my %changedfiles = %{$refChangedFiles};
1458 my %removedfiles = %{$refRemovedFiles};
1459 my %unknownfiles = %{$refUnknownFiles};
1460
1461 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1462
1463 if (%removedfiles) {
1464 print "DELETED files and directories. Recreated from repository:\n";
1465 my @removedPaths =
1466 ( sort { length $a > length $b } keys %removedfiles );
1467 print join( "\n", @removedPaths ) . "\n\n";
1468
1469 # copy files one by one from local repository to system
1470 # and also create directories
1471 # paths are sorted, so that directories are created first
1472 for my $real_path (@removedPaths) {
1473 if ( -d $removedfiles{"$real_path"} ) {
1474 mkpath("$real_path");
1475 } else {
1476 copy_file_from_repository_to_system($real_path);
1477 }
1478 }
1479 }
1480
1481 if (%changedfiles) {
1482 print "MODIFIED files. Copied from repository to the system:\n";
1483 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1484
1485 # copy files one by one from local repository to system
1486 for my $real_file ( keys(%changedfiles) ) {
1487 copy_file_from_repository_to_system($real_file);
1488 }
1489
1490 }
1491
1492 if (%unknownfiles) {
1493 print "UNKNOWN: insufficient permission to check files:\n";
1494 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1495
1496 $return_code = $RETURN_NOK;
1497 }
1498
1499 } else {
1500 print "no modified files found in $dirname_repo\n";
1501 }
1502
1503 return $return_code;
1504}
1505
1506sub blame(@)
1507{
1508 check_parameter( @_, 1 );
1509 check_env();
1510
1511 (
1512 my $basename,
1513 my $dirname_prod,
1514 my $dirname_repo,
1515 my $filename_prod,
1516 my $filename_repo
1517 ) = get_filenames( $_[0] );
1518
1519 my $retcode = run_interactive("$SVN blame --non-interactive $svnCheckoutCredentials $svnOptions $filename_repo");
1520 return $retcode;
1521}
1522
1523sub diff(@)
1524{
1525 check_parameter( @_, 1 );
1526 check_env();
1527
1528 (
1529 my $basename,
1530 my $dirname_prod,
1531 my $dirname_repo,
1532 my $filename_prod,
1533 my $filename_repo
1534 ) = get_filenames( $_[0] );
1535
1536 #print "$basename,$dirname_prod,$dirname_repo\n";
1537
1538 svn_update();
1539
1540 ( my $rc_diff, my @diff_result ) =
1541 run_command( $diff . " $filename_repo $filename_prod" );
1542
1543 print @diff_result;
1544 return $rc_diff;
1545}
1546
1547sub status(@)
1548{
1549 check_parameter( @_, 1 );
1550 check_env();
1551
1552 (
1553 my $basename,
1554 my $dirname_prod,
1555 my $dirname_repo,
1556 my $filename_prod,
1557 my $filename_repo
1558 ) = get_filenames( $_[0] || "/" );
1559
1560 # return code for the shell
1561 # default: error
1562 my $return_code = $RETURN_NOK;
1563
1564 #
1565 # update local repository
1566 #
1567 #svn_update( $filename_prod );
1568
1569 # perform plugins (required to see changes in plugin results)
1570 perform_plugins();
1571
1572 # get modified files
1573 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1574 getModifiedFiles($dirname_prod);
1575 my %changedfiles = %{$refChangedFiles};
1576 my %removedfiles = %{$refRemovedFiles};
1577 my %unknownfiles = %{$refUnknownFiles};
1578
1579 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1580
1581 if (%removedfiles) {
1582 print "DELETED: files found in repository, but not in system:\n";
1583 print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
1584 }
1585
1586 if (%changedfiles) {
1587 print "MODIFIED: files differs between repository and system:\n";
1588 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1589 }
1590
1591 if (%unknownfiles) {
1592 print "UNKNOWN: insufficient permission to check files:\n";
1593 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1594 }
1595
1596 } else {
1597 print "no modified files found in $dirname_repo\n";
1598 $return_code = $RETURN_OK;
1599 }
1600
1601 return $return_code;
1602}
1603
1604#
1605# return short status in Nagios plugin conform way
1606#
1607sub check()
1608{
1609 check_env();
1610
1611 # return code for the shell
1612 my $return_code = $RETURN_OK;
1613 my $return_string = "OK: no modified files";
1614
1615 # perform plugins (required to see changes in plugin results)
1616 perform_plugins();
1617
1618 # get modified files
1619 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1620 getModifiedFiles("/");
1621 my %changedfiles = %{$refChangedFiles};
1622 my %removedfiles = %{$refRemovedFiles};
1623 my %unknownfiles = %{$refUnknownFiles};
1624
1625 if ( %removedfiles or %changedfiles ) {
1626 $return_string = "Warning: ";
1627 if (%changedfiles) {
1628 $return_string .=
1629 "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
1630 }
1631 if (%removedfiles) {
1632 $return_string .=
1633 "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
1634 }
1635 if (%unknownfiles) {
1636 $return_string .=
1637 "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
1638 }
1639 $return_code = $RETURN_WARN;
1640 }
1641
1642 # addition nagios Service Status
1643 #Critical
1644 #Unknown
1645
1646 print "$return_string\n";
1647 return $return_code;
1648}
1649
1650sub permissions()
1651{
1652 check_env();
1653
1654 my $return_code = $RETURN_OK;
1655
1656 #
1657 # update local repository
1658 #
1659 #svn_update();
1660
1661 my $dir = $DASSCM_REPO;
1662 my @files = svn_ls("/");
1663
1664 if (@files) {
1665
1666 print "#\n";
1667 print "# created by dasscm permissions\n";
1668 print "# It is intended to be used for restoring permissions\n";
1669 print "#\n";
1670
1671 # generate and print permissions
1672 foreach my $line ( generatePermissionList(@files) ) {
1673 print "$line\n";
1674 }
1675
1676 }
1677
1678 return $return_code;
1679}
1680
1681#
1682# remove all uncommited changes in the repository
1683#
1684sub cleanup()
1685{
1686 my $return_code = $RETURN_OK;
1687
1688 check_env();
1689
1690 svn_revert($DASSCM_REPO);
1691 svn_remove_unknown_files($DASSCM_REPO);
1692
1693 return $return_code;
1694}
1695
1696#
1697# used for bash completion
1698# prints the next possible command line parameters
1699#
1700sub complete(@)
1701{
1702 my @input = @_;
1703 my %options_complete = ();
1704
1705 my $return_code = $RETURN_OK;
1706
1707 # check and remove global options. if options are wrong, nothing to do
1708 @ARGV = @input;
1709 if ( GetOptions( \%options_complete, @OPTIONS_GLOBAL ) ) {
1710 my $number_arguments = @input;
1711 if ( $number_arguments <= 1 ) {
1712
1713 # complete dasscm commands
1714 my $input = $input[0] || "";
1715 map { m/^$input/ && print $_, "\n" } ( keys %COMMANDS );
1716 } else {
1717
1718 # complete dasscm parameter
1719 my $command = get_command_uniform_name( $input[0] );
1720 if ($command) {
1721
1722 # remove command
1723 shift @input;
1724
1725 # check and remove options
1726 my @options = get_command_possible_options($command);
1727 @ARGV = @input;
1728 if ( ( not @options )
1729 || ( GetOptions( \%options_complete, @options ) ) )
1730 {
1731
1732 my @params = get_command_possible_params($command);
1733 if ($verbose) { print "params: ", Dumper(@params); }
1734
1735 my $number_arguments = @input;
1736
1737 #print "input: ", join( ",", @input ), " (", $number_arguments, ")\n";
1738
1739 if ( $number_arguments > 0 ) {
1740 my $parameter_number = $number_arguments - 1;
1741 if ( defined( $params[$parameter_number] )
1742 && $params[$parameter_number] )
1743 {
1744 my $param = $params[$parameter_number];
1745 if ($verbose) {
1746 print "param used: ", $param, "\n";
1747 }
1748 if ( $param eq "PATH_PROD" ) {
1749 complete_path(
1750 $input[ $number_arguments - 1 ] );
1751 } elsif ( $param eq "PATH_REPO" ) {
1752 complete_repopath(
1753 $input[ $number_arguments - 1 ] );
1754 }
1755 }
1756 }
1757 }
1758 }
1759 }
1760 }
1761 return $return_code;
1762}
1763
1764sub complete_path(@)
1765{
1766 my $return_code = $RETURN_OK;
1767 check_parameter( @_, 1 );
1768 check_env();
1769
1770 (
1771 my $basename,
1772 my $dirname_prod,
1773 my $dirname_repo,
1774 my $filename_prod,
1775 my $filename_repo
1776 ) = get_filenames( $_[0] );
1777
1778 my @files = get_complete_path_globbing($filename_prod);
1779
1780 if (@files) {
1781 print join( "\n", @files );
1782 print "\n";
1783 }
1784 return $return_code;
1785}
1786
1787sub complete_repopath(@)
1788{
1789 my $return_code = $RETURN_OK;
1790 check_parameter( @_, 1 );
1791 check_env();
1792
1793 (
1794 my $basename,
1795 my $dirname_prod,
1796 my $dirname_repo,
1797 my $filename_prod,
1798 my $filename_repo
1799 ) = get_filenames( $_[0] );
1800
1801 my @files = get_complete_path_globbing($filename_repo);
1802
1803 if (@files) {
1804
1805 # remove DASSCM_REPO path again
1806 print join(
1807 "\n",
1808 map( {
1809 s|^${DASSCM_REPO}|/|;
1810 $_
1811 } @files )
1812 );
1813 print "\n";
1814 }
1815 return $return_code;
1816}
1817
1818#####################################################################
1819#
1820# main
1821#
1822
1823my $return_code = $RETURN_OK;
1824my $number_arguments = @ARGV;
1825
1826# global options
1827# stops at first non-option
1828Getopt::Long::Configure('require_order');
1829if ( not GetOptions( \%options, @OPTIONS_GLOBAL ) ) {
1830 usage();
1831 exit $RETURN_NOK;
1832}
1833
1834# set verbose to command line option
1835$verbose = $options{'verbose'};
1836
1837if ( $options{'help'} ) {
1838 help(@ARGV);
1839 exit;
1840}
1841
1842# get subcommand and remove it from @ARGV
1843if ( defined( $ARGV[0] ) ) {
1844 $command = get_command_uniform_name( $ARGV[0] );
1845 shift @ARGV;
1846}
1847
1848if ( not defined($command) ) {
1849 usage();
1850 exit $RETURN_NOK;
1851}
1852
1853$DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1854$DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
1855
1856$DASSCM_PLUGIN_RESULTS_PATH =
1857 $config->{'DASSCM_LOCAL_REPOSITORY_BASE'} . "/" . "plugin-results/";
1858
1859# get list of additional directories and files, seperated by blank (" ")
1860# these files are always stored in subversion
1861if ( $config->{'DASSCM_ADDITIONAL_FILES'} ) {
1862 @DASSCM_ADDITIONAL_FILES = split / /, $config->{'DASSCM_ADDITIONAL_FILES'};
1863} else {
1864 @DASSCM_ADDITIONAL_FILES = ( $DASSCM_PLUGIN_RESULTS_PATH );
1865}
1866
1867# TODO: check variables
1868$DASSCM_SVN_REPOSITORY =
1869 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1870
1871$DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1872$DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
1873
1874#
1875# if a user is given by dasscm configuration file, we use it.
1876# Otherwise we expect that read-only account is configured
1877# as local subversion configuration.
1878# If this is also not the case,
1879# user is required to type username and password.
1880# This will be stored as local subversion configuration thereafter.
1881#
1882if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
1883 $svnCheckoutCredentials =
1884 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
1885}
1886
1887
1888# check for command options
1889my @cmd_options = get_command_possible_options($command);
1890if (@cmd_options) {
1891
1892 # get command line options and store them in options hash
1893 my $result = GetOptions( \%options, @cmd_options );
1894
1895 # print options
1896 foreach my $option ( keys %options ) {
1897 print "${option}: $options{$option}\n";
1898 }
1899}
1900
1901#
1902# action accordinly to command are taken
1903#
1904$return_code = &{ get_command_function($command) }(@ARGV);
1905
1906exit $return_code;
Note: See TracBrowser for help on using the repository browser.