source: trunk/dasscm/dasscm@ 216

Last change on this file since 216 was 216, checked in by joergs, on Jul 16, 2007 at 3:55:29 PM

added creation of permissions file in init. otherwise checkins will fail

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 18.0 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 216 2007-07-16 13:55:29Z 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 # and create permission file
383 my $retcode =
384 run_interactive(
385 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; touch $permissions_file"
386 );
387}
388
389sub ls(@)
390{
391 check_parameter( @_, 1 );
392 check_env();
393
394 my @files = svn_getStoredFiles(@_);
395
396 print join( "\n", @files );
397 print "\n";
398}
399
400
401sub update(@)
402{
403 check_parameter( @_, 1 );
404 check_env();
405
406 #
407 # update local repository
408 #
409 svn_update();
410}
411
412
413sub add_helper(@)
414{
415 (
416 my $basename,
417 my $dirname_prod,
418 my $dirname_repo,
419 my $filename_prod,
420 my $filename_repo
421 )
422 = get_filenames( $_[0] );
423
424 if ( $command eq "add" ) {
425 mkpath($dirname_repo);
426 }
427
428 copy( $filename_prod, $filename_repo ) or die $!;
429
430 if ( $command eq "add" ) {
431
432 # already checked in?
433 chdir($DASSCM_REPO);
434
435 # also add the path to filename.
436 for my $dir ( split( '/', $dirname_prod ) ) {
437 if ($dir) {
438 run_command("$SVN add --non-recursive $dir");
439 chdir $dir;
440 }
441 }
442 run_command("$SVN add $basename");
443 }
444}
445
446
447
448#
449# add (is used for command add and commit)
450#
451sub add(@)
452{
453 check_parameter( @_, 1 );
454 check_env();
455
456 #
457 # update local repository
458 #
459 svn_update();
460
461 # add file
462 add_helper( $_[0] );
463 # create new permissions file
464 permissions();
465 # add permissions file
466 add_helper( $permissions_file );
467
468 if ( $options{'message'} ) {
469 $svnOptions .= " --message \"$options{'message'}\" ";
470 }
471
472 # commit calls $EDITOR. uses "interactive" here, to display output
473 my $retcode =
474 run_interactive(
475 "$SVN commit $svnOptions --username $DASSCM_USERNAME $svnPasswordCredentials $DASSCM_REPO"
476 );
477
478 #print $filename_prod. "\n";
479 #print $dirname_repo. "\n";
480}
481
482
483
484sub blame(@)
485{
486 check_parameter( @_, 1 );
487 check_env();
488
489 (
490 my $basename,
491 my $dirname_prod,
492 my $dirname_repo,
493 my $filename_prod,
494 my $filename_repo
495 )
496 = get_filenames( $_[0] );
497
498 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
499}
500
501sub diff(@)
502{
503 check_parameter( @_, 1 );
504 check_env();
505
506 (
507 my $basename,
508 my $dirname_prod,
509 my $dirname_repo,
510 my $filename_prod,
511 my $filename_repo
512 )
513 = get_filenames( $_[0] );
514
515 #print "$basename,$dirname_prod,$dirname_repo\n";
516
517 ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
518 if ( $rc_update != 0 ) {
519 print @result;
520 die;
521 }
522
523 ( my $rc_diff, my @diff ) =
524 run_command("diff $filename_repo $filename_prod");
525 print @diff;
526}
527
528sub status(@)
529{
530 check_parameter( @_, 1 );
531 check_env();
532
533 #
534 # update local repository
535 #
536 svn_update();
537
538 # TODO: start at subdirectories ?
539 my $dir = $DASSCM_REPO;
540 my @files = svn_getStoredFiles($dir);
541
542 # Liste der geänderten Files ausgeben, falls nicht leer
543 if (@files) {
544
545 # stores result from status (cvscheck)
546 my %removedfiles = ();
547 my %changedfiles = ();
548
549 foreach my $file (@files) {
550
551 my $realfile = "/" . $file;
552 my $cvsworkfile = "${DASSCM_REPO}/${file}";
553
554 if ( -d $realfile ) {
555
556 # directory. do nothing
557 } elsif ( !-r $realfile ) {
558 $removedfiles{"$realfile"} = $cvsworkfile;
559 } else {
560 ( -r "$cvsworkfile" )
561 || die("Fehler: $cvsworkfile ist nicht lesbar");
562 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
563 $changedfiles{"$realfile"} = $cvsworkfile;
564 }
565 }
566 }
567
568 if (%removedfiles) {
569 print "deleted files (found in repository, but not in system):\n";
570 foreach my $key ( values %removedfiles ) {
571 print "$key\n";
572 }
573 print "\n";
574 }
575
576 if (%changedfiles) {
577 print "modified files:\n";
578 foreach my $key ( keys %changedfiles ) {
579 print "$key\n";
580 }
581 }
582 } else {
583 print "no modified files found in $dir\n";
584 }
585
586 print "\n";
587}
588
589
590
591sub permissions(@)
592{
593 check_parameter( @_, 1 );
594 check_env();
595
596 #
597 # update local repository
598 #
599 #svn_update();
600
601 # TODO: start at subdirectories ?
602 my $dir = $DASSCM_REPO;
603 my @files = svn_getStoredFiles($dir);
604
605 if (@files) {
606
607 # generieren der Permissions
608 my @permissions = generatePermissionList(@files);
609 my $OUTFILE;
610 my $tofile = 0; # Status für schreiben in File
611
612 if ( -w dirname($permissions_file) ) {
613
614 # Verzeichnis existiert => schreiben
615 print "storing permissions in file $permissions_file\n";
616 open( OUTFILE, ">$permissions_file" )
617 || die("failed to write to $permissions_file: $!");
618 $tofile = 1; # Merken, daß in File geschrieben wird
619 print OUTFILE "#\n";
620 print OUTFILE "# created by dasscm permissions\n";
621 print OUTFILE "# It is intended to be used for restoring permissions\n";
622 } else {
623
624 # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
625 # Alias Filehandle für stdout erzeugen
626 *OUTFILE = *STDOUT;
627 }
628 foreach my $line (@permissions) {
629 print OUTFILE "$line\n";
630 }
631
632 if ($tofile) {
633 close(OUTFILE);
634 }
635 }
636}
637
638#####################################################################
639#
640# main
641#
642
643my $number_arguments = @ARGV;
644
645if ( $number_arguments > 0 ) {
646
647 # get subcommand and remove it from @ARGV
648 $command = $ARGV[0];
649 shift @ARGV;
650
651 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
652 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
653
654 # TODO: check variables
655 $DASSCM_SVN_REPOSITORY =
656 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
657
658 my $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
659 my $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
660
661 #
662 # if a user is given by dasscm configuration file, we use it.
663 # Otherwise we expect that read-only account is configured
664 # as local subversion configuration.
665 # If this is also not the case,
666 # user is required to type username and password.
667 # This will be stored as local subversion configuration thereafter.
668 #
669 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
670 $svnCheckoutCredentials =
671 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
672 }
673
674 # get command line options and store them in options hash
675 my $result = GetOptions( \%options, 'verbose', 'message=s' );
676
677 # print options
678 foreach my $option ( keys %options ) {
679 print "${option}: $options{$option}\n";
680 }
681
682 # set verbose to command line option
683 $verbose = $options{'verbose'};
684
685 #
686 # action accordinly to command are taken
687 # $command is rewritten in standard format,
688 # so we can test for it later on more simply
689 #
690 $_ = $command;
691 if (m/help/i) {
692 help(@ARGV);
693 } elsif (m/login/i) {
694 $command = "login";
695 login(@ARGV);
696 } elsif (m/init/i) {
697 $command = "init";
698 init(@ARGV);
699 } elsif (m/ls/i) {
700 $command = "ls";
701 ls(@ARGV);
702 } elsif (m/up/i) {
703 $command = "update";
704 update(@ARGV);
705 } elsif (m/add/i) {
706 $command = "add";
707 add(@ARGV);
708 } elsif (m/commit/i) {
709 $command = "commit";
710 add(@ARGV);
711 } elsif (m/blame/i) {
712 $command = "blame";
713 blame(@ARGV);
714 } elsif (m/diff/i) {
715 $command = "diff";
716 diff(@ARGV);
717 } elsif (m/status/i) {
718 $command = "status";
719 status(@ARGV);
720 } elsif (m/permissions/i) {
721 $command = "permissions";
722 permissions(@ARGV);
723 } else {
724 print "unknown command: $command\n\n";
725 usage();
726 check_env();
727 }
728
729 # up
730 # cleanup (svn-commit.tmp)
731 # commitall
732 # revert
733 # activate
734}
Note: See TracBrowser for help on using the repository browser.