aboutsummaryrefslogtreecommitdiffstats
path: root/bin/youri-submit
blob: 7a43903e29b169f3433c9932d45f17b5da24731d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/perl
# $Id$

=head1 NAME

youri-submit - package submission tool

=head1 VERSION

Version 2.0

=head1 SYNOPSIS

youri-submit [options] <target> <files>

youri-submit --list <category> [target]

youri-submit --help [category] [item]
 
Options:

    --config <file>        use file <file> as config file
    --skip-pre <pre>       skip pre <pre>
    --skip-check <check>   skip check <check>
    --skip-action <action> skip action <action>
    --skip-post <post>     skip post <post>
    --skip-reject <reject> skip reject <reject>
    --define <key>=<value> pass additional values
    --clean                delete package after success
    --verbose              verbose run
    --test                 test run
    --list <category>      list items from given category
    --help [category]      display contextual help

=head1 DESCRIPTION

B<youri-submit> allows to submit packages to a repository.

All packages given on command lines are passed to a list of check plugins,
depending on given upload target. If none of them fails, all packages are
passed to a list of action plugins, depending also on given upload target.

=head1 OPTIONS

=over

=item B<--config> I<file>

Use given file as configuration, instead of normal one.

=item B<--skip-pre> I<id>

Skip pre transaction plugin with given identity

=item B<--skip-check> I<id>

Skip check plugin with given identity.

=item B<--skip-action> I<id>

Skip action plugin with given identity.

=item B<--skip-post> I<id>

Skip post transaction plugin with given identity.

=item B<--skip-reject> I<id>

Skip reject action plugin with given identity.

=item B<--define> <key>=<value>

Define additional parameters, to be used by plugins.

=item B<--clean>

Delete submited packages upon successfull submission.

=item B<--verbose>

Produce more verbose output (can be used more than once)

=item B<--test>

Don't perform any modification.

=item B<--list> I<category>
 
List available items from given category and exits. Category must be either
B<targets>, B<actions> or B<checks>. A target is needed for the two last ones.
 
=item B<--help> I<category>
 
Display help for given category and exits. Category must be either
B<repository>, B<action> or B<check>. An item is needed for the two last ones.
If no category given, display standard help.

=back

=head1 CONFIGURATION

Configuration is read from the first file found among:

=over

=item * the one specified by B<--config> option on command-line

=item * $HOME/.youri/submit.conf
 
=item * /usr/local/etc/youri/submit.conf

=back

The configuration file should be a YAML-format files, with the following
mandatory top-level directives:

=over

=item B<repository>

The definition of repository plugin to be used.

=item B<targets>

The list of available submission targets, each one being composed from the
following keys:

=over

=item B<checks>

The list of check plugins to use for this target.

=item B<actions>

The list of action plugins to use for this target.

=back

=item B<checks>

The list of check plugin definitions, indexed by their identity.

=item B<actions>
 
The list of action plugin definitions, indexed by their identity.

=back

=head1 SEE ALSO

Youri::Config, for additional details about configuration file format.

Each used plugin man page, for available options.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

use strict;
use warnings;

use Youri::Config;
use Youri::Utils;
use Pod::Usage;

my $config = Youri::Config->new(
    args => {
        'skip-check'  => '=s@',
        'skip-action' => '=s@',
        'define'      => '=s%',
        'verbose'     => '|v!',
        'clean'       => '!',
        'test'        => '|t!',
        'list'        => '|l!',
        'config'      => '=s',
        'skip-prei'   => '=s@',
        'skip-post'   => '=s@',
        'skip-reject' => '=s@',
    },
    directories => [ "$ENV{HOME}/.youri", '/usr/local/etc/youri' ],
    file => 'submit.conf',
);

if ($config->get_arg('list')) {
    my $category = $ARGV[0];
    pod2usage(-verbose => 0, -message => "No category specified, aborting\n")
        unless $category;
    if ($category eq 'targets') { 
        print join(' ', keys %{$config->get_param('targets')});
    } elsif ($category eq 'checks' || $category eq 'actions') {
        my $target = $ARGV[1];
        pod2usage(-verbose => 0, -message => "No target specified, aborting\n")
            unless $target;
        if ($category eq 'checks') {
            my $checks = $config->get_param('targets')->{$target}->{checks};
            print join(' ', @{$checks}) if $checks;
        } else {
            my $actions = $config->get_param('targets')->{$target}->{actions};
            print join(' ', @{$actions}) if $actions;
        }
    } else {
        pod2usage(-verbose => 0, -message => "Invalid category $category, aborting\n")
    }
    print "\n";
    exit 0;
}

if ($config->get_arg('help')) {
    my $category = $ARGV[0];
    my ($item, $section);
    if ($category eq 'repository') { 
        $section = $config->get_param('repository');
        pod2usage(
            -verbose => 0,
            -message => "No repository defined, aborting\n"
        ) unless $section;
    } elsif ($category eq 'check' || $category eq 'action') {
        $item = $ARGV[1];
        pod2usage(
            -verbose => 0,
            -message => "No item specified, aborting\n"
        ) unless $item;
        if ($category eq 'check') {
            $section = $config->get_param('checks')->{$item};
            pod2usage(
                -verbose => 0,
                -message => "No such check $item defined, aborting\n"
            ) unless $section;
        } else  {
            $section = $config->get_param('actions')->{$item};
            pod2usage(
                -verbose => 0,
                -message => "No such action $item defined, aborting\n"
            ) unless $section;
        }
    } else {
        pod2usage(-verbose => 0, -message => "Invalid category $category, aborting\n")
    }
    my $file = $section->{class} . '.pm';
    $file =~ s/::/\//g;
    pod2usage(
        -verbose  => 99,
        -sections => 'NAME|DESCRIPTION',
        -input    => $file,
        -pathlist => \@INC
    );
}


pod2usage(-verbose => 0, -message => "No target specified, aborting\n")
    unless @ARGV > 0;
pod2usage(-verbose => 0, -message => "No packages specified, aborting\n")
    unless @ARGV > 1;

# convenient global flags
my $test    = $config->get_arg('test');
my $verbose = $config->get_arg('verbose');

# check target
my $target = shift @ARGV;
my $target_conf = $config->get_param('targets')->{$target};

# create repository
my $repository;
my $repository_conf = $config->get_param('repository');
die "No repository declared" unless $repository_conf;
print "Creating repository\n" if $verbose;
eval {
    $repository = create_instance(
        'Youri::Repository',
        $repository_conf,
        {
            test    => $test,
            verbose => $verbose > 0 ? $verbose - 1 : 0,
            targets => [ keys %{$config->get_param('targets')} ],
        }
    );
};
die "Failed to create repository: $@\n" if $@;

# perfrom pre action
my $ok = 1;
my $pre_packages = [];
my $skip_pres = $config->get_arg('skip-pre');
my %skip_pres = $skip_pres ?  map { $_ => 1 } @{$skip_pres} : ();
foreach my $id (@{$target_conf->{pres}}) { 
    next if $skip_pres{$id};
    print "Creating pre $id\n" if $verbose;
    my $pre;
    my $pre_conf = $config->get_param('pres')->{$id};

    if (!$pre_conf) {
	print STDERR "No such pre $id, skipping\n";
	next;
    }
    eval {
	$pre = create_instance(
	    'Youri::Submit::Pre',
	    $pre_conf,
	    {
		id       => $id,
		test     => $test,
		verbose  => $verbose > 0 ? $verbose - 1 : 0,
	    }
	);
    };
    if ($@) {
	print STDERR "Failed to create pre $id: $@\n";
    } else {
	print "running pre $id\n" if $verbose;
	unless ($pre->run($pre_packages, $repository, $target, $config->get_param('define'))) {
	    print STDERR "Error: " . $pre->get_error() . "\n";
	    $ok = 0;
	}
    }
}
exit(1) unless $ok;

# create packages group
my @packages_group;
my $group_error;
foreach my $group ([ map { { section => "", file => $_ } } @ARGV ], @$pre_packages) {
    my @packages;
    foreach my $opt (@$group) {
	print "Preparing upload for $opt->{file}\n" if $verbose;
	$repository->{packages}{$opt->{file}}{section} = $opt->{section};
	push(
	    @packages,
	    create_instance(
		'Youri::Package',
		{
		    class => $repository->get_package_class(),
		},
		{
		    file => $opt->{file},
		    %$opt
		},
	    )
	);
    }
    @packages or next;

# check all packages pass all tests
    my %errors;
    my $skip_check = $config->get_arg('skip-check');
    my %skip_check = $skip_check ?  map { $_ => 1 } @{$skip_check} : ();
    my @error;
    foreach my $id (@{$target_conf->{checks}}) {
	next if $skip_check{$id};
	print "Creating check $id\n" if $verbose;
	my $check;
	my $check_conf = $config->get_param('checks')->{$id};

	if (!$check_conf) {
	    print STDERR "No such check $id, skipping\n";
	    next;
	}
	eval {
	    $check = create_instance(
		'Youri::Submit::Check',
		$check_conf,
		{ 
		    id       => $id,
		    test     => $test,
		    verbose  => $verbose > 0 ? $verbose - 1 : 0,
		}
	    );
	};
	if ($@) {
	    print STDERR "Failed to create check $id: $@\n";
	} else {
	    foreach my $package (@packages) {
		print "running check $id on package $package\n" if $verbose;
		my @errors = $check->run(
		    $package,
		    $repository,
		    $target,
		    $config->get_arg('define')
		);
		push(@{$errors{$package}}, @errors) if $errors[0];
	    }
	}
    }
    if (%errors) {
	print "Submission errors, aborting:\n";
	foreach my $package (keys %errors) {
	    print "- $package:\n";
	    foreach my $error (@{$errors{$package}}) {
		print " - $error\n";
	    }
	}
	# reject the packages
	my $skip_rejects = $config->get_arg('skip-reject');
	my %skip_rejects = $skip_rejects ?  map { $_ => 1 } @{$skip_rejects} : ();
	foreach my $id (@{$target_conf->{rejects}}) {
	    next if $skip_rejects{$id};
	    print "Creating reject $id\n" if $verbose;
	    my $reject;
	    my $reject_conf = $config->get_param('rejects')->{$id};
	
	    if (!$reject_conf) {
		print STDERR "No such reject $id, skipping\n";
		next;
	    }
	    eval {
		$reject = create_instance(
		    'Youri::Submit::Reject',
		    $reject_conf,
		    {
			id       => $id,
			test     => $test,
			verbose  => $verbose > 0 ? $verbose - 1 : 0,
		    }
		);
	    };
	    if ($@) {
		print STDERR "Failed to create reject $id: $@\n";
	    } else {
		foreach my $package (@packages) {
		    print "running reject $id on package $package\n" if $verbose;
		    eval {
			$reject->run($package, \%errors, $repository, $target, $config->get_arg('define'));
		    };
		    if ($@) {
			print STDERR "Failed to run action $id on package $package: $@\n";
		    }
		}
	    }
	}
	$group_error = 1;
	next
    } 

# proceed further
    my $skip_action = $config->get_arg('skip-action');
    my %skip_action = $skip_action ?  map { $_ => 1 } @{$skip_action} : ();
    foreach my $id (@{$target_conf->{actions}}) {
	next if $skip_action{$id};
	print "Creating action $id\n" if $verbose;
	my $action;
	my $action_conf = $config->get_param('actions')->{$id};

	if (!$action_conf) {
	    print STDERR "No such action $id, skipping\n";
	    next;
	}
	eval {
	    $action = create_instance(
		'Youri::Submit::Action',
		$action_conf,
		{
		    id       => $id,
		    test     => $test,
		    verbose  => $verbose > 0 ? $verbose - 1 : 0,
		}
	    );
	};
	if ($@) {
	    print STDERR "Failed to create action $id: $@\n";
	} else {
	    foreach my $package (@packages) {
		print "running action $id on package $package\n" if $verbose;
		eval {
		    $action->run(
			$package,
			$repository,
			$target,
			$config->get_arg('define')
		    );
		};
		if ($@) {
		    print STDERR "Failed to run action $id on package $package: $@\n";
		}
	    }
	}
    }

    if ($config->get_arg('clean')) {
	foreach my $package (@packages) {
	    print "cleaning file $package\n" if $verbose;
	    unlink $package->as_file();
	}
    }
}

# perfrom post action
my $skip_post = $config->get_arg('skip-post');
my %skip_post = $skip_post ?  map { $_ => 1 } @{$skip_post} : ();
foreach my $id (@{$target_conf->{post}}) {
    next if $skip_post{$id};
    print "Creating post $id\n" if $verbose;
    my $post;
    my $post_conf = $config->get_param('posts')->{$id};

    if (!$post_conf) {
	print STDERR "No such post $id, skipping\n";
	next;
    }
    eval {
	$post = create_instance(
	    'Youri::Submit::Post',
	    $post_conf,
	    { 
		id       => $id,
		test     => $test,
		verbose  => $verbose > 0 ? $verbose - 1 : 0,
	    }
	);
    };
    if ($@) {
	print STDERR "Failed to create post $id: $@\n";
    } else {
	print "running post $id\n" if $verbose;
	unless ($post->run($repository, $target, $config->get_arg('define'))) {
	    print STDERR "Error: " . $post->get_error() . "\n";
	}
    }
}

exit(1) if $group_error;