source: server/trunk/web/lib/Sophie/Controller/Admin.pm @ 42

Last change on this file since 42 was 42, checked in by nanardon, 14 years ago
  • add dump/load distrib config using Yaml
  • separate DB connection for session
File size: 7.0 KB
Line 
1package Sophie::Controller::Admin;
2use Moose;
3use namespace::autoclean;
4use YAML qw/freeze thaw/;
5
6BEGIN {extends 'Catalyst::Controller'; }
7
8=head1 NAME
9
10Sophie::Controller::Admin - Catalyst Controller
11
12=head1 DESCRIPTION
13
14Catalyst Controller.
15
16=head1 METHODS
17
18=cut
19
20=head2 index
21
22=cut
23
24sub index :Path :Args(0) {
25    my ( $self, $c ) = @_;
26
27    $c->response->body('Matched Sophie::Controller::Admin in Admin.');
28}
29
30sub create :XMLRPC {
31    my ( $self, $c, $distribution, $version, $arch ) = @_;
32
33    my $rs = $c->model('Base')->resultset('Distribution');
34    my $rs_d = $rs->find_or_create({ name => $distribution}) or do {
35        $c->stash->{xmlrpc} = 'Erreur adding distrib';
36        return;
37    };
38
39    my $rs_r = $rs_d->Release->find_or_create({ version => $version, }) or do {
40        $c->stash->{xmlrpc} = 'Erreur adding release';
41        return;
42    };
43
44    my $rs_a = $rs_r->Arch->find_or_create({ arch => $arch }) or do {
45        $c->stash->{xmlrpc} = 'Erreur adding arch';
46        return;
47    };
48
49    $c->stash->{xmlrpc} = 'Ok';
50
51    $c->model('Base')->storage->dbh->commit;
52
53}
54
55sub add_media :XMLRPC {
56    my ( $self, $c, $distribspec, $mediaspec) = @_;
57
58    my $d = $c->model('Base')->resultset('Distribution')
59        ->search(name => $distribspec->{distribution})
60        ->search_related('Release', version => $distribspec->{release})
61        ->search_related('Arch', arch => $distribspec->{arch})->next;
62    if ($d) {
63        my $new = my $rs = $c->model('Base')->resultset('Medias')
64            ->update_or_create({
65                %{ $mediaspec },
66                Arch => $d,
67            },
68            { key => 'label' }
69        );
70        if ($new) {
71            $c->stash->{xmlrpc} = 'OK';
72            $c->model('Base')->storage->dbh->commit;
73        } else {
74            $c->stash->{xmlrpc} = 'Erreur adding media';
75        }
76    }
77}
78
79sub list_path :XMLRPC {
80    my ($self, $c, $distribution, $version, $arch, $media) = @_;
81   
82    if (ref $distribution) {
83        ($distribution, $version, $arch, $media) = 
84        (
85            $distribution->{distribution},
86            $distribution->{release},
87            $distribution->{arch},
88            $version,
89        );
90    }
91
92    $c->stash->{xmlrpc}  = [
93    $c->model('Base')->resultset('Distribution')
94        ->search($distribution ? (name => $distribution) : ())
95        ->search_related('Release', $version ? (version => $version) : ())
96        ->search_related('Arch', $arch ? (arch => $arch) : ())
97        ->search_related('Medias', $media ? (label => $media) : ())
98        ->search_related('MediasPaths')
99        ->search_related('Paths')->get_column('path')
100        ->all ];
101}
102
103sub media_path :XMLRPC {
104    my ( $self, $c, $distribution, $version, $arch, $label, $path ) = @_;
105
106    if (ref $distribution) {
107        ($distribution, $version, $arch, $label, $path) = 
108        (
109            $distribution->{distribution},
110            $distribution->{release},
111            $distribution->{arch},
112            $version,
113            $arch,
114        );
115    }
116
117    $path =~ s/\/*$//;
118    $path =~ s/\/+/\//g;
119
120    my $med = $c->model('Base')->resultset('Distribution')
121        ->search(name => $distribution)
122        ->search_related('Release', version => $version)
123        ->search_related('Arch', arch => $arch)
124        ->search_related('Medias', label => $label)->next or return;
125
126    my $rspath = $c->model('Base')->resultset('Paths')
127        ->find_or_create({ path => $path }) or do {
128    };
129    my $new = $c->model('Base')->resultset('MediasPaths')->new({
130            Medias => $med,
131            Paths =>  $rspath,
132        });
133    $new->insert;
134
135    $c->model('Base')->storage->dbh->commit;
136}
137
138sub media_remove_path :XMLRPC {
139    my ( $self, $c, $distribution, $version, $arch, $label, $path ) = @_;
140
141    if (ref $distribution) {
142        ($distribution, $version, $arch, $label, $path) = 
143        (
144            $distribution->{distribution},
145            $distribution->{release},
146            $distribution->{arch},
147            $version,
148            $arch,
149        );
150    }
151
152    $path =~ s/\/*$//;
153    $path =~ s/\/+/\//g;
154
155    my $med = $c->model('Base')->resultset('Distribution')
156        ->search(name => $distribution)
157        ->search_related('Release', version => $version)
158        ->search_related('Arch', arch => $arch)
159        ->search_related('Medias', label => $label)->find or return;
160
161    my $rspath = $c->model('Base')->resultset('Paths')
162        ->find({ path => $path }) or do {
163            return;
164    };
165    my $new = $c->model('Base')->resultset('MediasPaths')->search({
166            d_media => $med->d_media_key,
167            d_path =>  $rspath->d_path_key,
168        })->next->delete;
169
170    $c->model('Base')->storage->dbh->commit;
171}
172
173sub ls_local : XMLRPC {
174    my ($self, $c, $path) = @_;
175
176    $c->stash->{xmlrpc} = [ <$path*> ];
177}
178
179sub dump_distrib : XMLRPC {
180    my ($self, $c, $distribution, $version, $arch) = @_;
181   
182    if (!ref $distribution) {
183        $distribution = {
184            distribution => $distribution,
185            release => $version,
186            arch => $arch,
187        };
188    }
189
190    my $ref = {
191        distrib => $distribution,
192    };
193
194    $ref->{media} = $c->forward('/distrib/struct', [ $distribution ]);
195
196    foreach (@{ $ref->{media} || []}) {
197        warn $_->{label};
198        $ref->{path}{$_->{label}} = $c->forward('list_path', [ $distribution,
199                $_->{label} ]);
200    }
201
202    $c->stash->{xmlrpc} = freeze($ref);
203}
204
205sub clean_distrib : XMLRPC {
206    my ($self, $c, $distribution, $version, $arch) = @_;
207   
208    if (!ref $distribution) {
209        $distribution = {
210            distribution => $distribution,
211            release => $version,
212            arch => $arch,
213        };
214    }
215
216    my $rsdist = $c->model('Base')->resultset('Distribution')
217        ->search(name => $distribution->{distribution})
218        ->search_related('Release', version => $distribution->{release})
219        ->search_related('Arch', arch => $distribution->{arch})
220        ->search_related('Medias');
221
222    my $new = $c->model('Base')->resultset('MediasPaths')->search({
223            d_media => { IN => $rsdist->get_column('d_media_key')->as_query },
224        })->delete;
225
226    # $c->model('Base')->storage->dbh->rollback;
227   
228}
229
230sub load_distrib : XMLRPC {
231    my ( $self, $c, $dump ) = @_;
232
233    my $ref = thaw($dump);
234
235    warn keys %{$ref->{path}};
236
237    $c->forward('clean_distrib', [ $ref->{distrib} ]);
238
239    $c->forward('create', [ 
240            $ref->{distrib}{distribution},
241            $ref->{distrib}{release},
242            $ref->{distrib}{arch},
243        ]);
244
245    foreach my $media (@{ $ref->{media} || []}) {
246        $c->forward('add_media', [ $ref->{distrib}, $media ]);
247    }
248    foreach my $media (keys %{ $ref->{path} || [] }) {
249        foreach my $path (@{ $ref->{path}{$media} || [] }) {
250            $c->forward('media_path', [ $ref->{distrib}, $media, $path ]);
251        }
252    }
253
254    #$c->model('Base')->storage->dbh->rollback;
255}
256
257=head1 AUTHOR
258
259Olivier Thauvin
260
261=head1 LICENSE
262
263This library is free software. You can redistribute it and/or modify
264it under the same terms as Perl itself.
265
266=cut
267
268__PACKAGE__->meta->make_immutable;
269
2701;
Note: See TracBrowser for help on using the repository browser.