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

Last change on this file since 918 was 918, checked in by joergs, on Jul 26, 2010 at 6:16:32 PM

make plugin support work, permissions are now also handeld by plugin, dasscm permissions outputs only to STDOUT

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