[Devel] Select fastest mirror capability for vztempl-dl
Pavel Odintsov
pavel.odintsov at gmail.com
Wed Dec 23 03:59:45 PST 2015
Hello, folks!
I have developed some code in context https://bugs.openvz.org/browse/OVZ-6641
All code you could find below:
#!/usr/bin/perl
use strict;
use warnings;
### Author pavel.odintsov at gmail.com
#
# This script will fight all lags related to mirror.openvz.org when
downloading templates with vztmpl-dl
# We select fastest mirror and use it instead default (it's definitely
connected to Internet with 56.6k dial-up modem)
#
# Install fastest mirror plugin
#`yum install yum-plugin-fastestmirror`;
# Build cache for it
#`yum makecache`;
my $architecture = `uname -m`;
chomp $architecture;
my @openvz_mirrors = `cat
/var/cache/yum/$architecture/6/openvz-utils/mirrorlist.txt`;
chomp @openvz_mirrors;
my @mirrors_with_time = `cat /var/cache/yum/$architecture/6/timedhosts.txt`;
chomp @mirrors_with_time;
my $mirror_time_hash = {};
for my $line (@mirrors_with_time) {
my @data = split /\s/, $line;
$mirror_time_hash-> { $data[0] } = $data[1];
}
sub get_latency {
my $mirror = shift;
if ($mirror =~ m#//(.*?)/#) {
my $short_mirror_name = $1;
return $mirror_time_hash->{$short_mirror_name};
} else {
return 9999999;
}
}
my @fastestst_openvz_mirrors = sort { get_latency($a) <=>
get_latency($b) } @openvz_mirrors;
my $fastest_mirror = $fastestst_openvz_mirrors[0];
print "Fastest mirror is: $fastest_mirror with latency " .
get_latency($fastest_mirror) . "\n";
my $url_for_download_conf = $fastest_mirror;
$url_for_download_conf =~ s#/current/$#/template/precreated#;
# Fastest mirror is: http://mirror.logol.ru/openvz/current/ with
latency 0.0241508483887
open my $fl, ">>", "/etc/vz/download.conf" or die "Can't open file for
writing\n";
print {$fl} qq(\nTMPL_REPO_PREFIX="$url_for_download_conf"\n);
close $fl;
--
Sincerely yours, Pavel Odintsov
More information about the Devel
mailing list