Skip to content

Instantly share code, notes, and snippets.

@hoo29
Created August 12, 2022 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoo29/23534afe8ccd2833b5e87af72c9cc562 to your computer and use it in GitHub Desktop.
Save hoo29/23534afe8ccd2833b5e87af72c9cc562 to your computer and use it in GitHub Desktop.
Scalr pre init hook for cdktf
#!/bin/bash
set -eo pipefail
# Scalr pre-init hook script to install nodejs lts in and synthesise the cdktf into terraform json.
# TODO: support custom runners on other ARCHs
ARCH="x64"
if test -f "cdk.tf.json"; then
echo "cdk.tf.json already exists, no re-synthing."
exit 0
fi
ROOT_DIR=$(pwd)
cd cdktf
export CI=true
# we use asdf https://asdf-vm.com/ as our version management tool.
echo "using node version from .tool-versions"
NODE_VERSION="v$(cat .tool-versions | grep -i nodejs | cut -d " " -f2)"
echo "got $NODE_VERSION as node version"
echo "downloading node"
curl -fsSLO --compressed "https://nodejs.org/dist/$NODE_VERSION/node-$NODE_VERSION-linux-$ARCH.tar.gz"
echo "creating temp folder"
mkdir -p /tmp/node
echo "extracting files"
tar -xzf "node-$NODE_VERSION-linux-$ARCH.tar.gz" -C /tmp/node --strip-components=1 --no-same-owner
rm -f "node-$NODE_VERSION-linux-$ARCH.tar.gz"
echo "updating PATH"
export PATH="$PATH:/tmp/node/bin"
echo "checking node version"
node --version
echo "checking npm version"
npm --version
echo "npm installing needed packages for building"
# To speed this step up and make life easier, build tools are included in normal deps
npm ci --omit=dev
echo "synthesise"
npm run synth
echo "moving stack files to top level"
WORKING_DIR=$(cat cdktf.out/manifest.json | jq -c '.stacks.STACK_NAME_REPLACE_ME.workingDirectory' -r)
WORKING_DIR="cdktf.out/${WORKING_DIR}"
echo "moving contents of $WORKING_DIR to $ROOT_DIR"
mv ${WORKING_DIR}/* "${ROOT_DIR}"
echo "cleanup"
# Scalr maintains the working directory between run stages by backing it up and restoring. Remove large folders to speed this process up.
rm -rf node_modules
rm -rf cdktf.out
cd "$ROOT_DIR"
rm main.tf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment