source: trunk/dasscm/dasscm@ 286

Last change on this file since 286 was 286, checked in by joergs, on Mar 9, 2009 at 5:39:15 PM

make permission file configuable

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