source: trunk/dasscm/dasscm@ 283

Last change on this file since 283 was 283, checked in by joergs, on Mar 9, 2009 at 2:57:14 PM

added $ to usage

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