Skip to content

Commit

Permalink
🤗 Add Terraform wrapper script (#146)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Woffenden <jacob.woffenden@digital.justice.gov.uk>
  • Loading branch information
jacobwoffenden authored Oct 21, 2024
1 parent 9cc1908 commit 20e7702
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ jobs:
file: images/base/Dockerfile
context: images/base
load: true
tags: ghcr.io/ministryofjustice/devcontainer-base:local-feature-test-${{ github.sha }}
tags: ghcr.io/ministryofjustice/devcontainer-base:local-feature-test

- name: Testing ${{ matrix.feature }}
id: test_feature
Expand All @@ -165,7 +165,7 @@ jobs:
--skip-scenarios \
--project-folder features \
--features ${{ matrix.feature }} \
--base-image ghcr.io/ministryofjustice/devcontainer-base:local-feature-test-${{ github.sha }}
--base-image ghcr.io/ministryofjustice/devcontainer-base:local-feature-test
- name: Testing ${{ matrix.feature }} scenarios
id: test_feature_scenarios
Expand All @@ -175,7 +175,7 @@ jobs:
--features ${{ matrix.feature }} \
--skip-autogenerated \
--skip-duplicated \
--base-image ghcr.io/ministryofjustice/devcontainer-base:local-feature-test-${{ github.sha }}
--base-image ghcr.io/ministryofjustice/devcontainer-base:local-feature-test
validate:
needs: [detect-changes]
Expand Down
6 changes: 6 additions & 0 deletions features/src/terraform/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.0] - 2024-10-17

### Added

- Default to installing wrapper script that sets `TF_DATA_DIR` to `/tmp/terraform/${currentDirectory}` ([#140](https://github.com/ministryofjustice/.devcontainer/issues/140))

## [1.1.0] - 2024-08-22

### Changed
Expand Down
9 changes: 7 additions & 2 deletions features/src/terraform/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "terraform",
"version": "1.1.0",
"version": "1.2.0",
"name": "Terraform",
"documentationURL": "https://github.com/ministryofjustice/.devcontainer/tree/main/features/src/terraform",
"description": "Installs the Terraform CLI and tflint",
"description": "Installs the Terraform CLI, tflint and a wrapper script to aid with permissions issues on macOS",
"options": {
"terraformCliVersion": {
"type": "string",
Expand All @@ -16,6 +16,11 @@
"description": "Version of the tflint to install",
"proposals": ["latest"],
"default": "latest"
},
"installTerraformWrapper": {
"type": "boolean",
"description": "Install a Terraform wrapper to help with this permissions bug (https://github.com/ministryofjustice/.devcontainer/issues/140)",
"default": true
}
},
"customizations": {
Expand Down
8 changes: 7 additions & 1 deletion features/src/terraform/install-terraform-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ get_system_architecture

GITHUB_REPOSITORY="hashicorp/terraform"
VERSION="${TERRAFORMCLIVERSION:-"latest"}"
INSTALL_TERRAFORM_WRAPPER="${INSTALLTERRAFORMWRAPPER:-"true"}"

if [[ "${VERSION}" == "latest" ]]; then
get_github_latest_tag "${GITHUB_REPOSITORY}"
Expand All @@ -25,7 +26,12 @@ curl --fail-with-body --location "https://releases.hashicorp.com/terraform/${VER

unzip "terraform_${VERSION_STRIP_V}_linux_${ARCHITECTURE}.zip"

install --owner=vscode --group=vscode --mode=775 terraform /usr/local/bin/terraform
if [[ "${INSTALL_TERRAFORM_WRAPPER}" == "true" ]]; then
install --owner=vscode --group=vscode --mode=775 "$(dirname "${0}")"/src/usr/local/bin/terraform /usr/local/bin/terraform
install --owner=vscode --group=vscode --mode=775 terraform /usr/local/bin/terraform-bin
else
install --owner=vscode --group=vscode --mode=775 terraform /usr/local/bin/terraform
fi

install --owner=vscode --group=vscode --mode=775 "$(dirname "${0}")"/src/home/vscode/.devcontainer/featurerc.d/terraform-cli.sh /home/vscode/.devcontainer/featurerc.d/terraform-cli.sh

Expand Down
9 changes: 9 additions & 0 deletions features/src/terraform/src/usr/local/bin/terraform
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

# If current directory contains .tf files, set TF_DATA_DIR to /tmp/terraform/${currentDirectory} otherwise use default
if ls -- *.tf &>/dev/null; then
currentDirectory="$(basename "$(pwd)")"
export TF_DATA_DIR="/tmp/terraform/${currentDirectory}"
fi

exec /usr/local/bin/terraform-bin "$@"
11 changes: 11 additions & 0 deletions features/test/terraform/no_wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -e

# shellcheck source=/dev/null
# file only accessible when using devcontainer CLI
source dev-container-features-test-lib

check "terraform version" terraform version

reportResults
10 changes: 10 additions & 0 deletions features/test/terraform/scenarios.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"no_wrapper": {
"image": "ghcr.io/ministryofjustice/devcontainer-base:local-feature-test",
"features": {
"terraform": {
"installTerraformWrapper": false
}
}
}
}
7 changes: 7 additions & 0 deletions scripts/features/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ devcontainer features test \
--skip-scenarios \
--skip-duplicated \
--base-image ghcr.io/ministryofjustice/devcontainer-base:local-feature-test

devcontainer features test \
--project-folder features \
--features "${FEATURE_TO_TEST}" \
--skip-autogenerated \
--skip-duplicated \
--base-image ghcr.io/ministryofjustice/devcontainer-base:local-feature-test

0 comments on commit 20e7702

Please sign in to comment.