自由帳

とりとめのない学習メモです。主に Web サービスのシステム基盤や運用に関することを書いています

tfenv を macOS にインストールしつつ、内部の動きを見てみた

はじめに

今まで Terraform を、Terraform の Docker イメージをワンライナー的に使っていたけど、環境変数の扱いとかが面倒で一行が長い。というところでやっぱり tfenv が良さそうと思いつつまだ使ったことなかったのでとりあえず macOS にインストールする...だけなのもイマイチなのでとりあえず内部がどんな感じなのかちょっと見てみた。

github.com

tfenv

リポジトリの README にも書かれているとおり、Terraform のバージョン管理ツール。これで複数のバージョンの Terraform を共存させて使い分けたりなどができる。 rbenv や pyenv などあるけど、rbenv に触発されたと書かれている。 中身をさらっと見ると bash スクリプトだった。

tfenv インストール

Homebrew でインストールできる。

$ brew install tfenv

Homebrew でインストールすると、必要なファイルが以下のような感じで配置される。/usr/local/Cellar/tfenv/2.2.0/bin/tfenv/usr/local/bin/tfenvシンボリックリンクされる。

/usr/local/Cellar/tfenv
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv
/usr/local/Cellar/tfenv/2.2.0/.brew/tfenv.rb
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-list-remote
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-init
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv---version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-uninstall
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-list
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-name
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-help
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-min-required

バージョン確認。

$ tfenv --version
tfenv 2.2.0

内部的には bin/tfenv から libexec の PATH をチェックしつつExportしている。

# Ensure libexec and bin are in $PATH
for dir in libexec bin; do
  case ":${PATH}:" in
    *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
    *) 
      log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
      export PATH="${TFENV_ROOT}/${dir}:${PATH}";
      ;;
  esac;
done;

そこにある libexec/tfenv---version のコマンドが呼ばれている模様。

case "${arg}" in
  "")
    log 'debug' 'No argument provided, dumping version and help and aborting';
    {
      tfenv---version;
      tfenv-help;
    } | abort && exit 1;
exit 1;
    ;;
  -v | --version )
    log 'debug' 'tfenv version requested...';
    exec tfenv---version;
    ;;
  :

ちなみに $TFENV_ROOT は、 tfenv がある場所(/usr/local/Cellar/tfenv/2.2.0/bin/) の一つ上の階層(/usr/local/Cellar/tfenv/2.2.0/)を見ているっぽいたぶん。

if [ -z "${TFENV_ROOT:-""}" ]; then
  # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
  readlink_f() {
    local target_file="${1}";
    local file_name;

    while [ "${target_file}" != "" ]; do
      cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
      file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
      target_file="$(readlink "${file_name}")";
    done;

    echo "$(pwd -P)/${file_name}";
  };

  TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
  [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
else
  TFENV_ROOT="${TFENV_ROOT%/}";
fi;
export TFENV_ROOT;

tfevn で利用可能な Terraform バージョン取得

tfenv list-remote で利用可能なバージョンを取得できる。

$ tfenv list-remote
1.1.5
1.1.4
:

これは https://releases.hashicorp.com/terraform/ の中からバージョン一覧を取得してコネコネしているようだった。

TFENV_REMOTE="${TFENV_REMOTE:-https://releases.hashicorp.com}"
log 'debug' "TFENV_REMOTE: ${TFENV_REMOTE}";

declare remote_versions;
remote_versions="$(curlw -sSf "${TFENV_REMOTE}/terraform/")" \
  || log 'error' "Failed to download remote versions from ${TFENV_REMOTE}/terraform/";

#log 'debug' "Remote versions available: ${remote_versions}"; # Even in debug mode this is too verbose

grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta|alpha|oci)[0-9]*)?" <<<"${remote_versions}" | uniq;

curlwlib/helpers.sh で function が定義されている。TLS のオプションが追加されている。

# Curl wrapper to switch TLS option for each OS
function curlw () {
  local TLS_OPT="--tlsv1.2";

  # Check if curl is 10.12.6 or above
  if [[ -n "$(command -v sw_vers 2>/dev/null)" && ("$(sw_vers)" =~ 10\.12\.([6-9]|[0-9]{2}) || "$(sw_vers)" =~ 10\.1[3-9]) ]]; then
    TLS_OPT="";
  fi;

  curl ${TLS_OPT} "$@";
}
export -f curlw;

tfevn で Terraform インストール

インストールコマンドは以下。

$ tfenv install 1.1.5

Debug レベルを上げて実行してみると、内部的は libexec/tfenv-installtfenv-resolve-version, tfenv-list-remote なども呼び出されて実行されている。 Debug レベル は TFENV_DEBUG で 0-3 で指定できる。0がなし。

% TFENV_DEBUG=2 tfenv install 1.1.5
[DEBUG] Sourcing helpers from /usr/local/Cellar/tfenv/2.2.0/lib/helpers.sh
/usr/local/bin/tfenv: DEBUG trap set
/usr/local/bin/tfenv: Helpers sourced successfully
/usr/local/bin/tfenv: $PATH does not contain '/usr/local/Cellar/tfenv/2.2.0/libexec', prepending and exporting it now
/usr/local/bin/tfenv: $PATH does not contain '/usr/local/Cellar/tfenv/2.2.0/bin', prepending and exporting it now
/usr/local/bin/tfenv: Setting TFENV_DIR to /Users/daisukeshimizu
/usr/local/bin/tfenv: tfenv argument is: install
/usr/local/bin/tfenv: Long argument provided: install
/usr/local/bin/tfenv: Resulting command-path: /usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install
/usr/local/bin/tfenv: Exec: "/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install" "1.1.5"
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: Resolving version with: tfenv-resolve-version 1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: Version Requested: 1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: Version is explicit: 1.1.5. Regex enforces the version: ^1.1.5$
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: Processing install for version 1.1.5, using regex ^1.1.5$
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-list-remote: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-list-remote: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-list-remote: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-list-remote: TFENV_REMOTE: https://releases.hashicorp.com
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: Installing Terraform v1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: Setting curl progress bar with "-#"
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: Downloading release tarball from https://releases.hashicorp.com/terraform/1.1.5/terraform_1.1.5_darwin_amd64.zip
######################################################################################################################################################################################################################################################### 100.0%
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.1.5/terraform_1.1.5_SHA256SUMS
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: No keybase install found, skipping OpenPGP signature verification
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: Archive:  /var/folders/6_/yxwcl8kn6dz_fpt7r0z1qd8h0000gn/T/tfenv_download.XXXXXX.OtnmaKHo/terraform_1.1.5_darwin_amd64.zip
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install:   inflating: /usr/local/Cellar/tfenv/2.2.0/versions/1.1.5/terraform
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-install: Installation of terraform v1.1.5 successful. To make this your default version, run 'tfenv use 1.1.5'

あんまり読めてないけど引数で受け取ったバージョンからリンク(https://releases.hashicorp.com/terraform/1.1.5/terraform_1.1.5_darwin_amd64.zip)を組み立てて、mktemp で一時ディレクトリを作成してそこに落としている。macOS だと $TMPDIR/var/folders/XXX 以下になっているのでそこになる。 その後、unzip で展開して ${TFENV_CONFIG_DIR}/versions/${version} に配置している、 ${TFENV_CONFIG_DIR}lib/helpers.sh で指定されるようになっていた。

if [ -z "${TFENV_CONFIG_DIR:-""}" ]; then
  TFENV_CONFIG_DIR="$TFENV_ROOT";
else
  TFENV_CONFIG_DIR="${TFENV_CONFIG_DIR%/}";
fi
export TFENV_CONFIG_DIR;

デフォルト設定

以下で設定する。

$ tfenv use 1.1.5
Switching default version to v1.1.5
Switching completed

これは結構複雑だった。ので、Debugレベルを上げて実行してみる。 結構いろんなファイルが呼び出されつつ、最終的には libexec/tfenv-exec$PATH 環境変数に該当バージョンの Terraform コマンドのパスを追加している。

% TFENV_DEBUG=2 tfenv use 1.1.5
[DEBUG] Sourcing helpers from /usr/local/Cellar/tfenv/2.2.0/lib/helpers.sh
/usr/local/bin/tfenv: DEBUG trap set
/usr/local/bin/tfenv: Helpers sourced successfully
/usr/local/bin/tfenv: $PATH does not contain '/usr/local/Cellar/tfenv/2.2.0/libexec', prepending and exporting it now
/usr/local/bin/tfenv: $PATH does not contain '/usr/local/Cellar/tfenv/2.2.0/bin', prepending and exporting it now
/usr/local/bin/tfenv: Setting TFENV_DIR to /Users/daisukeshimizu
/usr/local/bin/tfenv: tfenv argument is: use
/usr/local/bin/tfenv: Long argument provided: use
/usr/local/bin/tfenv: Resulting command-path: /usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use
/usr/local/bin/tfenv: Exec: "/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use" "1.1.5"
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: Resolving version with: tfenv-resolve-version 1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: Version Requested: 1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-resolve-version: Version is explicit: 1.1.5. Regex enforces the version: ^1.1.5$
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: Resolved to: 1.1.5:^1.1.5$
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: Searching /usr/local/Cellar/tfenv/2.2.0/versions/ for latest version matching ^1.1.5$
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: Found version: 1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: Switching default version to v1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: Writing "1.1.5" to "/usr/local/Cellar/tfenv/2.2.0/version"
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Looking for a version file in /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /Users/daisukeshimizu/.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /Users/.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: No version file found in /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Looking for a version file in /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /Users/daisukeshimizu/.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /Users/.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: No version file found in /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: No version file found in search paths. Defaulting to TFENV_CONFIG_DIR: /usr/local/Cellar/tfenv/2.2.0/version
/usr/local/Cellar/tfenv/2.2.0/bin/terraform: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/bin/terraform: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/bin/terraform: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/bin/terraform: program="terraform"
/usr/local/Cellar/tfenv/2.2.0/bin/terraform: Exec: "/usr/local/Cellar/tfenv/2.2.0/bin/tfenv" exec "version"
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv: Setting TFENV_DIR to /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv: tfenv argument is: exec
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv: Long argument provided: exec
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv: Resulting command-path: /usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec
/usr/local/Cellar/tfenv/2.2.0/bin/tfenv: Exec: "/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec" "version"
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec: Getting version from tfenv-version-name
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-name: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-name: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-name: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: TFENV_HELPERS is set, not sourcing helpers again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/libexec', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: $PATH already contains '/usr/local/Cellar/tfenv/2.2.0/bin', not adding it again
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Looking for a version file in /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /Users/daisukeshimizu/.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /Users/.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: No version file found in /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Looking for a version file in /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /Users/daisukeshimizu/.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /Users/.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: Not found at /.terraform-version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: No version file found in /Users/daisukeshimizu
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-file: No version file found in search paths. Defaulting to TFENV_CONFIG_DIR: /usr/local/Cellar/tfenv/2.2.0/version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-name: TFENV_VERSION_FILE retrieved from tfenv-version-file: /usr/local/Cellar/tfenv/2.2.0/version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-name: TFENV_VERSION specified in TFENV_VERSION_FILE: 1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-version-name: TFENV_VERSION does not use "latest" keyword
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec: TFENV_VERSION is 1.1.5
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec: TF_BIN_PATH added to PATH: /usr/local/Cellar/tfenv/2.2.0/versions/1.1.5/terraform
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-exec: Executing: /usr/local/Cellar/tfenv/2.2.0/versions/1.1.5/terraform version
/usr/local/Cellar/tfenv/2.2.0/libexec/tfenv-use: Switching completed

まとめ

tfenv を macOS にインストールしつつ、内部の動きを見てみた。 細かいところはまだ理解できてないけど、なんとなく全体感は把握できた。

参考

GitHub - tfutils/tfenv: Terraform version manager