From 88628ef927549686e0f1752c08c5265ece3f9d4d Mon Sep 17 00:00:00 2001 From: Bue Petersen <1579826+buep@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:37:03 +0200 Subject: [PATCH] fix: improve alias script for terraform/tofu (#30) Extract script from inline complex command to separate script and improve script with more information and cases. Makes it easier to debug and understand, and allows linting etc. On some environments we might have issues around the init phase, so this makes it easier to debug. --- .../config/alias-terraform-tofu-init.sh | 32 +++++++++++++++++++ devbox-plugins/base-config/plugin.json | 9 +++--- 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 devbox-plugins/base-config/config/alias-terraform-tofu-init.sh diff --git a/devbox-plugins/base-config/config/alias-terraform-tofu-init.sh b/devbox-plugins/base-config/config/alias-terraform-tofu-init.sh new file mode 100644 index 0000000..e1740b4 --- /dev/null +++ b/devbox-plugins/base-config/config/alias-terraform-tofu-init.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# Used to configure terraform/tofu aliases, and fake alias for the one not used to avoid +# confusion about which kind of project this is. + +set -eo pipefail + +if [[ -d provision ]]; +then + LOCKFILE=$(find ./provision -type f -name '.terraform.lock.hcl' | head -n1) + + if [[ ! -z "${LOCKFILE}" ]] + then + if head -n1 "${LOCKFILE}" | grep -q "tofu init" + then + mkdir -p .aliases + echo 'echo \"this project uses tofu\"' > .aliases/terraform + chmod +x .aliases/terraform + echo "open tofu project, made fake terraform alias" + else + mkdir -p .aliases + echo 'echo \"this project uses terraform\"' > .aliases/tofu + chmod +x .aliases/tofu + echo "terraform project, made fake tofu alias" + fi + else + echo "Provision folder found, but no terraform lock file found, skip terraform-opentofu alias setup" + fi + +else + echo "This project do not provision, skip terraform-opentofu alias setup" +fi diff --git a/devbox-plugins/base-config/plugin.json b/devbox-plugins/base-config/plugin.json index 2203bc9..1c0e34b 100644 --- a/devbox-plugins/base-config/plugin.json +++ b/devbox-plugins/base-config/plugin.json @@ -66,7 +66,8 @@ "{{ .Virtenv }}/ssh_host_connection_configuration_templates/ssh_host_connection_config.knownhost.j2": "config/ssh_host_connection_configuration_templates/ssh_host_connection_config.knownhost.j2", "{{ .Virtenv }}/ansible-requirements-helper.sh": "config/ansible-requirements-helper.sh", "{{ .Virtenv }}/devbox-requirements.txt": "config/devbox-requirements.txt", - "{{ .Virtenv }}/migrate-1x-to-2x-helper.sh": "config/migrate-1x-to-2x-helper.sh" + "{{ .Virtenv }}/migrate-1x-to-2x-helper.sh": "config/migrate-1x-to-2x-helper.sh", + "{{ .Virtenv }}/alias-terraform-tofu-init.sh": "config/alias-terraform-tofu-init.sh" }, "shell": { "init_hook": [ @@ -94,10 +95,8 @@ "mkdir -p .aliases", "export PATH=\"$PWD/.aliases\":$PATH", "if crudini --get .gitignore \"\" \".aliases\";then true; else echo -e '\n\n# Aliases dir with script for devbox\n.aliases' >> .gitignore; fi", - - // Create an alias script for terraform/tofu that is not used in this project, only runs if it can find a terraform lock file - // Note using both terraform and tofu in a project is not supported - "LOCKFILE=`find ./provision -type f -name '.terraform.lock.hcl' | head -n1`;if [ ! -z $LOCKFILE ];then if `head -n1 $LOCKFILE | grep -q \"tofu init\"`;then echo 'echo \"this project uses tofu\"' > .aliases/terraform; chmod +x .aliases/terraform;else echo 'echo \"this project uses terraform\"' > .aliases/tofu; chmod +x .aliases/tofu;fi;fi" + // run the different alias scripts: + "bash {{ .Virtenv }}/alias-terraform-tofu-init.sh" ], "scripts": { "bootstrap-envrc-private": [