Amazon Linux 2のyumとamazon-linux-extrasにプロキシを設定する

# What's？

あまりないかもしれませんが、Amazon Linux 2に対するパッケージインストールをプロキシ経由で行いたい場合の設定をメモしておきます。


# 環境

```shell:/etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
```

```shell
$ uname -srvmpio
Linux 4.14.252-195.483.amzn2.x86_64 #1 SMP Mon Nov 1 20:58:46 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
```

# yum

`yum`に関しては、`/etc/yum.conf`に`proxy`を設定すればOKです。

```shell:/etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=3
distroverpkg=system-release
timeout=5
retries=7
proxy=http://[your-proxy-host]:[your-proxy-port]


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
```

# amazon-linux-extras

`amazon-linux-extras`に関しては、`http_proxy`および`https_proxy`環境変数を設定すればよいという情報もありますが。

`amazon-linux-extras`の中身を確認すると、こんな感じのシェルスクリプトになっています。

```shell
$ cat $(which amazon-linux-extras)
#!/bin/sh
# Copyright 2017, 2018 Amazon.com, Inc. or its affiliates.

# This module is part of Amazon Linux Extras.
#
# Amazon Linux Extras is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License v2 as published
# by the Free Software Foundation.
#
# Amazon Linux Extras is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with Amazon Linux Extras.  If not, see <http://www.gnu.org/licenses/>.


declare -x CATALOGURL AWSDOMAIN AWSREGION RELEASEVER BASEARCH YUMCONFIG_FILE_NAME
declare -x URL_FORMAT USE_MIRRORLIST SRC_SUFFIX DEBUGINFO_SUFFIX
for config_file_location in /etc /usr/local/etc ${HOME:+"${HOME}/.config/amazon-linux"}; do
        test -r "${config_file_location}/amazon-linux-extras.conf" &&
                . "${config_file_location}/amazon-linux-extras.conf"
done

# Avoid encoding errors because default is ASCII.
if test "$ENVROOT"; then
        PATH=$ENVROOT:$PATH
fi
exec env PYTHONIOENCODING=UTF-8 ${PYTHON:-python} -m amazon_linux_extras "$@"
```

ここで、この部分を見ると`/etc`、`/usr/local/etc`、`${HOME}/.config/amazon-linux`ディレクトリのいずれかに`amazon-linux-extras.conf`というファイルがあればよさそうだったので

```shell
for config_file_location in /etc /usr/local/etc ${HOME:+"${HOME}/.config/amazon-linux"}; do
        test -r "${config_file_location}/amazon-linux-extras.conf" &&
                . "${config_file_location}/amazon-linux-extras.conf"
done
```

今回は`/etc/amazon-linux-extras.conf`としてファイルを作成してみました。

```shell:/etc/amazon-linux-extras.conf
export http_proxy=http://[your-proxy-host]:[your-proxy-port]
export https_proxy=http://[your-proxy-host]:[your-proxy-port]
```

これで、`amazon-linux-extras`がプロキシ越しに使えるようになります。