2 ms·
Have a project that requires multiple versions of terraform? what? you don't want to... clone the terraform source repo ~ git remote -v origin https://g
by mattpallissard 4y ago
Have a project that requires multiple versions of terraform? what? you don't want to...
clone the terraform source repo
~ git remote -v
origin https://github.com/hashicorp/terraform.git (fetch)
origin https://github.com/hashicorp/terraform.git (push)
check out the tag you want
~ git tag | tail -1
v1.1.5
~ git checkout v1.1.5
Previous HEAD position was 516295951 Release v1.1.4
HEAD is now at fe2ddc22a Release v1.1.5
compile/install the binary in a local bin dirctory,
~ go build
go: downloading github.com/aws/aws-sdk-go v1.42.35
~ install terraform ~/bin/terraform-v1.1.5
~ ls ~/bin/terraform\*
/home/matt/bin/terraform /home/matt/bin/terraform-v0.13.7 /home/matt/bin/terraform-v1.1.4
/home/matt/bin/terraform-v0.13.4 /home/matt/bin/terraform-v0.14.9 /home/matt/bin/terraform-v1.1.5
then manage a series of brittle aliases to disptach the proper version?
~ which terraform
terraform () {
binary=terraform
if [[ $1 == "v"\* ]] && [[ $1 != "validate" ]]
then
version=$1
shift
binary="$binary-$version"
[ -f ~/bin/$binary ] || bail 1 "missing binary $binary" || return 1
else
binary=/usr/bin/$binary
fi
dispatch --name terraform --scope --slice compilers.slice -c 35 -mh 2048M -mm 2048M -s 1M --binary $binary
"$@"
}
(dispatch just runs things with memory and cpu limits)
~ which dispatch
dispatch () {
if [[ $USER == "root" ]]
then
command "$binary" "$@"
return $?
fi
declare args=(--user --same-dir -p IOAccounting=yes -p MemoryAccounting=yes -p TasksAccounting=yes)
while (($#))
do
case "$1" in
(-c) args+="-p"
args+="CPUWeight=$2"
shift 2 ;;
(-mm) args+="-p"
args+="MemoryMax=$2"
shift 2 ;;
(-mh) args+="-p"
args+="MemoryHigh=$2"
shift 2 ;;
(-s) args+="-p"
args+="MemorySwapMax=$2"
shift 2 ;;
(--scope) args+=--scope
shift ;;
(--slice) args+="--slice=$2"
shift 2 ;;
(--name) name=$1
shift 2 ;;
(-P) args+=-P
shift ;;
(--binary) [ -z "$name" ] || name=$2
binary="$2"
shift ;;
(*) break ;;
esac
done
systemd-run $args "$@" 2> >(>&2 grep -vE 'Running.*as unit:')
}
and of course, then you'd need a shitty little script to call your alias when other tools decide they want to call terraform
~ cat ~/bin/terraform
#!/usr/bin/env zsh
source ~/.zshrc >/dev/null 2>/dev/null || exit 1
terraform $@
Because that would be silly and dumb, and I totally don't do that for everything.
Don't even get me started on virtualenvs.
- kubanczyk 4y agoWhy do you need that dispatch? My anecdata is that for Terraform, between v0.12 and v1.1, on the big three clouds, with quite complex configs, I never noticed a single problem which systemd unit would solve. Curiosity, not criticism. Ok there's criticism as well: why do you need an alias and a script that calls that alias? Get rid of the alias, just paste it into a script then?
- viraptor 4y agoDo you need different versions of terraform in the same directory? (I suspect not, since that would break lck files) If not, you can use a .tool-version file with a directory-specific terraform version.