[186] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | # $Id: dasscm 889 2010-06-25 22:19:34Z 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;
|
---|
[889] | 13 |
|
---|
| 14 | # used system("cp -a"), because File::Copy does not keep permissions
|
---|
[802] | 15 | #use File::Copy;
|
---|
[237] | 16 | use File::Find;
|
---|
[186] | 17 | use File::stat;
|
---|
| 18 | use File::Path;
|
---|
[214] | 19 | use Term::ReadKey;
|
---|
[239] | 20 |
|
---|
[234] | 21 | #use Data::Dumper;
|
---|
[186] | 22 |
|
---|
[189] | 23 | #####################################################################
|
---|
| 24 | #
|
---|
[186] | 25 | # global
|
---|
[189] | 26 | #
|
---|
[205] | 27 |
|
---|
[253] | 28 | # shell exit codes
|
---|
[252] | 29 | my $RETURN_OK = 0;
|
---|
| 30 | my $RETURN_NOK = 1;
|
---|
| 31 |
|
---|
[277] | 32 | # Nagios return codes
|
---|
[290] | 33 | my $RETURN_WARN = 1;
|
---|
| 34 | my $RETURN_CRIT = 2;
|
---|
[277] | 35 | my $RETURN_UNKNOWN = 3;
|
---|
| 36 |
|
---|
[238] | 37 | # documentation file (for usage)
|
---|
| 38 | my $doc_file = "/usr/share/doc/packages/dasscm/dasscm_howto.txt";
|
---|
| 39 |
|
---|
[884] | 40 | my $config_file = "/etc/dasscm.conf";
|
---|
| 41 | my $config = get_config($config_file);
|
---|
[252] | 42 |
|
---|
[884] | 43 | my %COMMANDS = (
|
---|
[889] | 44 | '--help' => 'help',
|
---|
| 45 | 'help' => 'help',
|
---|
| 46 | 'login' => 'login',
|
---|
| 47 | 'init' => 'init',
|
---|
| 48 | 'ls' => 'ls',
|
---|
| 49 | 'update' => 'update',
|
---|
| 50 | 'up' => 'update',
|
---|
| 51 | 'add' => 'add',
|
---|
| 52 | 'commit' => 'commit',
|
---|
| 53 | 'checkin' => 'commit',
|
---|
| 54 | 'ci' => 'commit',
|
---|
| 55 | 'revert' => 'revert',
|
---|
| 56 | 'blame' => 'blame',
|
---|
| 57 | 'diff' => 'diff',
|
---|
| 58 | 'status' => 'status',
|
---|
| 59 | 'st' => 'status',
|
---|
| 60 | 'check' => 'check',
|
---|
| 61 | 'permissions' => 'permissions',
|
---|
| 62 | 'cleanup' => 'cleanup',
|
---|
| 63 | 'complete' => 'complete',
|
---|
| 64 | 'complete_path' => 'complete_path',
|
---|
[887] | 65 | 'complete_repopath' => 'complete_repopath',
|
---|
[884] | 66 | );
|
---|
| 67 |
|
---|
| 68 | # desc: description (eg. for usage)
|
---|
| 69 | # params: parameters
|
---|
| 70 | # CMD
|
---|
| 71 | # USER
|
---|
| 72 | # PATH
|
---|
| 73 | # REPOPATH
|
---|
[889] | 74 | # require:
|
---|
[884] | 75 | # WRITE commands that require write access (and therefore a login)
|
---|
| 76 | my %COMMAND_DEFINITIONS = (
|
---|
| 77 | 'help' => {
|
---|
[889] | 78 | 'desc' => [],
|
---|
| 79 | 'params' => ["CMD"],
|
---|
[884] | 80 | 'function' => \&help,
|
---|
| 81 | },
|
---|
| 82 | 'login' => {
|
---|
[889] | 83 | 'desc' => [],
|
---|
| 84 | 'params' => ["USER"],
|
---|
[884] | 85 | 'function' => \&login
|
---|
| 86 | },
|
---|
| 87 | 'init' => {
|
---|
[889] | 88 | 'desc' => [
|
---|
| 89 | "initialize local subversion checkout.",
|
---|
| 90 | "This is the first thing to do (after configuring $config_file)"
|
---|
| 91 | ],
|
---|
| 92 | 'params' => [],
|
---|
[884] | 93 | 'function' => \&init
|
---|
| 94 | },
|
---|
| 95 | 'ls' => {
|
---|
[889] | 96 | 'desc' => [],
|
---|
| 97 | 'params' => ["REPOPATH"],
|
---|
[884] | 98 | 'function' => \&ls
|
---|
| 99 | },
|
---|
| 100 | 'update' => {
|
---|
[889] | 101 | 'desc' => [],
|
---|
| 102 | 'params' => ["PATH"],
|
---|
[884] | 103 | 'function' => \&update
|
---|
| 104 | },
|
---|
| 105 | 'add' => {
|
---|
[889] | 106 | 'desc' => [],
|
---|
| 107 | 'params' => ["PATH"],
|
---|
| 108 | 'require' => ["WRITE"],
|
---|
[884] | 109 | 'function' => \&add
|
---|
| 110 | },
|
---|
| 111 | 'commit' => {
|
---|
[889] | 112 | 'desc' => [],
|
---|
| 113 | 'params' => ["REPOPATH"],
|
---|
| 114 | 'require' => ["WRITE"],
|
---|
[884] | 115 | 'function' => \&commit
|
---|
| 116 | },
|
---|
| 117 | 'revert' => {
|
---|
[889] | 118 | 'desc' => ["revert local changes back to version from repository"],
|
---|
| 119 | 'params' => ["REPOPATH"],
|
---|
[884] | 120 | 'function' => \&revert
|
---|
| 121 | },
|
---|
| 122 | 'blame' => {
|
---|
[889] | 123 | 'desc' => [],
|
---|
| 124 | 'params' => ["PATH"],
|
---|
[884] | 125 | 'function' => \&blame
|
---|
| 126 | },
|
---|
| 127 | 'diff' => {
|
---|
[889] | 128 | 'desc' => [],
|
---|
| 129 | 'params' => ["PATH"],
|
---|
[884] | 130 | 'function' => \&diff
|
---|
| 131 | },
|
---|
| 132 | 'status' => {
|
---|
[889] | 133 | 'desc' => [],
|
---|
| 134 | 'params' => ["PATH"],
|
---|
[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 889 2010-06-25 22:19:34Z 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 |
|
---|
| 971 | # if only one result is available
|
---|
| 972 | # and this result is a directory,
|
---|
| 973 | # add another result entry
|
---|
| 974 | # (otherwise complete will stop here and continue with the next parameter)
|
---|
[889] | 975 | if ( $#files == 0 ) {
|
---|
[888] | 976 | my $path = $files[0];
|
---|
[889] | 977 | if ( -d $path ) {
|
---|
[888] | 978 | push( @files, $path . "/" );
|
---|
| 979 | }
|
---|
| 980 | }
|
---|
| 981 |
|
---|
| 982 | return @files;
|
---|
| 983 | }
|
---|
| 984 |
|
---|
[189] | 985 | #####################################################################
|
---|
| 986 | #
|
---|
[186] | 987 | # functions
|
---|
| 988 | sub help(;@)
|
---|
| 989 | {
|
---|
[205] | 990 | if ( @_ == 0 ) {
|
---|
| 991 | usage();
|
---|
| 992 | } else {
|
---|
| 993 | print "help for @_: ...\n";
|
---|
[214] | 994 | usage();
|
---|
[205] | 995 | }
|
---|
[186] | 996 | }
|
---|
| 997 |
|
---|
[203] | 998 | sub login(@)
|
---|
| 999 | {
|
---|
[205] | 1000 | check_parameter( @_, 1 );
|
---|
| 1001 | check_env();
|
---|
[203] | 1002 |
|
---|
[235] | 1003 | my $input_username = $_[0];
|
---|
[214] | 1004 |
|
---|
| 1005 | if ( not $input_username ) {
|
---|
| 1006 | my $output_username = "";
|
---|
| 1007 | if ($DASSCM_USERNAME) {
|
---|
| 1008 | $output_username = " ($DASSCM_USERNAME)";
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | print "Enter DASSCM user name", $output_username, ": ";
|
---|
| 1012 | $input_username = <STDIN>;
|
---|
| 1013 | chomp($input_username);
|
---|
[247] | 1014 |
|
---|
| 1015 | $input_username = $input_username || $DASSCM_USERNAME;
|
---|
[205] | 1016 | }
|
---|
[203] | 1017 |
|
---|
[205] | 1018 | # hidden password input
|
---|
[247] | 1019 | print "Enter password for $input_username: ";
|
---|
[205] | 1020 | ReadMode('noecho');
|
---|
| 1021 | my $input_password = <STDIN>;
|
---|
| 1022 | ReadMode('normal');
|
---|
| 1023 | chomp($input_password);
|
---|
[220] | 1024 | print "\n";
|
---|
[203] | 1025 |
|
---|
[247] | 1026 | # checking checkout username/password
|
---|
[252] | 1027 | svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
|
---|
| 1028 | $DASSCM_CHECKOUT_PASSWORD );
|
---|
[247] | 1029 | print "checkout access okay\n";
|
---|
[205] | 1030 |
|
---|
[247] | 1031 | svn_check_credentials( $input_username, $input_password );
|
---|
| 1032 |
|
---|
[205] | 1033 | #
|
---|
| 1034 | # set environment variables
|
---|
| 1035 | #
|
---|
[267] | 1036 | $ENV{'DASSCM_USERNAME'} = "$input_username";
|
---|
| 1037 | $ENV{'DASSCM_PASSWORD'} = "$input_password";
|
---|
[205] | 1038 |
|
---|
[209] | 1039 | print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
|
---|
| 1040 | "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
|
---|
| 1041 | "DASSCM_REPO: $DASSCM_REPO\n",
|
---|
[278] | 1042 | "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
|
---|
[205] | 1043 |
|
---|
[278] | 1044 | status();
|
---|
| 1045 |
|
---|
| 1046 | print "\n[dasscm shell]\n\n";
|
---|
[235] | 1047 | my $shell = $SHELL || "bash";
|
---|
| 1048 | exec($shell) or die "failed to start new shell";
|
---|
[203] | 1049 | }
|
---|
| 1050 |
|
---|
[260] | 1051 | #
|
---|
| 1052 | # initialize local checkout directory (initial checkout)
|
---|
| 1053 | #
|
---|
[205] | 1054 | sub init(@)
|
---|
| 1055 | {
|
---|
| 1056 | check_parameter( @_, 1 );
|
---|
| 1057 | check_env();
|
---|
| 1058 |
|
---|
[235] | 1059 | # don't do repository creation (svn mkdir) here,
|
---|
| 1060 | # because then their must be a lot of prior checks
|
---|
| 1061 |
|
---|
[205] | 1062 | # update complete repository
|
---|
[216] | 1063 | # and create permission file
|
---|
[801] | 1064 | my $retcode = run_interactive(
|
---|
[286] | 1065 | "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; mkdir -p `dirname $DASSCM_PERMISSION_FILE`; touch $DASSCM_PERMISSION_FILE"
|
---|
[801] | 1066 | );
|
---|
[205] | 1067 | }
|
---|
| 1068 |
|
---|
[215] | 1069 | sub ls(@)
|
---|
[186] | 1070 | {
|
---|
[205] | 1071 | check_parameter( @_, 1 );
|
---|
| 1072 | check_env();
|
---|
[186] | 1073 |
|
---|
[271] | 1074 | my @files = svn_ls(@_);
|
---|
[215] | 1075 |
|
---|
[275] | 1076 | if (@files) {
|
---|
[274] | 1077 | print join( "\n", @files );
|
---|
| 1078 | print "\n";
|
---|
| 1079 | }
|
---|
[215] | 1080 | }
|
---|
| 1081 |
|
---|
| 1082 | sub update(@)
|
---|
| 1083 | {
|
---|
| 1084 | check_parameter( @_, 1 );
|
---|
| 1085 | check_env();
|
---|
| 1086 |
|
---|
| 1087 | #
|
---|
| 1088 | # update local repository
|
---|
| 1089 | #
|
---|
| 1090 | svn_update();
|
---|
| 1091 | }
|
---|
| 1092 |
|
---|
[237] | 1093 | #
|
---|
| 1094 | # helper function for "add" command
|
---|
| 1095 | #
|
---|
[215] | 1096 | sub add_helper(@)
|
---|
| 1097 | {
|
---|
[205] | 1098 | (
|
---|
| 1099 | my $basename,
|
---|
| 1100 | my $dirname_prod,
|
---|
| 1101 | my $dirname_repo,
|
---|
| 1102 | my $filename_prod,
|
---|
| 1103 | my $filename_repo
|
---|
[801] | 1104 | ) = get_filenames( $_[0] );
|
---|
[186] | 1105 |
|
---|
[274] | 1106 | mkpath($dirname_repo);
|
---|
[889] | 1107 | copy_file_to_repository($filename_prod);
|
---|
[186] | 1108 |
|
---|
[274] | 1109 | # already checked in?
|
---|
| 1110 | chdir $DASSCM_REPO;
|
---|
[205] | 1111 |
|
---|
[274] | 1112 | # also add the path to filename.
|
---|
| 1113 | for my $dir ( split( '/', $dirname_prod ) ) {
|
---|
| 1114 | if ($dir) {
|
---|
[275] | 1115 | my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
|
---|
[274] | 1116 | if ( $rc > 0 ) {
|
---|
| 1117 | print join( "\n", @out );
|
---|
[205] | 1118 | }
|
---|
[274] | 1119 | chdir $dir;
|
---|
[205] | 1120 | }
|
---|
| 1121 | }
|
---|
[275] | 1122 | my ( $rc, @out ) = run_command("$SVN add '$basename'");
|
---|
[274] | 1123 | if ( $rc > 0 ) {
|
---|
| 1124 | print join( "\n", @out );
|
---|
| 1125 | }
|
---|
| 1126 | chdir $StartDirectory;
|
---|
| 1127 |
|
---|
[215] | 1128 | }
|
---|
[205] | 1129 |
|
---|
[215] | 1130 | #
|
---|
[274] | 1131 | # adding new files (or directories)
|
---|
[215] | 1132 | #
|
---|
| 1133 | sub add(@)
|
---|
| 1134 | {
|
---|
| 1135 | check_parameter( @_, 1 );
|
---|
| 1136 | check_env();
|
---|
| 1137 |
|
---|
| 1138 | #
|
---|
| 1139 | # update local repository
|
---|
| 1140 | #
|
---|
| 1141 | svn_update();
|
---|
| 1142 |
|
---|
[237] | 1143 | # get all regular files and links
|
---|
[239] | 1144 | my $href_files = get_files(@_);
|
---|
[220] | 1145 |
|
---|
[237] | 1146 | #print Dumper( $href_files );
|
---|
| 1147 |
|
---|
[239] | 1148 | my @files = @{ $href_files->{files} };
|
---|
| 1149 | my @links = @{ $href_files->{links} };
|
---|
[237] | 1150 |
|
---|
[239] | 1151 | if (@files) {
|
---|
[237] | 1152 | my $number = $#files + 1;
|
---|
| 1153 | print "files to check-in ($number): \n";
|
---|
| 1154 | print join( "\n", @files );
|
---|
| 1155 | print "\n";
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
[268] | 1158 | # TODO: check in links and also link target? At least warn about link target
|
---|
[239] | 1159 | if (@links) {
|
---|
[237] | 1160 | my $number = $#links + 1;
|
---|
| 1161 | print "\n";
|
---|
| 1162 | print "ignoring links ($number):\n";
|
---|
| 1163 | print join( "\n", @links );
|
---|
| 1164 | print "\n";
|
---|
| 1165 | }
|
---|
| 1166 |
|
---|
[238] | 1167 | # TODO: confirm
|
---|
| 1168 |
|
---|
[237] | 1169 | # copy files one by one to local repository
|
---|
| 1170 | for my $file (@files) {
|
---|
[239] | 1171 |
|
---|
[233] | 1172 | # add file
|
---|
[239] | 1173 | add_helper($file);
|
---|
[233] | 1174 | }
|
---|
| 1175 |
|
---|
[215] | 1176 | # create new permissions file
|
---|
| 1177 | permissions();
|
---|
[220] | 1178 |
|
---|
[215] | 1179 | # add permissions file
|
---|
[286] | 1180 | add_helper($DASSCM_PERMISSION_FILE);
|
---|
[215] | 1181 |
|
---|
[205] | 1182 | if ( $options{'message'} ) {
|
---|
| 1183 | $svnOptions .= " --message \"$options{'message'}\" ";
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
[239] | 1186 | # commit calls $EDITOR.
|
---|
[237] | 1187 | # use "interactive" here, to display output
|
---|
[801] | 1188 | my $retcode = run_interactive(
|
---|
[267] | 1189 | "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
|
---|
[801] | 1190 | );
|
---|
[205] | 1191 |
|
---|
[274] | 1192 | # svn commit does not deliever an error return code, if commit is canceld,
|
---|
| 1193 | # so a revert is performed in any case
|
---|
| 1194 | svn_revert();
|
---|
[186] | 1195 | }
|
---|
| 1196 |
|
---|
[271] | 1197 | #
|
---|
| 1198 | # checks in all modified files
|
---|
| 1199 | #
|
---|
| 1200 | sub commit(@)
|
---|
| 1201 | {
|
---|
| 1202 | check_parameter( @_, 1 );
|
---|
| 1203 | check_env();
|
---|
| 1204 |
|
---|
| 1205 | (
|
---|
| 1206 | my $basename,
|
---|
| 1207 | my $dirname_prod,
|
---|
| 1208 | my $dirname_repo,
|
---|
| 1209 | my $filename_prod,
|
---|
| 1210 | my $filename_repo
|
---|
[801] | 1211 | ) = get_filenames( $_[0] );
|
---|
[271] | 1212 |
|
---|
| 1213 | #
|
---|
| 1214 | # update local repository
|
---|
| 1215 | #
|
---|
| 1216 | svn_update();
|
---|
| 1217 |
|
---|
[275] | 1218 | ( my $refChangedFiles, my $refRemovedFiles ) =
|
---|
| 1219 | getModifiedFiles($filename_prod);
|
---|
[271] | 1220 | my %changedfiles = %{$refChangedFiles};
|
---|
| 1221 | my %removedfiles = %{$refRemovedFiles};
|
---|
| 1222 |
|
---|
[275] | 1223 | if (%removedfiles) {
|
---|
| 1224 | my $removedFilesString =
|
---|
| 1225 | '"' . join( '" "', values(%removedfiles) ) . '"';
|
---|
| 1226 | my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
|
---|
[271] | 1227 | if ( $rc > 0 ) {
|
---|
| 1228 | print join( "\n", @out );
|
---|
| 1229 | }
|
---|
| 1230 | }
|
---|
| 1231 |
|
---|
| 1232 | # copy files one by one to local repository
|
---|
| 1233 | for my $file ( keys(%changedfiles) ) {
|
---|
[275] | 1234 | copy_file_to_repository($file);
|
---|
[271] | 1235 | }
|
---|
| 1236 |
|
---|
| 1237 | # create new permissions file
|
---|
| 1238 | permissions();
|
---|
| 1239 |
|
---|
| 1240 | # add permissions file
|
---|
[286] | 1241 | add_helper($DASSCM_PERMISSION_FILE);
|
---|
[271] | 1242 |
|
---|
| 1243 | if ( $options{'message'} ) {
|
---|
| 1244 | $svnOptions .= " --message \"$options{'message'}\" ";
|
---|
| 1245 | }
|
---|
| 1246 |
|
---|
| 1247 | # commit calls $EDITOR.
|
---|
| 1248 | # use "interactive" here, to display output
|
---|
[801] | 1249 | my $retcode = run_interactive(
|
---|
[271] | 1250 | "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
|
---|
[801] | 1251 | );
|
---|
[274] | 1252 |
|
---|
| 1253 | # svn commit does not deliever an error return code, if commit is canceld,
|
---|
| 1254 | # so a revert is performed in any case
|
---|
| 1255 | svn_revert();
|
---|
[271] | 1256 | }
|
---|
| 1257 |
|
---|
[800] | 1258 | #
|
---|
| 1259 | # revert: copies files back from repository to system
|
---|
| 1260 | #
|
---|
| 1261 | sub revert(@)
|
---|
| 1262 | {
|
---|
| 1263 | check_parameter( @_, 1 );
|
---|
| 1264 | check_env();
|
---|
| 1265 |
|
---|
| 1266 | (
|
---|
| 1267 | my $basename,
|
---|
| 1268 | my $dirname_prod,
|
---|
| 1269 | my $dirname_repo,
|
---|
| 1270 | my $filename_prod,
|
---|
| 1271 | my $filename_repo
|
---|
[801] | 1272 | ) = get_filenames( $_[0] );
|
---|
[800] | 1273 |
|
---|
| 1274 | # return code for the shell
|
---|
| 1275 | # default: error
|
---|
| 1276 | my $return_code = $RETURN_OK;
|
---|
| 1277 |
|
---|
| 1278 | # cleanup repository
|
---|
[801] | 1279 | cleanup();
|
---|
[889] | 1280 |
|
---|
[800] | 1281 | #svn_update();
|
---|
| 1282 |
|
---|
| 1283 | ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
|
---|
| 1284 | getModifiedFiles($filename_prod);
|
---|
| 1285 | my %changedfiles = %{$refChangedFiles};
|
---|
| 1286 | my %removedfiles = %{$refRemovedFiles};
|
---|
| 1287 | my %unknownfiles = %{$refUnknownFiles};
|
---|
| 1288 |
|
---|
| 1289 | if ( %removedfiles or %changedfiles or %unknownfiles ) {
|
---|
| 1290 |
|
---|
| 1291 | if (%removedfiles) {
|
---|
| 1292 | print "DELETED files and directories. Recreated from repository:\n";
|
---|
[801] | 1293 | my @removedPaths =
|
---|
| 1294 | ( sort { length $a > length $b } keys %removedfiles );
|
---|
[800] | 1295 | print join( "\n", @removedPaths ) . "\n\n";
|
---|
| 1296 |
|
---|
| 1297 | # copy files one by one from local repository to system
|
---|
| 1298 | # and also create directories
|
---|
| 1299 | # paths are sorted, so that directories are created first
|
---|
[801] | 1300 | for my $real_path (@removedPaths) {
|
---|
| 1301 | if ( -d $removedfiles{"$real_path"} ) {
|
---|
[800] | 1302 | mkpath("$real_path");
|
---|
| 1303 | } else {
|
---|
[889] | 1304 | copy_file_from_repository_to_system($real_path);
|
---|
[800] | 1305 | }
|
---|
| 1306 | }
|
---|
| 1307 | }
|
---|
| 1308 |
|
---|
| 1309 | if (%changedfiles) {
|
---|
| 1310 | print "MODIFIED files. Copied from repository to the system:\n";
|
---|
| 1311 | print join( "\n", ( keys %changedfiles ) ) . "\n\n";
|
---|
| 1312 |
|
---|
| 1313 | # copy files one by one from local repository to system
|
---|
| 1314 | for my $real_file ( keys(%changedfiles) ) {
|
---|
[889] | 1315 | copy_file_from_repository_to_system($real_file);
|
---|
[800] | 1316 | }
|
---|
| 1317 |
|
---|
| 1318 | }
|
---|
| 1319 |
|
---|
| 1320 | if (%unknownfiles) {
|
---|
| 1321 | print "UNKNOWN: insufficient permission to check files:\n";
|
---|
| 1322 | print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
|
---|
| 1323 |
|
---|
| 1324 | $return_code = $RETURN_NOK;
|
---|
| 1325 | }
|
---|
| 1326 |
|
---|
| 1327 | } else {
|
---|
| 1328 | print "no modified files found in $dirname_repo\n";
|
---|
| 1329 | }
|
---|
| 1330 |
|
---|
| 1331 | return $return_code;
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
[193] | 1334 | sub blame(@)
|
---|
| 1335 | {
|
---|
[205] | 1336 | check_parameter( @_, 1 );
|
---|
| 1337 | check_env();
|
---|
[193] | 1338 |
|
---|
[205] | 1339 | (
|
---|
| 1340 | my $basename,
|
---|
| 1341 | my $dirname_prod,
|
---|
| 1342 | my $dirname_repo,
|
---|
| 1343 | my $filename_prod,
|
---|
| 1344 | my $filename_repo
|
---|
[801] | 1345 | ) = get_filenames( $_[0] );
|
---|
[205] | 1346 |
|
---|
| 1347 | my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
|
---|
[193] | 1348 | }
|
---|
| 1349 |
|
---|
[187] | 1350 | sub diff(@)
|
---|
| 1351 | {
|
---|
[205] | 1352 | check_parameter( @_, 1 );
|
---|
| 1353 | check_env();
|
---|
[187] | 1354 |
|
---|
[205] | 1355 | (
|
---|
| 1356 | my $basename,
|
---|
| 1357 | my $dirname_prod,
|
---|
| 1358 | my $dirname_repo,
|
---|
| 1359 | my $filename_prod,
|
---|
| 1360 | my $filename_repo
|
---|
[801] | 1361 | ) = get_filenames( $_[0] );
|
---|
[205] | 1362 |
|
---|
| 1363 | #print "$basename,$dirname_prod,$dirname_repo\n";
|
---|
| 1364 |
|
---|
[797] | 1365 | svn_update();
|
---|
[205] | 1366 |
|
---|
[238] | 1367 | ( my $rc_diff, my @diff_result ) =
|
---|
[239] | 1368 | run_command( $diff . " $filename_repo $filename_prod" );
|
---|
[238] | 1369 |
|
---|
| 1370 | print @diff_result;
|
---|
[187] | 1371 | }
|
---|
| 1372 |
|
---|
[209] | 1373 | sub status(@)
|
---|
| 1374 | {
|
---|
| 1375 | check_parameter( @_, 1 );
|
---|
| 1376 | check_env();
|
---|
| 1377 |
|
---|
[270] | 1378 | (
|
---|
| 1379 | my $basename,
|
---|
| 1380 | my $dirname_prod,
|
---|
| 1381 | my $dirname_repo,
|
---|
| 1382 | my $filename_prod,
|
---|
| 1383 | my $filename_repo
|
---|
[801] | 1384 | ) = get_filenames( $_[0] || "/" );
|
---|
[270] | 1385 |
|
---|
[252] | 1386 | # return code for the shell
|
---|
| 1387 | # default: error
|
---|
| 1388 | my $return_code = $RETURN_NOK;
|
---|
| 1389 |
|
---|
[209] | 1390 | #
|
---|
| 1391 | # update local repository
|
---|
| 1392 | #
|
---|
[278] | 1393 | #svn_update( $filename_prod );
|
---|
[209] | 1394 |
|
---|
[278] | 1395 | # check, if permissions have changed
|
---|
| 1396 | permissions();
|
---|
| 1397 |
|
---|
| 1398 | # get modified files
|
---|
| 1399 | ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
|
---|
[275] | 1400 | getModifiedFiles($dirname_prod);
|
---|
[268] | 1401 | my %changedfiles = %{$refChangedFiles};
|
---|
| 1402 | my %removedfiles = %{$refRemovedFiles};
|
---|
[278] | 1403 | my %unknownfiles = %{$refUnknownFiles};
|
---|
[209] | 1404 |
|
---|
[278] | 1405 | if ( %removedfiles or %changedfiles or %unknownfiles ) {
|
---|
| 1406 |
|
---|
[215] | 1407 | if (%removedfiles) {
|
---|
[278] | 1408 | print "DELETED: files found in repository, but not in system:\n";
|
---|
[800] | 1409 | print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
|
---|
[209] | 1410 | }
|
---|
| 1411 |
|
---|
[215] | 1412 | if (%changedfiles) {
|
---|
[278] | 1413 | print "MODIFIED: files differs between repository and system:\n";
|
---|
| 1414 | print join( "\n", ( keys %changedfiles ) ) . "\n\n";
|
---|
[209] | 1415 | }
|
---|
[278] | 1416 |
|
---|
| 1417 | if (%unknownfiles) {
|
---|
| 1418 | print "UNKNOWN: insufficient permission to check files:\n";
|
---|
| 1419 | print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
|
---|
| 1420 | }
|
---|
| 1421 |
|
---|
[209] | 1422 | } else {
|
---|
[270] | 1423 | print "no modified files found in $dirname_repo\n";
|
---|
[252] | 1424 | $return_code = $RETURN_OK;
|
---|
[209] | 1425 | }
|
---|
[215] | 1426 |
|
---|
[252] | 1427 | return $return_code;
|
---|
[215] | 1428 | }
|
---|
[209] | 1429 |
|
---|
[277] | 1430 | #
|
---|
| 1431 | # return short status in Nagios plugin conform way
|
---|
| 1432 | #
|
---|
| 1433 | sub check()
|
---|
| 1434 | {
|
---|
| 1435 | check_env();
|
---|
| 1436 |
|
---|
| 1437 | # return code for the shell
|
---|
[290] | 1438 | my $return_code = $RETURN_OK;
|
---|
[277] | 1439 | my $return_string = "OK: no modified files";
|
---|
| 1440 |
|
---|
[278] | 1441 | # check, if permissions have changed
|
---|
| 1442 | permissions();
|
---|
| 1443 |
|
---|
| 1444 | # get modified files
|
---|
| 1445 | ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
|
---|
[290] | 1446 | getModifiedFiles("/");
|
---|
[277] | 1447 | my %changedfiles = %{$refChangedFiles};
|
---|
| 1448 | my %removedfiles = %{$refRemovedFiles};
|
---|
[278] | 1449 | my %unknownfiles = %{$refUnknownFiles};
|
---|
[277] | 1450 |
|
---|
| 1451 | if ( %removedfiles or %changedfiles ) {
|
---|
| 1452 | $return_string = "Warning: ";
|
---|
[290] | 1453 | if (%changedfiles) {
|
---|
| 1454 | $return_string .=
|
---|
| 1455 | "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
|
---|
[277] | 1456 | }
|
---|
[290] | 1457 | if (%removedfiles) {
|
---|
| 1458 | $return_string .=
|
---|
| 1459 | "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
|
---|
[277] | 1460 | }
|
---|
[278] | 1461 | if (%unknownfiles) {
|
---|
[290] | 1462 | $return_string .=
|
---|
| 1463 | "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
|
---|
[278] | 1464 | }
|
---|
[277] | 1465 | $return_code = $RETURN_WARN;
|
---|
| 1466 | }
|
---|
| 1467 |
|
---|
| 1468 | # addition nagios Service Status
|
---|
| 1469 | #Critical
|
---|
| 1470 | #Unknown
|
---|
| 1471 |
|
---|
[290] | 1472 | print "$return_string\n";
|
---|
[277] | 1473 | return $return_code;
|
---|
| 1474 | }
|
---|
| 1475 |
|
---|
[270] | 1476 | sub permissions()
|
---|
[215] | 1477 | {
|
---|
| 1478 | check_env();
|
---|
| 1479 |
|
---|
[278] | 1480 | my $return_code = $RETURN_OK;
|
---|
| 1481 |
|
---|
[215] | 1482 | #
|
---|
| 1483 | # update local repository
|
---|
| 1484 | #
|
---|
| 1485 | #svn_update();
|
---|
| 1486 |
|
---|
[220] | 1487 | my $dir = $DASSCM_REPO;
|
---|
[275] | 1488 | my @files = svn_ls("/");
|
---|
[215] | 1489 |
|
---|
| 1490 | if (@files) {
|
---|
| 1491 |
|
---|
| 1492 | # generieren der Permissions
|
---|
| 1493 | my @permissions = generatePermissionList(@files);
|
---|
| 1494 | my $OUTFILE;
|
---|
| 1495 | my $tofile = 0; # Status für schreiben in File
|
---|
[220] | 1496 |
|
---|
[286] | 1497 | if ( -w dirname($DASSCM_PERMISSION_FILE) ) {
|
---|
[215] | 1498 |
|
---|
| 1499 | # Verzeichnis existiert => schreiben
|
---|
[286] | 1500 | open( OUTFILE, ">$DASSCM_PERMISSION_FILE" )
|
---|
| 1501 | || die("failed to write to $DASSCM_PERMISSION_FILE: $!");
|
---|
[215] | 1502 | $tofile = 1; # Merken, daß in File geschrieben wird
|
---|
| 1503 | print OUTFILE "#\n";
|
---|
| 1504 | print OUTFILE "# created by dasscm permissions\n";
|
---|
[220] | 1505 | print OUTFILE
|
---|
| 1506 | "# It is intended to be used for restoring permissions\n";
|
---|
[287] | 1507 | print OUTFILE "#\n";
|
---|
[215] | 1508 | } else {
|
---|
| 1509 |
|
---|
[290] | 1510 | if ( $command eq "permission" ) {
|
---|
| 1511 |
|
---|
[278] | 1512 | # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
|
---|
| 1513 | # Alias Filehandle für stdout erzeugen
|
---|
| 1514 | $return_code = $RETURN_WARN;
|
---|
[290] | 1515 | *OUTFILE = *STDOUT;
|
---|
[278] | 1516 | } else {
|
---|
[290] | 1517 |
|
---|
[278] | 1518 | # TODO: improve this. Check for diff?
|
---|
| 1519 | $return_code = $RETURN_CRIT;
|
---|
| 1520 | return $return_code;
|
---|
| 1521 | }
|
---|
[215] | 1522 | }
|
---|
[278] | 1523 |
|
---|
[215] | 1524 | foreach my $line (@permissions) {
|
---|
| 1525 | print OUTFILE "$line\n";
|
---|
| 1526 | }
|
---|
| 1527 |
|
---|
[220] | 1528 | if ($tofile) {
|
---|
[215] | 1529 | close(OUTFILE);
|
---|
| 1530 | }
|
---|
| 1531 | }
|
---|
[278] | 1532 |
|
---|
| 1533 | return $return_code;
|
---|
[209] | 1534 | }
|
---|
| 1535 |
|
---|
[274] | 1536 | #
|
---|
| 1537 | # remove all uncommited changes in the repository
|
---|
| 1538 | #
|
---|
| 1539 | sub cleanup()
|
---|
| 1540 | {
|
---|
| 1541 | check_env();
|
---|
[268] | 1542 |
|
---|
[275] | 1543 | svn_revert($DASSCM_REPO);
|
---|
[285] | 1544 | svn_remove_unknown_files($DASSCM_REPO);
|
---|
[274] | 1545 | }
|
---|
| 1546 |
|
---|
[884] | 1547 | #
|
---|
| 1548 | # used for bash completion
|
---|
| 1549 | # prints the next possible command line parameters
|
---|
| 1550 | #
|
---|
| 1551 | sub complete(@)
|
---|
| 1552 | {
|
---|
| 1553 | use Data::Dumper;
|
---|
| 1554 |
|
---|
| 1555 | my $number_arguments = @_;
|
---|
[889] | 1556 | my @input = @_;
|
---|
[884] | 1557 |
|
---|
| 1558 | if ( $number_arguments <= 1 ) {
|
---|
| 1559 |
|
---|
[887] | 1560 | # complete dasscm commands
|
---|
| 1561 | my $input = $input[0] || "";
|
---|
[884] | 1562 | foreach my $i ( keys %COMMANDS ) {
|
---|
| 1563 | $_ = $i;
|
---|
[889] | 1564 | if (m/^$input/) {
|
---|
[884] | 1565 | my $command = get_command_uniform_name($i);
|
---|
[887] | 1566 | print $command, "\n";
|
---|
[884] | 1567 | }
|
---|
| 1568 | }
|
---|
| 1569 | } else {
|
---|
[887] | 1570 |
|
---|
| 1571 | # complete dasscm parameter
|
---|
[889] | 1572 | my $command = get_command_uniform_name( $input[0] );
|
---|
| 1573 | my @params = get_command_possible_params( $input[0] );
|
---|
| 1574 | if ($verbose) { print "params: ", Dumper(@params); }
|
---|
| 1575 | if ( defined( $params[ $number_arguments - 2 ] )
|
---|
| 1576 | && $params[ $number_arguments - 2 ] )
|
---|
| 1577 | {
|
---|
| 1578 | my $param = $params[ $number_arguments - 2 ];
|
---|
| 1579 | if ( $param eq "PATH" ) {
|
---|
| 1580 | complete_path( $input[ $number_arguments - 1 ] );
|
---|
[887] | 1581 | } elsif ( $param eq "REPOPATH" ) {
|
---|
[889] | 1582 | complete_repopath( $input[ $number_arguments - 1 ] );
|
---|
[887] | 1583 | } else {
|
---|
| 1584 | print "param: $param\n";
|
---|
| 1585 | print Dumper($param);
|
---|
| 1586 | }
|
---|
[884] | 1587 | }
|
---|
| 1588 | }
|
---|
| 1589 | }
|
---|
| 1590 |
|
---|
[887] | 1591 | sub complete_path(@)
|
---|
| 1592 | {
|
---|
| 1593 | check_parameter( @_, 1 );
|
---|
| 1594 | check_env();
|
---|
| 1595 |
|
---|
| 1596 | (
|
---|
| 1597 | my $basename,
|
---|
| 1598 | my $dirname_prod,
|
---|
| 1599 | my $dirname_repo,
|
---|
| 1600 | my $filename_prod,
|
---|
| 1601 | my $filename_repo
|
---|
| 1602 | ) = get_filenames( $_[0] );
|
---|
| 1603 |
|
---|
[889] | 1604 | my @files = get_complete_path_globbing($filename_prod);
|
---|
[887] | 1605 |
|
---|
| 1606 | if (@files) {
|
---|
| 1607 | print join( "\n", @files );
|
---|
| 1608 | print "\n";
|
---|
| 1609 | }
|
---|
| 1610 | }
|
---|
| 1611 |
|
---|
| 1612 | sub complete_repopath(@)
|
---|
| 1613 | {
|
---|
| 1614 | check_parameter( @_, 1 );
|
---|
| 1615 | check_env();
|
---|
| 1616 |
|
---|
| 1617 | (
|
---|
| 1618 | my $basename,
|
---|
| 1619 | my $dirname_prod,
|
---|
| 1620 | my $dirname_repo,
|
---|
| 1621 | my $filename_prod,
|
---|
| 1622 | my $filename_repo
|
---|
| 1623 | ) = get_filenames( $_[0] );
|
---|
| 1624 |
|
---|
[889] | 1625 | my @files = get_complete_path_globbing($filename_repo);
|
---|
| 1626 |
|
---|
[887] | 1627 | if (@files) {
|
---|
[889] | 1628 |
|
---|
[887] | 1629 | # remove DASSCM_REPO path again
|
---|
[889] | 1630 | print join(
|
---|
| 1631 | "\n",
|
---|
| 1632 | map( {
|
---|
| 1633 | s|^${DASSCM_REPO}|/|;
|
---|
| 1634 | $_
|
---|
| 1635 | } @files )
|
---|
| 1636 | );
|
---|
[887] | 1637 | print "\n";
|
---|
| 1638 | }
|
---|
| 1639 |
|
---|
| 1640 | }
|
---|
| 1641 |
|
---|
[189] | 1642 | #####################################################################
|
---|
| 1643 | #
|
---|
[186] | 1644 | # main
|
---|
[189] | 1645 | #
|
---|
[186] | 1646 |
|
---|
[252] | 1647 | my $return_code = $RETURN_OK;
|
---|
[186] | 1648 | my $number_arguments = @ARGV;
|
---|
| 1649 |
|
---|
[205] | 1650 | if ( $number_arguments > 0 ) {
|
---|
[186] | 1651 |
|
---|
[205] | 1652 | # get subcommand and remove it from @ARGV
|
---|
[889] | 1653 | $command = get_command_uniform_name( $ARGV[0] );
|
---|
[205] | 1654 | shift @ARGV;
|
---|
[196] | 1655 |
|
---|
[205] | 1656 | $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
|
---|
| 1657 | $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
|
---|
[196] | 1658 |
|
---|
[205] | 1659 | # TODO: check variables
|
---|
| 1660 | $DASSCM_SVN_REPOSITORY =
|
---|
| 1661 | $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
|
---|
| 1662 |
|
---|
[247] | 1663 | $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
|
---|
| 1664 | $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
|
---|
[205] | 1665 |
|
---|
| 1666 | #
|
---|
| 1667 | # if a user is given by dasscm configuration file, we use it.
|
---|
| 1668 | # Otherwise we expect that read-only account is configured
|
---|
| 1669 | # as local subversion configuration.
|
---|
| 1670 | # If this is also not the case,
|
---|
| 1671 | # user is required to type username and password.
|
---|
| 1672 | # This will be stored as local subversion configuration thereafter.
|
---|
| 1673 | #
|
---|
| 1674 | if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
|
---|
| 1675 | $svnCheckoutCredentials =
|
---|
| 1676 | " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
|
---|
| 1677 | }
|
---|
| 1678 |
|
---|
[290] | 1679 | $DASSCM_PERMISSION_FILE = $config->{'DASSCM_PERMISSION_FILE'}
|
---|
| 1680 | || "/etc/permissions.d/dasscm.permission_backup";
|
---|
[286] | 1681 |
|
---|
[205] | 1682 | # get command line options and store them in options hash
|
---|
[214] | 1683 | my $result = GetOptions( \%options, 'verbose', 'message=s' );
|
---|
[205] | 1684 |
|
---|
| 1685 | # print options
|
---|
| 1686 | foreach my $option ( keys %options ) {
|
---|
[215] | 1687 | print "${option}: $options{$option}\n";
|
---|
[205] | 1688 | }
|
---|
| 1689 |
|
---|
[214] | 1690 | # set verbose to command line option
|
---|
| 1691 | $verbose = $options{'verbose'};
|
---|
| 1692 |
|
---|
| 1693 | #
|
---|
| 1694 | # action accordinly to command are taken
|
---|
| 1695 | # $command is rewritten in standard format,
|
---|
| 1696 | # so we can test for it later on more simply
|
---|
| 1697 | #
|
---|
[884] | 1698 |
|
---|
[889] | 1699 | if ( get_command_function($command) ) {
|
---|
| 1700 | &{ get_command_function($command) }(@ARGV);
|
---|
[205] | 1701 | } else {
|
---|
[215] | 1702 | print "unknown command: $command\n\n";
|
---|
[205] | 1703 | usage();
|
---|
| 1704 | check_env();
|
---|
[252] | 1705 | $return_code = $RETURN_NOK;
|
---|
[205] | 1706 | }
|
---|
[186] | 1707 | }
|
---|
[252] | 1708 |
|
---|
| 1709 | exit $return_code;
|
---|