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