source: trunk/dasscm/dasscm@ 215

Last change on this file since 215 was 215, checked in by joergs, on Jul 3, 2007 at 4:55:06 PM

added permission backup

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 17.9 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 215 2007-07-03 14:55:06Z joergs $
4
5use strict;
6
7use Env
8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD);
9use Cwd;
10use Getopt::Long;
11use File::Basename;
12use File::Compare;
13use File::Copy;
14use File::stat;
15use File::Path;
16use Term::ReadKey;
17
18#
19# used ConfigFile instead of SmartClient::Config,
20# because the huge amount of SmartClient dependencies
21#use SmartClient::Config;
22use ConfigFile;
23
24#####################################################################
25#
26# global
27#
28
29# file to store permissions
30my $permissions_file = "/etc/permissions.d/dasscm.permission_backup";
31# configuration file
32my $config_file = "/etc/dasscm.conf";
33my $config = ConfigFile::read_config_file($config_file);
34my $DASSCM_LOCAL_REPOSITORY_BASE;
35my $DASSCM_REPOSITORY_NAME;
36my $DASSCM_SVN_REPOSITORY;
37
38my $SVN = "svn ";
39my $svnOptions = "";
40my $svnCheckoutCredentials = "";
41my $svnPasswordCredentials = "";
42
43# command line options get stored in options hash
44my %options = ();
45
46# subcommand, that gets executed (add, commit, ...)
47my $command;
48
49my $verbose = 0;
50
51
52#####################################################################
53#
54# util functions
55#
56sub usage()
57{
58 print "usage: dasscm <subcommand> [options] [args]\n";
59 print "\n";
60 print "dasscm is intended to help versioning configuration files\n";
61 print "\n";
62 print "Available subcommands:\n";
63 print " help <subcommand>\n";
64 print " init\n";
65 print " login\n";
66 print " up\n";
67 print " ls\n";
68 print " add <filename>\n";
69 print " commit <filename>\n";
70 print " status <filename>\n";
71 print " diff <filename>\n";
72 print " permissions\n";
73 print "\n";
74 print "preperation:\n";
75 print "check out the configuration repository, e.g.\n";
76 print
77 "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/dasscm/HOSTNAME\n";
78 print "environment variables\n", " DASSCM_REPO\n", " DASSCM_PROD\n",
79 " DASSCM_USERNAME\n", " DASSCM_PASSWORD\n", "are evaluated.\n";
80 print "\n";
81}
82
83sub check_env()
84{
85
86 # DASSCM_PROD
87 if ( !$DASSCM_PROD ) {
88 $DASSCM_PROD = "/";
89 }
90
91 if ( !-d $DASSCM_PROD ) {
92 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
93 }
94 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
95
96 # DASSCM_REPOSITORY_NAME
97 if ( !$DASSCM_REPOSITORY_NAME ) {
98 die
99 "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";
100 }
101
102 # DASSCM_REPO
103 if ( !$DASSCM_REPO ) {
104 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
105 $DASSCM_REPO =
106 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
107 } else {
108 die
109 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
110 }
111 }
112 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
113
114 #
115 # check if local repository directory exist (if not creating by init)
116 #
117 if ( $command ne "init" ) {
118 if ( not -d $DASSCM_REPO ) {
119 die
120 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
121 }
122
123 #
124 # user settings
125 #
126
127 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
128 if ( !$DASSCM_USERNAME ) {
129 $DASSCM_USERNAME = $DASSCM_USER;
130 }
131
132 # user root is not allowed for checkins.
133 # if user is root, DASSCM_USER has to be set,
134 # otherwise USER can be used
135 if ( "$USER" eq "root" ) {
136 if ( ( not $DASSCM_USERNAME ) and ( $command ne "login" ) ) {
137 die
138 "Envirnonment variable DASSCM_USERNAME not set.\nSet DASSCM_USERNAME to your subversion user account.\n";
139 }
140 $svnOptions .= " --no-auth-cache ";
141 } elsif ( !$DASSCM_USERNAME ) {
142 $DASSCM_USERNAME = $USER;
143 }
144
145 #
146 # password
147 #
148 if ($DASSCM_PASSWORD) {
149 $svnPasswordCredentials = " --password $DASSCM_PASSWORD ";
150 }
151 }
152
153 #$svnOptions .= " --username $DASSCM_USERNAME "
154}
155
156sub check_parameter(@)
157{
158}
159
160sub get_filenames(@)
161{
162 my $filename_prod = $_[0];
163 if ( !( $filename_prod =~ m/^\// ) ) {
164 $filename_prod = cwd() . "/" . $filename_prod;
165 }
166
167 -r $filename_prod or die "$filename_prod is not accessable";
168
169 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
170 # "/etc" is used as filename
171 my $dirname_prod = dirname($filename_prod);
172 chdir $dirname_prod or die $!;
173 $dirname_prod = cwd();
174 my $basename = basename($filename_prod);
175
176 if ($verbose) {
177 print "dir: " . $dirname_prod . "\n";
178 print "fn: " . $basename . "\n";
179 }
180
181 my $dirname_repo = $DASSCM_REPO . "/" . $dirname_prod;
182 my $filename_repo = "$dirname_repo/$basename";
183
184 return (
185 $basename, $dirname_prod, $dirname_repo,
186 $filename_prod, $filename_repo
187 );
188}
189
190
191
192sub generatePermissionList
193{
194
195 # generieren der Zeilen für Permission-Savefile
196 my @files = @_;
197 my @permlist = ();
198 foreach my $file (@files) {
199 my $info = stat( "/" . $file ) || die "$file: stat error";
200 my $mode = get_type( $info->mode ) & 07777;
201 my $modestring = sprintf( "%04o", $mode );
202 my $uid = $info->uid;
203 my $uidname = getpwuid($uid);
204 my $gid = $info->gid;
205 my $gidname = getgrgid($gid);
206 push(
207 @permlist,
208 sprintf( "%-55s %-17s %4d",
209 $file, "${uidname}:${gidname}", $modestring )
210 );
211 }
212 return @permlist;
213}
214
215sub get_type
216{
217
218 # Funktion übernommen aus /usr/bin/chkstat
219 my $S_IFLNK = 0120000; # symbolic link
220 my $S_IFREG = 0100000; # regular file
221 my $S_IFDIR = 0040000; # directory
222 my $S_IFCHAR = 0020000; # character device
223 my $S_IFBLK = 0060000; # block device
224 my $S_IFFIFO = 0010000; # fifo
225 my $S_IFSOCK = 0140000; # socket
226 my $S_IFMT = 0170000; # type of file
227
228 my $S_m;
229 if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
230 elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
231 elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
232 elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
233 elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
234 elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
235 elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
236 $S_m;
237}
238
239sub run_command
240{
241 my $command = shift;
242
243 #print "executing command: " . $command . "\n";
244
245 open( RESULT, $command . ' 2>&1 |' );
246 my @result = <RESULT>;
247 close(RESULT);
248 my $retcode = $? >> 8;
249
250 #print @result;
251 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
252
253 return ( $retcode, @result );
254}
255
256sub run_interactive
257{
258
259 if ($verbose) {
260 print "run_interactive:" . join( " ", @_ ) . "\n";
261 }
262
263 system(@_);
264 if ( $? == -1 ) {
265 printf "failed to execute: $!\n";
266 } elsif ( $? & 127 ) {
267 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
268 ( $? & 128 ) ? 'with' : 'without';
269 } elsif ( $? >> 8 != 0 ) {
270 printf "child exited with value %d\n", $? >> 8;
271 }
272 return ( $? >> 8 );
273}
274
275sub svn_check_credentials( $$ )
276{
277 my $username = shift;
278 my $password = shift;
279
280 ( my $rc_update, my @result ) =
281 run_command(
282 "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
283 );
284
285 print @result;
286
287 if ( $rc_update != 0 ) {
288 die;
289 }
290
291}
292
293sub svn_update( ;$ )
294{
295 my $update_path = shift || $DASSCM_REPO;
296 ( my $rc_update, my @result ) =
297 run_command("$SVN update $svnCheckoutCredentials $update_path");
298 print @result;
299 if ( $rc_update != 0 ) {
300 die;
301 }
302}
303
304sub svn_getStoredFiles( ;$ )
305{
306
307 # TODO: get_filenames?
308 #my $rel_path = shift || "";
309 #my $path = "${DASSCM_REPO}/${rel_path}";
310 my $path = ${DASSCM_REPO};
311 ( my $rc, my @result ) =
312 run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
313 if ( $rc != 0 ) {
314 print @result;
315 die;
316 }
317 chomp(@result);
318 return @result;
319}
320
321#####################################################################
322#
323# functions
324
325sub help(;@)
326{
327 if ( @_ == 0 ) {
328 usage();
329 } else {
330 print "help for @_: ...\n";
331 usage();
332 }
333}
334
335sub login(@)
336{
337 check_parameter( @_, 1 );
338 check_env();
339
340 my $input_username = $1;
341
342 if ( not $input_username ) {
343 my $output_username = "";
344 if ($DASSCM_USERNAME) {
345 $output_username = " ($DASSCM_USERNAME)";
346 }
347
348 print "Enter DASSCM user name", $output_username, ": ";
349 $input_username = <STDIN>;
350 chomp($input_username);
351 }
352
353 # hidden password input
354 print "Enter DASSCM user password: ";
355 ReadMode('noecho');
356 my $input_password = <STDIN>;
357 ReadMode('normal');
358 chomp($input_password);
359
360 svn_check_credentials( $input_username, $input_password );
361
362 #
363 # set environment variables
364 #
365 $ENV{'DASSCM_USERNAME'} = $input_username;
366 $ENV{'DASSCM_PASSWORD'} = $input_password;
367
368 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
369 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
370 "DASSCM_REPO: $DASSCM_REPO\n",
371 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n", "[dasscm shell]\n\n";
372
373 exec("bash") or die "failed to start new shell";
374}
375
376sub init(@)
377{
378 check_parameter( @_, 1 );
379 check_env();
380
381 # update complete repository
382 my $retcode =
383 run_interactive(
384 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY"
385 );
386}
387
388sub ls(@)
389{
390 check_parameter( @_, 1 );
391 check_env();
392
393 my @files = svn_getStoredFiles(@_);
394
395 print join( "\n", @files );
396 print "\n";
397}
398
399
400sub update(@)
401{
402 check_parameter( @_, 1 );
403 check_env();
404
405 #
406 # update local repository
407 #
408 svn_update();
409}
410
411
412sub add_helper(@)
413{
414 (
415 my $basename,
416 my $dirname_prod,
417 my $dirname_repo,
418 my $filename_prod,
419 my $filename_repo
420 )
421 = get_filenames( $_[0] );
422
423 if ( $command eq "add" ) {
424 mkpath($dirname_repo);
425 }
426
427 copy( $filename_prod, $filename_repo ) or die $!;
428
429 if ( $command eq "add" ) {
430
431 # already checked in?
432 chdir($DASSCM_REPO);
433
434 # also add the path to filename.
435 for my $dir ( split( '/', $dirname_prod ) ) {
436 if ($dir) {
437 run_command("$SVN add --non-recursive $dir");
438 chdir $dir;
439 }
440 }
441 run_command("$SVN add $basename");
442 }
443}
444
445
446
447#
448# add (is used for command add and commit)
449#
450sub add(@)
451{
452 check_parameter( @_, 1 );
453 check_env();
454
455 #
456 # update local repository
457 #
458 svn_update();
459
460 # add file
461 add_helper( $_[0] );
462 # create new permissions file
463 permissions();
464 # add permissions file
465 add_helper( $permissions_file );
466
467 if ( $options{'message'} ) {
468 $svnOptions .= " --message \"$options{'message'}\" ";
469 }
470
471 # commit calls $EDITOR. uses "interactive" here, to display output
472 my $retcode =
473 run_interactive(
474 "$SVN commit $svnOptions --username $DASSCM_USERNAME $svnPasswordCredentials $DASSCM_REPO"
475 );
476
477 #print $filename_prod. "\n";
478 #print $dirname_repo. "\n";
479}
480
481
482
483sub blame(@)
484{
485 check_parameter( @_, 1 );
486 check_env();
487
488 (
489 my $basename,
490 my $dirname_prod,
491 my $dirname_repo,
492 my $filename_prod,
493 my $filename_repo
494 )
495 = get_filenames( $_[0] );
496
497 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
498}
499
500sub diff(@)
501{
502 check_parameter( @_, 1 );
503 check_env();
504
505 (
506 my $basename,
507 my $dirname_prod,
508 my $dirname_repo,
509 my $filename_prod,
510 my $filename_repo
511 )
512 = get_filenames( $_[0] );
513
514 #print "$basename,$dirname_prod,$dirname_repo\n";
515
516 ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
517 if ( $rc_update != 0 ) {
518 print @result;
519 die;
520 }
521
522 ( my $rc_diff, my @diff ) =
523 run_command("diff $filename_repo $filename_prod");
524 print @diff;
525}
526
527sub status(@)
528{
529 check_parameter( @_, 1 );
530 check_env();
531
532 #
533 # update local repository
534 #
535 svn_update();
536
537 # TODO: start at subdirectories ?
538 my $dir = $DASSCM_REPO;
539 my @files = svn_getStoredFiles($dir);
540
541 # Liste der geänderten Files ausgeben, falls nicht leer
542 if (@files) {
543
544 # stores result from status (cvscheck)
545 my %removedfiles = ();
546 my %changedfiles = ();
547
548 foreach my $file (@files) {
549
550 my $realfile = "/" . $file;
551 my $cvsworkfile = "${DASSCM_REPO}/${file}";
552
553 if ( -d $realfile ) {
554
555 # directory. do nothing
556 } elsif ( !-r $realfile ) {
557 $removedfiles{"$realfile"} = $cvsworkfile;
558 } else {
559 ( -r "$cvsworkfile" )
560 || die("Fehler: $cvsworkfile ist nicht lesbar");
561 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
562 $changedfiles{"$realfile"} = $cvsworkfile;
563 }
564 }
565 }
566
567 if (%removedfiles) {
568 print "deleted files (found in repository, but not in system):\n";
569 foreach my $key ( values %removedfiles ) {
570 print "$key\n";
571 }
572 print "\n";
573 }
574
575 if (%changedfiles) {
576 print "modified files:\n";
577 foreach my $key ( keys %changedfiles ) {
578 print "$key\n";
579 }
580 }
581 } else {
582 print "no modified files found in $dir\n";
583 }
584
585 print "\n";
586}
587
588
589
590sub permissions(@)
591{
592 check_parameter( @_, 1 );
593 check_env();
594
595 #
596 # update local repository
597 #
598 #svn_update();
599
600 # TODO: start at subdirectories ?
601 my $dir = $DASSCM_REPO;
602 my @files = svn_getStoredFiles($dir);
603
604 if (@files) {
605
606 # generieren der Permissions
607 my @permissions = generatePermissionList(@files);
608 my $OUTFILE;
609 my $tofile = 0; # Status für schreiben in File
610
611 if ( -w dirname($permissions_file) ) {
612
613 # Verzeichnis existiert => schreiben
614 print "storing permissions in file $permissions_file\n";
615 open( OUTFILE, ">$permissions_file" )
616 || die("failed to write to $permissions_file");
617 $tofile = 1; # Merken, daß in File geschrieben wird
618 print OUTFILE "#\n";
619 print OUTFILE "# created by dasscm permissions\n";
620 print OUTFILE "# It is intended to be used for restoring permissions\n";
621 } else {
622
623 # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
624 # Alias Filehandle für stdout erzeugen
625 *OUTFILE = *STDOUT;
626 }
627 foreach my $line (@permissions) {
628 print OUTFILE "$line\n";
629 }
630
631 if ($tofile) {
632 close(OUTFILE);
633 }
634 }
635}
636
637#####################################################################
638#
639# main
640#
641
642my $number_arguments = @ARGV;
643
644if ( $number_arguments > 0 ) {
645
646 # get subcommand and remove it from @ARGV
647 $command = $ARGV[0];
648 shift @ARGV;
649
650 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
651 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
652
653 # TODO: check variables
654 $DASSCM_SVN_REPOSITORY =
655 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
656
657 my $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
658 my $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
659
660 #
661 # if a user is given by dasscm configuration file, we use it.
662 # Otherwise we expect that read-only account is configured
663 # as local subversion configuration.
664 # If this is also not the case,
665 # user is required to type username and password.
666 # This will be stored as local subversion configuration thereafter.
667 #
668 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
669 $svnCheckoutCredentials =
670 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
671 }
672
673 # get command line options and store them in options hash
674 my $result = GetOptions( \%options, 'verbose', 'message=s' );
675
676 # print options
677 foreach my $option ( keys %options ) {
678 print "${option}: $options{$option}\n";
679 }
680
681 # set verbose to command line option
682 $verbose = $options{'verbose'};
683
684 #
685 # action accordinly to command are taken
686 # $command is rewritten in standard format,
687 # so we can test for it later on more simply
688 #
689 $_ = $command;
690 if (m/help/i) {
691 help(@ARGV);
692 } elsif (m/login/i) {
693 $command = "login";
694 login(@ARGV);
695 } elsif (m/init/i) {
696 $command = "init";
697 init(@ARGV);
698 } elsif (m/ls/i) {
699 $command = "ls";
700 ls(@ARGV);
701 } elsif (m/up/i) {
702 $command = "update";
703 update(@ARGV);
704 } elsif (m/add/i) {
705 $command = "add";
706 add(@ARGV);
707 } elsif (m/commit/i) {
708 $command = "commit";
709 add(@ARGV);
710 } elsif (m/blame/i) {
711 $command = "blame";
712 blame(@ARGV);
713 } elsif (m/diff/i) {
714 $command = "diff";
715 diff(@ARGV);
716 } elsif (m/status/i) {
717 $command = "status";
718 status(@ARGV);
719 } elsif (m/permissions/i) {
720 $command = "permissions";
721 permissions(@ARGV);
722 } else {
723 print "unknown command: $command\n\n";
724 usage();
725 check_env();
726 }
727
728 # up
729 # cleanup (svn-commit.tmp)
730 # commitall
731 # revert
732 # activate
733}
Note: See TracBrowser for help on using the repository browser.