Skip to content

Commit

Permalink
Merge pull request #10 from mintel/fix-tool-installation
Browse files Browse the repository at this point in the history
Use asdf properly for tool installation and fix build caching issues
  • Loading branch information
chrlwrd authored Jan 20, 2023
2 parents a32cc5c + d83e132 commit 6f037c8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [1.2.0_mintel.0.3.0] - 2023-01-20
### Changed
- Use asdf to install tools rather than individual bash functions

## [1.2.0_mintel.0.2.0] - 2023-01-17
### Added
- Add changelog
Expand Down
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ RUN apt-get update && \
zip \
&& rm -rf /var/lib/apt/lists/*

USER tfc-agent

# Install tools via asdf (grabbing versions from build-harness-extensions)
ADD --chown=tfc-agent https://raw.githubusercontent.com/mintel/build-harness-extensions/main/modules/satoshi/tf-tool-versions .
COPY install-binaries.sh .
RUN ./install-binaries.sh

USER tfc-agent
# Set environment variables required for asdf-installed tools to function when used by the tfc-agent:
# - PATH is required since the asdf bash plugin isn't installed, so we need to set where to find asdf's shims for the installed binaries
# - ASDF_DATA_DIR is required since the agent overrides the HOME var to ensure a clean environment each run, which confuses asdf's shims
ENV PATH="/home/tfc-agent/.asdf/shims:$PATH" \
ASDF_DATA_DIR="/home/tfc-agent/.asdf"
52 changes: 7 additions & 45 deletions install-binaries.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,12 @@
#!/usr/bin/bash
# Unfortunately due to the way that the agent works we are unable to use asdf so functions to install all additional
# binaries required by the Terraform Cloud agent should be added here in the format "install_${APP_NAME}". App names and
# versions are pulled from the common .terraform-tools file at
# https://github.com/mintel/build-harness-extensions/blob/main/modules/satoshi/tf-tool-versions. If a function hasn't
# been written for an app in that file then the build will fail.
# Tool versions are pulled at build time from https://github.com/mintel/build-harness-extensions/blob/main/modules/satoshi/tf-tool-versions

set -ex

function install_awscli() {
local version; version=$1
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-${version}.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install
rm awscliv2.zip
}
# Install asdf
git clone https://github.com/asdf-vm/asdf.git $HOME/.asdf

function install_terraform-docs() {
local version; version=$1
curl -Lo ./terraform-docs.tar.gz "https://github.com/terraform-docs/terraform-docs/releases/download/v${version}/terraform-docs-v${version}-$(uname)-amd64.tar.gz"
tar -xzf terraform-docs.tar.gz
chmod +x terraform-docs
mv terraform-docs /usr/local/terraform-docs
rm terraform-docs.tar.gz
}

function install_tflint() {
local version; version=$1
curl -s "https://raw.githubusercontent.com/terraform-linters/tflint/v${version}/install_linux.sh" | bash
}

function install_tfsec {
local version; version=$1
curl -Lo ./tfsec "https://github.com/aquasecurity/tfsec/releases/download/v${version}/tfsec-linux-amd64"
chmod +x tfsec
mv tfsec /usr/local/tfsec
}

curl -s https://raw.githubusercontent.com/mintel/build-harness-extensions/main/modules/satoshi/tf-tool-versions | grep -vP '(^terraform\s|\sterraform$)' | grep '^[A-Za-z]' > "$HOME/.tool-versions"
while read -r line; do
app=$(echo "$line" | cut -d' ' -f1)
version=$(echo "$line" | cut -d' ' -f2)
func="install_${app}"
if declare -f "$func"; then
"$func" "$version"
else
echo "ERROR: Function not available to install $app"
exit 1
fi
done < "$HOME/.tool-versions"
# Install tools via asdf
grep -vP '(^terraform\s|\sterraform$)' tf-tool-versions > $HOME/.tool-versions
grep -E "^#asdf:" $HOME/.tool-versions | cut -d':' -f2- | tr '\n' '\0' | xargs -0 -n1 -Icmd -- sh -c '$HOME/.asdf/bin/asdf cmd'
$HOME/.asdf/bin/asdf install

0 comments on commit 6f037c8

Please sign in to comment.