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

Last change on this file since 924 was 924, checked in by joergs, on Jul 30, 2010 at 5:51:47 PM

set LANG=C before running commands

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