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
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 803 2009-11-18 15:28:12Z 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;
13# used system("cp -a"), because File::Copy does not keep permissions
14#use File::Copy;
15use File::Find;
16use File::stat;
17use File::Path;
18use Term::ReadKey;
19
20#use Data::Dumper;
21
22#####################################################################
23#
24# global
25#
26
27# shell exit codes
28my $RETURN_OK = 0;
29my $RETURN_NOK = 1;
30
31# Nagios return codes
32my $RETURN_WARN = 1;
33my $RETURN_CRIT = 2;
34my $RETURN_UNKNOWN = 3;
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 803 2009-11-18 15:28:12Z 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 " revert <path>\n";
95 print " diff <path>\n";
96 print " status <path>\n";
97 print " check\n";
98 print " cleanup\n";
99 print " permissions\n";
100 print "\n";
101 print "If dasscm is not yet configured, read $doc_file\n";
102}
103
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{
116 error(@_);
117
118 #print "Exiting\n";
119 exit 1;
120}
121
122#
123# reading config file and return key/value pairs as hash
124#
125sub get_config
126{
127 my $file = $_[0];
128
129 if ( !$file ) {
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 ) {
145
146 # splitting in 2 fields at maximum
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
166#
167# check and evaluate environment variables
168#
169sub check_env()
170{
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
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 }
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 }
198 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
199
200 #
201 # subversion checkout user
202 #
203 if ( !$DASSCM_CHECKOUT_USERNAME ) {
204 fatalerror(
205 "variable DASSCM_CHECKOUT_USERNAME is not defined.",
206 "Use file $config_file to configure it."
207 );
208 }
209
210 if ( !$DASSCM_CHECKOUT_PASSWORD ) {
211 fatalerror(
212 "variable DASSCM_CHECKOUT_PASSWORD is not defined.",
213 "Use file $config_file to configure it."
214 );
215 }
216
217 #
218 # check if local repository directory exist
219 # (if not creating by init)
220 #
221 if ( $command ne "init" ) {
222 if ( not -d $DASSCM_REPO ) {
223 die
224 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
225 }
226
227 #
228 # user settings
229 #
230
231 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
232 if ( !$DASSCM_USERNAME ) {
233 $DASSCM_USERNAME = $DASSCM_USER;
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" ) {
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 );
250 }
251 $svnOptions .= " --no-auth-cache ";
252 } elsif ( !$DASSCM_USERNAME ) {
253 $DASSCM_USERNAME = $USER;
254 }
255
256 #
257 # password
258 #
259 if ($DASSCM_PASSWORD) {
260 $svnPasswordCredentials = " --password '$DASSCM_PASSWORD' ";
261 }
262 }
263
264 #$svnOptions .= " --username $DASSCM_USERNAME "
265}
266
267#
268# has been intendend,
269# to check addtitional parameters.
270# Currently not used.
271#
272sub check_parameter(@)
273{
274}
275
276#
277# normalize path namens:
278# - directories should end with "/"
279# - use only single "/"
280#
281sub normalize_path($)
282{
283 my $path = shift || "";
284
285 if ( $path =~ m|^/| ) {
286
287 # full path
288 if ( -d $path ) {
289
290 # ensure, a directory ends with '/'
291 $path .= '/';
292 }
293 } elsif ( -d cwd() . '/' . $path ) {
294
295 # ensure, a directory ends with '/'
296 $path .= '/';
297 }
298
299 # remove double (triple) slashes (/)
300 $path =~ s|/[/]*|/|g;
301
302 return $path;
303}
304
305#
306# generate from (relative) filename
307# all required file and directory names:
308# $basename, $dirname_prod, $dirname_repo,
309# $filename_prod, $filename_repo
310#
311sub get_filenames(@)
312{
313 my $filename_prod = $_[0] || ".";
314
315 # make filename absolut
316 if ( !( $filename_prod =~ m/^\// ) ) {
317 $filename_prod = cwd() . '/' . $filename_prod;
318 }
319
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 }
325 }
326
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 "/"
331 $filename_prod = normalize_path($filename_prod);
332
333 ( my $basename, my $dirname_prod ) = fileparse($filename_prod);
334
335 # normalize path.
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" ) {
339
340 # uses chdir to determine real directory in a unique way
341 chdir $dirname_prod
342 or fatalerror( "failed to access directory $dirname_prod: " . $! );
343 $dirname_prod = normalize_path( cwd() );
344 chdir $StartDirectory;
345 }
346
347 my $dirname_repo = normalize_path( $DASSCM_REPO . "/" . $dirname_prod );
348 my $filename_repo = normalize_path("$dirname_repo/$basename");
349
350 if ($verbose) {
351 print "filename_repo: " . $filename_repo . "\n";
352 print "dirname_repo: " . $dirname_repo . "\n";
353 print "filename_prod: " . $filename_prod . "\n";
354 print "dirname_prod: " . $dirname_prod . "\n";
355 print "basename: " . $basename . "\n";
356 }
357
358 return (
359 $basename, $dirname_prod, $dirname_repo,
360 $filename_prod, $filename_repo
361 );
362}
363
364sub copy_file_to_repository( $ )
365{
366 my $filename = shift;
367
368 (
369 my $basename,
370 my $dirname_prod,
371 my $dirname_repo,
372 my $filename_prod,
373 my $filename_repo
374 ) = get_filenames($filename);
375
376 #copy( $filename_prod, $filename_repo )
377 ( my $rc, my @result ) = run_command( "cp -a \"$filename_prod\" \"$filename_repo\"" );
378 if( $rc != 0 ) {
379 error( "failed to copy $filename_prod to repository: ", @result );
380 }
381
382 # return success
383 return $rc == 0;
384}
385
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
400 ( my $rc, my @result ) = run_command( "cp -a \"$filename_repo\" \"$filename_prod\"" );
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
411#
412# creates a file with permissions
413#
414sub generatePermissionList
415{
416
417 # generieren der Zeilen für Permission-Savefile
418 my @files = @_;
419 my @permlist = ();
420 foreach my $file (@files) {
421 $file = "/" . $file;
422 if ( -e $file ) {
423 my $info = stat($file) || die "failed to stat $file: aborting";
424 my $mode = get_type( $info->mode ) & 07777;
425 my $modestring = sprintf( "%04o", $mode );
426 my $uidnumber = $info->uid;
427 my $uid = getpwuid($uidnumber) || $uidnumber;
428 my $gidnumber = $info->gid;
429 my $gid = getgrgid($gidnumber) || $gidnumber;
430 push(
431 @permlist,
432 sprintf( "%-55s %-17s %4d",
433 $file, "${uid}:${gid}", $modestring )
434 );
435 }
436 }
437 return @permlist;
438}
439
440sub get_type
441{
442
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
452
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;
462}
463
464sub run_command
465{
466 my $command = shift;
467
468 #print "executing command: " . $command . "\n";
469
470 open( RESULT, $command . ' 2>&1 |' );
471 my @result = <RESULT>;
472 close(RESULT);
473 my $retcode = $? >> 8;
474
475 #print @result;
476 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
477
478 return ( $retcode, @result );
479}
480
481sub run_interactive
482{
483
484 if ($verbose) {
485 print "run_interactive:" . join( " ", @_ ) . "\n";
486 }
487
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
500sub svn_check_credentials( $$;$$ )
501{
502 my $username = shift;
503 my $password = shift;
504
505 # check silently are allow user interaction?
506 my $interactive = shift || 0;
507
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
514 if ( !$username ) {
515 fatalerror("no username given");
516 }
517
518 if ( !$password ) {
519 fatalerror("no password given");
520 }
521
522 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
523
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
532 my $rc_update;
533 if ($interactive) {
534 $rc_update = run_interactive(
535 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
536 );
537 } else {
538 ( $rc_update, my @result ) = run_command(
539 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
540 );
541
542 if ( $rc_update != 0 ) {
543 print "\n", @result;
544 if ($fatalerror) {
545 fatalerror();
546 }
547 return;
548 }
549 }
550
551 # return success
552 return $rc_update == 0;
553}
554
555sub svn_update( ;$ )
556{
557 my $update_path = shift || "";
558
559 # return value
560 my $update_ok = 1;
561
562 # use this flag to do only one update per run
563 if ( !$svnRepositoryIsUptodate ) {
564 ( my $rc_update, my @result ) = run_command(
565 "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'"
566 );
567 print @result;
568 if ( $rc_update != 0 ) {
569 error("failed to update local repository ($update_path)");
570 $update_ok = 0;
571 } elsif ( not $update_path ) {
572
573 # set this flag if a full update is done
574 $svnRepositoryIsUptodate = 1;
575 }
576 }
577 return $update_ok;
578}
579
580sub svn_ls( ;@ )
581{
582 (
583 my $basename,
584 my $dirname_prod,
585 my $dirname_repo,
586 my $filename_prod,
587 my $filename_repo
588 ) = get_filenames( $_[0] );
589
590 # svn ls -R is better, but much, much slower
591 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
592
593 my @files = ();
594 my @links = ();
595 my @dirs = ();
596 my @others = ();
597
598 find(
599 {
600 wanted => sub {
601 my $name = normalize_path($File::Find::name);
602 $name =~ s|^$dirname_repo||;
603
604 #print "($name)\n";# . $File::Find::dir . "\n";
605 if ( not $name ) {
606
607 # name string is empty (top directory).
608 # do nothing
609 } elsif ( $name =~ m/\.svn/ ) {
610
611 # skip svn meta data
612 } elsif ( -l $_ ) {
613
614 # soft link
615 # important: check for links first
616 # to exclude them from further checks
617 push( @links, $name );
618 } elsif ( -d $_ ) {
619
620 # directories
621 push( @dirs, $name );
622 } elsif ( -f $_ ) {
623
624 # regular file
625 push( @files, $name );
626 } else {
627 push( @others, $name );
628 }
629 }
630 },
631 ($filename_repo)
632 );
633
634 return ( sort( @dirs, @files ) );
635}
636
637sub svn_revert( ;$ )
638{
639 my $path = shift || $DASSCM_REPO;
640
641 ( my $rc_update, my @result ) = run_command("$SVN revert -R '$path'");
642
643 if ( $rc_update != 0 ) {
644 print "\n", @result;
645 error("failed to revert subversion repository changes");
646 }
647}
648
649sub svn_remove_unknown_files( ;$ )
650{
651 my $path = shift || $DASSCM_REPO;
652
653 ( my $rc_update, my @result ) = run_command("$SVN status '$path'");
654
655 if ( $rc_update != 0 ) {
656 print "\n", @result;
657 error("failed to receive subversion repository information");
658 } else {
659 foreach (@result) {
660 if (s/^\? +//) {
661 chomp;
662
663 # if file is unknown to subversion (line starts with "?")
664 # remove it
665 print "removing $_\n";
666
667 # unlink doesn't work recursive, there "rm -rf" is used
668 #unlink($_);
669 system("rm -rf $_");
670 }
671 }
672 }
673}
674
675sub getModifiedFiles( ;$ )
676{
677 (
678 my $basename,
679 my $dirname_prod,
680 my $dirname_repo,
681 my $filename_prod,
682 my $filename_repo
683 ) = get_filenames( $_[0] );
684
685 my @files = svn_ls($filename_prod);
686
687 # stores result from status (cvscheck)
688 my %removedfiles = ();
689 my %changedfiles = ();
690 my %unknownfiles = ();
691
692 # create list of modified files
693 if (@files) {
694
695 foreach my $file (@files) {
696
697 my $realfile = $dirname_prod . $file;
698 my $cvsworkfile = $dirname_repo . $file;
699
700 if ( -d $realfile ) {
701
702 # directory
703 if ( !-d "$cvsworkfile" ) {
704
705 # real is directory, repository is not. This is a problem
706 $changedfiles{"$realfile"} = $cvsworkfile;
707 }
708 } elsif ( !-e $realfile ) {
709 $removedfiles{"$realfile"} = $cvsworkfile;
710 } elsif ( !-r $realfile ) {
711
712 # don't have permission to read the file,
713 # can't check it
714 $unknownfiles{"$realfile"} = $cvsworkfile;
715 } else {
716 ( -r "$cvsworkfile" )
717 || fatalerror("failed to read $cvsworkfile");
718 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
719 $changedfiles{"$realfile"} = $cvsworkfile;
720 }
721 }
722 }
723 }
724
725 return ( \%changedfiles, \%removedfiles, \%unknownfiles );
726}
727
728#
729# from an array of files/dirs,
730# generates list of files
731# sorted by type
732#
733sub get_files( @ )
734{
735 my @files = ();
736 my @links = ();
737 my @dirs = ();
738 my @others = ();
739
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 $_ ) {
752
753 # directories
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 );
766 }
767
768 # don't rely on others.
769 # If more specific file types are needed,
770 # they will be added
771 return {
772 files => \@files,
773 links => \@links,
774 dirs => \@dirs,
775 others => \@others
776 };
777}
778
779#####################################################################
780#
781# functions
782
783sub help(;@)
784{
785 if ( @_ == 0 ) {
786 usage();
787 } else {
788 print "help for @_: ...\n";
789 usage();
790 }
791}
792
793sub login(@)
794{
795 check_parameter( @_, 1 );
796 check_env();
797
798 my $input_username = $_[0];
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);
809
810 $input_username = $input_username || $DASSCM_USERNAME;
811 }
812
813 # hidden password input
814 print "Enter password for $input_username: ";
815 ReadMode('noecho');
816 my $input_password = <STDIN>;
817 ReadMode('normal');
818 chomp($input_password);
819 print "\n";
820
821 # checking checkout username/password
822 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
823 $DASSCM_CHECKOUT_PASSWORD );
824 print "checkout access okay\n";
825
826 svn_check_credentials( $input_username, $input_password );
827
828 #
829 # set environment variables
830 #
831 $ENV{'DASSCM_USERNAME'} = "$input_username";
832 $ENV{'DASSCM_PASSWORD'} = "$input_password";
833
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",
837 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
838
839 status();
840
841 print "\n[dasscm shell]\n\n";
842 my $shell = $SHELL || "bash";
843 exec($shell) or die "failed to start new shell";
844}
845
846#
847# initialize local checkout directory (initial checkout)
848#
849sub init(@)
850{
851 check_parameter( @_, 1 );
852 check_env();
853
854 # don't do repository creation (svn mkdir) here,
855 # because then their must be a lot of prior checks
856
857 # update complete repository
858 # and create permission file
859 my $retcode = run_interactive(
860 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; mkdir -p `dirname $DASSCM_PERMISSION_FILE`; touch $DASSCM_PERMISSION_FILE"
861 );
862}
863
864sub ls(@)
865{
866 check_parameter( @_, 1 );
867 check_env();
868
869 my @files = svn_ls(@_);
870
871 if (@files) {
872 print join( "\n", @files );
873 print "\n";
874 }
875}
876
877sub update(@)
878{
879 check_parameter( @_, 1 );
880 check_env();
881
882 #
883 # update local repository
884 #
885 svn_update();
886}
887
888#
889# helper function for "add" command
890#
891sub add_helper(@)
892{
893 (
894 my $basename,
895 my $dirname_prod,
896 my $dirname_repo,
897 my $filename_prod,
898 my $filename_repo
899 ) = get_filenames( $_[0] );
900
901 mkpath($dirname_repo);
902 copy_file_to_repository( $filename_prod );
903
904 # already checked in?
905 chdir $DASSCM_REPO;
906
907 # also add the path to filename.
908 for my $dir ( split( '/', $dirname_prod ) ) {
909 if ($dir) {
910 my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
911 if ( $rc > 0 ) {
912 print join( "\n", @out );
913 }
914 chdir $dir;
915 }
916 }
917 my ( $rc, @out ) = run_command("$SVN add '$basename'");
918 if ( $rc > 0 ) {
919 print join( "\n", @out );
920 }
921 chdir $StartDirectory;
922
923}
924
925#
926# adding new files (or directories)
927#
928sub add(@)
929{
930 check_parameter( @_, 1 );
931 check_env();
932
933 #
934 # update local repository
935 #
936 svn_update();
937
938 # get all regular files and links
939 my $href_files = get_files(@_);
940
941 #print Dumper( $href_files );
942
943 my @files = @{ $href_files->{files} };
944 my @links = @{ $href_files->{links} };
945
946 if (@files) {
947 my $number = $#files + 1;
948 print "files to check-in ($number): \n";
949 print join( "\n", @files );
950 print "\n";
951 }
952
953 # TODO: check in links and also link target? At least warn about link target
954 if (@links) {
955 my $number = $#links + 1;
956 print "\n";
957 print "ignoring links ($number):\n";
958 print join( "\n", @links );
959 print "\n";
960 }
961
962 # TODO: confirm
963
964 # copy files one by one to local repository
965 for my $file (@files) {
966
967 # add file
968 add_helper($file);
969 }
970
971 # create new permissions file
972 permissions();
973
974 # add permissions file
975 add_helper($DASSCM_PERMISSION_FILE);
976
977 if ( $options{'message'} ) {
978 $svnOptions .= " --message \"$options{'message'}\" ";
979 }
980
981 # commit calls $EDITOR.
982 # use "interactive" here, to display output
983 my $retcode = run_interactive(
984 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
985 );
986
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();
990}
991
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
1006 ) = get_filenames( $_[0] );
1007
1008 #
1009 # update local repository
1010 #
1011 svn_update();
1012
1013 ( my $refChangedFiles, my $refRemovedFiles ) =
1014 getModifiedFiles($filename_prod);
1015 my %changedfiles = %{$refChangedFiles};
1016 my %removedfiles = %{$refRemovedFiles};
1017
1018 if (%removedfiles) {
1019 my $removedFilesString =
1020 '"' . join( '" "', values(%removedfiles) ) . '"';
1021 my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
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) ) {
1029 copy_file_to_repository($file);
1030 }
1031
1032 # create new permissions file
1033 permissions();
1034
1035 # add permissions file
1036 add_helper($DASSCM_PERMISSION_FILE);
1037
1038 if ( $options{'message'} ) {
1039 $svnOptions .= " --message \"$options{'message'}\" ";
1040 }
1041
1042 # commit calls $EDITOR.
1043 # use "interactive" here, to display output
1044 my $retcode = run_interactive(
1045 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1046 );
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();
1051}
1052
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
1067 ) = get_filenames( $_[0] );
1068
1069 # return code for the shell
1070 # default: error
1071 my $return_code = $RETURN_OK;
1072
1073 # cleanup repository
1074 cleanup();
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";
1087 my @removedPaths =
1088 ( sort { length $a > length $b } keys %removedfiles );
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
1094 for my $real_path (@removedPaths) {
1095 if ( -d $removedfiles{"$real_path"} ) {
1096 mkpath("$real_path");
1097 } else {
1098 copy_file_from_repository_to_system( $real_path );
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) ) {
1109 copy_file_from_repository_to_system( $real_file );
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
1128sub blame(@)
1129{
1130 check_parameter( @_, 1 );
1131 check_env();
1132
1133 (
1134 my $basename,
1135 my $dirname_prod,
1136 my $dirname_repo,
1137 my $filename_prod,
1138 my $filename_repo
1139 ) = get_filenames( $_[0] );
1140
1141 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
1142}
1143
1144sub diff(@)
1145{
1146 check_parameter( @_, 1 );
1147 check_env();
1148
1149 (
1150 my $basename,
1151 my $dirname_prod,
1152 my $dirname_repo,
1153 my $filename_prod,
1154 my $filename_repo
1155 ) = get_filenames( $_[0] );
1156
1157 #print "$basename,$dirname_prod,$dirname_repo\n";
1158
1159 svn_update();
1160
1161 ( my $rc_diff, my @diff_result ) =
1162 run_command( $diff . " $filename_repo $filename_prod" );
1163
1164 print @diff_result;
1165}
1166
1167sub status(@)
1168{
1169 check_parameter( @_, 1 );
1170 check_env();
1171
1172 (
1173 my $basename,
1174 my $dirname_prod,
1175 my $dirname_repo,
1176 my $filename_prod,
1177 my $filename_repo
1178 ) = get_filenames( $_[0] || "/" );
1179
1180 # return code for the shell
1181 # default: error
1182 my $return_code = $RETURN_NOK;
1183
1184 #
1185 # update local repository
1186 #
1187 #svn_update( $filename_prod );
1188
1189 # check, if permissions have changed
1190 permissions();
1191
1192 # get modified files
1193 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1194 getModifiedFiles($dirname_prod);
1195 my %changedfiles = %{$refChangedFiles};
1196 my %removedfiles = %{$refRemovedFiles};
1197 my %unknownfiles = %{$refUnknownFiles};
1198
1199 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1200
1201 if (%removedfiles) {
1202 print "DELETED: files found in repository, but not in system:\n";
1203 print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
1204 }
1205
1206 if (%changedfiles) {
1207 print "MODIFIED: files differs between repository and system:\n";
1208 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1209 }
1210
1211 if (%unknownfiles) {
1212 print "UNKNOWN: insufficient permission to check files:\n";
1213 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1214 }
1215
1216 } else {
1217 print "no modified files found in $dirname_repo\n";
1218 $return_code = $RETURN_OK;
1219 }
1220
1221 return $return_code;
1222}
1223
1224#
1225# return short status in Nagios plugin conform way
1226#
1227sub check()
1228{
1229 check_env();
1230
1231 # return code for the shell
1232 my $return_code = $RETURN_OK;
1233 my $return_string = "OK: no modified files";
1234
1235 # check, if permissions have changed
1236 permissions();
1237
1238 # get modified files
1239 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1240 getModifiedFiles("/");
1241 my %changedfiles = %{$refChangedFiles};
1242 my %removedfiles = %{$refRemovedFiles};
1243 my %unknownfiles = %{$refUnknownFiles};
1244
1245 if ( %removedfiles or %changedfiles ) {
1246 $return_string = "Warning: ";
1247 if (%changedfiles) {
1248 $return_string .=
1249 "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
1250 }
1251 if (%removedfiles) {
1252 $return_string .=
1253 "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
1254 }
1255 if (%unknownfiles) {
1256 $return_string .=
1257 "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
1258 }
1259 $return_code = $RETURN_WARN;
1260 }
1261
1262 # addition nagios Service Status
1263 #Critical
1264 #Unknown
1265
1266 print "$return_string\n";
1267 return $return_code;
1268}
1269
1270sub permissions()
1271{
1272 check_env();
1273
1274 my $return_code = $RETURN_OK;
1275
1276 #
1277 # update local repository
1278 #
1279 #svn_update();
1280
1281 my $dir = $DASSCM_REPO;
1282 my @files = svn_ls("/");
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
1290
1291 if ( -w dirname($DASSCM_PERMISSION_FILE) ) {
1292
1293 # Verzeichnis existiert => schreiben
1294 open( OUTFILE, ">$DASSCM_PERMISSION_FILE" )
1295 || die("failed to write to $DASSCM_PERMISSION_FILE: $!");
1296 $tofile = 1; # Merken, daß in File geschrieben wird
1297 print OUTFILE "#\n";
1298 print OUTFILE "# created by dasscm permissions\n";
1299 print OUTFILE
1300 "# It is intended to be used for restoring permissions\n";
1301 print OUTFILE "#\n";
1302 } else {
1303
1304 if ( $command eq "permission" ) {
1305
1306 # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
1307 # Alias Filehandle für stdout erzeugen
1308 $return_code = $RETURN_WARN;
1309 *OUTFILE = *STDOUT;
1310 } else {
1311
1312 # TODO: improve this. Check for diff?
1313 $return_code = $RETURN_CRIT;
1314 return $return_code;
1315 }
1316 }
1317
1318 foreach my $line (@permissions) {
1319 print OUTFILE "$line\n";
1320 }
1321
1322 if ($tofile) {
1323 close(OUTFILE);
1324 }
1325 }
1326
1327 return $return_code;
1328}
1329
1330#
1331# remove all uncommited changes in the repository
1332#
1333sub cleanup()
1334{
1335 check_env();
1336
1337 svn_revert($DASSCM_REPO);
1338 svn_remove_unknown_files($DASSCM_REPO);
1339}
1340
1341#####################################################################
1342#
1343# main
1344#
1345
1346my $return_code = $RETURN_OK;
1347my $number_arguments = @ARGV;
1348
1349if ( $number_arguments > 0 ) {
1350
1351 # get subcommand and remove it from @ARGV
1352 $command = $ARGV[0];
1353 shift @ARGV;
1354
1355 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1356 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
1357
1358 # TODO: check variables
1359 $DASSCM_SVN_REPOSITORY =
1360 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1361
1362 $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1363 $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
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
1378 $DASSCM_PERMISSION_FILE = $config->{'DASSCM_PERMISSION_FILE'}
1379 || "/etc/permissions.d/dasscm.permission_backup";
1380
1381 # get command line options and store them in options hash
1382 my $result = GetOptions( \%options, 'verbose', 'message=s' );
1383
1384 # print options
1385 foreach my $option ( keys %options ) {
1386 print "${option}: $options{$option}\n";
1387 }
1388
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 #
1397 $_ = $command;
1398 if (m/^help$/i) {
1399 help(@ARGV);
1400 } elsif (m/^login$/i) {
1401 $command = "login";
1402 login(@ARGV);
1403 } elsif (m/^init$/i) {
1404 $command = "init";
1405 init(@ARGV);
1406 } elsif (m/^ls$/i) {
1407 $command = "ls";
1408 ls(@ARGV);
1409 } elsif ( (m/^update$/i) || (m/^up$/i) ) {
1410 $command = "update";
1411 update(@ARGV);
1412 } elsif (m/^add$/i) {
1413 $command = "add";
1414 add(@ARGV);
1415 } elsif ( (m/^commit$/i) || (m/^checkin$/i) || (m/^ci$/i) ) {
1416 $command = "commit";
1417 commit(@ARGV);
1418 } elsif (m/^revert$/i) {
1419 $command = "revert";
1420 $return_code = revert(@ARGV);
1421 } elsif (m/^blame$/i) {
1422 $command = "blame";
1423 blame(@ARGV);
1424 } elsif (m/^diff$/i) {
1425 $command = "diff";
1426 diff(@ARGV);
1427 } elsif ( (m/^status$/i) || (m/^st$/i) ) {
1428 $command = "status";
1429 $return_code = status(@ARGV);
1430 } elsif (m/^check$/i) {
1431 $command = "check";
1432 $return_code = check();
1433 } elsif (m/^permissions$/i) {
1434 $command = "permissions";
1435 $return_code = permissions();
1436 } elsif (m/^cleanup$/i) {
1437 $command = "cleanup";
1438 cleanup();
1439 } else {
1440 print "unknown command: $command\n\n";
1441 usage();
1442 check_env();
1443 $return_code = $RETURN_NOK;
1444 }
1445}
1446
1447exit $return_code;
Note: See TracBrowser for help on using the repository browser.