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

Last change on this file since 916 was 916, checked in by joergs, on Jul 18, 2010 at 9:20:40 PM

added plugin support

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