aboutsummaryrefslogtreecommitdiffstats
path: root/bin/youri-upload.in
blob: 50e860f954d567dcc43a8ef226fa98155308aaf0 (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
#!/usr/bin/perl
# $Id$

=head1 NAME

youri-upload - package upload agent

=head1 VERSION

Version 2.0

=head1 SYNOPSIS

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

Options:

    --config <file>        use file <file> as config file
    --skip-check <check>   skip check <check>
    --skip-action <action> skip action <action>
    --define <key>=<value> pass additional values
    --list-targets         list available targets
    --list-checks <target> list configured checks for for <target>
    --list-actions <target> list configured actions for for <target>
    --verbose              verbose run
    --test                 test run
    --help                 print this help message

=head1 DESCRIPTION

B<youri-upload> allows to upload packages in 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-check> I<id>

Skip check plugin with given identity.

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

Skip action plugin with given identity.

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

Define additional parameters, to be used by plugins.

=item B<--verbose>

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

=item B<--test>

Don't perform any modification.

=item B<--list-targets>

List available targets

=item B<--list-checks> I<target>

List configured checks for given target 

=item B<--list-actions> I<target>

List configured actions for given target 

=item B<--help>

Print a brief help message and exits.

=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/upload.conf

=item * /usr/local/etc/youri/upload.conf

=back

All additional configuration files specified by B<includes> directive are then
processed. Then command line options. Any directive overrides prior definition.

=over

=item B<includes> I<files>

Uses space-separated list I<files> as a list of additional configuration files.

=item B<repository> I<id>

Declares a repository object with identity I<id>.

=item B<targets> I<ids>

Declares a list of upload target objects with identity taken in space-separated list I<ids>.

=back

Each object declared in configuration must be fully defined later, using a
configuration section, starting with bracketed object identity, followed by at
least a class directive, then any number of additional object-specific
directives.

Example:

        objects = foo
        
        [foo]
        class = Foo::Bar
        key1  = value1
        key2  = value2

=head1 SEE ALSO

Youri::Config, for 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(
    command_spec => [
        'config=s',
        'skip-check=s@',
        'skip-action=s@',
        'list-targets!',
        'list-checks=s',
        'list-actions=s',
        'define=s%',
        'help|h!',
        'test|t!',
        'verbose|v!'
    ],
    file_spec => [
        'includes=s',
        'targets=s',
        'repository=s',
    ],
    directories => [ '@sysconfigdir', "$ENV{HOME}/.youri" ],
    file_name   => 'upload.conf',
    caller => $0,
);

# compute available targets first
my %targets = map { $_ => 1 } split(/\s+/, $config->get('targets'));

if ($config->get('list-targets')) {
    print join(' ', keys %targets) . "\n";
    exit 0;
} elsif ($config->get('list-checks')) {
    my %target = get_target_config($config->get('list-checks'));
    print join(' ', @{$target{checks}}) . "\n";
    exit 0;
} elsif ($config->get('list-actions')) {
    my %target = get_target_config($config->get('list-actions'));
    print join(' ', @{$target{actions}}) . "\n";
    exit 0;
}

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('test');
my $verbose = $config->get('verbose');

# check target
my $target = shift @ARGV;
my %target = get_target_config($target);

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

# create packages
my @packages;
foreach my $file (@ARGV) {
    push(
        @packages,
        create_instance(
            'Youri::Package',
            class => $repository->get_package_class(),
            file => $file
        )
    );
}

# check all packages pass all tests
my %skip_checks = map { $_ => 1 } @{$config->get('skip-check')};
foreach my $id (@{$target{checks}}) {
    next if $skip_checks{$id};
    print "Creating check $id\n" if $verbose;
    my $check;
    eval {
        $check = create_instance(
            'Youri::Upload::Check',
            id       => $id,
            test     => $test,
	    user     => $config->get('define')->{user},
            verbose  => $verbose > 0 ? $verbose - 1 : 0,
            $config->get_section($id)
        );
    };
    if ($@) {
        print STDERR "Failed to create check $id: $@\n";
    } else {
        foreach my $package (@packages) {
            print "running check $id on package $package\n" if $verbose;
            unless ($check->run($package, $repository, $target, $config->get('define'))) {
                print STDERR "Error: " . $check->get_error() . ", aborting\n";
                exit(1);
            }
        }
    }
}

# proceed further
my %skip_actions = map { $_ => 1 } @{$config->get('skip-action')};
foreach my $id (@{$target{actions}}) {
    next if $skip_actions{$id};
    print "Creating action $id\n" if $verbose;
    my $action;
    eval {
        $action = create_instance(
            'Youri::Upload::Action',
            id       => $id,
            test     => $test,
            verbose  => $verbose > 0 ? $verbose - 1 : 0,
            $config->get_section($id)
        );
    };
    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('define'));
                };
            if ($@) {
                print STDERR "Failed to run action $id on package $package: $@\n";
            }
        }
    }
}

sub get_target_config {
    my ($id) = @_;
    die "Unavailable target $target" unless $targets{$id};

    my %target = $config->get_section($id);
    die "Undefined target $id" unless %target;
    return %target;
}