From 6eb045707b067d43c246846f710c05061064b636 Mon Sep 17 00:00:00 2001 From: ashishjullia19 Date: Mon, 18 Sep 2023 09:45:29 -0400 Subject: [PATCH] updated script and dockerfile --- Dockerfile | 5 +++-- script.sh | 50 ++++++++++++++++++++++++-------------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Dockerfile b/Dockerfile index c8ff283..2308391 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ @@ -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 diff --git a/script.sh b/script.sh index 63a01aa..06d8eac 100644 --- a/script.sh +++ b/script.sh @@ -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