Установка tfswitch в Unix/Linux

Установка tfswitch в Unix/Linux


Tfswitch — Инструмент командной строки позволяет переключаться между различными версиями terraform. Если у вас не установлена определенная версия terraform, tfswitch позволяет загрузить желаемую версию. Установка минимальна и проста. После установки просто выберите нужную версию из раскрывающегося списка и начните использовать terraform.

Установка tfswitch в Unix/Linux

Установка очень простая и приведу пару примеров как можно установить желаемую тулзу.

Установка tfswitch в Mac OS

Для начала, стоит установить HOMEBREW, и после чего, выполняем установку:

$ brew install warrensbox/tap/tfswitch

Перейдем к настройке и использовании.

Установка tfswitch в Linux

Для Линукса, разработчики предусмотрели шел-скрипт:

$ curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh | bash

Или, через Snapcraft для CentOS, Ubuntu, Linux Mint, RHEL, Debian, Fedora:

$ sudo snap install tfswitch

Или, можно скомпилить тулу с исходного кода:

$ git clone https://github.com/warrensbox/terraform-switcher.git

Можно использовать!

Настройка и использование tfswitch в Unix/Linux

Если вы используете bash, то для автоматического переключения можно добавить следующую настройку в «~/.bashrc»:

cdtfswitch(){ 
  builtin cd "$@";
  cdir=$PWD;
  if [ -e "$cdir/.tfswitchrc" ]; then
    tfswitch
  fi
}
alias cd='cdtfswitch'

Замечание, — можно использовать «.tfswitchrc» или «.tfswitch.toml» или «.terraform-version».

Если вы используете zsh, то для автоматического переключения можно добавить следующую настройку в «~/.zshrc»:

nload-tfswitch() {
  local tfswitchrc_path=".tfswitchrc"

  if [ -f "$tfswitchrc_path" ]; then   
    tfswitch
  fi
}
add-zsh-hook chpwd load-tfswitch
load-tfswitch

Если вы используете fish, то для автоматического переключения можно добавить следующую настройку в «~/.config/fish/config.fish»:

function switch_terraform --on-event fish_postexec
   string match --regex '^cd\s' "$argv" > /dev/null
   set --local is_command_cd $status

   if test $is_command_cd -eq 0 
     if count *.tf > /dev/null

       grep -c "required_version" *.tf > /dev/null
       set --local tf_contains_version $status

       if test $tf_contains_version -eq 0     
           command tfswitch
       end
     end
   end
end

Как-то так.

Использование tfswitch

Переходим в проект с Терраформ кодом и выполняем:

~/Projects/Terraform/aws/examples/transit_gateway   dev  tfswitch
Reading required version from terraform file, constraint: ~> 0.13
Matched version: 0.13.5
2020/12/08 19:51:46 Creating directory for terraform: /Users/captain/.terraform.versions/
Downloading https://releases.hashicorp.com/terraform/0.13.5/terraform_0.13.5_darwin_amd64.zip to terraform_0.13.5_darwin_amd64.zip
Downloading ...
35665893 bytes downloaded.
Switched terraform to version "0.13.5"
 ~/Projects/Terraform/aws/examples/transit_gateway   dev 

После этого, tfswitch скачает нужную версию ПО которая описана в вашем файле:

terraform {
 required_version = "~> 0.13"
}

Использование tfswitch в Continuous Integration

Например, можно внедрить tfswitch в Jenkins пайплайну:

#!/bin/bash 

echo "Installing tfswitch locally"
wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh #Get the installer on to your machine

chmod 755 install.sh #Make installer executable

./install.sh -b `pwd`/.bin     #Install tfswitch in a location you have permission

CUSTOMBIN=`pwd`/.bin           #set custom bin path

export PATH=$PATH:$CUSTOMBIN   #Add custom bin path to PATH environment

$CUSTOMBIN/tfswitch -b $CUSTOMBIN/terraform 0.11.7 #or simply tfswitch -b $CUSTOMBIN/terraform 0.11.7

terraform -v                   #testing version

Так же, можно внедрить tfswitch в Circle CI пайплайну:

version: 2
jobs:
 build:
   docker:
     - image: ubuntu

   working_directory: /go/src/github.com/warrensbox/terraform-switcher

   steps:
     - checkout
     - run: 
         command: |   
           set +e  
           apt-get update 
           apt-get install -y wget 
           rm -rf /var/lib/apt/lists/*

           echo "Installing tfswitch locally"

           wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh #Get the installer on to your machine

           chmod 755 install.sh           #Make installer executable

           ./install.sh -b `pwd`/.bin     #Install tfswitch in a location you have permission

           CUSTOMBIN=`pwd`/.bin           #set custom bin path

           export PATH=$PATH:$CUSTOMBIN   #Add custom bin path to PATH environment

           $CUSTOMBIN/tfswitch -b $CUSTOMBIN/terraform 0.11.7 #or simply tfswitch -b $CUSTOMBIN/terraform 0.11.7

           terraform -v                   #testing version

Вот и все, статья «Установка tfswitch в Unix/Linux» завершена.







Report Page