Skip to content

Commit

Permalink
updated script and dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishjullia committed Sep 18, 2023
1 parent db63710 commit 6eb0457
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM amazon/aws-cli
RUN yum -y update && yum install -y \
RUN yum -y update && yum -y updateinfo && yum install -y \
git \
jq \
unzip \
Expand All @@ -16,8 +16,9 @@ RUN yum -y update && yum install -y \
&& curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 \
&& chmod 700 get_helm.sh \
&& sh ./get_helm.sh \
&& yum upgrade openssl \
&& pip3 install print-env \
&& yum clean all \
&& pip3 install print-env
ENV PATH="/root/.tfenv/bin:$PATH"
RUN mkdir /work
WORKDIR /work
Expand Down
50 changes: 24 additions & 26 deletions script.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
#!/usr/bin/env bash
# This shebang is more portable and will find bash wherever it's located in the system.

# Set script to exit on error or if an undefined variable is used
# Exit on error or if an undefined variable is used
set -eu

# Function to print an error message and exit the script
# Arguments:
# 1. Error message (required)
# 2. Exit status code (optional; default is 1)
# Check for dependencies
for cmd in jq tfenv aws curl; do
command -v "$cmd" >/dev/null 2>&1 || error_exit "$cmd is required but not installed."
done

# Print error and exit
function error_exit {
# Print the error message to stderr
echo "$1" >&2
# Exit the script with the provided status code, or 1 if none was provided
exit "${2:-1}"
}

while IFS="=" read -r key value; do export "$key=$(printf %b "$value")"; done < <(print-env --api https://portunusapiprod.ashishjullia.com/env --format json | jq -r 'to_entries[] | "\(.key)=\(.value)"')

# Check if the necessary environment variables are set, and exit with an error message if they're not
# Ensure PORTUNUS_TOKEN is set
[[ -z "${PORTUNUS_TOKEN}" ]] && error_exit "PORTUNUS_TOKEN is not set."

# If TF_VERSION is set, use that. Otherwise, default to the latest version
# Fetch and export environment variables from a given API
while IFS="=" read -r key value; do
export "$key=$(printf %b "$value")"
done < <(print-env --api "https://portunusapiprod.ashishjullia.com/env" --format json | jq -r 'to_entries[] | "\(.key)=\(.value)"')

# Install specified Terraform version or default to the latest
TF_VERSION="${TF_VERSION:-latest}"
echo "Installing Terraform version: ${TF_VERSION}"
tfenv install $TF_VERSION || error_exit "Failed to install Terraform version: ${TF_VERSION}"
tfenv use $TF_VERSION || error_exit "Failed to switch to Terraform version: ${TF_VERSION}"
tfenv install "$TF_VERSION" || error_exit "Failed to install Terraform version: ${TF_VERSION}"
tfenv use "$TF_VERSION" || error_exit "Failed to switch to Terraform version: ${TF_VERSION}"

# Configure AWS
# Configure AWS with specified credentials
echo "Configuring AWS with region: ${AWS_REGION}"
# Set the AWS region, and exit with an error message if the command fails
aws configure set region $AWS_REGION || error_exit "Failed to set AWS region: ${AWS_REGION}"
# Set the AWS access key, and exit with an error message if the command fails
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID || error_exit "Failed to set AWS access key."
# Set the AWS secret access key, and exit with an error message if the command fails
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY || error_exit "Failed to set AWS secret access key."

# Update the kubeconfig for the specified cluster if NAME_OF_CLUSTER is set
aws configure set region "$AWS_REGION" || error_exit "Failed to set AWS region: ${AWS_REGION}"
aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" || error_exit "Failed to set AWS access key."
aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" || error_exit "Failed to set AWS secret access key."

# Update kubeconfig if cluster name is provided
if [[ -n "${NAME_OF_CLUSTER}" ]]; then
aws eks update-kubeconfig --region $AWS_REGION --name $NAME_OF_CLUSTER || error_exit "Failed to update kubeconfig for cluster: ${NAME_OF_CLUSTER}"
aws eks update-kubeconfig --region "$AWS_REGION" --name "$NAME_OF_CLUSTER" || error_exit "Failed to update kubeconfig for cluster: ${NAME_OF_CLUSTER}"
fi

# If the FLUX_VERSION environment variable is set, install that version of Flux
# Install specified Flux version if provided
if [[ -n "$FLUX_VERSION" ]]; then
echo "Installing Flux version: ${FLUX_VERSION}"
# Download and run the Flux installation script, and exit with an error message if the command fails
curl -s https://fluxcd.io/install.sh | bash || error_exit "Failed to install Flux version: ${FLUX_VERSION}"
fi

Expand Down

0 comments on commit 6eb0457

Please sign in to comment.