source: dasscm/trunk/dasscm@ 803

Last change on this file since 803 was 803, checked in by joergs, on Nov 18, 2009 at 4:28:12 PM

bugfix: quoting for cp

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 36.3 KB
RevLine 
[186]1#!/usr/bin/perl -w
2
3# $Id: dasscm 803 2009-11-18 15:28:12Z 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;
[214]10use Getopt::Long;
[186]11use File::Basename;
[209]12use File::Compare;
[802]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
[234]20#use 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
[253]39# commands that require write access (and therefore a login)
40# to the repository server
[274]41my @COMMANDS_REQUIRE_WRITE = ( "add", "commit" );
[252]42
[215]43# configuration file
[205]44my $config_file = "/etc/dasscm.conf";
[234]45my $config = get_config($config_file);
[205]46my $DASSCM_LOCAL_REPOSITORY_BASE;
47my $DASSCM_REPOSITORY_NAME;
48my $DASSCM_SVN_REPOSITORY;
[247]49my $DASSCM_CHECKOUT_USERNAME;
50my $DASSCM_CHECKOUT_PASSWORD;
[286]51my $DASSCM_PERMISSION_FILE;
[205]52
[238]53# current directory at program start
54my $StartDirectory = cwd();
55
[268]56my $diff = "diff --exclude .svn ";
[205]57my $SVN = "svn ";
58my $svnOptions = "";
59my $svnCheckoutCredentials = "";
60my $svnPasswordCredentials = "";
[275]61
[268]62# flag. Set to true by svn_update
63# This prevents, that svn_update is called multiple times
64my $svnRepositoryIsUptodate = 0;
[205]65
[196]66# command line options get stored in options hash
[205]67my %options = ();
68
[197]69# subcommand, that gets executed (add, commit, ...)
[196]70my $command;
[186]71
[205]72my $verbose = 0;
73
[189]74#####################################################################
75#
[186]76# util functions
[189]77#
[187]78sub usage()
79{
[283]80 print '$Id: dasscm 803 2009-11-18 15:28:12Z joergs $';
81 print "\n\n";
[205]82 print "usage: dasscm <subcommand> [options] [args]\n";
83 print "\n";
84 print "dasscm is intended to help versioning configuration files\n";
85 print "\n";
86 print "Available subcommands:\n";
[215]87 print " help <subcommand>\n";
[205]88 print " init\n";
[274]89 print " login <username>\n";
90 print " up <path>\n";
91 print " ls <path>\n";
92 print " add <path>\n";
93 print " commit <path>\n";
[800]94 print " revert <path>\n";
[274]95 print " diff <path>\n";
96 print " status <path>\n";
[277]97 print " check\n";
[274]98 print " cleanup\n";
[215]99 print " permissions\n";
[205]100 print "\n";
[800]101 print "If dasscm is not yet configured, read $doc_file\n";
[187]102}
103
[233]104sub warning(@)
105{
106 print "Warning: " . join( "\n ", @_ ) . "\n";
107}
108
109sub error(@)
110{
111 print "Error: " . join( "\n ", @_ ) . "\n";
112}
113
114sub fatalerror(@)
115{
[239]116 error(@_);
117
[233]118 #print "Exiting\n";
[239]119 exit 1;
[233]120}
121
[238]122#
123# reading config file and return key/value pairs as hash
124#
[234]125sub get_config
126{
[239]127 my $file = $_[0];
[234]128
[239]129 if ( !$file ) {
[234]130 fatalerror( "failed to open config file" . $file );
131 }
132
133 my $data = {};
134
135 # try to open config file
136 if ( !open( FH, $file ) ) {
137 fatalerror( "failed to open config file" . $file );
138 } else {
139 while (<FH>) {
140 chomp;
141 if (/^#/) {
142 next;
143 }
144 if ( $_ =~ /=/g ) {
[239]145
[238]146 # splitting in 2 fields at maximum
[234]147 my ( $option, $value ) = split( /=/, $_, 2 );
148 $option =~ s/^\s+//g;
149 $option =~ s/\s+$//g;
150 $option =~ s/\"+//g;
151 $value =~ s/^\s+//g;
152 $value =~ s/\s+$//g;
153 $value =~ s/\"+//g;
154
155 if ( length($option) ) {
156 $data->{$option} = $value;
157 }
158 }
159 }
160 }
161 close(FH);
162
163 return $data;
164}
165
[270]166#
167# check and evaluate environment variables
168#
[186]169sub check_env()
170{
[205]171
172 # DASSCM_PROD
173 if ( !$DASSCM_PROD ) {
174 $DASSCM_PROD = "/";
175 }
176
177 if ( !-d $DASSCM_PROD ) {
178 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
179 }
180 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
181
182 # DASSCM_REPOSITORY_NAME
[208]183 if ( !$DASSCM_REPOSITORY_NAME ) {
184 die
185 "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";
186 }
[205]187
188 # DASSCM_REPO
189 if ( !$DASSCM_REPO ) {
190 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
191 $DASSCM_REPO =
192 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
193 } else {
194 die
195 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
196 }
197 }
[215]198 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
[205]199
200 #
[248]201 # subversion checkout user
202 #
[252]203 if ( !$DASSCM_CHECKOUT_USERNAME ) {
204 fatalerror(
[248]205 "variable DASSCM_CHECKOUT_USERNAME is not defined.",
[252]206 "Use file $config_file to configure it."
207 );
[248]208 }
209
[252]210 if ( !$DASSCM_CHECKOUT_PASSWORD ) {
211 fatalerror(
[248]212 "variable DASSCM_CHECKOUT_PASSWORD is not defined.",
[252]213 "Use file $config_file to configure it."
214 );
[248]215 }
216
217 #
[239]218 # check if local repository directory exist
[235]219 # (if not creating by init)
[205]220 #
221 if ( $command ne "init" ) {
222 if ( not -d $DASSCM_REPO ) {
223 die
[208]224 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
[205]225 }
[208]226
[205]227 #
228 # user settings
229 #
[208]230
[205]231 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
[208]232 if ( !$DASSCM_USERNAME ) {
233 $DASSCM_USERNAME = $DASSCM_USER;
[205]234 }
235
236 # user root is not allowed for checkins.
237 # if user is root, DASSCM_USER has to be set,
238 # otherwise USER can be used
239 if ( "$USER" eq "root" ) {
[252]240 if ( ( not $DASSCM_USERNAME )
241 and ( grep { m|^$command$| } @COMMANDS_REQUIRE_WRITE ) )
242 {
243
244 #( $command ne "login" ) and ( $command ne "status" ) ) {
245 fatalerror(
246 "Envirnonment variable DASSCM_USERNAME not set.",
247 "Set DASSCM_USERNAME to your subversion user account or",
248 "use 'dasscm login'"
249 );
[205]250 }
251 $svnOptions .= " --no-auth-cache ";
252 } elsif ( !$DASSCM_USERNAME ) {
253 $DASSCM_USERNAME = $USER;
254 }
255
256 #
257 # password
258 #
[208]259 if ($DASSCM_PASSWORD) {
[267]260 $svnPasswordCredentials = " --password '$DASSCM_PASSWORD' ";
[205]261 }
262 }
263
264 #$svnOptions .= " --username $DASSCM_USERNAME "
[186]265}
266
[270]267#
268# has been intendend,
269# to check addtitional parameters.
270# Currently not used.
271#
[186]272sub check_parameter(@)
273{
274}
275
[238]276#
[287]277# normalize path namens:
278# - directories should end with "/"
279# - use only single "/"
280#
281sub normalize_path($)
282{
283 my $path = shift || "";
284
[290]285 if ( $path =~ m|^/| ) {
286
[287]287 # full path
[290]288 if ( -d $path ) {
289
[287]290 # ensure, a directory ends with '/'
291 $path .= '/';
292 }
[290]293 } elsif ( -d cwd() . '/' . $path ) {
294
295 # ensure, a directory ends with '/'
296 $path .= '/';
[287]297 }
298
299 # remove double (triple) slashes (/)
300 $path =~ s|/[/]*|/|g;
301
302 return $path;
303}
304
305#
[239]306# generate from (relative) filename
[238]307# all required file and directory names:
308# $basename, $dirname_prod, $dirname_repo,
309# $filename_prod, $filename_repo
310#
[187]311sub get_filenames(@)
312{
[250]313 my $filename_prod = $_[0] || ".";
[237]314
[238]315 # make filename absolut
[205]316 if ( !( $filename_prod =~ m/^\// ) ) {
[270]317 $filename_prod = cwd() . '/' . $filename_prod;
[205]318 }
[187]319
[800]320 # file must be readable. Only exception is, when file should be reverted
321 if ( $command ne "revert" ) {
322 if ( not -r $filename_prod ) {
323 fatalerror( $filename_prod . " is not accessable" );
324 }
[233]325 }
[205]326
[274]327 # dirname buggy: eg. "/etc/" is reduced to "/",
328 # "/etc" is used as filename
329 # herefore make sure, that if filename is a directory,
330 # it will end by "/"
[290]331 $filename_prod = normalize_path($filename_prod);
[238]332
[270]333 ( my $basename, my $dirname_prod ) = fileparse($filename_prod);
334
[801]335 # normalize path.
[800]336 # not done for reverting, because in this case, the directory may not exist
337 # and the correct path should already be stored in the repository
338 if ( $command ne "revert" ) {
[801]339
[800]340 # uses chdir to determine real directory in a unique way
[801]341 chdir $dirname_prod
342 or fatalerror( "failed to access directory $dirname_prod: " . $! );
[800]343 $dirname_prod = normalize_path( cwd() );
344 chdir $StartDirectory;
345 }
[238]346
[287]347 my $dirname_repo = normalize_path( $DASSCM_REPO . "/" . $dirname_prod );
[290]348 my $filename_repo = normalize_path("$dirname_repo/$basename");
[287]349
[214]350 if ($verbose) {
[287]351 print "filename_repo: " . $filename_repo . "\n";
[290]352 print "dirname_repo: " . $dirname_repo . "\n";
[287]353 print "filename_prod: " . $filename_prod . "\n";
[290]354 print "dirname_prod: " . $dirname_prod . "\n";
[287]355 print "basename: " . $basename . "\n";
[214]356 }
[205]357
358 return (
359 $basename, $dirname_prod, $dirname_repo,
360 $filename_prod, $filename_repo
361 );
[187]362}
363
[271]364sub copy_file_to_repository( $ )
365{
366 my $filename = shift;
[256]367
[271]368 (
369 my $basename,
370 my $dirname_prod,
371 my $dirname_repo,
372 my $filename_prod,
373 my $filename_repo
[801]374 ) = get_filenames($filename);
[271]375
[802]376 #copy( $filename_prod, $filename_repo )
[803]377 ( my $rc, my @result ) = run_command( "cp -a \"$filename_prod\" \"$filename_repo\"" );
[802]378 if( $rc != 0 ) {
379 error( "failed to copy $filename_prod to repository: ", @result );
380 }
381
382 # return success
383 return $rc == 0;
[271]384}
385
[802]386
387
388sub copy_file_from_repository_to_system( $ )
389{
390 my $filename = shift;
391
392 (
393 my $basename,
394 my $dirname_prod,
395 my $dirname_repo,
396 my $filename_prod,
397 my $filename_repo
398 ) = get_filenames($filename);
399
[803]400 ( my $rc, my @result ) = run_command( "cp -a \"$filename_repo\" \"$filename_prod\"" );
[802]401 if( $rc != 0 ) {
402 error( "failed to copy $filename_repo to $filename_prod: ", @result );
403 }
404
405 # return success
406 return $rc == 0;
407}
408
409
410
[256]411#
412# creates a file with permissions
413#
[215]414sub generatePermissionList
[209]415{
416
[215]417 # generieren der Zeilen für Permission-Savefile
418 my @files = @_;
419 my @permlist = ();
420 foreach my $file (@files) {
[227]421 $file = "/" . $file;
[239]422 if ( -e $file ) {
423 my $info = stat($file) || die "failed to stat $file: aborting";
424 my $mode = get_type( $info->mode ) & 07777;
[227]425 my $modestring = sprintf( "%04o", $mode );
[278]426 my $uidnumber = $info->uid;
427 my $uid = getpwuid($uidnumber) || $uidnumber;
428 my $gidnumber = $info->gid;
429 my $gid = getgrgid($gidnumber) || $gidnumber;
[227]430 push(
431 @permlist,
432 sprintf( "%-55s %-17s %4d",
[278]433 $file, "${uid}:${gid}", $modestring )
[227]434 );
435 }
[215]436 }
437 return @permlist;
438}
[209]439
[215]440sub get_type
441{
[209]442
[215]443 # Funktion übernommen aus /usr/bin/chkstat
444 my $S_IFLNK = 0120000; # symbolic link
445 my $S_IFREG = 0100000; # regular file
446 my $S_IFDIR = 0040000; # directory
447 my $S_IFCHAR = 0020000; # character device
448 my $S_IFBLK = 0060000; # block device
449 my $S_IFFIFO = 0010000; # fifo
450 my $S_IFSOCK = 0140000; # socket
451 my $S_IFMT = 0170000; # type of file
[209]452
[215]453 my $S_m;
454 if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
455 elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
456 elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
457 elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
458 elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
459 elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
460 elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
461 $S_m;
[209]462}
463
[186]464sub run_command
465{
[205]466 my $command = shift;
[186]467
[205]468 #print "executing command: " . $command . "\n";
[186]469
[205]470 open( RESULT, $command . ' 2>&1 |' );
471 my @result = <RESULT>;
472 close(RESULT);
473 my $retcode = $? >> 8;
[186]474
[205]475 #print @result;
476 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
[186]477
[205]478 return ( $retcode, @result );
[186]479}
480
[205]481sub run_interactive
482{
[186]483
[208]484 if ($verbose) {
[205]485 print "run_interactive:" . join( " ", @_ ) . "\n";
486 }
[196]487
[205]488 system(@_);
489 if ( $? == -1 ) {
490 printf "failed to execute: $!\n";
491 } elsif ( $? & 127 ) {
492 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
493 ( $? & 128 ) ? 'with' : 'without';
494 } elsif ( $? >> 8 != 0 ) {
495 printf "child exited with value %d\n", $? >> 8;
496 }
497 return ( $? >> 8 );
498}
499
[247]500sub svn_check_credentials( $$;$$ )
[196]501{
[205]502 my $username = shift;
503 my $password = shift;
504
[252]505 # check silently are allow user interaction?
[247]506 my $interactive = shift || 0;
[220]507
[247]508 # default: exit program, if repository is not accessable
509 # (do not exit for 'init')
510 my $fatalerror = shift || 1;
511
512 print "checking credentials ";
513
[239]514 if ( !$username ) {
515 fatalerror("no username given");
[238]516 }
517
[239]518 if ( !$password ) {
519 fatalerror("no password given");
[238]520 }
521
[247]522 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
523
[220]524 # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
525 # therefore switching to "svn status"
526 # ( my $rc_update, my @result ) =
527 # run_command(
528 # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
529 # );
530 #print @result;
531
[247]532 my $rc_update;
[252]533 if ($interactive) {
[801]534 $rc_update = run_interactive(
[267]535 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
[801]536 );
[247]537 } else {
[801]538 ( $rc_update, my @result ) = run_command(
[267]539 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
[801]540 );
[252]541
[247]542 if ( $rc_update != 0 ) {
543 print "\n", @result;
[252]544 if ($fatalerror) {
[247]545 fatalerror();
546 }
547 return;
548 }
[205]549 }
550
[247]551 # return success
552 return $rc_update == 0;
[196]553}
554
[205]555sub svn_update( ;$ )
556{
[270]557 my $update_path = shift || "";
558
[797]559 # return value
560 my $update_ok = 1;
561
[270]562 # use this flag to do only one update per run
[275]563 if ( !$svnRepositoryIsUptodate ) {
[801]564 ( my $rc_update, my @result ) = run_command(
[275]565 "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'"
[801]566 );
[268]567 print @result;
568 if ( $rc_update != 0 ) {
[290]569 error("failed to update local repository ($update_path)");
[797]570 $update_ok = 0;
[270]571 } elsif ( not $update_path ) {
[275]572
[270]573 # set this flag if a full update is done
[268]574 $svnRepositoryIsUptodate = 1;
575 }
[205]576 }
[797]577 return $update_ok;
[215]578}
[196]579
[271]580sub svn_ls( ;@ )
[215]581{
[270]582 (
583 my $basename,
584 my $dirname_prod,
585 my $dirname_repo,
586 my $filename_prod,
587 my $filename_repo
[801]588 ) = get_filenames( $_[0] );
[220]589
[218]590 # svn ls -R is better, but much, much slower
591 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
[270]592
[271]593 my @files = ();
594 my @links = ();
595 my @dirs = ();
596 my @others = ();
597
[289]598 find(
599 {
600 wanted => sub {
601 my $name = normalize_path($File::Find::name);
602 $name =~ s|^$dirname_repo||;
[290]603
[289]604 #print "($name)\n";# . $File::Find::dir . "\n";
605 if ( not $name ) {
[290]606
[289]607 # name string is empty (top directory).
608 # do nothing
609 } elsif ( $name =~ m/\.svn/ ) {
[275]610
[289]611 # skip svn meta data
612 } elsif ( -l $_ ) {
[275]613
[289]614 # soft link
615 # important: check for links first
616 # to exclude them from further checks
617 push( @links, $name );
618 } elsif ( -d $_ ) {
[290]619
620 # directories
621 push( @dirs, $name );
[289]622 } elsif ( -f $_ ) {
[275]623
[289]624 # regular file
625 push( @files, $name );
626 } else {
627 push( @others, $name );
628 }
[290]629 }
[289]630 },
631 ($filename_repo)
632 );
[271]633
[287]634 return ( sort( @dirs, @files ) );
[205]635}
636
[274]637sub svn_revert( ;$ )
638{
639 my $path = shift || $DASSCM_REPO;
640
[275]641 ( my $rc_update, my @result ) = run_command("$SVN revert -R '$path'");
[274]642
643 if ( $rc_update != 0 ) {
644 print "\n", @result;
[275]645 error("failed to revert subversion repository changes");
[274]646 }
647}
648
[285]649sub svn_remove_unknown_files( ;$ )
650{
651 my $path = shift || $DASSCM_REPO;
652
[290]653 ( my $rc_update, my @result ) = run_command("$SVN status '$path'");
[285]654
655 if ( $rc_update != 0 ) {
656 print "\n", @result;
657 error("failed to receive subversion repository information");
658 } else {
[290]659 foreach (@result) {
660 if (s/^\? +//) {
[285]661 chomp;
[290]662
[285]663 # if file is unknown to subversion (line starts with "?")
664 # remove it
665 print "removing $_\n";
[801]666
[800]667 # unlink doesn't work recursive, there "rm -rf" is used
668 #unlink($_);
[801]669 system("rm -rf $_");
[285]670 }
671 }
672 }
673}
674
[268]675sub getModifiedFiles( ;$ )
676{
[270]677 (
678 my $basename,
679 my $dirname_prod,
680 my $dirname_repo,
681 my $filename_prod,
682 my $filename_repo
[801]683 ) = get_filenames( $_[0] );
[268]684
[275]685 my @files = svn_ls($filename_prod);
[270]686
[268]687 # stores result from status (cvscheck)
688 my %removedfiles = ();
689 my %changedfiles = ();
[278]690 my %unknownfiles = ();
[268]691
692 # create list of modified files
693 if (@files) {
694
695 foreach my $file (@files) {
696
[271]697 my $realfile = $dirname_prod . $file;
698 my $cvsworkfile = $dirname_repo . $file;
[268]699
700 if ( -d $realfile ) {
[290]701
[278]702 # directory
[290]703 if ( !-d "$cvsworkfile" ) {
704
[278]705 # real is directory, repository is not. This is a problem
706 $changedfiles{"$realfile"} = $cvsworkfile;
707 }
708 } elsif ( !-e $realfile ) {
709 $removedfiles{"$realfile"} = $cvsworkfile;
[268]710 } elsif ( !-r $realfile ) {
[290]711
[278]712 # don't have permission to read the file,
713 # can't check it
714 $unknownfiles{"$realfile"} = $cvsworkfile;
[268]715 } else {
716 ( -r "$cvsworkfile" )
[278]717 || fatalerror("failed to read $cvsworkfile");
[268]718 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
719 $changedfiles{"$realfile"} = $cvsworkfile;
720 }
721 }
722 }
723 }
724
[278]725 return ( \%changedfiles, \%removedfiles, \%unknownfiles );
[268]726}
727
[238]728#
729# from an array of files/dirs,
730# generates list of files
731# sorted by type
732#
[237]733sub get_files( @ )
734{
735 my @files = ();
736 my @links = ();
737 my @dirs = ();
738 my @others = ();
739
[239]740 if (@_) {
741 find(
742 {
743 wanted => sub {
744 my $fullname = cwd() . "/" . $_;
745 if ( -l $_ ) {
746
747 # soft link
748 # important: check for links first
749 # to exclude them from further checks
750 push( @links, $fullname );
751 } elsif ( -d $_ ) {
[271]752
753 # directories
[239]754 push( @dirs, $fullname );
755 } elsif ( -f $_ ) {
756
757 # regular file
758 push( @files, $fullname );
759 } else {
760 push( @others, $fullname );
761 }
762 }
763 },
764 @_
765 );
[237]766 }
767
[239]768 # don't rely on others.
[237]769 # If more specific file types are needed,
770 # they will be added
771 return {
[239]772 files => \@files,
773 links => \@links,
774 dirs => \@dirs,
775 others => \@others
776 };
[237]777}
778
[189]779#####################################################################
780#
[186]781# functions
782
783sub help(;@)
784{
[205]785 if ( @_ == 0 ) {
786 usage();
787 } else {
788 print "help for @_: ...\n";
[214]789 usage();
[205]790 }
[186]791}
792
[203]793sub login(@)
794{
[205]795 check_parameter( @_, 1 );
796 check_env();
[203]797
[235]798 my $input_username = $_[0];
[214]799
800 if ( not $input_username ) {
801 my $output_username = "";
802 if ($DASSCM_USERNAME) {
803 $output_username = " ($DASSCM_USERNAME)";
804 }
805
806 print "Enter DASSCM user name", $output_username, ": ";
807 $input_username = <STDIN>;
808 chomp($input_username);
[247]809
810 $input_username = $input_username || $DASSCM_USERNAME;
[205]811 }
[203]812
[205]813 # hidden password input
[247]814 print "Enter password for $input_username: ";
[205]815 ReadMode('noecho');
816 my $input_password = <STDIN>;
817 ReadMode('normal');
818 chomp($input_password);
[220]819 print "\n";
[203]820
[247]821 # checking checkout username/password
[252]822 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
823 $DASSCM_CHECKOUT_PASSWORD );
[247]824 print "checkout access okay\n";
[205]825
[247]826 svn_check_credentials( $input_username, $input_password );
827
[205]828 #
829 # set environment variables
830 #
[267]831 $ENV{'DASSCM_USERNAME'} = "$input_username";
832 $ENV{'DASSCM_PASSWORD'} = "$input_password";
[205]833
[209]834 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
835 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
836 "DASSCM_REPO: $DASSCM_REPO\n",
[278]837 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
[205]838
[278]839 status();
840
841 print "\n[dasscm shell]\n\n";
[235]842 my $shell = $SHELL || "bash";
843 exec($shell) or die "failed to start new shell";
[203]844}
845
[260]846#
847# initialize local checkout directory (initial checkout)
848#
[205]849sub init(@)
850{
851 check_parameter( @_, 1 );
852 check_env();
853
[235]854 # don't do repository creation (svn mkdir) here,
855 # because then their must be a lot of prior checks
856
[205]857 # update complete repository
[216]858 # and create permission file
[801]859 my $retcode = run_interactive(
[286]860 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; mkdir -p `dirname $DASSCM_PERMISSION_FILE`; touch $DASSCM_PERMISSION_FILE"
[801]861 );
[205]862}
863
[215]864sub ls(@)
[186]865{
[205]866 check_parameter( @_, 1 );
867 check_env();
[186]868
[271]869 my @files = svn_ls(@_);
[215]870
[275]871 if (@files) {
[274]872 print join( "\n", @files );
873 print "\n";
874 }
[215]875}
876
877sub update(@)
878{
879 check_parameter( @_, 1 );
880 check_env();
881
882 #
883 # update local repository
884 #
885 svn_update();
886}
887
[237]888#
889# helper function for "add" command
890#
[215]891sub add_helper(@)
892{
[205]893 (
894 my $basename,
895 my $dirname_prod,
896 my $dirname_repo,
897 my $filename_prod,
898 my $filename_repo
[801]899 ) = get_filenames( $_[0] );
[186]900
[274]901 mkpath($dirname_repo);
[802]902 copy_file_to_repository( $filename_prod );
[186]903
[274]904 # already checked in?
905 chdir $DASSCM_REPO;
[205]906
[274]907 # also add the path to filename.
908 for my $dir ( split( '/', $dirname_prod ) ) {
909 if ($dir) {
[275]910 my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
[274]911 if ( $rc > 0 ) {
912 print join( "\n", @out );
[205]913 }
[274]914 chdir $dir;
[205]915 }
916 }
[275]917 my ( $rc, @out ) = run_command("$SVN add '$basename'");
[274]918 if ( $rc > 0 ) {
919 print join( "\n", @out );
920 }
921 chdir $StartDirectory;
922
[215]923}
[205]924
[215]925#
[274]926# adding new files (or directories)
[215]927#
928sub add(@)
929{
930 check_parameter( @_, 1 );
931 check_env();
932
933 #
934 # update local repository
935 #
936 svn_update();
937
[237]938 # get all regular files and links
[239]939 my $href_files = get_files(@_);
[220]940
[237]941 #print Dumper( $href_files );
942
[239]943 my @files = @{ $href_files->{files} };
944 my @links = @{ $href_files->{links} };
[237]945
[239]946 if (@files) {
[237]947 my $number = $#files + 1;
948 print "files to check-in ($number): \n";
949 print join( "\n", @files );
950 print "\n";
951 }
952
[268]953 # TODO: check in links and also link target? At least warn about link target
[239]954 if (@links) {
[237]955 my $number = $#links + 1;
956 print "\n";
957 print "ignoring links ($number):\n";
958 print join( "\n", @links );
959 print "\n";
960 }
961
[238]962 # TODO: confirm
963
[237]964 # copy files one by one to local repository
965 for my $file (@files) {
[239]966
[233]967 # add file
[239]968 add_helper($file);
[233]969 }
970
[215]971 # create new permissions file
972 permissions();
[220]973
[215]974 # add permissions file
[286]975 add_helper($DASSCM_PERMISSION_FILE);
[215]976
[205]977 if ( $options{'message'} ) {
978 $svnOptions .= " --message \"$options{'message'}\" ";
979 }
980
[239]981 # commit calls $EDITOR.
[237]982 # use "interactive" here, to display output
[801]983 my $retcode = run_interactive(
[267]984 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
[801]985 );
[205]986
[274]987 # svn commit does not deliever an error return code, if commit is canceld,
988 # so a revert is performed in any case
989 svn_revert();
[186]990}
991
[271]992#
993# checks in all modified files
994#
995sub commit(@)
996{
997 check_parameter( @_, 1 );
998 check_env();
999
1000 (
1001 my $basename,
1002 my $dirname_prod,
1003 my $dirname_repo,
1004 my $filename_prod,
1005 my $filename_repo
[801]1006 ) = get_filenames( $_[0] );
[271]1007
1008 #
1009 # update local repository
1010 #
1011 svn_update();
1012
[275]1013 ( my $refChangedFiles, my $refRemovedFiles ) =
1014 getModifiedFiles($filename_prod);
[271]1015 my %changedfiles = %{$refChangedFiles};
1016 my %removedfiles = %{$refRemovedFiles};
1017
[275]1018 if (%removedfiles) {
1019 my $removedFilesString =
1020 '"' . join( '" "', values(%removedfiles) ) . '"';
1021 my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
[271]1022 if ( $rc > 0 ) {
1023 print join( "\n", @out );
1024 }
1025 }
1026
1027 # copy files one by one to local repository
1028 for my $file ( keys(%changedfiles) ) {
[275]1029 copy_file_to_repository($file);
[271]1030 }
1031
1032 # create new permissions file
1033 permissions();
1034
1035 # add permissions file
[286]1036 add_helper($DASSCM_PERMISSION_FILE);
[271]1037
1038 if ( $options{'message'} ) {
1039 $svnOptions .= " --message \"$options{'message'}\" ";
1040 }
1041
1042 # commit calls $EDITOR.
1043 # use "interactive" here, to display output
[801]1044 my $retcode = run_interactive(
[271]1045 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
[801]1046 );
[274]1047
1048 # svn commit does not deliever an error return code, if commit is canceld,
1049 # so a revert is performed in any case
1050 svn_revert();
[271]1051}
1052
[800]1053#
1054# revert: copies files back from repository to system
1055#
1056sub revert(@)
1057{
1058 check_parameter( @_, 1 );
1059 check_env();
1060
1061 (
1062 my $basename,
1063 my $dirname_prod,
1064 my $dirname_repo,
1065 my $filename_prod,
1066 my $filename_repo
[801]1067 ) = get_filenames( $_[0] );
[800]1068
1069 # return code for the shell
1070 # default: error
1071 my $return_code = $RETURN_OK;
1072
1073 # cleanup repository
[801]1074 cleanup();
[800]1075 #svn_update();
1076
1077 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1078 getModifiedFiles($filename_prod);
1079 my %changedfiles = %{$refChangedFiles};
1080 my %removedfiles = %{$refRemovedFiles};
1081 my %unknownfiles = %{$refUnknownFiles};
1082
1083 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1084
1085 if (%removedfiles) {
1086 print "DELETED files and directories. Recreated from repository:\n";
[801]1087 my @removedPaths =
1088 ( sort { length $a > length $b } keys %removedfiles );
[800]1089 print join( "\n", @removedPaths ) . "\n\n";
1090
1091 # copy files one by one from local repository to system
1092 # and also create directories
1093 # paths are sorted, so that directories are created first
[801]1094 for my $real_path (@removedPaths) {
1095 if ( -d $removedfiles{"$real_path"} ) {
[800]1096 mkpath("$real_path");
1097 } else {
[802]1098 copy_file_from_repository_to_system( $real_path );
[800]1099 }
1100 }
1101 }
1102
1103 if (%changedfiles) {
1104 print "MODIFIED files. Copied from repository to the system:\n";
1105 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1106
1107 # copy files one by one from local repository to system
1108 for my $real_file ( keys(%changedfiles) ) {
[802]1109 copy_file_from_repository_to_system( $real_file );
[800]1110 }
1111
1112 }
1113
1114 if (%unknownfiles) {
1115 print "UNKNOWN: insufficient permission to check files:\n";
1116 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1117
1118 $return_code = $RETURN_NOK;
1119 }
1120
1121 } else {
1122 print "no modified files found in $dirname_repo\n";
1123 }
1124
1125 return $return_code;
1126}
1127
[193]1128sub blame(@)
1129{
[205]1130 check_parameter( @_, 1 );
1131 check_env();
[193]1132
[205]1133 (
1134 my $basename,
1135 my $dirname_prod,
1136 my $dirname_repo,
1137 my $filename_prod,
1138 my $filename_repo
[801]1139 ) = get_filenames( $_[0] );
[205]1140
1141 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
[193]1142}
1143
[187]1144sub diff(@)
1145{
[205]1146 check_parameter( @_, 1 );
1147 check_env();
[187]1148
[205]1149 (
1150 my $basename,
1151 my $dirname_prod,
1152 my $dirname_repo,
1153 my $filename_prod,
1154 my $filename_repo
[801]1155 ) = get_filenames( $_[0] );
[205]1156
1157 #print "$basename,$dirname_prod,$dirname_repo\n";
1158
[797]1159 svn_update();
[205]1160
[238]1161 ( my $rc_diff, my @diff_result ) =
[239]1162 run_command( $diff . " $filename_repo $filename_prod" );
[238]1163
1164 print @diff_result;
[187]1165}
1166
[209]1167sub status(@)
1168{
1169 check_parameter( @_, 1 );
1170 check_env();
1171
[270]1172 (
1173 my $basename,
1174 my $dirname_prod,
1175 my $dirname_repo,
1176 my $filename_prod,
1177 my $filename_repo
[801]1178 ) = get_filenames( $_[0] || "/" );
[270]1179
[252]1180 # return code for the shell
1181 # default: error
1182 my $return_code = $RETURN_NOK;
1183
[209]1184 #
1185 # update local repository
1186 #
[278]1187 #svn_update( $filename_prod );
[209]1188
[278]1189 # check, if permissions have changed
1190 permissions();
1191
1192 # get modified files
1193 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
[275]1194 getModifiedFiles($dirname_prod);
[268]1195 my %changedfiles = %{$refChangedFiles};
1196 my %removedfiles = %{$refRemovedFiles};
[278]1197 my %unknownfiles = %{$refUnknownFiles};
[209]1198
[278]1199 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1200
[215]1201 if (%removedfiles) {
[278]1202 print "DELETED: files found in repository, but not in system:\n";
[800]1203 print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
[209]1204 }
1205
[215]1206 if (%changedfiles) {
[278]1207 print "MODIFIED: files differs between repository and system:\n";
1208 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
[209]1209 }
[278]1210
1211 if (%unknownfiles) {
1212 print "UNKNOWN: insufficient permission to check files:\n";
1213 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1214 }
1215
[209]1216 } else {
[270]1217 print "no modified files found in $dirname_repo\n";
[252]1218 $return_code = $RETURN_OK;
[209]1219 }
[215]1220
[252]1221 return $return_code;
[215]1222}
[209]1223
[277]1224#
1225# return short status in Nagios plugin conform way
1226#
1227sub check()
1228{
1229 check_env();
1230
1231 # return code for the shell
[290]1232 my $return_code = $RETURN_OK;
[277]1233 my $return_string = "OK: no modified files";
1234
[278]1235 # check, if permissions have changed
1236 permissions();
1237
1238 # get modified files
1239 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
[290]1240 getModifiedFiles("/");
[277]1241 my %changedfiles = %{$refChangedFiles};
1242 my %removedfiles = %{$refRemovedFiles};
[278]1243 my %unknownfiles = %{$refUnknownFiles};
[277]1244
1245 if ( %removedfiles or %changedfiles ) {
1246 $return_string = "Warning: ";
[290]1247 if (%changedfiles) {
1248 $return_string .=
1249 "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
[277]1250 }
[290]1251 if (%removedfiles) {
1252 $return_string .=
1253 "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
[277]1254 }
[278]1255 if (%unknownfiles) {
[290]1256 $return_string .=
1257 "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
[278]1258 }
[277]1259 $return_code = $RETURN_WARN;
1260 }
1261
1262 # addition nagios Service Status
1263 #Critical
1264 #Unknown
1265
[290]1266 print "$return_string\n";
[277]1267 return $return_code;
1268}
1269
[270]1270sub permissions()
[215]1271{
1272 check_env();
1273
[278]1274 my $return_code = $RETURN_OK;
1275
[215]1276 #
1277 # update local repository
1278 #
1279 #svn_update();
1280
[220]1281 my $dir = $DASSCM_REPO;
[275]1282 my @files = svn_ls("/");
[215]1283
1284 if (@files) {
1285
1286 # generieren der Permissions
1287 my @permissions = generatePermissionList(@files);
1288 my $OUTFILE;
1289 my $tofile = 0; # Status für schreiben in File
[220]1290
[286]1291 if ( -w dirname($DASSCM_PERMISSION_FILE) ) {
[215]1292
1293 # Verzeichnis existiert => schreiben
[286]1294 open( OUTFILE, ">$DASSCM_PERMISSION_FILE" )
1295 || die("failed to write to $DASSCM_PERMISSION_FILE: $!");
[215]1296 $tofile = 1; # Merken, daß in File geschrieben wird
1297 print OUTFILE "#\n";
1298 print OUTFILE "# created by dasscm permissions\n";
[220]1299 print OUTFILE
1300 "# It is intended to be used for restoring permissions\n";
[287]1301 print OUTFILE "#\n";
[215]1302 } else {
1303
[290]1304 if ( $command eq "permission" ) {
1305
[278]1306 # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
1307 # Alias Filehandle für stdout erzeugen
1308 $return_code = $RETURN_WARN;
[290]1309 *OUTFILE = *STDOUT;
[278]1310 } else {
[290]1311
[278]1312 # TODO: improve this. Check for diff?
1313 $return_code = $RETURN_CRIT;
1314 return $return_code;
1315 }
[215]1316 }
[278]1317
[215]1318 foreach my $line (@permissions) {
1319 print OUTFILE "$line\n";
1320 }
1321
[220]1322 if ($tofile) {
[215]1323 close(OUTFILE);
1324 }
1325 }
[278]1326
1327 return $return_code;
[209]1328}
1329
[274]1330#
1331# remove all uncommited changes in the repository
1332#
1333sub cleanup()
1334{
1335 check_env();
[268]1336
[275]1337 svn_revert($DASSCM_REPO);
[285]1338 svn_remove_unknown_files($DASSCM_REPO);
[274]1339}
1340
[189]1341#####################################################################
1342#
[186]1343# main
[189]1344#
[186]1345
[252]1346my $return_code = $RETURN_OK;
[186]1347my $number_arguments = @ARGV;
1348
[205]1349if ( $number_arguments > 0 ) {
[186]1350
[205]1351 # get subcommand and remove it from @ARGV
1352 $command = $ARGV[0];
1353 shift @ARGV;
[196]1354
[205]1355 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1356 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
[196]1357
[205]1358 # TODO: check variables
1359 $DASSCM_SVN_REPOSITORY =
1360 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1361
[247]1362 $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1363 $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
[205]1364
1365 #
1366 # if a user is given by dasscm configuration file, we use it.
1367 # Otherwise we expect that read-only account is configured
1368 # as local subversion configuration.
1369 # If this is also not the case,
1370 # user is required to type username and password.
1371 # This will be stored as local subversion configuration thereafter.
1372 #
1373 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
1374 $svnCheckoutCredentials =
1375 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
1376 }
1377
[290]1378 $DASSCM_PERMISSION_FILE = $config->{'DASSCM_PERMISSION_FILE'}
1379 || "/etc/permissions.d/dasscm.permission_backup";
[286]1380
[205]1381 # get command line options and store them in options hash
[214]1382 my $result = GetOptions( \%options, 'verbose', 'message=s' );
[205]1383
1384 # print options
1385 foreach my $option ( keys %options ) {
[215]1386 print "${option}: $options{$option}\n";
[205]1387 }
1388
[214]1389 # set verbose to command line option
1390 $verbose = $options{'verbose'};
1391
1392 #
1393 # action accordinly to command are taken
1394 # $command is rewritten in standard format,
1395 # so we can test for it later on more simply
1396 #
[205]1397 $_ = $command;
[274]1398 if (m/^help$/i) {
[205]1399 help(@ARGV);
[274]1400 } elsif (m/^login$/i) {
[208]1401 $command = "login";
[205]1402 login(@ARGV);
[274]1403 } elsif (m/^init$/i) {
[208]1404 $command = "init";
[205]1405 init(@ARGV);
[274]1406 } elsif (m/^ls$/i) {
[215]1407 $command = "ls";
1408 ls(@ARGV);
[274]1409 } elsif ( (m/^update$/i) || (m/^up$/i) ) {
[215]1410 $command = "update";
1411 update(@ARGV);
[274]1412 } elsif (m/^add$/i) {
[205]1413 $command = "add";
1414 add(@ARGV);
[274]1415 } elsif ( (m/^commit$/i) || (m/^checkin$/i) || (m/^ci$/i) ) {
[205]1416 $command = "commit";
[271]1417 commit(@ARGV);
[800]1418 } elsif (m/^revert$/i) {
[801]1419 $command = "revert";
[800]1420 $return_code = revert(@ARGV);
[274]1421 } elsif (m/^blame$/i) {
[208]1422 $command = "blame";
[205]1423 blame(@ARGV);
[274]1424 } elsif (m/^diff$/i) {
[208]1425 $command = "diff";
[205]1426 diff(@ARGV);
[274]1427 } elsif ( (m/^status$/i) || (m/^st$/i) ) {
[252]1428 $command = "status";
1429 $return_code = status(@ARGV);
[277]1430 } elsif (m/^check$/i) {
1431 $command = "check";
1432 $return_code = check();
[274]1433 } elsif (m/^permissions$/i) {
[290]1434 $command = "permissions";
[278]1435 $return_code = permissions();
[274]1436 } elsif (m/^cleanup$/i) {
1437 $command = "cleanup";
1438 cleanup();
[205]1439 } else {
[215]1440 print "unknown command: $command\n\n";
[205]1441 usage();
1442 check_env();
[252]1443 $return_code = $RETURN_NOK;
[205]1444 }
[186]1445}
[252]1446
1447exit $return_code;
Note: See TracBrowser for help on using the repository browser.