以编程方式查找最接近的Apache Software Foundation镜像

Ped*_*ano 6 apache mirror

对于我的部署自动化需求,我希望以动态和编程方式确定最接近的Apache Software Foundation镜像,因为服务器分布在不同的地理位置,因此动态确定最佳镜像是理想的,而无需在某处硬编码.

到目前为止,我能想到的唯一方法是废弃http://www.apache.org/dyn/closer.cgi页面,查看那里最近的镜像,但它看起来有点麻烦和脆弱.

是否有Web API端点以稳定可靠的方式提供此功能?

Joe*_*Joe 9

页面中的镜像URL标记为<strong>,因此您可以抓取页面以获得如下所示的最高建议:

curl 'https://www.apache.org/dyn/closer.cgi' |
  grep -o '<strong>[^<]*</strong>' |
  sed 's/<[^>]*>//g' |
  head -1
Run Code Online (Sandbox Code Playgroud)

此外,closer.cgi支持?as_json=1查询参数以提供与JSON相同的信息.结果preferred对于最近的镜子以及http替代镜子来说是关键.

  • `?as_json = 1`查询参数实际上是我正在寻找的优雅替代方案,以避免需要进行任何HTML报废. (3认同)

sou*_*ine 7

使用jq有一种更优雅的方法:

curl -s 'https://www.apache.org/dyn/closer.cgi?as_json=1' | jq --raw-output '.preferred'
Run Code Online (Sandbox Code Playgroud)