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