source: dasscm/trunk/usr/bin/dasscm

Last change on this file was 1229, checked in by joergs, on Jan 17, 2017 at 4:34:56 PM

dasscm init can accept new certificates permanently again.

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