Commit 92d7c648 authored by Daniel Scholl's avatar Daniel Scholl
Browse files

Merge branch 'bundler' into 'master'

Added in terraform bundle support

See merge request !1
parents 7442cb1e d5d19ee3
Pipeline #75952 passed with stages
in 7 minutes and 19 seconds
......@@ -7,6 +7,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.0.2] - 2021-03-23
### Terraform Bundle
- Terraform Version moved to 0.14.8
- Terraform Bundle Support Added
- Initial Providers Added
- Entrypoint set
## [0.0.1] - 2021-03-18
### Initial Code
......
......@@ -7,27 +7,35 @@ FROM ${STEP_1_IMAGE} AS STEP_1
ARG TERRAFORM_VERSION=0.14.8
ARG AZURE_CLI_VERSION=2.20.0
ENV PATH="~/bin:${PATH}"
ENV BUILD_PACKAGES \
bash \
curl \
tar \
openssh-client \
sshpass \
git
git \
alpine-sdk \
bash \
jq \
ncurses
WORKDIR /osdu
RUN set -x \
&& apk update \
&& apk upgrade \
&& apk add --no-cache ${BUILD_PACKAGES}
&& apk add --no-cache ${BUILD_PACKAGES} \
&& mkdir bin
# Terraform
ENV TF_DEV=true
ENV TF_RELEASE=true
COPY scripts scripts
COPY bundle.json bundle.json
RUN scripts/bundle.sh v${TERRAFORM_VERSION}
RUN scripts/build.sh linux ${TERRAFORM_VERSION}
RUN unzip build/terraform_*.zip -d /bin
WORKDIR $GOPATH/src/github.com/hashicorp/terraform
RUN git clone https://github.com/hashicorp/terraform.git ./ \
&& git checkout v${TERRAFORM_VERSION} \
&& /bin/bash scripts/build.sh
FROM ${STEP_2_IMAGE} AS STEP_2
......@@ -36,9 +44,6 @@ ARG AZURE_CLI_VERSION=2.16.0
LABEL Name="danielscholl/docker-terrraform-azure" \
Version=${IMAGE_TAG}
# Copy from Step 1
COPY --from=STEP_1 /go/bin/terraform /usr/bin/terraform
ENV BASE_PACKAGES \
gettext \
git \
......@@ -68,9 +73,14 @@ RUN set -x \
&& rm -rf /var/cache/apk/* \
&& ln -s /usr/bin/python3 /usr/bin/python
# Copy from Step 1
COPY --from=STEP_1 /bin/terraform /usr/bin/terraform
# Create Terraform User
RUN addgroup -S terraform && adduser -S terraform -G terraform
USER terraform
WORKDIR /home/terraform
ENTRYPOINT ["/usr/bin/terraform"]
CMD ["--help"]
{
"providers": [
{
"name": "azurerm",
"version": "2.52.0"
},
{
"name": "azuread",
"version": "1.4.0"
},
{
"name": "azuredevops",
"url": "https://github.com/microsoft/terraform-provider-azuredevops",
"version": "v0.1.3"
}
]
}
#!/usr/bin/env bash
#
# Purpose: Build Terraform using Terraform-Bundle
# Usage:
# build.sh linux 0.14.8
# https://elrey.casa/bash/scripting/harden
set -${-//[sc]/}eu${DEBUG+xv}o pipefail
###############################
## ARGUMENT INPUT ##
###############################
usage() { echo "Usage: build.sh <TARGET_PLATFORM> <TARGET_VERSION>" 1>&2; exit 1; }
BASE_DIR=$PWD;
uname_str=$(uname)
if [ $uname_str = "Darwin" ]; then
RUNNING_PLATFORM="darwin"
else
RUNNING_PLATFORM="linux"
fi
if [ ! -z $1 ]; then TARGET_PLATFORM=$1; fi
if [ -z $TARGET_PLATFORM ]; then
tput setaf 1; echo 'ERROR: TARGET_PLATFORM not provided' ; tput sgr0
usage;
fi
if [ ! -z $2 ]; then TERRAFORM_VERSION=$2; fi
if [ -z $TERRAFORM_VERSION ]; then
tput setaf 1; echo 'ERROR: TERRAFORM_VERSION not provided' ; tput sgr0
usage;
fi
bundle_json="$(cat $BASE_DIR/bundle.json)"
build_dir=$BASE_DIR/build
plugins_dir=$BASE_DIR/build/plugins
scripts_dir=$BASE_DIR/scripts
###############################
## FUNCTIONS ##
###############################
function prepareDirectories() {
if ls $build_dir 1> /dev/null 2>&1; then
rm -rf $build_dir
fi
mkdir $build_dir
if ls $plugins_dir 1> /dev/null 2>&1; then
rm -rf $plugins_dir
fi
mkdir $plugins_dir
}
function downloadProviders() {
echo $bundle_json | jq -c -r '.providers[]' | while read provider ; do
provider_name=$(echo $provider | jq -r '.name')
provider_version=$(echo $provider | jq -r '.version')
provider_url=$(echo $provider | jq -r '.url')
$scripts_dir/providers.sh $provider_name $provider_version $provider_url $RUNNING_PLATFORM $TARGET_PLATFORM
done
}
function generateBundle() {
# Clean up file if exists
cat /dev/null > $build_dir/terraform-bundle.hcl
# Add terraform block
cat >>$build_dir/terraform-bundle.hcl <<CONFIG
terraform {
# Version of Terraform to include in the bundle. An exact version number
# is required.
version = "$TERRAFORM_VERSION"
CONFIG
# Add providers block
echo " required_providers {" >> $build_dir/terraform-bundle.hcl
echo $bundle_json | jq -c -r '.providers[]' | while read provider ; do
provider_name=$(echo $provider | jq -r '.name')
provider_version=$(echo $provider | jq -r '.version')
cat >>$build_dir/terraform-bundle.hcl <<CONFIG
$provider_name = {
version = "$provider_version"
source = "hashicorp/$provider_name"
}
CONFIG
done
# Close providers section
echo " }" >> $build_dir/terraform-bundle.hcl
# Close terraform block section
echo "}" >> $build_dir/terraform-bundle.hcl
}
function buildBundle() {
pushd $build_dir
$BASE_DIR/bin/terraform-bundle-${TERRAFORM_VERSION}_${RUNNING_PLATFORM}_amd64 package -os=$TARGET_PLATFORM -arch=amd64 $build_dir/terraform-bundle.hcl
popd
}
##############################
## Execution ##
##############################
prepareDirectories
downloadProviders
generateBundle
buildBundle
#!/usr/bin/env bash
# https://elrey.casa/bash/scripting/harden
set -${-//[sc]/}eu${DEBUG+xv}o pipefail
###############################
## ARGUMENT INPUT ##
###############################
usage() { echo "Usage: bundle.sh v<TARGET_VERSION>" 1>&2; exit 1; }
if [[ ! $(command -v git) ]]; then
echo "git is required"
exit 1
fi
TF_TAG=$1
TF_REPO="https://github.com/hashicorp/terraform"
BUNDLE_PATH="tools/terraform-bundle"
PLATFORMS="linux darwin"
###############################
## FUNCTIONS ##
###############################
function build() {
local platform=$1
local artifact_name=$2
CGO_ENABLED=0 GOOS="$platform" go build -trimpath -o "$artifact_name"
}
##############################
## Execution ##
##############################
tmp=$(mktemp -d)
bundler_dir=$(pwd)
cd "$tmp"
git clone --single-branch --depth 1 -b "$TF_TAG" "$TF_REPO" .
cd "${BUNDLE_PATH}"
for platform in $PLATFORMS; do
artifact="terraform-bundle-${TF_TAG#v}_${platform}_amd64"
build "$platform" "$artifact"
cp "$tmp/$BUNDLE_PATH/$artifact" "$bundler_dir/bin/"
done
rm -rf "$tmp"
#!/usr/bin/env bash
#
# Purpose: Clean up Build Folders
# Usage:
# clean.sh
# https://elrey.casa/bash/scripting/harden
set -${-//[sc]/}eu${DEBUG+xv}o pipefail
###############################
## ARGUMENT INPUT ##
###############################
usage() { echo "Usage: clean.sh" 1>&2; exit 1; }
BASE_DIR=$PWD;
##############################
## Execution ##
##############################
# Remove the Build Folder
if ls $BASE_DIR/build 1> /dev/null 2>&1; then
printf "\n"
tput setaf 2; echo "Removing Build Folder" ; tput sgr0
tput setaf 3; echo "-----------------------------" ; tput sgr0
rm -rf $BASE_DIR/build
fi
# Remove the Bundle
if [ -d "$BASE_DIR/docker/bundle.zip" ]; then
printf "\n"
tput setaf 2; echo "Removing Bundle" ; tput sgr0
tput setaf 3; echo "-----------------------------" ; tput sgr0
rm $BASE_DIR/docker/bundle.zip;
fi
#!/usr/bin/env bash
#
# Purpose: Install Providers into a subdirectory
# Usage:
# providers.sh
# https://elrey.casa/bash/scripting/harden
set -${-//[sc]/}eu${DEBUG+xv}o pipefail
###############################
## ARGUMENT INPUT ##
###############################
usage() { echo "Usage: providers.sh <PROVIDER_NAME> <PROVIDER_VERSION> <PROVIDER_GITHUB_URL> <RUNNING_PLATFORM> <ARCH>" 1>&2; exit 1; }
if [ ! -z $1 ]; then PROVIDER_NAME=$1; fi
if [ -z $PROVIDER_NAME ]; then
tput setaf 1; echo 'ERROR: PROVIDER_NAME not provided' ; tput sgr0
usage;
fi
if [ ! -z $2 ]; then PROVIDER_VERSION=$2; fi
if [ -z $PROVIDER_VERSION ]; then
tput setaf 1; echo 'ERROR: PROVIDER_VERSION not provided' ; tput sgr0
usage;
fi
if [ ! -z $3 ]; then PROVIDER_GITHUB_URL=$3; fi
if [ -z $PROVIDER_GITHUB_URL ]; then
tput setaf 1; echo 'ERROR: PROVIDER_GITHUB_URL not provided' ; tput sgr0
usage;
fi
if [ ! -z $4 ]; then RUNNING_PLATFORM=$4; fi
if [ -z $RUNNING_PLATFORM ]; then
tput setaf 1; echo 'ERROR: RUNNING_PLATFORM not provided' ; tput sgr0
usage;
fi
if [ ! -z $5 ]; then ARCH=$5; fi
if [ -z $ARCH ]; then
tput setaf 1; echo 'ERROR: ARCH not provided' ; tput sgr0
usage;
fi
BASE_DIR=$PWD;
OUTPUT_DIRECTORY="${BASE_DIR}/build/plugins"
##############################
## Execution ##
##############################
pushd "${BASE_DIR}" > /dev/null
# Download 'fetch' if it doesn't already exist.
if ! [ -x "$(command -v fetch)" ]; then
wget "https://github.com/gruntwork-io/fetch/releases/download/v0.3.6/fetch_${RUNNING_PLATFORM}_amd64" -O bin/fetch && chmod a+x bin/fetch
fi
# Delete any previously downloaded release for the current provider.
rm -f terraform-provider-"${PROVIDER_NAME}"*.zip
if [ ${PROVIDER_GITHUB_URL} = null ]; then
wget "https://releases.hashicorp.com/terraform-provider-${PROVIDER_NAME}/${PROVIDER_VERSION}/terraform-provider-${PROVIDER_NAME}_${PROVIDER_VERSION}_${ARCH}_amd64.zip"
else
bin/fetch --tag="${PROVIDER_VERSION}" --repo="${PROVIDER_GITHUB_URL}" --release-asset="terraform-provider-${PROVIDER_NAME}_.*_${ARCH}_amd64.zip" .
fi
# Unzip the downloaded release.
unzip -o terraform-provider-"${PROVIDER_NAME}"*.zip -d "${OUTPUT_DIRECTORY}"
pushd "${OUTPUT_DIRECTORY}" > /dev/null
# Make sure that the resulting binary is correctly named ('terraform-provider-<name>_<version>', where '<version>' starts with a 'v').
[[ ${PROVIDER_VERSION} == v* ]] || PROVIDER_VERSION="v${PROVIDER_VERSION}"
[[ -f "terraform-provider-${PROVIDER_NAME}" ]] && mv "terraform-provider-${PROVIDER_NAME}" "terraform-provider-${PROVIDER_NAME}_${PROVIDER_VERSION}"
popd > /dev/null
# Delete the downloaded release.
rm -f terraform-provider-"${PROVIDER_NAME}"*.zip
popd > /dev/null
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment