aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Thauvin <nanardon@mageia.org>2010-10-02 13:31:35 +0000
committerOlivier Thauvin <nanardon@mageia.org>2010-10-02 13:31:35 +0000
commita9ade04230fc99e5d12e7bd1af1e2dd31907d15a (patch)
tree32f25d1699e483eb219400e44e2e96d782feb7ea
parent6f42c4ffb976cdffa257a09e4dcb33119750abfc (diff)
downloadmgamirrors-a9ade04230fc99e5d12e7bd1af1e2dd31907d15a.tar
mgamirrors-a9ade04230fc99e5d12e7bd1af1e2dd31907d15a.tar.gz
mgamirrors-a9ade04230fc99e5d12e7bd1af1e2dd31907d15a.tar.bz2
mgamirrors-a9ade04230fc99e5d12e7bd1af1e2dd31907d15a.tar.xz
mgamirrors-a9ade04230fc99e5d12e7bd1af1e2dd31907d15a.zip
- handle distributions, protocol andothers things
-rw-r--r--lib/MGA/Mirrors/Controller/Distrib.pm48
-rw-r--r--lib/MGA/Mirrors/Controller/Mirrors.pm66
-rw-r--r--lib/MGA/Mirrors/Controller/New.pm13
-rw-r--r--lib/MGA/Mirrors/DB.pm102
-rw-r--r--root/html/includes/header.tt3
-rw-r--r--root/html/includes/host_information.tt50
-rw-r--r--root/html/includes/mirrorslist.tt29
-rw-r--r--root/html/includes/new/confirm.tt2
-rw-r--r--root/html/includes/new/host_information.tt19
-rw-r--r--root/html/includes/new/new_host.tt9
-rw-r--r--root/html/includes/new/url_form.tt4
-rw-r--r--root/html/pages/index.tt3
-rw-r--r--t/controller_Distrib.t9
-rw-r--r--t/controller_Mirrors.t9
14 files changed, 334 insertions, 32 deletions
diff --git a/lib/MGA/Mirrors/Controller/Distrib.pm b/lib/MGA/Mirrors/Controller/Distrib.pm
new file mode 100644
index 0000000..0d49e55
--- /dev/null
+++ b/lib/MGA/Mirrors/Controller/Distrib.pm
@@ -0,0 +1,48 @@
+package MGA::Mirrors::Controller::Distrib;
+use Moose;
+use namespace::autoclean;
+
+BEGIN {extends 'Catalyst::Controller'; }
+
+=head1 NAME
+
+MGA::Mirrors::Controller::Distrib - Catalyst Controller
+
+=head1 DESCRIPTION
+
+Catalyst Controller.
+
+=head1 METHODS
+
+=cut
+
+
+=head2 index
+
+=cut
+
+sub index :Path :Args(0) {
+ my ( $self, $c ) = @_;
+
+}
+
+sub list :Path :Args(1) {
+ my ( $self, $c ) = @_;
+ $c->stash->{current_view} = 'TTBlock';
+ $c->stash->{template} = 'distrib/distrib.tt';
+}
+
+=head1 AUTHOR
+
+Olivier Thauvin
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/lib/MGA/Mirrors/Controller/Mirrors.pm b/lib/MGA/Mirrors/Controller/Mirrors.pm
new file mode 100644
index 0000000..5d07ca5
--- /dev/null
+++ b/lib/MGA/Mirrors/Controller/Mirrors.pm
@@ -0,0 +1,66 @@
+package MGA::Mirrors::Controller::Mirrors;
+use Moose;
+use namespace::autoclean;
+
+BEGIN {extends 'Catalyst::Controller'; }
+
+=head1 NAME
+
+MGA::Mirrors::Controller::Mirrors - Catalyst Controller
+
+=head1 DESCRIPTION
+
+Catalyst Controller.
+
+=head1 METHODS
+
+=cut
+
+
+=head2 index
+
+=cut
+
+sub index :Path :Args(0) {
+ my ( $self, $c ) = @_;
+
+ $c->response->body('Matched MGA::Mirrors::Controller::Mirrors in Mirrors.');
+}
+
+sub mirror :Path :Args(1) {
+ my ( $self, $c, $host ) = @_;
+ $c->stash->{hostname} = $host;
+
+ if ($c->req->param('hostinfo')) {
+ my $hinfo = $c->model('Mirrors')->find_mirrors({
+ hostname => $host,
+ })->[0];
+ if (! $hinfo->{readonly}) {
+ $c->model('Mirrors')->add_or_update_host($host,
+ bandwidth => $c->req->param('bandwidth'),
+ city => $c->req->param('city'),
+ country => $c->req->param('country'),
+ syncfrom=> $c->req->param('syncfrom'),
+ );
+ }
+ }
+
+ $c->stash->{host} = $c->model('Mirrors')->find_mirrors({
+ hostname => $host,
+ })->[0];
+}
+
+=head1 AUTHOR
+
+Olivier Thauvin
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/lib/MGA/Mirrors/Controller/New.pm b/lib/MGA/Mirrors/Controller/New.pm
index fbb1fbe..d3ec38c 100644
--- a/lib/MGA/Mirrors/Controller/New.pm
+++ b/lib/MGA/Mirrors/Controller/New.pm
@@ -44,8 +44,10 @@ sub index :Path :Args(0) {
hostname => $uri->host, });
if (@{$urls || []}) {
$c->stash->{exists_url} = $urls;
- $c->stash->{subtemplate} = 'new/mirror_exists.tt';
- return;
+ if ($urls->[0]->{valid}) {
+ $c->stash->{subtemplate} = 'new/mirror_exists.tt';
+ return;
+ }
}
if (!$c->model('Mirrors')->mirror_validity($uri)) {
@@ -62,17 +64,18 @@ sub index :Path :Args(0) {
my @ips = $c->model('Mirrors')->host_ips($uri->host);
$c->stash->{location} = $c->model('Mirrors')->locate_ips(@ips);
+ $c->stash->{host}{country} = $c->stash->{location}{code};
my $mirror = $c->model('Mirrors')->find_mirrors(
{ hostname => $uri->host, });
if (@{ $mirror || []}) {
$c->stash->{mirror} = $mirror->[0];
} elsif ($c->req->param('hostinfo')) {
- foreach (qw(city country)) {
+ foreach (qw(city country syncfrom bandwidth)) {
$c->session->{hostinfo}{$_} = $c->req->param($_);
}
} else {
- $c->stash->{subtemplate} = 'new/host_information.tt';
+ $c->stash->{subtemplate} = 'new/new_host.tt';
return;
}
@@ -94,6 +97,8 @@ sub confirm :Path :Args(1) {
$uri->host,
city => $c->session->{hostinfo}{city},
country => $c->session->{hostinfo}{country},
+ bandwidth => $c->session->{hostinfo}{bandwidth},
+ syncfrom => $c->session->{hostinfo}{syncfrom},
);
} else {
return;
diff --git a/lib/MGA/Mirrors/DB.pm b/lib/MGA/Mirrors/DB.pm
index 5ff0649..31eb4cc 100644
--- a/lib/MGA/Mirrors/DB.pm
+++ b/lib/MGA/Mirrors/DB.pm
@@ -74,6 +74,35 @@ sub locate_ips {
return;
}
+sub protocol_list {
+ my ($self) = @_;
+ my $select = $self->db->prepare(q{
+ select name from protocol order by name
+ });
+ $select->execute;
+ return keys %{ $select->fetchall_hashref([ 'name' ]) }
+}
+
+sub bandwidth_name {
+ my ($self, $value) = @_;
+ my $select = $self->db->prepare(q{
+ select name from bandwidth where value = ?
+ });
+ $select->execute($value);
+ my $res = $select->fetchrow_hashref;
+ $select->finish;
+ $res->{name}
+}
+
+sub bandwidth_list {
+ my ($self) = @_;
+ my $list = $self->db->prepare(q{
+ select * from bandwidth order by value
+ });
+ $list->execute;
+ return $list->fetchall_arrayref({});
+}
+
sub country_list {
my ($self) = @_;
my $list = $self->db->prepare(q{
@@ -83,6 +112,27 @@ sub country_list {
return $list->fetchall_arrayref({});
}
+sub version_list {
+ my ($self) = @_;
+ my $list = $self->db->prepare(q{
+ select * from distributions
+ });
+ $list->execute;
+ return $list->fetchall_hashref([qw(version)]);
+}
+
+sub arch_list {
+ my ($self, $version) = @_;
+ my $list = $self->db->prepare(q{
+ select * from distributions
+ } . ($version
+ ? q{ where version = ? }
+ : q{})
+ );
+ $list->execute($version ? $version : ());
+ return $list->fetchall_hashref([qw(arch)]);
+}
+
sub mirror_validity {
my ($self, $uri) = @_;
my $listf = $self->db->prepare(q{
@@ -153,7 +203,7 @@ sub _check_url {
my ($fh, $filename) = tempfile();
close($fh);
my $cmd =
- $furi->scheme =~ /^http|ftp$/ ? "wget -nv -t 1 -T 4 -O $filename " . $furi->as_string :
+ $furi->scheme =~ /^http|ftp$/ ? "wget --no-check-certificate -nv -t 1 -T 4 -O $filename " . $furi->as_string :
$furi->scheme eq 'rsync' ? "rsync --timeout 4 -q " . $furi->as_string . " $filename" : '';
my $ok = (system($cmd) == 0);
unlink($filename);
@@ -196,6 +246,7 @@ sub find_mirrors {
}
if (my $field = {
protocol => 'protocol',
+ valid => 'url.valid',
}->{$_}) {
push(@uw, sprintf('%s = any(?)', $field));
push(@uvals, ref $filters->{$_} ? $filters->{$_} : [ $filters->{$_} ]);
@@ -220,17 +271,59 @@ sub _find_urls {
};
my @vals;
if (keys %{ $filters || {} }) {
- $query .= ' where ';
my @w;
foreach (keys %$filters) {
+ $filters->{$_} or next;
+ my $field = {
+ hostname => 'hosts.hostname',
+ protocol => 'urls.protocol',
+ }->{$_} or next;
+
+ push(@w, sprintf('%s = any(?)', $field));
+ push(@vals, ref $filters->{$_} ? $filters->{$_} : [ $filters->{$_} ]);
+ }
+ $query .= ' where ' if (@w);
+ $query .= join(' and ', @w);
+ }
+ my $list = $self->db->prepare($query);
+ $list->execute(@vals);
+ return $list->fetchall_arrayref({});
+}
+
+sub find_distributions {
+ my ($self, $filters, $key) = @_;
+ return [
+ map { $_->{url} = $self->fmt_url($_); $_ }
+ @{ $self->_find_distributions($filters) || []}]
+}
+
+sub _find_distributions {
+ my ($self, $filters, $key) = @_;
+
+ my $query = q{
+ select *, urls.path || distributions.relpath as path from hosts
+ join urls on urls.hostname = hosts.hostname
+ join mirrors_distributions on urls.key = mirrors_distributions.urlskey
+ join distributions on distributions.dkey = mirrors_distributions.distributionkey
+ };
+
+ my @vals;
+ if (keys %{ $filters || {} }) {
+ my @w;
+ foreach (keys %$filters) {
+ $filters->{$_} or next;
my $field = {
hostname => 'hosts.hostname',
protocol => 'urls.protocol',
+ version => 'distributions.version',
+ arch => 'distributions.arch',
+ country => 'mirrors.country',
}->{$_} or next;
push(@w, sprintf('%s = any(?)', $field));
push(@vals, ref $filters->{$_} ? $filters->{$_} : [ $filters->{$_} ]);
}
+ $query .= ' where ' if (@w);
$query .= join(' and ', @w);
}
my $list = $self->db->prepare($query);
@@ -258,7 +351,7 @@ sub add_or_update_host {
my (@fields, @vals);
while (my ($field, $val) = each(%info)) {
push(@fields, $field);
- push(@vals, $val);
+ push(@vals, $val || undef);
}
if (keys %info) {
my $upd = $self->db->prepare(sprintf(q{
@@ -289,7 +382,7 @@ sub add_or_update_url {
}
my $update = $self->db->prepare(q{
- update urls set path = ?, port = ?
+ update urls set path = ?, port = ?, valid = true
where hostname = ? and protocol = ?
});
@@ -349,6 +442,7 @@ sub find_urls {
sub fmt_url {
my ($self, $dburl) = @_;
+ $dburl->{path} =~ s:/+:/:g;
my $uri = URI->new(
sprintf('%s://%s%s',
$dburl->{protocol},
diff --git a/root/html/includes/header.tt b/root/html/includes/header.tt
index 5319e52..7f8b1d5 100644
--- a/root/html/includes/header.tt
+++ b/root/html/includes/header.tt
@@ -8,3 +8,6 @@
</head>
<body>
+<p>Mageia mirrors database</p>
+
+<p>Mirror not found ? <a href="[% c.uri_for('/new') %]">register it</a></p>
diff --git a/root/html/includes/host_information.tt b/root/html/includes/host_information.tt
new file mode 100644
index 0000000..c0cb7cb
--- /dev/null
+++ b/root/html/includes/host_information.tt
@@ -0,0 +1,50 @@
+<!-- $Id$ -->
+<form action="[% action || c.uri_for() %]" method="POST">
+<input type="hidden" name="hostinfo" value="1">
+<table border=0>
+<tr><th>Country</th>
+<td>
+<select name="country">
+<option value=""></option>
+[% FOREACH country = c.model('Mirrors').country_list %]
+<option value="[% country.code %]" [% "selected=select" IF country.code == host.country %]>[% country.name | html %]</option>
+[% END %]
+</select>
+</td>
+</tr>
+
+<tr><th>City</th>
+<td>
+<input type="text" name="city" value="[% host.city | html %]">
+</td>
+</tr>
+
+[% FOREACH mirror = c.model('Mirrors').find_mirrors %]
+[% IF loop.first %]
+<tr><th>Synchronized from</th>
+<td>
+<select name="syncfrom">
+<option value="">Unknown</option>
+[% END %]
+[% IF mirror.public %]
+<option value="[% mirror.hostname | html %]" [% "selected=select" IF host.syncfrom == mirror.hostname %]>[% mirror.hostname | html %]</option>
+[% END %]
+[% IF loop.last %]
+</select>
+</td>
+</tr>
+[% END %]
+[% END %]
+<tr><th>Approximated bandwidth</th>
+<td>
+<select name="bandwidth">
+<option value=""></option>
+[% FOREACH bd = c.model('Mirrors').bandwidth_list %]
+<option value="[% bd.value %]" [% "selected=select" IF host.bandwidth == bd.value %]>[% bd.name | html %]</option>
+[% END %]
+</select>
+</td>
+</tr>
+</table>
+<input type="submit">
+</form>
diff --git a/root/html/includes/mirrorslist.tt b/root/html/includes/mirrorslist.tt
index fd855f5..0986717 100644
--- a/root/html/includes/mirrorslist.tt
+++ b/root/html/includes/mirrorslist.tt
@@ -5,6 +5,13 @@
<option value="[% country.code %]" [% "selected=select" IF country.code == c.req.param('country') %]>[% country.name | html %]</option>
[% END %]
</select>
+<select name="protocol">
+<option value="">--</option>
+[% FOREACH p = c.model('Mirrors').protocol_list %]
+<option value="[% p %]" [% "selected=select" IF p == c.req.param('protocol') %]>[% p %]</option>
+[% END %]
+</select>
+
<input type="submit">
</form>
@@ -16,7 +23,13 @@
[% FOREACH item = mirrorslist %]
[% IF loop.first %]
<table border=1>
-<tr><th>Server name</th><th>Location</th><th>Urls</th><tr>
+<tr>
+ <th>Server name</th>
+ <th>Location</th>
+ <th>Bandwidth</th>
+ <th>Source</th>
+ <th>Urls</th>
+<tr>
[% END %]
<tr>
[% urls = db.find_urls({ "hostname" => item.hostname }) %]
@@ -29,8 +42,22 @@
[% END %]
</td>
<td>
+[% c.model('Mirrors').bandwidth_name(item.bandwidth) | html %]
+</td>
+<td>
+[% item.syncfrom | html %]
+</td>
+<td>
+[% IF item.public %]
[% FOREACH u = urls %]
+[% IF u.valid %]
<a href="[% u.url %]">[% u.protocol %]</a>
+[% ELSE %]
+<span style="color: red">[% u.protocol %]</span>
+[% END %]
+[% END %]
+[% ELSE %]
+Private access
[% END %]
</td>
</tr>
diff --git a/root/html/includes/new/confirm.tt b/root/html/includes/new/confirm.tt
index b3eb3f3..e0bb7d4 100644
--- a/root/html/includes/new/confirm.tt
+++ b/root/html/includes/new/confirm.tt
@@ -1,4 +1,6 @@
<div id="foo">
+<p>Really adding [% c.session.new_uri | html %] ?</p>
+
[% c.prototype.form_remote_tag({ url => c.uri_for('confirm'), update => 'foo' } ) %]
<input type="hidden" name="confirm" value="1">
<input type="submit">
diff --git a/root/html/includes/new/host_information.tt b/root/html/includes/new/host_information.tt
deleted file mode 100644
index 78c04c2..0000000
--- a/root/html/includes/new/host_information.tt
+++ /dev/null
@@ -1,19 +0,0 @@
-Enter Info for [% uri.host %]
-
-<form action="[% c.uri_for() %]" method="POST">
-<input type="hidden" name="url" value="[% uri | html %]">
-<input type="hidden" name="hostinfo" value="1">
-
-[% location.name %] / [% location.continent %]<br>
-
-Country:
-<select name="country">
-<option value="">--</option>
-[% FOREACH country = c.model('Mirrors').country_list %]
-<option value="[% country.code %]" [% "selected=select" IF country.code == location.code %]>[% country.name | html %]</option>
-[% END %]
-</select>
-
-City: <input type="text" name="city">
-<input type="submit">
-</form>
diff --git a/root/html/includes/new/new_host.tt b/root/html/includes/new/new_host.tt
new file mode 100644
index 0000000..a4296d2
--- /dev/null
+++ b/root/html/includes/new/new_host.tt
@@ -0,0 +1,9 @@
+Enter Info for [% uri.host %]
+
+<form action="[% c.uri_for() %]" method="POST">
+<input type="hidden" name="url" value="[% uri | html %]">
+<input type="hidden" name="hostinfo" value="1">
+
+[% location.name %] / [% location.continent %]<br>
+
+[% INCLUDE 'host_information.tt' %]
diff --git a/root/html/includes/new/url_form.tt b/root/html/includes/new/url_form.tt
index f8840b9..169fe73 100644
--- a/root/html/includes/new/url_form.tt
+++ b/root/html/includes/new/url_form.tt
@@ -1,5 +1,7 @@
<form action="[% c.uri_for() %]" method="POST">
-Enter the url to the top level mirror tree:<br>
+Enter the url to the top level mirror tree, supported protocol are
+[% c.model('Mirrors').protocol_list.join(', ') | html %].<br>
+
<input type="text" name="url" size=40 value="[% c.req.param('url') | html %]">
<input type=submit>
</form>
diff --git a/root/html/pages/index.tt b/root/html/pages/index.tt
index f1f0251..9acd3cd 100644
--- a/root/html/pages/index.tt
+++ b/root/html/pages/index.tt
@@ -1,6 +1,3 @@
<!-- $Id$ -->
-<p>Mageia mirrors database</p>
-
-<p>Mirror not found ? <a href="[% c.uri_for('/new') %]">register it</a></p>
[% INCLUDE 'mirrorslist.tt' %]
diff --git a/t/controller_Distrib.t b/t/controller_Distrib.t
new file mode 100644
index 0000000..d1efc3c
--- /dev/null
+++ b/t/controller_Distrib.t
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN { use_ok 'Catalyst::Test', 'MGA::Mirrors' }
+BEGIN { use_ok 'MGA::Mirrors::Controller::Distrib' }
+
+ok( request('/distrib')->is_success, 'Request should succeed' );
+done_testing();
diff --git a/t/controller_Mirrors.t b/t/controller_Mirrors.t
new file mode 100644
index 0000000..bfb9912
--- /dev/null
+++ b/t/controller_Mirrors.t
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN { use_ok 'Catalyst::Test', 'MGA::Mirrors' }
+BEGIN { use_ok 'MGA::Mirrors::Controller::Mirrors' }
+
+ok( request('/mirrors')->is_success, 'Request should succeed' );
+done_testing();