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

Last change on this file since 916 was 916, checked in by joergs, on Jul 18, 2010 at 9:20:40 PM

added plugin support

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 47.6 KB
RevLine 
[186]1#!/usr/bin/perl -w
2
3# $Id: dasscm 916 2010-07-18 19:20:40Z 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' =>
164 ["internal, used to update information about file permissions"],
[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;
[286]203my $DASSCM_PERMISSION_FILE;
[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 916 2010-07-18 19:20:40Z 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(
371 "Can't access local repository DASSCM_REPO",
372 "($DASSCM_REPO)",
373 "Check configuration and execute",
374 "dasscm init"
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;
726 if( $mode ) {
727 run_command( "stty echo" );
728 } else {
729 run_command( "stty -echo" );
730 }
731}
732
733
734sub write_array_to_file( $@ )
735{
736 my $filename = shift;
737 my @array = @_;
738
739 if ( ! -w dirname($filename) ) {
740 warning( "failed to write to $filename", "directory does not exist" );
741 return;
742 }
743
744 # directory exists => write
745 if( ! open( OUTFILE, ">$filename" ) ) {
746 warning( "failed to open $filename: $!" );
747 return;
748 }
749
750 foreach my $line (@array) {
751 print OUTFILE "$line";
752 }
753 close(OUTFILE);
754
755 return 1;
756}
757
758
759sub perform_plugins()
760{
761 check_env();
762
763 my @plugin_results = ();
764
765 # get all defined plugins.
766 # Plugin definitions starting with DASSCM_PLUGIN_
767 my @plugins = grep(/^DASSCM_PLUGIN_CMD_/, keys( %{$config} ));
768
769 for my $plugin (@plugins) {
770 my $plugin_name = substr( $plugin, length( "DASSCM_PLUGIN_CMD_" ) );
771 my $plugin_test = $config->{'DASSCM_PLUGIN_TEST_' . $plugin_name};
772 ( my $rc_test, my @result_test ) = run_command( $plugin_test );
773 if ($verbose) { print "Plugin $plugin_name: "; }
774 if( $rc_test != 0 ) {
775 if ($verbose) { print "skipped\n"; }
776 } else {
777 if ($verbose) { print "$config->{$plugin}\n"; }
778 ( my $rc, my @result ) = run_command( $config->{$plugin} );
779 if( $rc != 0 ) {
780 warning( "failed to run plugin $plugin" );
781 } else {
782 my $plugin_result_file = $DASSCM_PLUGIN_RESULTS_PATH . "/" . $plugin_name;
783 write_array_to_file( $plugin_result_file, @result );
784 push @plugin_results, $plugin_result_file;
785 }
786 }
787 }
788 return @plugin_results;
789}
790
791
792
[247]793sub svn_check_credentials( $$;$$ )
[196]794{
[205]795 my $username = shift;
796 my $password = shift;
797
[252]798 # check silently are allow user interaction?
[247]799 my $interactive = shift || 0;
[220]800
[247]801 # default: exit program, if repository is not accessable
802 # (do not exit for 'init')
803 my $fatalerror = shift || 1;
804
805 print "checking credentials ";
806
[239]807 if ( !$username ) {
808 fatalerror("no username given");
[238]809 }
810
[239]811 if ( !$password ) {
812 fatalerror("no password given");
[238]813 }
814
[247]815 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
816
[220]817 # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
818 # therefore switching to "svn status"
819 # ( my $rc_update, my @result ) =
820 # run_command(
821 # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
822 # );
823 #print @result;
824
[247]825 my $rc_update;
[252]826 if ($interactive) {
[801]827 $rc_update = run_interactive(
[267]828 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
[801]829 );
[247]830 } else {
[801]831 ( $rc_update, my @result ) = run_command(
[267]832 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
[801]833 );
[252]834
[247]835 if ( $rc_update != 0 ) {
836 print "\n", @result;
[252]837 if ($fatalerror) {
[247]838 fatalerror();
839 }
840 return;
841 }
[205]842 }
843
[247]844 # return success
845 return $rc_update == 0;
[196]846}
847
[205]848sub svn_update( ;$ )
849{
[270]850 my $update_path = shift || "";
851
[797]852 # return value
853 my $update_ok = 1;
854
[270]855 # use this flag to do only one update per run
[275]856 if ( !$svnRepositoryIsUptodate ) {
[801]857 ( my $rc_update, my @result ) = run_command(
[275]858 "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'"
[801]859 );
[268]860 print @result;
861 if ( $rc_update != 0 ) {
[290]862 error("failed to update local repository ($update_path)");
[797]863 $update_ok = 0;
[270]864 } elsif ( not $update_path ) {
[275]865
[270]866 # set this flag if a full update is done
[268]867 $svnRepositoryIsUptodate = 1;
868 }
[205]869 }
[797]870 return $update_ok;
[215]871}
[196]872
[271]873sub svn_ls( ;@ )
[215]874{
[270]875 (
876 my $basename,
877 my $dirname_prod,
878 my $dirname_repo,
879 my $filename_prod,
880 my $filename_repo
[801]881 ) = get_filenames( $_[0] );
[220]882
[218]883 # svn ls -R is better, but much, much slower
884 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
[270]885
[271]886 my @files = ();
887 my @links = ();
888 my @dirs = ();
889 my @others = ();
890
[289]891 find(
892 {
893 wanted => sub {
894 my $name = normalize_path($File::Find::name);
895 $name =~ s|^$dirname_repo||;
[290]896
[289]897 #print "($name)\n";# . $File::Find::dir . "\n";
898 if ( not $name ) {
[290]899
[289]900 # name string is empty (top directory).
901 # do nothing
902 } elsif ( $name =~ m/\.svn/ ) {
[275]903
[289]904 # skip svn meta data
905 } elsif ( -l $_ ) {
[275]906
[289]907 # soft link
908 # important: check for links first
909 # to exclude them from further checks
910 push( @links, $name );
911 } elsif ( -d $_ ) {
[290]912
913 # directories
914 push( @dirs, $name );
[289]915 } elsif ( -f $_ ) {
[275]916
[289]917 # regular file
918 push( @files, $name );
919 } else {
920 push( @others, $name );
921 }
[290]922 }
[289]923 },
924 ($filename_repo)
925 );
[271]926
[287]927 return ( sort( @dirs, @files ) );
[205]928}
929
[274]930sub svn_revert( ;$ )
931{
932 my $path = shift || $DASSCM_REPO;
933
[275]934 ( my $rc_update, my @result ) = run_command("$SVN revert -R '$path'");
[274]935
936 if ( $rc_update != 0 ) {
937 print "\n", @result;
[275]938 error("failed to revert subversion repository changes");
[274]939 }
940}
941
[285]942sub svn_remove_unknown_files( ;$ )
943{
944 my $path = shift || $DASSCM_REPO;
945
[290]946 ( my $rc_update, my @result ) = run_command("$SVN status '$path'");
[285]947
948 if ( $rc_update != 0 ) {
949 print "\n", @result;
950 error("failed to receive subversion repository information");
951 } else {
[290]952 foreach (@result) {
953 if (s/^\? +//) {
[285]954 chomp;
[290]955
[285]956 # if file is unknown to subversion (line starts with "?")
957 # remove it
958 print "removing $_\n";
[801]959
[800]960 # unlink doesn't work recursive, there "rm -rf" is used
961 #unlink($_);
[801]962 system("rm -rf $_");
[285]963 }
964 }
965 }
966}
967
[268]968sub getModifiedFiles( ;$ )
969{
[270]970 (
971 my $basename,
972 my $dirname_prod,
973 my $dirname_repo,
974 my $filename_prod,
975 my $filename_repo
[801]976 ) = get_filenames( $_[0] );
[268]977
[275]978 my @files = svn_ls($filename_prod);
[270]979
[268]980 # stores result from status (cvscheck)
981 my %removedfiles = ();
982 my %changedfiles = ();
[278]983 my %unknownfiles = ();
[268]984
985 # create list of modified files
986 if (@files) {
987
988 foreach my $file (@files) {
989
[271]990 my $realfile = $dirname_prod . $file;
991 my $cvsworkfile = $dirname_repo . $file;
[268]992
993 if ( -d $realfile ) {
[290]994
[278]995 # directory
[290]996 if ( !-d "$cvsworkfile" ) {
997
[278]998 # real is directory, repository is not. This is a problem
999 $changedfiles{"$realfile"} = $cvsworkfile;
1000 }
1001 } elsif ( !-e $realfile ) {
1002 $removedfiles{"$realfile"} = $cvsworkfile;
[268]1003 } elsif ( !-r $realfile ) {
[290]1004
[278]1005 # don't have permission to read the file,
1006 # can't check it
1007 $unknownfiles{"$realfile"} = $cvsworkfile;
[268]1008 } else {
1009 ( -r "$cvsworkfile" )
[278]1010 || fatalerror("failed to read $cvsworkfile");
[268]1011 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
1012 $changedfiles{"$realfile"} = $cvsworkfile;
1013 }
1014 }
1015 }
1016 }
1017
[278]1018 return ( \%changedfiles, \%removedfiles, \%unknownfiles );
[268]1019}
1020
[238]1021#
1022# from an array of files/dirs,
1023# generates list of files
1024# sorted by type
1025#
[237]1026sub get_files( @ )
1027{
1028 my @files = ();
1029 my @links = ();
1030 my @dirs = ();
1031 my @others = ();
1032
[239]1033 if (@_) {
1034 find(
1035 {
1036 wanted => sub {
1037 my $fullname = cwd() . "/" . $_;
1038 if ( -l $_ ) {
1039
1040 # soft link
1041 # important: check for links first
1042 # to exclude them from further checks
1043 push( @links, $fullname );
1044 } elsif ( -d $_ ) {
[271]1045
1046 # directories
[239]1047 push( @dirs, $fullname );
1048 } elsif ( -f $_ ) {
1049
1050 # regular file
1051 push( @files, $fullname );
1052 } else {
1053 push( @others, $fullname );
1054 }
1055 }
1056 },
1057 @_
1058 );
[237]1059 }
1060
[239]1061 # don't rely on others.
[237]1062 # If more specific file types are needed,
1063 # they will be added
1064 return {
[239]1065 files => \@files,
1066 links => \@links,
1067 dirs => \@dirs,
1068 others => \@others
1069 };
[237]1070}
1071
[888]1072#
1073# use globbing to get lsit of files
1074# that matches the given prefix
1075# used for bash completion
1076#
1077sub get_complete_path_globbing( $ )
1078{
1079 my $path = shift;
1080
1081 # add globbing
1082 $path .= "*";
1083
1084 # get files
[889]1085 my @files = glob($path);
[888]1086
[889]1087 if ( $#files == 0 ) {
[891]1088
1089 # if only one result is available
1090 # and this result is a directory,
1091 # add another result entry
1092 # (directory with and withour trainling /),
1093 # otherwise complete will stop here and continue with the next parameter
1094 my $path = normalize_path( $files[0] );
[889]1095 if ( -d $path ) {
[891]1096 @files = ( substr( $path, 0, -1 ), $path );
[888]1097 }
[890]1098 } else {
[891]1099
[890]1100 # add "/" to all directories
[891]1101 @files = map( { normalize_path($_) } @files );
[888]1102 }
1103
1104 return @files;
1105}
1106
[189]1107#####################################################################
1108#
[186]1109# functions
1110sub help(;@)
1111{
[893]1112 if ( not @_ ) {
[205]1113 usage();
1114 } else {
[893]1115 print "help for ", join( " ", @_ ), ": ...\n";
[214]1116 usage();
[205]1117 }
[186]1118}
1119
[203]1120sub login(@)
1121{
[205]1122 check_parameter( @_, 1 );
1123 check_env();
[203]1124
[235]1125 my $input_username = $_[0];
[214]1126
1127 if ( not $input_username ) {
1128 my $output_username = "";
1129 if ($DASSCM_USERNAME) {
1130 $output_username = " ($DASSCM_USERNAME)";
1131 }
1132
1133 print "Enter DASSCM user name", $output_username, ": ";
1134 $input_username = <STDIN>;
1135 chomp($input_username);
[247]1136
1137 $input_username = $input_username || $DASSCM_USERNAME;
[205]1138 }
[203]1139
[205]1140 # hidden password input
[916]1141 print "Enter password for $input_username: ";
1142 setEchoMode( 0 );
[205]1143 my $input_password = <STDIN>;
[916]1144 setEchoMode( 1 );
[205]1145 chomp($input_password);
[220]1146 print "\n";
[203]1147
[247]1148 # checking checkout username/password
[252]1149 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
1150 $DASSCM_CHECKOUT_PASSWORD );
[247]1151 print "checkout access okay\n";
[205]1152
[247]1153 svn_check_credentials( $input_username, $input_password );
1154
[205]1155 #
1156 # set environment variables
1157 #
[267]1158 $ENV{'DASSCM_USERNAME'} = "$input_username";
1159 $ENV{'DASSCM_PASSWORD'} = "$input_password";
[205]1160
[209]1161 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
1162 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
1163 "DASSCM_REPO: $DASSCM_REPO\n",
[278]1164 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
[205]1165
[278]1166 status();
1167
1168 print "\n[dasscm shell]\n\n";
[235]1169 my $shell = $SHELL || "bash";
1170 exec($shell) or die "failed to start new shell";
[203]1171}
1172
[260]1173#
1174# initialize local checkout directory (initial checkout)
1175#
[205]1176sub init(@)
1177{
1178 check_parameter( @_, 1 );
1179 check_env();
1180
[235]1181 # don't do repository creation (svn mkdir) here,
1182 # because then their must be a lot of prior checks
1183
[205]1184 # update complete repository
[216]1185 # and create permission file
[801]1186 my $retcode = run_interactive(
[286]1187 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; mkdir -p `dirname $DASSCM_PERMISSION_FILE`; touch $DASSCM_PERMISSION_FILE"
[801]1188 );
[205]1189}
1190
[215]1191sub ls(@)
[186]1192{
[205]1193 check_parameter( @_, 1 );
1194 check_env();
[186]1195
[271]1196 my @files = svn_ls(@_);
[215]1197
[275]1198 if (@files) {
[274]1199 print join( "\n", @files );
1200 print "\n";
1201 }
[215]1202}
1203
1204sub update(@)
1205{
1206 check_parameter( @_, 1 );
1207 check_env();
1208
1209 #
1210 # update local repository
1211 #
1212 svn_update();
1213}
1214
[237]1215#
1216# helper function for "add" command
1217#
[215]1218sub add_helper(@)
1219{
[205]1220 (
1221 my $basename,
1222 my $dirname_prod,
1223 my $dirname_repo,
1224 my $filename_prod,
1225 my $filename_repo
[801]1226 ) = get_filenames( $_[0] );
[186]1227
[274]1228 mkpath($dirname_repo);
[889]1229 copy_file_to_repository($filename_prod);
[186]1230
[274]1231 # already checked in?
1232 chdir $DASSCM_REPO;
[205]1233
[274]1234 # also add the path to filename.
1235 for my $dir ( split( '/', $dirname_prod ) ) {
1236 if ($dir) {
[275]1237 my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
[274]1238 if ( $rc > 0 ) {
1239 print join( "\n", @out );
[205]1240 }
[274]1241 chdir $dir;
[205]1242 }
1243 }
[275]1244 my ( $rc, @out ) = run_command("$SVN add '$basename'");
[274]1245 if ( $rc > 0 ) {
1246 print join( "\n", @out );
1247 }
1248 chdir $StartDirectory;
1249
[215]1250}
[205]1251
[215]1252#
[274]1253# adding new files (or directories)
[215]1254#
1255sub add(@)
1256{
1257 check_parameter( @_, 1 );
1258 check_env();
1259
1260 #
1261 # update local repository
1262 #
1263 svn_update();
1264
[237]1265 # get all regular files and links
[239]1266 my $href_files = get_files(@_);
[220]1267
[237]1268 #print Dumper( $href_files );
1269
[239]1270 my @files = @{ $href_files->{files} };
1271 my @links = @{ $href_files->{links} };
[237]1272
[239]1273 if (@files) {
[237]1274 my $number = $#files + 1;
1275 print "files to check-in ($number): \n";
1276 print join( "\n", @files );
1277 print "\n";
1278 }
1279
[268]1280 # TODO: check in links and also link target? At least warn about link target
[239]1281 if (@links) {
[237]1282 my $number = $#links + 1;
1283 print "\n";
1284 print "ignoring links ($number):\n";
1285 print join( "\n", @links );
1286 print "\n";
1287 }
1288
[238]1289 # TODO: confirm
1290
[237]1291 # copy files one by one to local repository
1292 for my $file (@files) {
[239]1293
[233]1294 # add file
[239]1295 add_helper($file);
[233]1296 }
1297
[215]1298 # create new permissions file
1299 permissions();
[220]1300
[215]1301 # add permissions file
[286]1302 add_helper($DASSCM_PERMISSION_FILE);
[215]1303
[916]1304 for my $file ( perform_plugins() ) {
1305 add_helper($file);
1306 }
1307
1308
[205]1309 if ( $options{'message'} ) {
1310 $svnOptions .= " --message \"$options{'message'}\" ";
1311 }
1312
[239]1313 # commit calls $EDITOR.
[237]1314 # use "interactive" here, to display output
[801]1315 my $retcode = run_interactive(
[267]1316 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
[801]1317 );
[205]1318
[274]1319 # svn commit does not deliever an error return code, if commit is canceld,
1320 # so a revert is performed in any case
1321 svn_revert();
[186]1322}
1323
[271]1324#
1325# checks in all modified files
1326#
1327sub commit(@)
1328{
1329 check_parameter( @_, 1 );
1330 check_env();
1331
1332 (
1333 my $basename,
1334 my $dirname_prod,
1335 my $dirname_repo,
1336 my $filename_prod,
1337 my $filename_repo
[801]1338 ) = get_filenames( $_[0] );
[271]1339
1340 #
1341 # update local repository
1342 #
1343 svn_update();
1344
[275]1345 ( my $refChangedFiles, my $refRemovedFiles ) =
1346 getModifiedFiles($filename_prod);
[271]1347 my %changedfiles = %{$refChangedFiles};
1348 my %removedfiles = %{$refRemovedFiles};
1349
[275]1350 if (%removedfiles) {
1351 my $removedFilesString =
1352 '"' . join( '" "', values(%removedfiles) ) . '"';
1353 my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
[271]1354 if ( $rc > 0 ) {
1355 print join( "\n", @out );
1356 }
1357 }
1358
1359 # copy files one by one to local repository
1360 for my $file ( keys(%changedfiles) ) {
[275]1361 copy_file_to_repository($file);
[271]1362 }
1363
1364 # create new permissions file
1365 permissions();
1366
1367 # add permissions file
[286]1368 add_helper($DASSCM_PERMISSION_FILE);
[271]1369
[916]1370 for my $file ( perform_plugins() ) {
1371 add_helper($file);
1372 }
1373
1374
1375
[271]1376 if ( $options{'message'} ) {
1377 $svnOptions .= " --message \"$options{'message'}\" ";
1378 }
1379
1380 # commit calls $EDITOR.
1381 # use "interactive" here, to display output
[801]1382 my $retcode = run_interactive(
[271]1383 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
[801]1384 );
[274]1385
1386 # svn commit does not deliever an error return code, if commit is canceld,
1387 # so a revert is performed in any case
1388 svn_revert();
[271]1389}
1390
[800]1391#
1392# revert: copies files back from repository to system
1393#
1394sub revert(@)
1395{
1396 check_parameter( @_, 1 );
1397 check_env();
1398
1399 (
1400 my $basename,
1401 my $dirname_prod,
1402 my $dirname_repo,
1403 my $filename_prod,
1404 my $filename_repo
[801]1405 ) = get_filenames( $_[0] );
[800]1406
1407 # return code for the shell
1408 # default: error
1409 my $return_code = $RETURN_OK;
1410
1411 # cleanup repository
[801]1412 cleanup();
[889]1413
[800]1414 #svn_update();
1415
1416 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1417 getModifiedFiles($filename_prod);
1418 my %changedfiles = %{$refChangedFiles};
1419 my %removedfiles = %{$refRemovedFiles};
1420 my %unknownfiles = %{$refUnknownFiles};
1421
1422 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1423
1424 if (%removedfiles) {
1425 print "DELETED files and directories. Recreated from repository:\n";
[801]1426 my @removedPaths =
1427 ( sort { length $a > length $b } keys %removedfiles );
[800]1428 print join( "\n", @removedPaths ) . "\n\n";
1429
1430 # copy files one by one from local repository to system
1431 # and also create directories
1432 # paths are sorted, so that directories are created first
[801]1433 for my $real_path (@removedPaths) {
1434 if ( -d $removedfiles{"$real_path"} ) {
[800]1435 mkpath("$real_path");
1436 } else {
[889]1437 copy_file_from_repository_to_system($real_path);
[800]1438 }
1439 }
1440 }
1441
1442 if (%changedfiles) {
1443 print "MODIFIED files. Copied from repository to the system:\n";
1444 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1445
1446 # copy files one by one from local repository to system
1447 for my $real_file ( keys(%changedfiles) ) {
[889]1448 copy_file_from_repository_to_system($real_file);
[800]1449 }
1450
1451 }
1452
1453 if (%unknownfiles) {
1454 print "UNKNOWN: insufficient permission to check files:\n";
1455 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1456
1457 $return_code = $RETURN_NOK;
1458 }
1459
1460 } else {
1461 print "no modified files found in $dirname_repo\n";
1462 }
1463
1464 return $return_code;
1465}
1466
[193]1467sub blame(@)
1468{
[205]1469 check_parameter( @_, 1 );
1470 check_env();
[193]1471
[205]1472 (
1473 my $basename,
1474 my $dirname_prod,
1475 my $dirname_repo,
1476 my $filename_prod,
1477 my $filename_repo
[801]1478 ) = get_filenames( $_[0] );
[205]1479
1480 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
[193]1481}
1482
[187]1483sub diff(@)
1484{
[205]1485 check_parameter( @_, 1 );
1486 check_env();
[187]1487
[205]1488 (
1489 my $basename,
1490 my $dirname_prod,
1491 my $dirname_repo,
1492 my $filename_prod,
1493 my $filename_repo
[801]1494 ) = get_filenames( $_[0] );
[205]1495
1496 #print "$basename,$dirname_prod,$dirname_repo\n";
1497
[797]1498 svn_update();
[205]1499
[238]1500 ( my $rc_diff, my @diff_result ) =
[239]1501 run_command( $diff . " $filename_repo $filename_prod" );
[238]1502
1503 print @diff_result;
[187]1504}
1505
[209]1506sub status(@)
1507{
1508 check_parameter( @_, 1 );
1509 check_env();
1510
[270]1511 (
1512 my $basename,
1513 my $dirname_prod,
1514 my $dirname_repo,
1515 my $filename_prod,
1516 my $filename_repo
[801]1517 ) = get_filenames( $_[0] || "/" );
[270]1518
[252]1519 # return code for the shell
1520 # default: error
1521 my $return_code = $RETURN_NOK;
1522
[209]1523 #
1524 # update local repository
1525 #
[278]1526 #svn_update( $filename_prod );
[209]1527
[278]1528 # check, if permissions have changed
1529 permissions();
[916]1530 perform_plugins();
[278]1531
1532 # get modified files
1533 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
[275]1534 getModifiedFiles($dirname_prod);
[268]1535 my %changedfiles = %{$refChangedFiles};
1536 my %removedfiles = %{$refRemovedFiles};
[278]1537 my %unknownfiles = %{$refUnknownFiles};
[209]1538
[278]1539 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1540
[215]1541 if (%removedfiles) {
[278]1542 print "DELETED: files found in repository, but not in system:\n";
[800]1543 print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
[209]1544 }
1545
[215]1546 if (%changedfiles) {
[278]1547 print "MODIFIED: files differs between repository and system:\n";
1548 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
[209]1549 }
[278]1550
1551 if (%unknownfiles) {
1552 print "UNKNOWN: insufficient permission to check files:\n";
1553 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1554 }
1555
[209]1556 } else {
[270]1557 print "no modified files found in $dirname_repo\n";
[252]1558 $return_code = $RETURN_OK;
[209]1559 }
[215]1560
[252]1561 return $return_code;
[215]1562}
[209]1563
[277]1564#
1565# return short status in Nagios plugin conform way
1566#
1567sub check()
1568{
1569 check_env();
1570
1571 # return code for the shell
[290]1572 my $return_code = $RETURN_OK;
[277]1573 my $return_string = "OK: no modified files";
1574
[278]1575 # check, if permissions have changed
1576 permissions();
[916]1577 perform_plugins();
[278]1578
1579 # get modified files
1580 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
[290]1581 getModifiedFiles("/");
[277]1582 my %changedfiles = %{$refChangedFiles};
1583 my %removedfiles = %{$refRemovedFiles};
[278]1584 my %unknownfiles = %{$refUnknownFiles};
[277]1585
1586 if ( %removedfiles or %changedfiles ) {
1587 $return_string = "Warning: ";
[290]1588 if (%changedfiles) {
1589 $return_string .=
1590 "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
[277]1591 }
[290]1592 if (%removedfiles) {
1593 $return_string .=
1594 "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
[277]1595 }
[278]1596 if (%unknownfiles) {
[290]1597 $return_string .=
1598 "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
[278]1599 }
[277]1600 $return_code = $RETURN_WARN;
1601 }
1602
1603 # addition nagios Service Status
1604 #Critical
1605 #Unknown
1606
[290]1607 print "$return_string\n";
[277]1608 return $return_code;
1609}
1610
[270]1611sub permissions()
[215]1612{
1613 check_env();
1614
[278]1615 my $return_code = $RETURN_OK;
1616
[215]1617 #
1618 # update local repository
1619 #
1620 #svn_update();
1621
[220]1622 my $dir = $DASSCM_REPO;
[275]1623 my @files = svn_ls("/");
[215]1624
1625 if (@files) {
1626
1627 # generieren der Permissions
1628 my @permissions = generatePermissionList(@files);
1629 my $OUTFILE;
1630 my $tofile = 0; # Status für schreiben in File
[220]1631
[286]1632 if ( -w dirname($DASSCM_PERMISSION_FILE) ) {
[215]1633
1634 # Verzeichnis existiert => schreiben
[286]1635 open( OUTFILE, ">$DASSCM_PERMISSION_FILE" )
1636 || die("failed to write to $DASSCM_PERMISSION_FILE: $!");
[215]1637 $tofile = 1; # Merken, daß in File geschrieben wird
1638 print OUTFILE "#\n";
1639 print OUTFILE "# created by dasscm permissions\n";
[220]1640 print OUTFILE
1641 "# It is intended to be used for restoring permissions\n";
[287]1642 print OUTFILE "#\n";
[215]1643 } else {
1644
[290]1645 if ( $command eq "permission" ) {
1646
[278]1647 # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
1648 # Alias Filehandle für stdout erzeugen
1649 $return_code = $RETURN_WARN;
[290]1650 *OUTFILE = *STDOUT;
[278]1651 } else {
[290]1652
[278]1653 # TODO: improve this. Check for diff?
1654 $return_code = $RETURN_CRIT;
1655 return $return_code;
1656 }
[215]1657 }
[278]1658
[215]1659 foreach my $line (@permissions) {
1660 print OUTFILE "$line\n";
1661 }
1662
[220]1663 if ($tofile) {
[215]1664 close(OUTFILE);
1665 }
1666 }
[278]1667
1668 return $return_code;
[209]1669}
1670
[274]1671#
1672# remove all uncommited changes in the repository
1673#
1674sub cleanup()
1675{
1676 check_env();
[268]1677
[275]1678 svn_revert($DASSCM_REPO);
[285]1679 svn_remove_unknown_files($DASSCM_REPO);
[274]1680}
1681
[884]1682#
1683# used for bash completion
1684# prints the next possible command line parameters
1685#
1686sub complete(@)
1687{
[889]1688 my @input = @_;
[894]1689 my %options_complete = ();
[884]1690
[894]1691 # check and remove global options. if options are wrong, nothing to do
[907]1692 @ARGV = @input;
[914]1693 if ( GetOptions( \%options_complete, @OPTIONS_GLOBAL ) ) {
[894]1694 my $number_arguments = @input;
1695 if ( $number_arguments <= 1 ) {
[893]1696
[894]1697 # complete dasscm commands
1698 my $input = $input[0] || "";
1699 map { m/^$input/ && print $_, "\n" } ( keys %COMMANDS );
1700 } else {
[914]1701
[894]1702 # complete dasscm parameter
1703 my $command = get_command_uniform_name( $input[0] );
[914]1704 if ($command) {
1705
[895]1706 # remove command
1707 shift @input;
[887]1708
[895]1709 # check and remove options
[914]1710 my @options = get_command_possible_options($command);
[907]1711 @ARGV = @input;
[914]1712 if ( ( not @options )
1713 || ( GetOptions( \%options_complete, @options ) ) )
1714 {
[895]1715
[914]1716 my @params = get_command_possible_params($command);
[895]1717 if ($verbose) { print "params: ", Dumper(@params); }
1718
1719 my $number_arguments = @input;
1720
1721 #print "input: ", join( ",", @input ), " (", $number_arguments, ")\n";
1722
[914]1723 if ( $number_arguments > 0 ) {
[895]1724 my $parameter_number = $number_arguments - 1;
1725 if ( defined( $params[$parameter_number] )
1726 && $params[$parameter_number] )
1727 {
1728 my $param = $params[$parameter_number];
[914]1729 if ($verbose) {
1730 print "param used: ", $param, "\n";
1731 }
[895]1732 if ( $param eq "PATH_PROD" ) {
[914]1733 complete_path(
1734 $input[ $number_arguments - 1 ] );
[895]1735 } elsif ( $param eq "PATH_REPO" ) {
[914]1736 complete_repopath(
1737 $input[ $number_arguments - 1 ] );
[895]1738 }
1739 }
1740 }
[894]1741 }
[887]1742 }
[884]1743 }
1744 }
1745}
1746
[887]1747sub complete_path(@)
1748{
1749 check_parameter( @_, 1 );
1750 check_env();
1751
1752 (
1753 my $basename,
1754 my $dirname_prod,
1755 my $dirname_repo,
1756 my $filename_prod,
1757 my $filename_repo
1758 ) = get_filenames( $_[0] );
1759
[889]1760 my @files = get_complete_path_globbing($filename_prod);
[887]1761
1762 if (@files) {
1763 print join( "\n", @files );
1764 print "\n";
1765 }
1766}
1767
1768sub complete_repopath(@)
1769{
1770 check_parameter( @_, 1 );
1771 check_env();
1772
1773 (
1774 my $basename,
1775 my $dirname_prod,
1776 my $dirname_repo,
1777 my $filename_prod,
1778 my $filename_repo
1779 ) = get_filenames( $_[0] );
1780
[889]1781 my @files = get_complete_path_globbing($filename_repo);
1782
[887]1783 if (@files) {
[889]1784
[887]1785 # remove DASSCM_REPO path again
[889]1786 print join(
1787 "\n",
1788 map( {
1789 s|^${DASSCM_REPO}|/|;
1790 $_
1791 } @files )
1792 );
[887]1793 print "\n";
1794 }
1795
1796}
1797
[189]1798#####################################################################
1799#
[186]1800# main
[189]1801#
[186]1802
[252]1803my $return_code = $RETURN_OK;
[186]1804my $number_arguments = @ARGV;
1805
[893]1806# global options
1807# stops at first non-option
[914]1808Getopt::Long::Configure('require_order');
1809if ( not GetOptions( \%options, @OPTIONS_GLOBAL ) ) {
[893]1810 usage();
1811 exit $RETURN_NOK;
1812}
1813
1814# set verbose to command line option
1815$verbose = $options{'verbose'};
1816
[914]1817if ( $options{'help'} ) {
[893]1818 help(@ARGV);
1819 exit;
1820}
1821
[892]1822# get subcommand and remove it from @ARGV
[914]1823if ( defined( $ARGV[0] ) ) {
[889]1824 $command = get_command_uniform_name( $ARGV[0] );
[205]1825 shift @ARGV;
[892]1826}
[893]1827
[914]1828if ( not defined($command) ) {
1829 usage();
1830 exit $RETURN_NOK;
[893]1831}
[196]1832
[893]1833$DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1834$DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
[205]1835
[916]1836$DASSCM_PLUGIN_RESULTS_PATH = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'} . "/" . "plugin-results/";
1837
[893]1838# TODO: check variables
1839$DASSCM_SVN_REPOSITORY =
[914]1840 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
[205]1841
[893]1842$DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1843$DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
[205]1844
[893]1845#
1846# if a user is given by dasscm configuration file, we use it.
1847# Otherwise we expect that read-only account is configured
1848# as local subversion configuration.
1849# If this is also not the case,
1850# user is required to type username and password.
1851# This will be stored as local subversion configuration thereafter.
1852#
1853if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
1854 $svnCheckoutCredentials =
[914]1855 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
[893]1856}
[286]1857
[893]1858$DASSCM_PERMISSION_FILE = $config->{'DASSCM_PERMISSION_FILE'}
[914]1859 || "/etc/permissions.d/dasscm.permission_backup";
[893]1860
1861# check for command options
[914]1862my @cmd_options = get_command_possible_options($command);
1863if (@cmd_options) {
1864
[205]1865 # get command line options and store them in options hash
[893]1866 my $result = GetOptions( \%options, @cmd_options );
[205]1867
1868 # print options
1869 foreach my $option ( keys %options ) {
[215]1870 print "${option}: $options{$option}\n";
[205]1871 }
[893]1872}
[205]1873
[893]1874#
1875# action accordinly to command are taken
1876#
1877&{ get_command_function($command) }(@ARGV);
[214]1878
[252]1879exit $return_code;
Note: See TracBrowser for help on using the repository browser.