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

Last change on this file since 944 was 944, checked in by joergs, on Feb 22, 2011 at 7:42:38 PM

bugfix: LANG=C for executing commands

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