2 ##############################################################################
3 # Copyright (c) 2016 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 # Select closest fuel mirror based on latency measured with ping.
13 # Failsafe: The us1 mirror
15 from subprocess import Popen,PIPE
17 from operator import itemgetter
19 mirrors = [ "us1", "cz1" ]
20 FNULL = open('/dev/null', 'w')
22 re_avg = re.compile(r'.* = [^/]*/([^/]*).*')
25 for mirror in mirrors:
26 fqdn = "mirror.seed-"+mirror+".fuel-infra.org"
28 pipe = Popen("ping -c 3 " + fqdn + " | tail -1",shell = True, stdout=PIPE, stderr=FNULL)
29 avg = pipe.communicate()[0]
31 pingtime[fqdn] = float(re_avg.split(avg)[1])
33 print sorted(pingtime.items(), key=itemgetter(1))[0][0]
35 print "mirror.seed-"+mirrors[0]+".fuel-infra.org"