[186] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | # $Id: dasscm 891 2010-06-26 10:57:52Z joergs $
|
---|
| 4 |
|
---|
| 5 | use strict;
|
---|
| 6 |
|
---|
[208] | 7 | use Env
|
---|
[235] | 8 | qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD $SHELL);
|
---|
[186] | 9 | use Cwd;
|
---|
[214] | 10 | use Getopt::Long;
|
---|
[186] | 11 | use File::Basename;
|
---|
[209] | 12 | use File::Compare;
|
---|
[891] | 13 | ## used system("cp -a"), because File::Copy does not keep permissions
|
---|
| 14 | ##use File::Copy;
|
---|
[237] | 15 | use File::Find;
|
---|
[186] | 16 | use File::stat;
|
---|
| 17 | use File::Path;
|
---|
[214] | 18 | use Term::ReadKey;
|
---|
[239] | 19 |
|
---|
[234] | 20 | #use Data::Dumper;
|
---|
[186] | 21 |
|
---|
[189] | 22 | #####################################################################
|
---|
| 23 | #
|
---|
[186] | 24 | # global
|
---|
[189] | 25 | #
|
---|
[205] | 26 |
|
---|
[253] | 27 | # shell exit codes
|
---|
[252] | 28 | my $RETURN_OK = 0;
|
---|
| 29 | my $RETURN_NOK = 1;
|
---|
| 30 |
|
---|
[277] | 31 | # Nagios return codes
|
---|
[290] | 32 | my $RETURN_WARN = 1;
|
---|
| 33 | my $RETURN_CRIT = 2;
|
---|
[277] | 34 | my $RETURN_UNKNOWN = 3;
|
---|
| 35 |
|
---|
[238] | 36 | # documentation file (for usage)
|
---|
| 37 | my $doc_file = "/usr/share/doc/packages/dasscm/dasscm_howto.txt";
|
---|
| 38 |
|
---|
[884] | 39 | my $config_file = "/etc/dasscm.conf";
|
---|
| 40 | my $config = get_config($config_file);
|
---|
[252] | 41 |
|
---|
[884] | 42 | my %COMMANDS = (
|
---|
[889] | 43 | '--help' => 'help',
|
---|
| 44 | 'help' => 'help',
|
---|
| 45 | 'login' => 'login',
|
---|
| 46 | 'init' => 'init',
|
---|
| 47 | 'ls' => 'ls',
|
---|
| 48 | 'update' => 'update',
|
---|
| 49 | 'up' => 'update',
|
---|
| 50 | 'add' => 'add',
|
---|
| 51 | 'commit' => 'commit',
|
---|
| 52 | 'checkin' => 'commit',
|
---|
| 53 | 'ci' => 'commit',
|
---|
| 54 | 'revert' => 'revert',
|
---|
| 55 | 'blame' => 'blame',
|
---|
| 56 | 'diff' => 'diff',
|
---|
| 57 | 'status' => 'status',
|
---|
| 58 | 'st' => 'status',
|
---|
| 59 | 'check' => 'check',
|
---|
| 60 | 'permissions' => 'permissions',
|
---|
| 61 | 'cleanup' => 'cleanup',
|
---|
| 62 | 'complete' => 'complete',
|
---|
| 63 | 'complete_path' => 'complete_path',
|
---|
[887] | 64 | 'complete_repopath' => 'complete_repopath',
|
---|
[884] | 65 | );
|
---|
| 66 |
|
---|
| 67 | # desc: description (eg. for usage)
|
---|
| 68 | # params: parameters
|
---|
| 69 | # CMD
|
---|
| 70 | # USER
|
---|
[891] | 71 | # PATH_PROD
|
---|
| 72 | # PATH_REPO
|
---|
[889] | 73 | # require:
|
---|
[884] | 74 | # WRITE commands that require write access (and therefore a login)
|
---|
| 75 | my %COMMAND_DEFINITIONS = (
|
---|
| 76 | 'help' => {
|
---|
[889] | 77 | 'desc' => [],
|
---|
| 78 | 'params' => ["CMD"],
|
---|
[884] | 79 | 'function' => \&help,
|
---|
| 80 | },
|
---|
| 81 | 'login' => {
|
---|
[889] | 82 | 'desc' => [],
|
---|
| 83 | 'params' => ["USER"],
|
---|
[884] | 84 | 'function' => \&login
|
---|
| 85 | },
|
---|
| 86 | 'init' => {
|
---|
[889] | 87 | 'desc' => [
|
---|
| 88 | "initialize local subversion checkout.",
|
---|
| 89 | "This is the first thing to do (after configuring $config_file)"
|
---|
| 90 | ],
|
---|
| 91 | 'params' => [],
|
---|
[884] | 92 | 'function' => \&init
|
---|
| 93 | },
|
---|
| 94 | 'ls' => {
|
---|
[889] | 95 | 'desc' => [],
|
---|
[891] | 96 | 'params' => ["PATH_REPO"],
|
---|
[884] | 97 | 'function' => \&ls
|
---|
| 98 | },
|
---|
| 99 | 'update' => {
|
---|
[889] | 100 | 'desc' => [],
|
---|
[891] | 101 | 'params' => ["PATH_REPO"],
|
---|
[884] | 102 | 'function' => \&update
|
---|
| 103 | },
|
---|
| 104 | 'add' => {
|
---|
[889] | 105 | 'desc' => [],
|
---|
[891] | 106 | 'params' => ["PATH_PROD"],
|
---|
[889] | 107 | 'require' => ["WRITE"],
|
---|
[884] | 108 | 'function' => \&add
|
---|
| 109 | },
|
---|
| 110 | 'commit' => {
|
---|
[889] | 111 | 'desc' => [],
|
---|
[891] | 112 | 'params' => ["PATH_REPO"],
|
---|
[889] | 113 | 'require' => ["WRITE"],
|
---|
[884] | 114 | 'function' => \&commit
|
---|
| 115 | },
|
---|
| 116 | 'revert' => {
|
---|
[889] | 117 | 'desc' => ["revert local changes back to version from repository"],
|
---|
[891] | 118 | 'params' => ["PATH_REPO"],
|
---|
[884] | 119 | 'function' => \&revert
|
---|
| 120 | },
|
---|
| 121 | 'blame' => {
|
---|
[891] | 122 | 'desc' => [],
|
---|
| 123 | ## TODO: only files from PATH_REPO
|
---|
| 124 | 'params' => ["PATH_REPO"],
|
---|
[884] | 125 | 'function' => \&blame
|
---|
| 126 | },
|
---|
| 127 | 'diff' => {
|
---|
[889] | 128 | 'desc' => [],
|
---|
[891] | 129 | 'params' => ["PATH_REPO"],
|
---|
[884] | 130 | 'function' => \&diff
|
---|
| 131 | },
|
---|
| 132 | 'status' => {
|
---|
[889] | 133 | 'desc' => [],
|
---|
[891] | 134 | 'params' => ["PATH_REPO"],
|
---|
[884] | 135 | 'function' => \&status
|
---|
| 136 | },
|
---|
| 137 | 'check' => {
|
---|
[889] | 138 | 'desc' => ["perform Nagios NRPE conform check"],
|
---|
| 139 | 'params' => [],
|
---|
[884] | 140 | 'function' => \&check
|
---|
| 141 | },
|
---|
| 142 | 'permissions' => {
|
---|
[889] | 143 | 'desc' => [],
|
---|
| 144 | 'params' => [],
|
---|
[884] | 145 | 'function' => \&permissions
|
---|
| 146 | },
|
---|
| 147 | 'cleanup' => {
|
---|
[889] | 148 | 'desc' => [],
|
---|
| 149 | 'params' => [],
|
---|
[884] | 150 | 'function' => \&cleanup
|
---|
| 151 | },
|
---|
| 152 | 'complete' => {
|
---|
[889] | 153 | 'desc' => ["internally, used for bash completion"],
|
---|
| 154 | 'params' => ["CMD"],
|
---|
[884] | 155 | 'function' => \&complete
|
---|
| 156 | },
|
---|
[887] | 157 | 'complete_path' => {
|
---|
[889] | 158 | 'desc' => ["internally, used for bash completion"],
|
---|
| 159 | 'params' => [],
|
---|
[887] | 160 | 'function' => \&complete_path
|
---|
| 161 | },
|
---|
| 162 | 'complete_repopath' => {
|
---|
[889] | 163 | 'desc' => ["internally, used for bash completion"],
|
---|
| 164 | 'params' => [],
|
---|
[887] | 165 | 'function' => \&complete_repopath
|
---|
| 166 | },
|
---|
[884] | 167 | );
|
---|
| 168 |
|
---|
[215] | 169 | # configuration file
|
---|
[205] | 170 | my $DASSCM_LOCAL_REPOSITORY_BASE;
|
---|
| 171 | my $DASSCM_REPOSITORY_NAME;
|
---|
| 172 | my $DASSCM_SVN_REPOSITORY;
|
---|
[247] | 173 | my $DASSCM_CHECKOUT_USERNAME;
|
---|
| 174 | my $DASSCM_CHECKOUT_PASSWORD;
|
---|
[286] | 175 | my $DASSCM_PERMISSION_FILE;
|
---|
[205] | 176 |
|
---|
[238] | 177 | # current directory at program start
|
---|
| 178 | my $StartDirectory = cwd();
|
---|
| 179 |
|
---|
[268] | 180 | my $diff = "diff --exclude .svn ";
|
---|
[205] | 181 | my $SVN = "svn ";
|
---|
| 182 | my $svnOptions = "";
|
---|
| 183 | my $svnCheckoutCredentials = "";
|
---|
| 184 | my $svnPasswordCredentials = "";
|
---|
[275] | 185 |
|
---|
[268] | 186 | # flag. Set to true by svn_update
|
---|
| 187 | # This prevents, that svn_update is called multiple times
|
---|
| 188 | my $svnRepositoryIsUptodate = 0;
|
---|
[205] | 189 |
|
---|
[196] | 190 | # command line options get stored in options hash
|
---|
[205] | 191 | my %options = ();
|
---|
| 192 |
|
---|
[197] | 193 | # subcommand, that gets executed (add, commit, ...)
|
---|
[196] | 194 | my $command;
|
---|
[186] | 195 |
|
---|
[205] | 196 | my $verbose = 0;
|
---|
| 197 |
|
---|
[189] | 198 | #####################################################################
|
---|
| 199 | #
|
---|
[186] | 200 | # util functions
|
---|
[189] | 201 | #
|
---|
[187] | 202 | sub usage()
|
---|
| 203 | {
|
---|
[283] | 204 | print '$Id: dasscm 891 2010-06-26 10:57:52Z joergs $';
|
---|
| 205 | print "\n\n";
|
---|
[205] | 206 | print "usage: dasscm <subcommand> [options] [args]\n";
|
---|
| 207 | print "\n";
|
---|
| 208 | print "dasscm is intended to help versioning configuration files\n";
|
---|
| 209 | print "\n";
|
---|
| 210 | print "Available subcommands:\n";
|
---|
[889] | 211 | foreach my $i ( sort keys(%COMMAND_DEFINITIONS) ) {
|
---|
| 212 | print " ", $i, " ", join( " ", get_command_possible_params($i) ),
|
---|
| 213 | "\n";
|
---|
[884] | 214 | foreach my $line ( get_command_desc($i) ) {
|
---|
[889] | 215 | print " " x 20, $line, "\n";
|
---|
[884] | 216 | }
|
---|
| 217 | }
|
---|
[205] | 218 | print "\n";
|
---|
[800] | 219 | print "If dasscm is not yet configured, read $doc_file\n";
|
---|
[187] | 220 | }
|
---|
| 221 |
|
---|
[233] | 222 | sub warning(@)
|
---|
| 223 | {
|
---|
| 224 | print "Warning: " . join( "\n ", @_ ) . "\n";
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | sub error(@)
|
---|
| 228 | {
|
---|
| 229 | print "Error: " . join( "\n ", @_ ) . "\n";
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | sub fatalerror(@)
|
---|
| 233 | {
|
---|
[239] | 234 | error(@_);
|
---|
| 235 |
|
---|
[233] | 236 | #print "Exiting\n";
|
---|
[239] | 237 | exit 1;
|
---|
[233] | 238 | }
|
---|
| 239 |
|
---|
[238] | 240 | #
|
---|
| 241 | # reading config file and return key/value pairs as hash
|
---|
| 242 | #
|
---|
[234] | 243 | sub get_config
|
---|
| 244 | {
|
---|
[239] | 245 | my $file = $_[0];
|
---|
[234] | 246 |
|
---|
[239] | 247 | if ( !$file ) {
|
---|
[234] | 248 | fatalerror( "failed to open config file" . $file );
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | my $data = {};
|
---|
| 252 |
|
---|
| 253 | # try to open config file
|
---|
| 254 | if ( !open( FH, $file ) ) {
|
---|
| 255 | fatalerror( "failed to open config file" . $file );
|
---|
| 256 | } else {
|
---|
| 257 | while (<FH>) {
|
---|
| 258 | chomp;
|
---|
| 259 | if (/^#/) {
|
---|
| 260 | next;
|
---|
| 261 | }
|
---|
| 262 | if ( $_ =~ /=/g ) {
|
---|
[239] | 263 |
|
---|
[238] | 264 | # splitting in 2 fields at maximum
|
---|
[234] | 265 | my ( $option, $value ) = split( /=/, $_, 2 );
|
---|
| 266 | $option =~ s/^\s+//g;
|
---|
| 267 | $option =~ s/\s+$//g;
|
---|
| 268 | $option =~ s/\"+//g;
|
---|
| 269 | $value =~ s/^\s+//g;
|
---|
| 270 | $value =~ s/\s+$//g;
|
---|
| 271 | $value =~ s/\"+//g;
|
---|
| 272 |
|
---|
| 273 | if ( length($option) ) {
|
---|
| 274 | $data->{$option} = $value;
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | close(FH);
|
---|
| 280 |
|
---|
| 281 | return $data;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[270] | 284 | #
|
---|
| 285 | # check and evaluate environment variables
|
---|
| 286 | #
|
---|
[186] | 287 | sub check_env()
|
---|
| 288 | {
|
---|
[205] | 289 |
|
---|
| 290 | # DASSCM_PROD
|
---|
| 291 | if ( !$DASSCM_PROD ) {
|
---|
| 292 | $DASSCM_PROD = "/";
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | if ( !-d $DASSCM_PROD ) {
|
---|
| 296 | die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
|
---|
| 297 | }
|
---|
| 298 | if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
|
---|
| 299 |
|
---|
| 300 | # DASSCM_REPOSITORY_NAME
|
---|
[208] | 301 | if ( !$DASSCM_REPOSITORY_NAME ) {
|
---|
| 302 | die
|
---|
| 303 | "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";
|
---|
| 304 | }
|
---|
[205] | 305 |
|
---|
| 306 | # DASSCM_REPO
|
---|
| 307 | if ( !$DASSCM_REPO ) {
|
---|
| 308 | if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
|
---|
| 309 | $DASSCM_REPO =
|
---|
| 310 | $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
|
---|
| 311 | } else {
|
---|
| 312 | die
|
---|
| 313 | "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
[887] | 316 | $DASSCM_REPO = normalize_path($DASSCM_REPO);
|
---|
[215] | 317 | if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
|
---|
[205] | 318 |
|
---|
| 319 | #
|
---|
[248] | 320 | # subversion checkout user
|
---|
| 321 | #
|
---|
[252] | 322 | if ( !$DASSCM_CHECKOUT_USERNAME ) {
|
---|
| 323 | fatalerror(
|
---|
[248] | 324 | "variable DASSCM_CHECKOUT_USERNAME is not defined.",
|
---|
[252] | 325 | "Use file $config_file to configure it."
|
---|
| 326 | );
|
---|
[248] | 327 | }
|
---|
| 328 |
|
---|
[252] | 329 | if ( !$DASSCM_CHECKOUT_PASSWORD ) {
|
---|
| 330 | fatalerror(
|
---|
[248] | 331 | "variable DASSCM_CHECKOUT_PASSWORD is not defined.",
|
---|
[252] | 332 | "Use file $config_file to configure it."
|
---|
| 333 | );
|
---|
[248] | 334 | }
|
---|
| 335 |
|
---|
| 336 | #
|
---|
[239] | 337 | # check if local repository directory exist
|
---|
[235] | 338 | # (if not creating by init)
|
---|
[205] | 339 | #
|
---|
| 340 | if ( $command ne "init" ) {
|
---|
| 341 | if ( not -d $DASSCM_REPO ) {
|
---|
| 342 | die
|
---|
[208] | 343 | "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
|
---|
[205] | 344 | }
|
---|
[208] | 345 |
|
---|
[205] | 346 | #
|
---|
| 347 | # user settings
|
---|
| 348 | #
|
---|
[208] | 349 |
|
---|
[205] | 350 | # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
|
---|
[208] | 351 | if ( !$DASSCM_USERNAME ) {
|
---|
| 352 | $DASSCM_USERNAME = $DASSCM_USER;
|
---|
[205] | 353 | }
|
---|
| 354 |
|
---|
| 355 | # user root is not allowed for checkins.
|
---|
| 356 | # if user is root, DASSCM_USER has to be set,
|
---|
| 357 | # otherwise USER can be used
|
---|
| 358 | if ( "$USER" eq "root" ) {
|
---|
[252] | 359 | if ( ( not $DASSCM_USERNAME )
|
---|
[889] | 360 | and ( get_command_requires_write($command) ) )
|
---|
[252] | 361 | {
|
---|
| 362 |
|
---|
| 363 | #( $command ne "login" ) and ( $command ne "status" ) ) {
|
---|
| 364 | fatalerror(
|
---|
| 365 | "Envirnonment variable DASSCM_USERNAME not set.",
|
---|
| 366 | "Set DASSCM_USERNAME to your subversion user account or",
|
---|
| 367 | "use 'dasscm login'"
|
---|
| 368 | );
|
---|
[205] | 369 | }
|
---|
| 370 | $svnOptions .= " --no-auth-cache ";
|
---|
| 371 | } elsif ( !$DASSCM_USERNAME ) {
|
---|
| 372 | $DASSCM_USERNAME = $USER;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | #
|
---|
| 376 | # password
|
---|
| 377 | #
|
---|
[208] | 378 | if ($DASSCM_PASSWORD) {
|
---|
[267] | 379 | $svnPasswordCredentials = " --password '$DASSCM_PASSWORD' ";
|
---|
[205] | 380 | }
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | #$svnOptions .= " --username $DASSCM_USERNAME "
|
---|
[186] | 384 | }
|
---|
| 385 |
|
---|
[270] | 386 | #
|
---|
| 387 | # has been intendend,
|
---|
| 388 | # to check addtitional parameters.
|
---|
| 389 | # Currently not used.
|
---|
| 390 | #
|
---|
[186] | 391 | sub check_parameter(@)
|
---|
| 392 | {
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[884] | 395 | sub get_command_uniform_name( $ )
|
---|
| 396 | {
|
---|
| 397 | my $command_abbrivation = $_[0];
|
---|
[889] | 398 | if ( defined( $COMMANDS{$command_abbrivation} ) ) {
|
---|
[884] | 399 | return $COMMANDS{$command_abbrivation};
|
---|
| 400 | }
|
---|
| 401 | return;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | sub get_command_desc( $ )
|
---|
| 405 | {
|
---|
[889] | 406 | my $command = get_command_uniform_name( $_[0] );
|
---|
| 407 | my @desc = ();
|
---|
| 408 | if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'desc'} ) ) {
|
---|
| 409 | @desc = @{ $COMMAND_DEFINITIONS{$command}{'desc'} };
|
---|
[884] | 410 | }
|
---|
| 411 | return @desc;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | sub get_command_function( $ )
|
---|
| 415 | {
|
---|
[889] | 416 | my $command = get_command_uniform_name( $_[0] );
|
---|
[884] | 417 | my $func;
|
---|
[889] | 418 | if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'function'} ) ) {
|
---|
[884] | 419 | $func = $COMMAND_DEFINITIONS{$command}{'function'};
|
---|
| 420 | }
|
---|
| 421 | return $func;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | sub get_command_possible_params( $ )
|
---|
| 425 | {
|
---|
[889] | 426 | my $command = get_command_uniform_name( $_[0] );
|
---|
| 427 | my @params = ();
|
---|
| 428 | if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'params'} ) ) {
|
---|
| 429 | @params = @{ $COMMAND_DEFINITIONS{$command}{'params'} };
|
---|
[884] | 430 | }
|
---|
| 431 | return @params;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | sub get_command_requirements( $ )
|
---|
| 435 | {
|
---|
[889] | 436 | my $command = get_command_uniform_name( $_[0] );
|
---|
[884] | 437 | my @requirements = ();
|
---|
[889] | 438 | if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'require'} ) ) {
|
---|
| 439 | @requirements = @{ $COMMAND_DEFINITIONS{$command}{'require'} };
|
---|
[884] | 440 | }
|
---|
| 441 | return @requirements;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | sub get_command_requires_write( $ )
|
---|
| 445 | {
|
---|
[889] | 446 | return grep( /^WRITE$/, get_command_requirements( $_[0] ) );
|
---|
[884] | 447 | }
|
---|
| 448 |
|
---|
[238] | 449 | #
|
---|
[287] | 450 | # normalize path namens:
|
---|
| 451 | # - directories should end with "/"
|
---|
| 452 | # - use only single "/"
|
---|
| 453 | #
|
---|
| 454 | sub normalize_path($)
|
---|
| 455 | {
|
---|
| 456 | my $path = shift || "";
|
---|
| 457 |
|
---|
[290] | 458 | if ( $path =~ m|^/| ) {
|
---|
| 459 |
|
---|
[287] | 460 | # full path
|
---|
[290] | 461 | if ( -d $path ) {
|
---|
| 462 |
|
---|
[287] | 463 | # ensure, a directory ends with '/'
|
---|
| 464 | $path .= '/';
|
---|
| 465 | }
|
---|
[290] | 466 | } elsif ( -d cwd() . '/' . $path ) {
|
---|
| 467 |
|
---|
| 468 | # ensure, a directory ends with '/'
|
---|
| 469 | $path .= '/';
|
---|
[287] | 470 | }
|
---|
| 471 |
|
---|
| 472 | # remove double (triple) slashes (/)
|
---|
| 473 | $path =~ s|/[/]*|/|g;
|
---|
| 474 |
|
---|
[887] | 475 | # remove self reference path
|
---|
| 476 | $path =~ s|/./|/|g;
|
---|
| 477 |
|
---|
[287] | 478 | return $path;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | #
|
---|
[239] | 482 | # generate from (relative) filename
|
---|
[238] | 483 | # all required file and directory names:
|
---|
| 484 | # $basename, $dirname_prod, $dirname_repo,
|
---|
| 485 | # $filename_prod, $filename_repo
|
---|
| 486 | #
|
---|
[187] | 487 | sub get_filenames(@)
|
---|
| 488 | {
|
---|
[250] | 489 | my $filename_prod = $_[0] || ".";
|
---|
[237] | 490 |
|
---|
[238] | 491 | # make filename absolut
|
---|
[205] | 492 | if ( !( $filename_prod =~ m/^\// ) ) {
|
---|
[270] | 493 | $filename_prod = cwd() . '/' . $filename_prod;
|
---|
[205] | 494 | }
|
---|
[187] | 495 |
|
---|
[889] | 496 | # file must be readable.
|
---|
| 497 | # The only exceptions are,
|
---|
[887] | 498 | # - if the file parameter is to be completed or
|
---|
| 499 | # - if a file should be reverted
|
---|
| 500 | if ( $command ne "revert" && $command !~ m/^complete/ ) {
|
---|
[800] | 501 | if ( not -r $filename_prod ) {
|
---|
| 502 | fatalerror( $filename_prod . " is not accessable" );
|
---|
| 503 | }
|
---|
[233] | 504 | }
|
---|
[205] | 505 |
|
---|
[274] | 506 | # dirname buggy: eg. "/etc/" is reduced to "/",
|
---|
| 507 | # "/etc" is used as filename
|
---|
| 508 | # herefore make sure, that if filename is a directory,
|
---|
| 509 | # it will end by "/"
|
---|
[290] | 510 | $filename_prod = normalize_path($filename_prod);
|
---|
[238] | 511 |
|
---|
[270] | 512 | ( my $basename, my $dirname_prod ) = fileparse($filename_prod);
|
---|
| 513 |
|
---|
[801] | 514 | # normalize path.
|
---|
[800] | 515 | # not done for reverting, because in this case, the directory may not exist
|
---|
| 516 | # and the correct path should already be stored in the repository
|
---|
| 517 | if ( $command ne "revert" ) {
|
---|
[801] | 518 |
|
---|
[800] | 519 | # uses chdir to determine real directory in a unique way
|
---|
[801] | 520 | chdir $dirname_prod
|
---|
| 521 | or fatalerror( "failed to access directory $dirname_prod: " . $! );
|
---|
[800] | 522 | $dirname_prod = normalize_path( cwd() );
|
---|
| 523 | chdir $StartDirectory;
|
---|
| 524 | }
|
---|
[238] | 525 |
|
---|
[287] | 526 | my $dirname_repo = normalize_path( $DASSCM_REPO . "/" . $dirname_prod );
|
---|
[290] | 527 | my $filename_repo = normalize_path("$dirname_repo/$basename");
|
---|
[287] | 528 |
|
---|
[214] | 529 | if ($verbose) {
|
---|
[287] | 530 | print "filename_repo: " . $filename_repo . "\n";
|
---|
[290] | 531 | print "dirname_repo: " . $dirname_repo . "\n";
|
---|
[287] | 532 | print "filename_prod: " . $filename_prod . "\n";
|
---|
[290] | 533 | print "dirname_prod: " . $dirname_prod . "\n";
|
---|
[287] | 534 | print "basename: " . $basename . "\n";
|
---|
[214] | 535 | }
|
---|
[205] | 536 |
|
---|
| 537 | return (
|
---|
| 538 | $basename, $dirname_prod, $dirname_repo,
|
---|
| 539 | $filename_prod, $filename_repo
|
---|
| 540 | );
|
---|
[187] | 541 | }
|
---|
| 542 |
|
---|
[271] | 543 | sub copy_file_to_repository( $ )
|
---|
| 544 | {
|
---|
| 545 | my $filename = shift;
|
---|
[256] | 546 |
|
---|
[271] | 547 | (
|
---|
| 548 | my $basename,
|
---|
| 549 | my $dirname_prod,
|
---|
| 550 | my $dirname_repo,
|
---|
| 551 | my $filename_prod,
|
---|
| 552 | my $filename_repo
|
---|
[801] | 553 | ) = get_filenames($filename);
|
---|
[271] | 554 |
|
---|
[802] | 555 | #copy( $filename_prod, $filename_repo )
|
---|
[889] | 556 | ( my $rc, my @result ) =
|
---|
| 557 | run_command("cp -a \"$filename_prod\" \"$filename_repo\"");
|
---|
| 558 | if ( $rc != 0 ) {
|
---|
[802] | 559 | error( "failed to copy $filename_prod to repository: ", @result );
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | # return success
|
---|
| 563 | return $rc == 0;
|
---|
[271] | 564 | }
|
---|
| 565 |
|
---|
[802] | 566 | sub copy_file_from_repository_to_system( $ )
|
---|
| 567 | {
|
---|
| 568 | my $filename = shift;
|
---|
| 569 |
|
---|
| 570 | (
|
---|
| 571 | my $basename,
|
---|
| 572 | my $dirname_prod,
|
---|
| 573 | my $dirname_repo,
|
---|
| 574 | my $filename_prod,
|
---|
| 575 | my $filename_repo
|
---|
| 576 | ) = get_filenames($filename);
|
---|
| 577 |
|
---|
[889] | 578 | ( my $rc, my @result ) =
|
---|
| 579 | run_command("cp -a \"$filename_repo\" \"$filename_prod\"");
|
---|
| 580 | if ( $rc != 0 ) {
|
---|
[802] | 581 | error( "failed to copy $filename_repo to $filename_prod: ", @result );
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | # return success
|
---|
| 585 | return $rc == 0;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
[256] | 588 | #
|
---|
| 589 | # creates a file with permissions
|
---|
| 590 | #
|
---|
[215] | 591 | sub generatePermissionList
|
---|
[209] | 592 | {
|
---|
| 593 |
|
---|
[215] | 594 | # generieren der Zeilen für Permission-Savefile
|
---|
| 595 | my @files = @_;
|
---|
| 596 | my @permlist = ();
|
---|
| 597 | foreach my $file (@files) {
|
---|
[227] | 598 | $file = "/" . $file;
|
---|
[239] | 599 | if ( -e $file ) {
|
---|
| 600 | my $info = stat($file) || die "failed to stat $file: aborting";
|
---|
| 601 | my $mode = get_type( $info->mode ) & 07777;
|
---|
[227] | 602 | my $modestring = sprintf( "%04o", $mode );
|
---|
[278] | 603 | my $uidnumber = $info->uid;
|
---|
| 604 | my $uid = getpwuid($uidnumber) || $uidnumber;
|
---|
| 605 | my $gidnumber = $info->gid;
|
---|
| 606 | my $gid = getgrgid($gidnumber) || $gidnumber;
|
---|
[227] | 607 | push(
|
---|
| 608 | @permlist,
|
---|
| 609 | sprintf( "%-55s %-17s %4d",
|
---|
[278] | 610 | $file, "${uid}:${gid}", $modestring )
|
---|
[227] | 611 | );
|
---|
| 612 | }
|
---|
[215] | 613 | }
|
---|
| 614 | return @permlist;
|
---|
| 615 | }
|
---|
[209] | 616 |
|
---|
[215] | 617 | sub get_type
|
---|
| 618 | {
|
---|
[209] | 619 |
|
---|
[215] | 620 | # Funktion übernommen aus /usr/bin/chkstat
|
---|
| 621 | my $S_IFLNK = 0120000; # symbolic link
|
---|
| 622 | my $S_IFREG = 0100000; # regular file
|
---|
| 623 | my $S_IFDIR = 0040000; # directory
|
---|
| 624 | my $S_IFCHAR = 0020000; # character device
|
---|
| 625 | my $S_IFBLK = 0060000; # block device
|
---|
| 626 | my $S_IFFIFO = 0010000; # fifo
|
---|
| 627 | my $S_IFSOCK = 0140000; # socket
|
---|
| 628 | my $S_IFMT = 0170000; # type of file
|
---|
[209] | 629 |
|
---|
[215] | 630 | my $S_m;
|
---|
| 631 | if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
|
---|
| 632 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
|
---|
| 633 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
|
---|
| 634 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
|
---|
| 635 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
|
---|
| 636 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
|
---|
| 637 | elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
|
---|
| 638 | $S_m;
|
---|
[209] | 639 | }
|
---|
| 640 |
|
---|
[186] | 641 | sub run_command
|
---|
| 642 | {
|
---|
[205] | 643 | my $command = shift;
|
---|
[186] | 644 |
|
---|
[205] | 645 | #print "executing command: " . $command . "\n";
|
---|
[186] | 646 |
|
---|
[205] | 647 | open( RESULT, $command . ' 2>&1 |' );
|
---|
| 648 | my @result = <RESULT>;
|
---|
| 649 | close(RESULT);
|
---|
| 650 | my $retcode = $? >> 8;
|
---|
[186] | 651 |
|
---|
[205] | 652 | #print @result;
|
---|
| 653 | #if( $retcode ) { print "return code: " . $retcode . "\n"; }
|
---|
[186] | 654 |
|
---|
[205] | 655 | return ( $retcode, @result );
|
---|
[186] | 656 | }
|
---|
| 657 |
|
---|
[205] | 658 | sub run_interactive
|
---|
| 659 | {
|
---|
[186] | 660 |
|
---|
[208] | 661 | if ($verbose) {
|
---|
[205] | 662 | print "run_interactive:" . join( " ", @_ ) . "\n";
|
---|
| 663 | }
|
---|
[196] | 664 |
|
---|
[205] | 665 | system(@_);
|
---|
| 666 | if ( $? == -1 ) {
|
---|
| 667 | printf "failed to execute: $!\n";
|
---|
| 668 | } elsif ( $? & 127 ) {
|
---|
| 669 | printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
|
---|
| 670 | ( $? & 128 ) ? 'with' : 'without';
|
---|
| 671 | } elsif ( $? >> 8 != 0 ) {
|
---|
| 672 | printf "child exited with value %d\n", $? >> 8;
|
---|
| 673 | }
|
---|
| 674 | return ( $? >> 8 );
|
---|
| 675 | }
|
---|
| 676 |
|
---|
[247] | 677 | sub svn_check_credentials( $$;$$ )
|
---|
[196] | 678 | {
|
---|
[205] | 679 | my $username = shift;
|
---|
| 680 | my $password = shift;
|
---|
| 681 |
|
---|
[252] | 682 | # check silently are allow user interaction?
|
---|
[247] | 683 | my $interactive = shift || 0;
|
---|
[220] | 684 |
|
---|
[247] | 685 | # default: exit program, if repository is not accessable
|
---|
| 686 | # (do not exit for 'init')
|
---|
| 687 | my $fatalerror = shift || 1;
|
---|
| 688 |
|
---|
| 689 | print "checking credentials ";
|
---|
| 690 |
|
---|
[239] | 691 | if ( !$username ) {
|
---|
| 692 | fatalerror("no username given");
|
---|
[238] | 693 | }
|
---|
| 694 |
|
---|
[239] | 695 | if ( !$password ) {
|
---|
| 696 | fatalerror("no password given");
|
---|
[238] | 697 | }
|
---|
| 698 |
|
---|
[247] | 699 | print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
|
---|
| 700 |
|
---|
[220] | 701 | # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
|
---|
| 702 | # therefore switching to "svn status"
|
---|
| 703 | # ( my $rc_update, my @result ) =
|
---|
| 704 | # run_command(
|
---|
| 705 | # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
|
---|
| 706 | # );
|
---|
| 707 | #print @result;
|
---|
| 708 |
|
---|
[247] | 709 | my $rc_update;
|
---|
[252] | 710 | if ($interactive) {
|
---|
[801] | 711 | $rc_update = run_interactive(
|
---|
[267] | 712 | "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
|
---|
[801] | 713 | );
|
---|
[247] | 714 | } else {
|
---|
[801] | 715 | ( $rc_update, my @result ) = run_command(
|
---|
[267] | 716 | "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
|
---|
[801] | 717 | );
|
---|
[252] | 718 |
|
---|
[247] | 719 | if ( $rc_update != 0 ) {
|
---|
| 720 | print "\n", @result;
|
---|
[252] | 721 | if ($fatalerror) {
|
---|
[247] | 722 | fatalerror();
|
---|
| 723 | }
|
---|
| 724 | return;
|
---|
| 725 | }
|
---|
[205] | 726 | }
|
---|
| 727 |
|
---|
[247] | 728 | # return success
|
---|
| 729 | return $rc_update == 0;
|
---|
[196] | 730 | }
|
---|
| 731 |
|
---|
[205] | 732 | sub svn_update( ;$ )
|
---|
| 733 | {
|
---|
[270] | 734 | my $update_path = shift || "";
|
---|
| 735 |
|
---|
[797] | 736 | # return value
|
---|
| 737 | my $update_ok = 1;
|
---|
| 738 |
|
---|
[270] | 739 | # use this flag to do only one update per run
|
---|
[275] | 740 | if ( !$svnRepositoryIsUptodate ) {
|
---|
[801] | 741 | ( my $rc_update, my @result ) = run_command(
|
---|
[275] | 742 | "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'"
|
---|
[801] | 743 | );
|
---|
[268] | 744 | print @result;
|
---|
| 745 | if ( $rc_update != 0 ) {
|
---|
[290] | 746 | error("failed to update local repository ($update_path)");
|
---|
[797] | 747 | $update_ok = 0;
|
---|
[270] | 748 | } elsif ( not $update_path ) {
|
---|
[275] | 749 |
|
---|
[270] | 750 | # set this flag if a full update is done
|
---|
[268] | 751 | $svnRepositoryIsUptodate = 1;
|
---|
| 752 | }
|
---|
[205] | 753 | }
|
---|
[797] | 754 | return $update_ok;
|
---|
[215] | 755 | }
|
---|
[196] | 756 |
|
---|
[271] | 757 | sub svn_ls( ;@ )
|
---|
[215] | 758 | {
|
---|
[270] | 759 | (
|
---|
| 760 | my $basename,
|
---|
| 761 | my $dirname_prod,
|
---|
| 762 | my $dirname_repo,
|
---|
| 763 | my $filename_prod,
|
---|
| 764 | my $filename_repo
|
---|
[801] | 765 | ) = get_filenames( $_[0] );
|
---|
[220] | 766 |
|
---|
[218] | 767 | # svn ls -R is better, but much, much slower
|
---|
| 768 | # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
|
---|
[270] | 769 |
|
---|
[271] | 770 | my @files = ();
|
---|
| 771 | my @links = ();
|
---|
| 772 | my @dirs = ();
|
---|
| 773 | my @others = ();
|
---|
| 774 |
|
---|
[289] | 775 | find(
|
---|
| 776 | {
|
---|
| 777 | wanted => sub {
|
---|
| 778 | my $name = normalize_path($File::Find::name);
|
---|
| 779 | $name =~ s|^$dirname_repo||;
|
---|
[290] | 780 |
|
---|
[289] | 781 | #print "($name)\n";# . $File::Find::dir . "\n";
|
---|
| 782 | if ( not $name ) {
|
---|
[290] | 783 |
|
---|
[289] | 784 | # name string is empty (top directory).
|
---|
| 785 | # do nothing
|
---|
| 786 | } elsif ( $name =~ m/\.svn/ ) {
|
---|
[275] | 787 |
|
---|
[289] | 788 | # skip svn meta data
|
---|
| 789 | } elsif ( -l $_ ) {
|
---|
[275] | 790 |
|
---|
[289] | 791 | # soft link
|
---|
| 792 | # important: check for links first
|
---|
| 793 | # to exclude them from further checks
|
---|
| 794 | push( @links, $name );
|
---|
| 795 | } elsif ( -d $_ ) {
|
---|
[290] | 796 |
|
---|
| 797 | # directories
|
---|
| 798 | push( @dirs, $name );
|
---|
[289] | 799 | } elsif ( -f $_ ) {
|
---|
[275] | 800 |
|
---|
[289] | 801 | # regular file
|
---|
| 802 | push( @files, $name );
|
---|
| 803 | } else {
|
---|
| 804 | push( @others, $name );
|
---|
| 805 | }
|
---|
[290] | 806 | }
|
---|
[289] | 807 | },
|
---|
| 808 | ($filename_repo)
|
---|
| 809 | );
|
---|
[271] | 810 |
|
---|
[287] | 811 | return ( sort( @dirs, @files ) );
|
---|
[205] | 812 | }
|
---|
| 813 |
|
---|
[274] | 814 | sub svn_revert( ;$ )
|
---|
| 815 | {
|
---|
| 816 | my $path = shift || $DASSCM_REPO;
|
---|
| 817 |
|
---|
[275] | 818 | ( my $rc_update, my @result ) = run_command("$SVN revert -R '$path'");
|
---|
[274] | 819 |
|
---|
| 820 | if ( $rc_update != 0 ) {
|
---|
| 821 | print "\n", @result;
|
---|
[275] | 822 | error("failed to revert subversion repository changes");
|
---|
[274] | 823 | }
|
---|
| 824 | }
|
---|
| 825 |
|
---|
[285] | 826 | sub svn_remove_unknown_files( ;$ )
|
---|
| 827 | {
|
---|
| 828 | my $path = shift || $DASSCM_REPO;
|
---|
| 829 |
|
---|
[290] | 830 | ( my $rc_update, my @result ) = run_command("$SVN status '$path'");
|
---|
[285] | 831 |
|
---|
| 832 | if ( $rc_update != 0 ) {
|
---|
| 833 | print "\n", @result;
|
---|
| 834 | error("failed to receive subversion repository information");
|
---|
| 835 | } else {
|
---|
[290] | 836 | foreach (@result) {
|
---|
| 837 | if (s/^\? +//) {
|
---|
[285] | 838 | chomp;
|
---|
[290] | 839 |
|
---|
[285] | 840 | # if file is unknown to subversion (line starts with "?")
|
---|
| 841 | # remove it
|
---|
| 842 | print "removing $_\n";
|
---|
[801] | 843 |
|
---|
[800] | 844 | # unlink doesn't work recursive, there "rm -rf" is used
|
---|
| 845 | #unlink($_);
|
---|
[801] | 846 | system("rm -rf $_");
|
---|
[285] | 847 | }
|
---|
| 848 | }
|
---|
| 849 | }
|
---|
| 850 | }
|
---|
| 851 |
|
---|
[268] | 852 | sub getModifiedFiles( ;$ )
|
---|
| 853 | {
|
---|
[270] | 854 | (
|
---|
| 855 | my $basename,
|
---|
| 856 | my $dirname_prod,
|
---|
| 857 | my $dirname_repo,
|
---|
| 858 | my $filename_prod,
|
---|
| 859 | my $filename_repo
|
---|
[801] | 860 | ) = get_filenames( $_[0] );
|
---|
[268] | 861 |
|
---|
[275] | 862 | my @files = svn_ls($filename_prod);
|
---|
[270] | 863 |
|
---|
[268] | 864 | # stores result from status (cvscheck)
|
---|
| 865 | my %removedfiles = ();
|
---|
| 866 | my %changedfiles = ();
|
---|
[278] | 867 | my %unknownfiles = ();
|
---|
[268] | 868 |
|
---|
| 869 | # create list of modified files
|
---|
| 870 | if (@files) {
|
---|
| 871 |
|
---|
| 872 | foreach my $file (@files) {
|
---|
| 873 |
|
---|
[271] | 874 | my $realfile = $dirname_prod . $file;
|
---|
| 875 | my $cvsworkfile = $dirname_repo . $file;
|
---|
[268] | 876 |
|
---|
| 877 | if ( -d $realfile ) {
|
---|
[290] | 878 |
|
---|
[278] | 879 | # directory
|
---|
[290] | 880 | if ( !-d "$cvsworkfile" ) {
|
---|
| 881 |
|
---|
[278] | 882 | # real is directory, repository is not. This is a problem
|
---|
| 883 | $changedfiles{"$realfile"} = $cvsworkfile;
|
---|
| 884 | }
|
---|
| 885 | } elsif ( !-e $realfile ) {
|
---|
| 886 | $removedfiles{"$realfile"} = $cvsworkfile;
|
---|
[268] | 887 | } elsif ( !-r $realfile ) {
|
---|
[290] | 888 |
|
---|
[278] | 889 | # don't have permission to read the file,
|
---|
| 890 | # can't check it
|
---|
| 891 | $unknownfiles{"$realfile"} = $cvsworkfile;
|
---|
[268] | 892 | } else {
|
---|
| 893 | ( -r "$cvsworkfile" )
|
---|
[278] | 894 | || fatalerror("failed to read $cvsworkfile");
|
---|
[268] | 895 | if ( compare( $cvsworkfile, $realfile ) != 0 ) {
|
---|
| 896 | $changedfiles{"$realfile"} = $cvsworkfile;
|
---|
| 897 | }
|
---|
| 898 | }
|
---|
| 899 | }
|
---|
| 900 | }
|
---|
| 901 |
|
---|
[278] | 902 | return ( \%changedfiles, \%removedfiles, \%unknownfiles );
|
---|
[268] | 903 | }
|
---|
| 904 |
|
---|
[238] | 905 | #
|
---|
| 906 | # from an array of files/dirs,
|
---|
| 907 | # generates list of files
|
---|
| 908 | # sorted by type
|
---|
| 909 | #
|
---|
[237] | 910 | sub get_files( @ )
|
---|
| 911 | {
|
---|
| 912 | my @files = ();
|
---|
| 913 | my @links = ();
|
---|
| 914 | my @dirs = ();
|
---|
| 915 | my @others = ();
|
---|
| 916 |
|
---|
[239] | 917 | if (@_) {
|
---|
| 918 | find(
|
---|
| 919 | {
|
---|
| 920 | wanted => sub {
|
---|
| 921 | my $fullname = cwd() . "/" . $_;
|
---|
| 922 | if ( -l $_ ) {
|
---|
| 923 |
|
---|
| 924 | # soft link
|
---|
| 925 | # important: check for links first
|
---|
| 926 | # to exclude them from further checks
|
---|
| 927 | push( @links, $fullname );
|
---|
| 928 | } elsif ( -d $_ ) {
|
---|
[271] | 929 |
|
---|
| 930 | # directories
|
---|
[239] | 931 | push( @dirs, $fullname );
|
---|
| 932 | } elsif ( -f $_ ) {
|
---|
| 933 |
|
---|
| 934 | # regular file
|
---|
| 935 | push( @files, $fullname );
|
---|
| 936 | } else {
|
---|
| 937 | push( @others, $fullname );
|
---|
| 938 | }
|
---|
| 939 | }
|
---|
| 940 | },
|
---|
| 941 | @_
|
---|
| 942 | );
|
---|
[237] | 943 | }
|
---|
| 944 |
|
---|
[239] | 945 | # don't rely on others.
|
---|
[237] | 946 | # If more specific file types are needed,
|
---|
| 947 | # they will be added
|
---|
| 948 | return {
|
---|
[239] | 949 | files => \@files,
|
---|
| 950 | links => \@links,
|
---|
| 951 | dirs => \@dirs,
|
---|
| 952 | others => \@others
|
---|
| 953 | };
|
---|
[237] | 954 | }
|
---|
| 955 |
|
---|
[888] | 956 | #
|
---|
| 957 | # use globbing to get lsit of files
|
---|
| 958 | # that matches the given prefix
|
---|
| 959 | # used for bash completion
|
---|
| 960 | #
|
---|
| 961 | sub get_complete_path_globbing( $ )
|
---|
| 962 | {
|
---|
| 963 | my $path = shift;
|
---|
| 964 |
|
---|
| 965 | # add globbing
|
---|
| 966 | $path .= "*";
|
---|
| 967 |
|
---|
| 968 | # get files
|
---|
[889] | 969 | my @files = glob($path);
|
---|
[888] | 970 |
|
---|
[889] | 971 | if ( $#files == 0 ) {
|
---|
[891] | 972 |
|
---|
| 973 | # if only one result is available
|
---|
| 974 | # and this result is a directory,
|
---|
| 975 | # add another result entry
|
---|
| 976 | # (directory with and withour trainling /),
|
---|
| 977 | # otherwise complete will stop here and continue with the next parameter
|
---|
| 978 | my $path = normalize_path( $files[0] );
|
---|
[889] | 979 | if ( -d $path ) {
|
---|
[891] | 980 | @files = ( substr( $path, 0, -1 ), $path );
|
---|
[888] | 981 | }
|
---|
[890] | 982 | } else {
|
---|
[891] | 983 |
|
---|
[890] | 984 | # add "/" to all directories
|
---|
[891] | 985 | @files = map( { normalize_path($_) } @files );
|
---|
[888] | 986 | }
|
---|
| 987 |
|
---|
| 988 | return @files;
|
---|
| 989 | }
|
---|
| 990 |
|
---|
[189] | 991 | #####################################################################
|
---|
| 992 | #
|
---|
[186] | 993 | # functions
|
---|
| 994 | sub help(;@)
|
---|
| 995 | {
|
---|
[205] | 996 | if ( @_ == 0 ) {
|
---|
| 997 | usage();
|
---|
| 998 | } else {
|
---|
| 999 | print "help for @_: ...\n";
|
---|
[214] | 1000 | usage();
|
---|
[205] | 1001 | }
|
---|
[186] | 1002 | }
|
---|
| 1003 |
|
---|
[203] | 1004 | sub login(@)
|
---|
| 1005 | {
|
---|
[205] | 1006 | check_parameter( @_, 1 );
|
---|
| 1007 | check_env();
|
---|
[203] | 1008 |
|
---|
[235] | 1009 | my $input_username = $_[0];
|
---|
[214] | 1010 |
|
---|
| 1011 | if ( not $input_username ) {
|
---|
| 1012 | my $output_username = "";
|
---|
| 1013 | if ($DASSCM_USERNAME) {
|
---|
| 1014 | $output_username = " ($DASSCM_USERNAME)";
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | print "Enter DASSCM user name", $output_username, ": ";
|
---|
| 1018 | $input_username = <STDIN>;
|
---|
| 1019 | chomp($input_username);
|
---|
[247] | 1020 |
|
---|
| 1021 | $input_username = $input_username || $DASSCM_USERNAME;
|
---|
[205] | 1022 | }
|
---|
[203] | 1023 |
|
---|
[205] | 1024 | # hidden password input
|
---|
[247] | 1025 | print "Enter password for $input_username: ";
|
---|
[205] | 1026 | ReadMode('noecho');
|
---|
| 1027 | my $input_password = <STDIN>;
|
---|
| 1028 | ReadMode('normal');
|
---|
| 1029 | chomp($input_password);
|
---|
[220] | 1030 | print "\n";
|
---|
[203] | 1031 |
|
---|
[247] | 1032 | # checking checkout username/password
|
---|
[252] | 1033 | svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
|
---|
| 1034 | $DASSCM_CHECKOUT_PASSWORD );
|
---|
[247] | 1035 | print "checkout access okay\n";
|
---|
[205] | 1036 |
|
---|
[247] | 1037 | svn_check_credentials( $input_username, $input_password );
|
---|
| 1038 |
|
---|
[205] | 1039 | #
|
---|
| 1040 | # set environment variables
|
---|
| 1041 | #
|
---|
[267] | 1042 | $ENV{'DASSCM_USERNAME'} = "$input_username";
|
---|
| 1043 | $ENV{'DASSCM_PASSWORD'} = "$input_password";
|
---|
[205] | 1044 |
|
---|
[209] | 1045 | print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
|
---|
| 1046 | "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
|
---|
| 1047 | "DASSCM_REPO: $DASSCM_REPO\n",
|
---|
[278] | 1048 | "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
|
---|
[205] | 1049 |
|
---|
[278] | 1050 | status();
|
---|
| 1051 |
|
---|
| 1052 | print "\n[dasscm shell]\n\n";
|
---|
[235] | 1053 | my $shell = $SHELL || "bash";
|
---|
| 1054 | exec($shell) or die "failed to start new shell";
|
---|
[203] | 1055 | }
|
---|
| 1056 |
|
---|
[260] | 1057 | #
|
---|
| 1058 | # initialize local checkout directory (initial checkout)
|
---|
| 1059 | #
|
---|
[205] | 1060 | sub init(@)
|
---|
| 1061 | {
|
---|
| 1062 | check_parameter( @_, 1 );
|
---|
| 1063 | check_env();
|
---|
| 1064 |
|
---|
[235] | 1065 | # don't do repository creation (svn mkdir) here,
|
---|
| 1066 | # because then their must be a lot of prior checks
|
---|
| 1067 |
|
---|
[205] | 1068 | # update complete repository
|
---|
[216] | 1069 | # and create permission file
|
---|
[801] | 1070 | my $retcode = run_interactive(
|
---|
[286] | 1071 | "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; mkdir -p `dirname $DASSCM_PERMISSION_FILE`; touch $DASSCM_PERMISSION_FILE"
|
---|
[801] | 1072 | );
|
---|
[205] | 1073 | }
|
---|
| 1074 |
|
---|
[215] | 1075 | sub ls(@)
|
---|
[186] | 1076 | {
|
---|
[205] | 1077 | check_parameter( @_, 1 );
|
---|
| 1078 | check_env();
|
---|
[186] | 1079 |
|
---|
[271] | 1080 | my @files = svn_ls(@_);
|
---|
[215] | 1081 |
|
---|
[275] | 1082 | if (@files) {
|
---|
[274] | 1083 | print join( "\n", @files );
|
---|
| 1084 | print "\n";
|
---|
| 1085 | }
|
---|
[215] | 1086 | }
|
---|
| 1087 |
|
---|
| 1088 | sub update(@)
|
---|
| 1089 | {
|
---|
| 1090 | check_parameter( @_, 1 );
|
---|
| 1091 | check_env();
|
---|
| 1092 |
|
---|
| 1093 | #
|
---|
| 1094 | # update local repository
|
---|
| 1095 | #
|
---|
| 1096 | svn_update();
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
[237] | 1099 | #
|
---|
| 1100 | # helper function for "add" command
|
---|
| 1101 | #
|
---|
[215] | 1102 | sub add_helper(@)
|
---|
| 1103 | {
|
---|
[205] | 1104 | (
|
---|
| 1105 | my $basename,
|
---|
| 1106 | my $dirname_prod,
|
---|
| 1107 | my $dirname_repo,
|
---|
| 1108 | my $filename_prod,
|
---|
| 1109 | my $filename_repo
|
---|
[801] | 1110 | ) = get_filenames( $_[0] );
|
---|
[186] | 1111 |
|
---|
[274] | 1112 | mkpath($dirname_repo);
|
---|
[889] | 1113 | copy_file_to_repository($filename_prod);
|
---|
[186] | 1114 |
|
---|
[274] | 1115 | # already checked in?
|
---|
| 1116 | chdir $DASSCM_REPO;
|
---|
[205] | 1117 |
|
---|
[274] | 1118 | # also add the path to filename.
|
---|
| 1119 | for my $dir ( split( '/', $dirname_prod ) ) {
|
---|
| 1120 | if ($dir) {
|
---|
[275] | 1121 | my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
|
---|
[274] | 1122 | if ( $rc > 0 ) {
|
---|
| 1123 | print join( "\n", @out );
|
---|
[205] | 1124 | }
|
---|
[274] | 1125 | chdir $dir;
|
---|
[205] | 1126 | }
|
---|
| 1127 | }
|
---|
[275] | 1128 | my ( $rc, @out ) = run_command("$SVN add '$basename'");
|
---|
[274] | 1129 | if ( $rc > 0 ) {
|
---|
| 1130 | print join( "\n", @out );
|
---|
| 1131 | }
|
---|
| 1132 | chdir $StartDirectory;
|
---|
| 1133 |
|
---|
[215] | 1134 | }
|
---|
[205] | 1135 |
|
---|
[215] | 1136 | #
|
---|
[274] | 1137 | # adding new files (or directories)
|
---|
[215] | 1138 | #
|
---|
| 1139 | sub add(@)
|
---|
| 1140 | {
|
---|
| 1141 | check_parameter( @_, 1 );
|
---|
| 1142 | check_env();
|
---|
| 1143 |
|
---|
| 1144 | #
|
---|
| 1145 | # update local repository
|
---|
| 1146 | #
|
---|
| 1147 | svn_update();
|
---|
| 1148 |
|
---|
[237] | 1149 | # get all regular files and links
|
---|
[239] | 1150 | my $href_files = get_files(@_);
|
---|
[220] | 1151 |
|
---|
[237] | 1152 | #print Dumper( $href_files );
|
---|
| 1153 |
|
---|
[239] | 1154 | my @files = @{ $href_files->{files} };
|
---|
| 1155 | my @links = @{ $href_files->{links} };
|
---|
[237] | 1156 |
|
---|
[239] | 1157 | if (@files) {
|
---|
[237] | 1158 | my $number = $#files + 1;
|
---|
| 1159 | print "files to check-in ($number): \n";
|
---|
| 1160 | print join( "\n", @files );
|
---|
| 1161 | print "\n";
|
---|
| 1162 | }
|
---|
| 1163 |
|
---|
[268] | 1164 | # TODO: check in links and also link target? At least warn about link target
|
---|
[239] | 1165 | if (@links) {
|
---|
[237] | 1166 | my $number = $#links + 1;
|
---|
| 1167 | print "\n";
|
---|
| 1168 | print "ignoring links ($number):\n";
|
---|
| 1169 | print join( "\n", @links );
|
---|
| 1170 | print "\n";
|
---|
| 1171 | }
|
---|
| 1172 |
|
---|
[238] | 1173 | # TODO: confirm
|
---|
| 1174 |
|
---|
[237] | 1175 | # copy files one by one to local repository
|
---|
| 1176 | for my $file (@files) {
|
---|
[239] | 1177 |
|
---|
[233] | 1178 | # add file
|
---|
[239] | 1179 | add_helper($file);
|
---|
[233] | 1180 | }
|
---|
| 1181 |
|
---|
[215] | 1182 | # create new permissions file
|
---|
| 1183 | permissions();
|
---|
[220] | 1184 |
|
---|
[215] | 1185 | # add permissions file
|
---|
[286] | 1186 | add_helper($DASSCM_PERMISSION_FILE);
|
---|
[215] | 1187 |
|
---|
[205] | 1188 | if ( $options{'message'} ) {
|
---|
| 1189 | $svnOptions .= " --message \"$options{'message'}\" ";
|
---|
| 1190 | }
|
---|
| 1191 |
|
---|
[239] | 1192 | # commit calls $EDITOR.
|
---|
[237] | 1193 | # use "interactive" here, to display output
|
---|
[801] | 1194 | my $retcode = run_interactive(
|
---|
[267] | 1195 | "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
|
---|
[801] | 1196 | );
|
---|
[205] | 1197 |
|
---|
[274] | 1198 | # svn commit does not deliever an error return code, if commit is canceld,
|
---|
| 1199 | # so a revert is performed in any case
|
---|
| 1200 | svn_revert();
|
---|
[186] | 1201 | }
|
---|
| 1202 |
|
---|
[271] | 1203 | #
|
---|
| 1204 | # checks in all modified files
|
---|
| 1205 | #
|
---|
| 1206 | sub commit(@)
|
---|
| 1207 | {
|
---|
| 1208 | check_parameter( @_, 1 );
|
---|
| 1209 | check_env();
|
---|
| 1210 |
|
---|
| 1211 | (
|
---|
| 1212 | my $basename,
|
---|
| 1213 | my $dirname_prod,
|
---|
| 1214 | my $dirname_repo,
|
---|
| 1215 | my $filename_prod,
|
---|
| 1216 | my $filename_repo
|
---|
[801] | 1217 | ) = get_filenames( $_[0] );
|
---|
[271] | 1218 |
|
---|
| 1219 | #
|
---|
| 1220 | # update local repository
|
---|
| 1221 | #
|
---|
| 1222 | svn_update();
|
---|
| 1223 |
|
---|
[275] | 1224 | ( my $refChangedFiles, my $refRemovedFiles ) =
|
---|
| 1225 | getModifiedFiles($filename_prod);
|
---|
[271] | 1226 | my %changedfiles = %{$refChangedFiles};
|
---|
| 1227 | my %removedfiles = %{$refRemovedFiles};
|
---|
| 1228 |
|
---|
[275] | 1229 | if (%removedfiles) {
|
---|
| 1230 | my $removedFilesString =
|
---|
| 1231 | '"' . join( '" "', values(%removedfiles) ) . '"';
|
---|
| 1232 | my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
|
---|
[271] | 1233 | if ( $rc > 0 ) {
|
---|
| 1234 | print join( "\n", @out );
|
---|
| 1235 | }
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 | # copy files one by one to local repository
|
---|
| 1239 | for my $file ( keys(%changedfiles) ) {
|
---|
[275] | 1240 | copy_file_to_repository($file);
|
---|
[271] | 1241 | }
|
---|
| 1242 |
|
---|
| 1243 | # create new permissions file
|
---|
| 1244 | permissions();
|
---|
| 1245 |
|
---|
| 1246 | # add permissions file
|
---|
[286] | 1247 | add_helper($DASSCM_PERMISSION_FILE);
|
---|
[271] | 1248 |
|
---|
| 1249 | if ( $options{'message'} ) {
|
---|
| 1250 | $svnOptions .= " --message \"$options{'message'}\" ";
|
---|
| 1251 | }
|
---|
| 1252 |
|
---|
| 1253 | # commit calls $EDITOR.
|
---|
| 1254 | # use "interactive" here, to display output
|
---|
[801] | 1255 | my $retcode = run_interactive(
|
---|
[271] | 1256 | "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
|
---|
[801] | 1257 | );
|
---|
[274] | 1258 |
|
---|
| 1259 | # svn commit does not deliever an error return code, if commit is canceld,
|
---|
| 1260 | # so a revert is performed in any case
|
---|
| 1261 | svn_revert();
|
---|
[271] | 1262 | }
|
---|
| 1263 |
|
---|
[800] | 1264 | #
|
---|
| 1265 | # revert: copies files back from repository to system
|
---|
| 1266 | #
|
---|
| 1267 | sub revert(@)
|
---|
| 1268 | {
|
---|
| 1269 | check_parameter( @_, 1 );
|
---|
| 1270 | check_env();
|
---|
| 1271 |
|
---|
| 1272 | (
|
---|
| 1273 | my $basename,
|
---|
| 1274 | my $dirname_prod,
|
---|
| 1275 | my $dirname_repo,
|
---|
| 1276 | my $filename_prod,
|
---|
| 1277 | my $filename_repo
|
---|
[801] | 1278 | ) = get_filenames( $_[0] );
|
---|
[800] | 1279 |
|
---|
| 1280 | # return code for the shell
|
---|
| 1281 | # default: error
|
---|
| 1282 | my $return_code = $RETURN_OK;
|
---|
| 1283 |
|
---|
| 1284 | # cleanup repository
|
---|
[801] | 1285 | cleanup();
|
---|
[889] | 1286 |
|
---|
[800] | 1287 | #svn_update();
|
---|
| 1288 |
|
---|
| 1289 | ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
|
---|
| 1290 | getModifiedFiles($filename_prod);
|
---|
| 1291 | my %changedfiles = %{$refChangedFiles};
|
---|
| 1292 | my %removedfiles = %{$refRemovedFiles};
|
---|
| 1293 | my %unknownfiles = %{$refUnknownFiles};
|
---|
| 1294 |
|
---|
| 1295 | if ( %removedfiles or %changedfiles or %unknownfiles ) {
|
---|
| 1296 |
|
---|
| 1297 | if (%removedfiles) {
|
---|
| 1298 | print "DELETED files and directories. Recreated from repository:\n";
|
---|
[801] | 1299 | my @removedPaths =
|
---|
| 1300 | ( sort { length $a > length $b } keys %removedfiles );
|
---|
[800] | 1301 | print join( "\n", @removedPaths ) . "\n\n";
|
---|
| 1302 |
|
---|
| 1303 | # copy files one by one from local repository to system
|
---|
| 1304 | # and also create directories
|
---|
| 1305 | # paths are sorted, so that directories are created first
|
---|
[801] | 1306 | for my $real_path (@removedPaths) {
|
---|
| 1307 | if ( -d $removedfiles{"$real_path"} ) {
|
---|
[800] | 1308 | mkpath("$real_path");
|
---|
| 1309 | } else {
|
---|
[889] | 1310 | copy_file_from_repository_to_system($real_path);
|
---|
[800] | 1311 | }
|
---|
| 1312 | }
|
---|
| 1313 | }
|
---|
| 1314 |
|
---|
| 1315 | if (%changedfiles) {
|
---|
| 1316 | print "MODIFIED files. Copied from repository to the system:\n";
|
---|
| 1317 | print join( "\n", ( keys %changedfiles ) ) . "\n\n";
|
---|
| 1318 |
|
---|
| 1319 | # copy files one by one from local repository to system
|
---|
| 1320 | for my $real_file ( keys(%changedfiles) ) {
|
---|
[889] | 1321 | copy_file_from_repository_to_system($real_file);
|
---|
[800] | 1322 | }
|
---|
| 1323 |
|
---|
| 1324 | }
|
---|
| 1325 |
|
---|
| 1326 | if (%unknownfiles) {
|
---|
| 1327 | print "UNKNOWN: insufficient permission to check files:\n";
|
---|
| 1328 | print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
|
---|
| 1329 |
|
---|
| 1330 | $return_code = $RETURN_NOK;
|
---|
| 1331 | }
|
---|
| 1332 |
|
---|
| 1333 | } else {
|
---|
| 1334 | print "no modified files found in $dirname_repo\n";
|
---|
| 1335 | }
|
---|
| 1336 |
|
---|
| 1337 | return $return_code;
|
---|
| 1338 | }
|
---|
| 1339 |
|
---|
[193] | 1340 | sub blame(@)
|
---|
| 1341 | {
|
---|
[205] | 1342 | check_parameter( @_, 1 );
|
---|
| 1343 | check_env();
|
---|
[193] | 1344 |
|
---|
[205] | 1345 | (
|
---|
| 1346 | my $basename,
|
---|
| 1347 | my $dirname_prod,
|
---|
| 1348 | my $dirname_repo,
|
---|
| 1349 | my $filename_prod,
|
---|
| 1350 | my $filename_repo
|
---|
[801] | 1351 | ) = get_filenames( $_[0] );
|
---|
[205] | 1352 |
|
---|
| 1353 | my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
|
---|
[193] | 1354 | }
|
---|
| 1355 |
|
---|
[187] | 1356 | sub diff(@)
|
---|
| 1357 | {
|
---|
[205] | 1358 | check_parameter( @_, 1 );
|
---|
| 1359 | check_env();
|
---|
[187] | 1360 |
|
---|
[205] | 1361 | (
|
---|
| 1362 | my $basename,
|
---|
| 1363 | my $dirname_prod,
|
---|
| 1364 | my $dirname_repo,
|
---|
| 1365 | my $filename_prod,
|
---|
| 1366 | my $filename_repo
|
---|
[801] | 1367 | ) = get_filenames( $_[0] );
|
---|
[205] | 1368 |
|
---|
| 1369 | #print "$basename,$dirname_prod,$dirname_repo\n";
|
---|
| 1370 |
|
---|
[797] | 1371 | svn_update();
|
---|
[205] | 1372 |
|
---|
[238] | 1373 | ( my $rc_diff, my @diff_result ) =
|
---|
[239] | 1374 | run_command( $diff . " $filename_repo $filename_prod" );
|
---|
[238] | 1375 |
|
---|
| 1376 | print @diff_result;
|
---|
[187] | 1377 | }
|
---|
| 1378 |
|
---|
[209] | 1379 | sub status(@)
|
---|
| 1380 | {
|
---|
| 1381 | check_parameter( @_, 1 );
|
---|
| 1382 | check_env();
|
---|
| 1383 |
|
---|
[270] | 1384 | (
|
---|
| 1385 | my $basename,
|
---|
| 1386 | my $dirname_prod,
|
---|
| 1387 | my $dirname_repo,
|
---|
| 1388 | my $filename_prod,
|
---|
| 1389 | my $filename_repo
|
---|
[801] | 1390 | ) = get_filenames( $_[0] || "/" );
|
---|
[270] | 1391 |
|
---|
[252] | 1392 | # return code for the shell
|
---|
| 1393 | # default: error
|
---|
| 1394 | my $return_code = $RETURN_NOK;
|
---|
| 1395 |
|
---|
[209] | 1396 | #
|
---|
| 1397 | # update local repository
|
---|
| 1398 | #
|
---|
[278] | 1399 | #svn_update( $filename_prod );
|
---|
[209] | 1400 |
|
---|
[278] | 1401 | # check, if permissions have changed
|
---|
| 1402 | permissions();
|
---|
| 1403 |
|
---|
| 1404 | # get modified files
|
---|
| 1405 | ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
|
---|
[275] | 1406 | getModifiedFiles($dirname_prod);
|
---|
[268] | 1407 | my %changedfiles = %{$refChangedFiles};
|
---|
| 1408 | my %removedfiles = %{$refRemovedFiles};
|
---|
[278] | 1409 | my %unknownfiles = %{$refUnknownFiles};
|
---|
[209] | 1410 |
|
---|
[278] | 1411 | if ( %removedfiles or %changedfiles or %unknownfiles ) {
|
---|
| 1412 |
|
---|
[215] | 1413 | if (%removedfiles) {
|
---|
[278] | 1414 | print "DELETED: files found in repository, but not in system:\n";
|
---|
[800] | 1415 | print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
|
---|
[209] | 1416 | }
|
---|
| 1417 |
|
---|
[215] | 1418 | if (%changedfiles) {
|
---|
[278] | 1419 | print "MODIFIED: files differs between repository and system:\n";
|
---|
| 1420 | print join( "\n", ( keys %changedfiles ) ) . "\n\n";
|
---|
[209] | 1421 | }
|
---|
[278] | 1422 |
|
---|
| 1423 | if (%unknownfiles) {
|
---|
| 1424 | print "UNKNOWN: insufficient permission to check files:\n";
|
---|
| 1425 | print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
|
---|
| 1426 | }
|
---|
| 1427 |
|
---|
[209] | 1428 | } else {
|
---|
[270] | 1429 | print "no modified files found in $dirname_repo\n";
|
---|
[252] | 1430 | $return_code = $RETURN_OK;
|
---|
[209] | 1431 | }
|
---|
[215] | 1432 |
|
---|
[252] | 1433 | return $return_code;
|
---|
[215] | 1434 | }
|
---|
[209] | 1435 |
|
---|
[277] | 1436 | #
|
---|
| 1437 | # return short status in Nagios plugin conform way
|
---|
| 1438 | #
|
---|
| 1439 | sub check()
|
---|
| 1440 | {
|
---|
| 1441 | check_env();
|
---|
| 1442 |
|
---|
| 1443 | # return code for the shell
|
---|
[290] | 1444 | my $return_code = $RETURN_OK;
|
---|
[277] | 1445 | my $return_string = "OK: no modified files";
|
---|
| 1446 |
|
---|
[278] | 1447 | # check, if permissions have changed
|
---|
| 1448 | permissions();
|
---|
| 1449 |
|
---|
| 1450 | # get modified files
|
---|
| 1451 | ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
|
---|
[290] | 1452 | getModifiedFiles("/");
|
---|
[277] | 1453 | my %changedfiles = %{$refChangedFiles};
|
---|
| 1454 | my %removedfiles = %{$refRemovedFiles};
|
---|
[278] | 1455 | my %unknownfiles = %{$refUnknownFiles};
|
---|
[277] | 1456 |
|
---|
| 1457 | if ( %removedfiles or %changedfiles ) {
|
---|
| 1458 | $return_string = "Warning: ";
|
---|
[290] | 1459 | if (%changedfiles) {
|
---|
| 1460 | $return_string .=
|
---|
| 1461 | "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
|
---|
[277] | 1462 | }
|
---|
[290] | 1463 | if (%removedfiles) {
|
---|
| 1464 | $return_string .=
|
---|
| 1465 | "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
|
---|
[277] | 1466 | }
|
---|
[278] | 1467 | if (%unknownfiles) {
|
---|
[290] | 1468 | $return_string .=
|
---|
| 1469 | "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
|
---|
[278] | 1470 | }
|
---|
[277] | 1471 | $return_code = $RETURN_WARN;
|
---|
| 1472 | }
|
---|
| 1473 |
|
---|
| 1474 | # addition nagios Service Status
|
---|
| 1475 | #Critical
|
---|
| 1476 | #Unknown
|
---|
| 1477 |
|
---|
[290] | 1478 | print "$return_string\n";
|
---|
[277] | 1479 | return $return_code;
|
---|
| 1480 | }
|
---|
| 1481 |
|
---|
[270] | 1482 | sub permissions()
|
---|
[215] | 1483 | {
|
---|
| 1484 | check_env();
|
---|
| 1485 |
|
---|
[278] | 1486 | my $return_code = $RETURN_OK;
|
---|
| 1487 |
|
---|
[215] | 1488 | #
|
---|
| 1489 | # update local repository
|
---|
| 1490 | #
|
---|
| 1491 | #svn_update();
|
---|
| 1492 |
|
---|
[220] | 1493 | my $dir = $DASSCM_REPO;
|
---|
[275] | 1494 | my @files = svn_ls("/");
|
---|
[215] | 1495 |
|
---|
| 1496 | if (@files) {
|
---|
| 1497 |
|
---|
| 1498 | # generieren der Permissions
|
---|
| 1499 | my @permissions = generatePermissionList(@files);
|
---|
| 1500 | my $OUTFILE;
|
---|
| 1501 | my $tofile = 0; # Status für schreiben in File
|
---|
[220] | 1502 |
|
---|
[286] | 1503 | if ( -w dirname($DASSCM_PERMISSION_FILE) ) {
|
---|
[215] | 1504 |
|
---|
| 1505 | # Verzeichnis existiert => schreiben
|
---|
[286] | 1506 | open( OUTFILE, ">$DASSCM_PERMISSION_FILE" )
|
---|
| 1507 | || die("failed to write to $DASSCM_PERMISSION_FILE: $!");
|
---|
[215] | 1508 | $tofile = 1; # Merken, daß in File geschrieben wird
|
---|
| 1509 | print OUTFILE "#\n";
|
---|
| 1510 | print OUTFILE "# created by dasscm permissions\n";
|
---|
[220] | 1511 | print OUTFILE
|
---|
| 1512 | "# It is intended to be used for restoring permissions\n";
|
---|
[287] | 1513 | print OUTFILE "#\n";
|
---|
[215] | 1514 | } else {
|
---|
| 1515 |
|
---|
[290] | 1516 | if ( $command eq "permission" ) {
|
---|
| 1517 |
|
---|
[278] | 1518 | # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
|
---|
| 1519 | # Alias Filehandle für stdout erzeugen
|
---|
| 1520 | $return_code = $RETURN_WARN;
|
---|
[290] | 1521 | *OUTFILE = *STDOUT;
|
---|
[278] | 1522 | } else {
|
---|
[290] | 1523 |
|
---|
[278] | 1524 | # TODO: improve this. Check for diff?
|
---|
| 1525 | $return_code = $RETURN_CRIT;
|
---|
| 1526 | return $return_code;
|
---|
| 1527 | }
|
---|
[215] | 1528 | }
|
---|
[278] | 1529 |
|
---|
[215] | 1530 | foreach my $line (@permissions) {
|
---|
| 1531 | print OUTFILE "$line\n";
|
---|
| 1532 | }
|
---|
| 1533 |
|
---|
[220] | 1534 | if ($tofile) {
|
---|
[215] | 1535 | close(OUTFILE);
|
---|
| 1536 | }
|
---|
| 1537 | }
|
---|
[278] | 1538 |
|
---|
| 1539 | return $return_code;
|
---|
[209] | 1540 | }
|
---|
| 1541 |
|
---|
[274] | 1542 | #
|
---|
| 1543 | # remove all uncommited changes in the repository
|
---|
| 1544 | #
|
---|
| 1545 | sub cleanup()
|
---|
| 1546 | {
|
---|
| 1547 | check_env();
|
---|
[268] | 1548 |
|
---|
[275] | 1549 | svn_revert($DASSCM_REPO);
|
---|
[285] | 1550 | svn_remove_unknown_files($DASSCM_REPO);
|
---|
[274] | 1551 | }
|
---|
| 1552 |
|
---|
[884] | 1553 | #
|
---|
| 1554 | # used for bash completion
|
---|
| 1555 | # prints the next possible command line parameters
|
---|
| 1556 | #
|
---|
| 1557 | sub complete(@)
|
---|
| 1558 | {
|
---|
| 1559 | use Data::Dumper;
|
---|
| 1560 |
|
---|
| 1561 | my $number_arguments = @_;
|
---|
[889] | 1562 | my @input = @_;
|
---|
[884] | 1563 |
|
---|
| 1564 | if ( $number_arguments <= 1 ) {
|
---|
| 1565 |
|
---|
[887] | 1566 | # complete dasscm commands
|
---|
| 1567 | my $input = $input[0] || "";
|
---|
[884] | 1568 | foreach my $i ( keys %COMMANDS ) {
|
---|
| 1569 | $_ = $i;
|
---|
[889] | 1570 | if (m/^$input/) {
|
---|
[891] | 1571 |
|
---|
| 1572 | #my $command = get_command_uniform_name($i);
|
---|
| 1573 | #print $command, "\n";
|
---|
| 1574 | print $i, "\n";
|
---|
[884] | 1575 | }
|
---|
| 1576 | }
|
---|
| 1577 | } else {
|
---|
[887] | 1578 |
|
---|
| 1579 | # complete dasscm parameter
|
---|
[889] | 1580 | my $command = get_command_uniform_name( $input[0] );
|
---|
| 1581 | my @params = get_command_possible_params( $input[0] );
|
---|
| 1582 | if ($verbose) { print "params: ", Dumper(@params); }
|
---|
[891] | 1583 |
|
---|
| 1584 | # arg 1: dasscm
|
---|
| 1585 | # arg 2: command
|
---|
| 1586 | # arg 3-x: parameter, therefore parameter_number = $number_arguments - 2
|
---|
| 1587 | my $parameter_number = $number_arguments - 2;
|
---|
| 1588 | if ( defined( $params[$parameter_number] )
|
---|
| 1589 | && $params[$parameter_number] )
|
---|
[889] | 1590 | {
|
---|
[891] | 1591 | my $param = $params[$parameter_number];
|
---|
| 1592 | if ($verbose) { print "param used: ", $param, "\n"; }
|
---|
| 1593 | if ( $param eq "PATH_PROD" ) {
|
---|
[889] | 1594 | complete_path( $input[ $number_arguments - 1 ] );
|
---|
[891] | 1595 | } elsif ( $param eq "PATH_REPO" ) {
|
---|
[889] | 1596 | complete_repopath( $input[ $number_arguments - 1 ] );
|
---|
[887] | 1597 | }
|
---|
[884] | 1598 | }
|
---|
| 1599 | }
|
---|
| 1600 | }
|
---|
| 1601 |
|
---|
[887] | 1602 | sub complete_path(@)
|
---|
| 1603 | {
|
---|
| 1604 | check_parameter( @_, 1 );
|
---|
| 1605 | check_env();
|
---|
| 1606 |
|
---|
| 1607 | (
|
---|
| 1608 | my $basename,
|
---|
| 1609 | my $dirname_prod,
|
---|
| 1610 | my $dirname_repo,
|
---|
| 1611 | my $filename_prod,
|
---|
| 1612 | my $filename_repo
|
---|
| 1613 | ) = get_filenames( $_[0] );
|
---|
| 1614 |
|
---|
[889] | 1615 | my @files = get_complete_path_globbing($filename_prod);
|
---|
[887] | 1616 |
|
---|
| 1617 | if (@files) {
|
---|
| 1618 | print join( "\n", @files );
|
---|
| 1619 | print "\n";
|
---|
| 1620 | }
|
---|
| 1621 | }
|
---|
| 1622 |
|
---|
| 1623 | sub complete_repopath(@)
|
---|
| 1624 | {
|
---|
| 1625 | check_parameter( @_, 1 );
|
---|
| 1626 | check_env();
|
---|
| 1627 |
|
---|
| 1628 | (
|
---|
| 1629 | my $basename,
|
---|
| 1630 | my $dirname_prod,
|
---|
| 1631 | my $dirname_repo,
|
---|
| 1632 | my $filename_prod,
|
---|
| 1633 | my $filename_repo
|
---|
| 1634 | ) = get_filenames( $_[0] );
|
---|
| 1635 |
|
---|
[889] | 1636 | my @files = get_complete_path_globbing($filename_repo);
|
---|
| 1637 |
|
---|
[887] | 1638 | if (@files) {
|
---|
[889] | 1639 |
|
---|
[887] | 1640 | # remove DASSCM_REPO path again
|
---|
[889] | 1641 | print join(
|
---|
| 1642 | "\n",
|
---|
| 1643 | map( {
|
---|
| 1644 | s|^${DASSCM_REPO}|/|;
|
---|
| 1645 | $_
|
---|
| 1646 | } @files )
|
---|
| 1647 | );
|
---|
[887] | 1648 | print "\n";
|
---|
| 1649 | }
|
---|
| 1650 |
|
---|
| 1651 | }
|
---|
| 1652 |
|
---|
[189] | 1653 | #####################################################################
|
---|
| 1654 | #
|
---|
[186] | 1655 | # main
|
---|
[189] | 1656 | #
|
---|
[186] | 1657 |
|
---|
[252] | 1658 | my $return_code = $RETURN_OK;
|
---|
[186] | 1659 | my $number_arguments = @ARGV;
|
---|
| 1660 |
|
---|
[205] | 1661 | if ( $number_arguments > 0 ) {
|
---|
[186] | 1662 |
|
---|
[205] | 1663 | # get subcommand and remove it from @ARGV
|
---|
[889] | 1664 | $command = get_command_uniform_name( $ARGV[0] );
|
---|
[205] | 1665 | shift @ARGV;
|
---|
[196] | 1666 |
|
---|
[205] | 1667 | $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
|
---|
| 1668 | $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
|
---|
[196] | 1669 |
|
---|
[205] | 1670 | # TODO: check variables
|
---|
| 1671 | $DASSCM_SVN_REPOSITORY =
|
---|
| 1672 | $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
|
---|
| 1673 |
|
---|
[247] | 1674 | $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
|
---|
| 1675 | $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
|
---|
[205] | 1676 |
|
---|
| 1677 | #
|
---|
| 1678 | # if a user is given by dasscm configuration file, we use it.
|
---|
| 1679 | # Otherwise we expect that read-only account is configured
|
---|
| 1680 | # as local subversion configuration.
|
---|
| 1681 | # If this is also not the case,
|
---|
| 1682 | # user is required to type username and password.
|
---|
| 1683 | # This will be stored as local subversion configuration thereafter.
|
---|
| 1684 | #
|
---|
| 1685 | if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
|
---|
| 1686 | $svnCheckoutCredentials =
|
---|
| 1687 | " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
|
---|
| 1688 | }
|
---|
| 1689 |
|
---|
[290] | 1690 | $DASSCM_PERMISSION_FILE = $config->{'DASSCM_PERMISSION_FILE'}
|
---|
| 1691 | || "/etc/permissions.d/dasscm.permission_backup";
|
---|
[286] | 1692 |
|
---|
[205] | 1693 | # get command line options and store them in options hash
|
---|
[214] | 1694 | my $result = GetOptions( \%options, 'verbose', 'message=s' );
|
---|
[205] | 1695 |
|
---|
| 1696 | # print options
|
---|
| 1697 | foreach my $option ( keys %options ) {
|
---|
[215] | 1698 | print "${option}: $options{$option}\n";
|
---|
[205] | 1699 | }
|
---|
| 1700 |
|
---|
[214] | 1701 | # set verbose to command line option
|
---|
| 1702 | $verbose = $options{'verbose'};
|
---|
| 1703 |
|
---|
| 1704 | #
|
---|
| 1705 | # action accordinly to command are taken
|
---|
| 1706 | # $command is rewritten in standard format,
|
---|
| 1707 | # so we can test for it later on more simply
|
---|
| 1708 | #
|
---|
[884] | 1709 |
|
---|
[889] | 1710 | if ( get_command_function($command) ) {
|
---|
| 1711 | &{ get_command_function($command) }(@ARGV);
|
---|
[205] | 1712 | } else {
|
---|
[215] | 1713 | print "unknown command: $command\n\n";
|
---|
[205] | 1714 | usage();
|
---|
| 1715 | check_env();
|
---|
[252] | 1716 | $return_code = $RETURN_NOK;
|
---|
[205] | 1717 | }
|
---|
[186] | 1718 | }
|
---|
[252] | 1719 |
|
---|
| 1720 | exit $return_code;
|
---|