#!/bin/bash
# aws-local-env.sh
#
# Disclaimer: Building AWS Packer images locally has not been tested. The Azure process has.
#
# Variables you'll need to trigger Packer image builds locally
# If you have an ~/.aws/credentials file, you'll need to override that with
# the credentials given to you via this repo: https://github.com/hashicorp/licensing-binaries
# and this job: https://tfe.hashicorp.engineering/terraform/licensing/environments/binaries/changes/runs
#
# The procedure for getting your binary credentials is documented in the [SE Handbook](https://docs.google.com/document/d/1lRYgJMIGejYbaxTpZmc3hnbj7aWRg7dFXCN3_x87mYQ/edit#heading=h.6blw4fxx8vz1)
#
# Below is one example of how to use it
#
#
## Example usage:
#
# $ cd /root/of/this/repository
# $ source aws-local-env.sh
# $ cd hashistack # or cd into any dir containing an AWS Packer build file
# $ packer build hashistack.json
#
# TODO: See if we need a separate set of variables for the binary downloads
# so we're not conflicting with the credentials being used to build the image.

# Source versions from this repository
source versions.sh

if [ -z ${S3BUCKET} ]; then
  read -p $'\033[1;32mPlease enter an S3 bucket name for enterprise binary download: \033[0m' S3BUCKET
  export S3BUCKET="${S3BUCKET}"
else
  export S3BUCKET="${S3BUCKET}"
fi

if [ -z ${AWS_ACCESS_KEY_ID} ]; then
  read -p $'\033[1;32mPlease enter an AWS access key ID for enterprise binary download: \033[0m' AWS_ACCESS_KEY_ID
  export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}"
else
  export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}"
fi

if [ -z ${AWS_SECRET_ACCESS_KEY} ]; then
  read -p $'\033[1;32mPlease enter an AWS secret access key for enterprise binary download: \033[0m' AWS_SECRET_ACCESS_KEY
  export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}"
else
  export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}"
fi

export VCS_NAME="local"
export RELEASE_VERSION="${RELEASE_VERSION}"
export CONSUL_VERSION="${CONSUL_VERSION}"
export VAULT_VERSION="${VAULT_VERSION}"
export NOMAD_VERSION="${NOMAD_VERSION}"
# Re-source this file for every Packer run to re-generate URLs
# They are set to expire after 10 minutes
export CONSUL_ENT_URL=$(AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
  AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
  aws s3 presign \
  --region="us-east-1" \
  s3://${S3BUCKET}/consul-enterprise/${CONSUL_VERSION}/consul-enterprise_${CONSUL_VERSION}+ent_linux_amd64.zip \
  --expires-in 600)
export VAULT_ENT_URL=$(AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
  AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
  aws s3 presign \
  --region="us-east-1" \
  s3://${S3BUCKET}/vault/prem/${VAULT_VERSION}/vault-enterprise_${VAULT_VERSION}+prem_linux_amd64.zip \
  --expires-in 600)
export NOMAD_ENT_URL=$(AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
  AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
  aws s3 presign \
  --region="us-east-1" \
  s3://${S3BUCKET}/nomad-enterprise/${NOMAD_VERSION}/nomad-enterprise_${NOMAD_VERSION}+ent_linux_amd64.zip \
  --expires-in 600)

# Feel free to comment out the below reminder if you're familiar with this process
echo -e "\n\033[0;32mBinary downloads generated by this script expire in 10 minutes."
echo -e "Make sure to re-source this file to regenerate URLs for every Packer build."
