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

Last change on this file since 1182 was 1182, checked in by joergs, on Jun 10, 2015 at 9:45:05 PM

set --no-auth-cache more generally

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