Skip to content

Commit

Permalink
base-image-n-script - Updated to leverage ubuntu as based image to ma…
Browse files Browse the repository at this point in the history
…ke it more dynamic and script updated to address the same
  • Loading branch information
ashishjullia committed Nov 24, 2023
1 parent 1591a0a commit ba833b4
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-build-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'prod-*'
env:
REGISTRY: ashishjullia19
IMAGE_NAME: terraform-aws-cli
IMAGE_NAME: docker-dev-env
jobs:
build:
runs-on: ubuntu-latest
Expand Down
41 changes: 30 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
FROM amazon/aws-cli
RUN yum -y update && yum -y updateinfo && yum install -y \
FROM ubuntu:20.04

ENV NVM_DIR /usr/local/nvm

# Install all dependencies, NVM, GitHub CLI, and other tools in a single RUN command
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
git \
jq \
unzip \
curl \
wget \
tar \
openssl \
which \
python3 \
python3-pip \
&& git clone https://github.com/tfutils/tfenv.git ~/.tfenv \
&& curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
&& unzip awscliv2.zip \
&& ./aws/install \
&& curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \
&& install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl \
&& 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 urllib3==1.26.7 \
&& pip3 install print-env \
&& yum clean all
ENV PATH="/root/.tfenv/bin:$PATH"
RUN mkdir /work
&& ./get_helm.sh \
&& git clone https://github.com/tfutils/tfenv.git ~/.tfenv \
&& pip3 install urllib3==1.26.7 print-env \
&& mkdir -p $NVM_DIR \
&& curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
# Install GitHub CLI
&& curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& apt update \
&& apt install gh \
# Clean up
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /awscliv2.zip

# Set environment path for tfenv and NVM
ENV PATH="/root/.tfenv/bin:$NVM_DIR/versions/node/$(nvm version)/bin:$PATH"

# Create and set the working directory
WORKDIR /work

# Copy the script into the container
COPY ./script.sh /
RUN chmod u+x /script.sh
74 changes: 31 additions & 43 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,44 @@
### To build your own image
```bash
git clone https://github.com/ashishjullia/docker-dev-awscli.git && cd ansible/single-liner
```
# Setup

### Note: This is only required if you are not using portunus
#### Create and ".env" file to pass the secrets
```bash
KEY=VALUE
.........so on
```
```bash
docker build -t terraform-aws-cli .
```
```bash
docker image tag terraform-aws-cli ashishjullia19/terraform-aws-cli
```
###### Note: If you are pushing the image to a private dockerhub repository, make sure to run "docker login" first.
```bash
docker image push ashishjullia19/terraform-aws-cli
```
### Edit your "~/.bashrc" and place the following "aliases" at the bottom of the file
Create the following function in your `~/.bashrc` and then either:
- logout and login
- `source ~/.bashrc` (I don't prefer this as sometimes it works sometimes it does not)

```bash
vim ~/.bashrc
```
function dev() {
local docker_cmd="sudo docker run -it --rm -v $PWD:/work -w /work"

```bash
alias dev="docker run -it --rm -v $PWD:/work -w /work --env-file=.env --entrypoint /script.sh ashishjullia19/terraform-aws-cli"
# Check for incorrect usage (more than one argument or invalid format)
if [ "$#" -gt 1 ] || ( [ "$#" -eq 1 ] && ! [[ $1 =~ ^.+/+.+$ ]] ); then
echo "Usage: dev [<project-name>/<stage>]"
return 1
fi

# Append PORTUNUS_TOKEN if it's set and exactly one correctly formatted argument is provided
if [ -n "$PORTUNUS_TOKEN" ] && [ "$#" -eq 1 ]; then
docker_cmd+=" -e PORTUNUS_TOKEN=${PORTUNUS_TOKEN}/$1"
fi

docker_cmd+=" --entrypoint /script.sh ashishjullia19/docker-dev-env"
eval $docker_cmd
}
```

With portunus integration:
## With portunus integration:

For `PORTUNUS_TOKEN` make sure to grab this from portunus ui `portunus.ashishjullia.com` and you'll get the token in format:
`PORTUNUS_TOKEN=<TOKEN>/<PORTUNUS_TEAM>/<PORTUNUS_PROJECT>/<PORTUNUS_STAGE>`. Then `<PORTUNUS_TEAM>` will not be same as you'll see in ui, it will be a random value.
By default, you can set the token on your host system under PORTUNUS_TOKEN until `<TOKEN>/<PORTUNUS_TEAM>`, the other parts of the token can be populated inside the container via `.env` file to keep things more dynamic (as once created the `PORTUNUS_TEAM` value remains the same for a user until and unless they are part of other teams as well) OR you can also pass `<PORTUNUS_PROJECT>` `<PORTUNUS_STAGE>` via docker command itself just do `-e <PORTUNUS_PROJECT>=<PORTUNUS_PROJECT>` `-e <PORTUNUS_STAGE>=<PORTUNUS_PROJECT>`
For `.env`, use this:
```bash
PORTUNUS_PROJECT=<value>
PORTUNUS_PROJECT=<value>
```
```bash
alias dev="docker run -it --rm -v $PWD:/work -w /work --env-file=.env -e PORTUNUS_TOKEN=$PORTUNUS_TOKEN --entrypoint /script.sh ashishjullia19/terraform-aws-cli"
```
- `PORTUNUS_TOKEN=<TOKEN>/<PORTUNUS_TEAM>/<PORTUNUS_PROJECT>/<PORTUNUS_STAGE>`. Then `<PORTUNUS_TEAM>` will not be same as you'll see in ui, it will be a random value.
- by default, you can set the token on your host system under PORTUNUS_TOKEN until `<TOKEN>/<PORTUNUS_TEAM>`.

Notes:
- If you run it with just `dev` on terminal then nothing from `script.sh` will be installed/configured
- the utilities specified in the `script.sh` will only work if portunus integration is used and correct key names are used on portunus side that matches to env variables specified in `script.sh`
- to run with portunus integration `dev <portunus-project>/<portunus-stage>`
- if more you specify something like `dev <portunus-project>/<portunus-stage> argument2` -> it will throw error

```bash
alias dot="source ~/.bashrc"
```
### Action
###### Run the following commands series in whichever directory you want the docker development environment.
###### Run the following command in whichever directory you want the docker development environment.

```bash
dot
```
```bash
dev
```
92 changes: 56 additions & 36 deletions script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,63 @@ function error_exit {
exit "${2:-1}"
}

# Check for dependencies
for cmd in jq tfenv aws curl print-env; do
command -v "$cmd" >/dev/null 2>&1 || error_exit "$cmd is required but not installed."
done

PORTUNUS_TOKEN="${PORTUNUS_TOKEN}/${PORTUNUS_PROJECT}/${PORTUNUS_STAGE}"

# Ensure PORTUNUS_TOKEN is set
[[ -z "${PORTUNUS_TOKEN}" ]] && error_exit "PORTUNUS_TOKEN is not set."

# 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 the 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}"

# Configure AWS with the specified or default credentials
AWS_REGION="${AWS_REGION:-us-west-1}" # Consider adding a default if appropriate
echo "Configuring AWS with region: ${AWS_REGION}"
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 a 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}"
fi
# Only execute the following block if PORTUNUS_TOKEN is set
if [ -n "${PORTUNUS_TOKEN}" ]; then

# 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)"')

# Conditionally install Terraform version if TF_VERSION is set
if [ -n "${TF_VERSION}" ]; then
echo "TF_VERSION is set to ${TF_VERSION}. 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}"
else
echo "TF_VERSION is not set. Skipping Terraform installation."
fi

# Conditionally configure AWS if AWS_REGION, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY are set
if [ -n "${AWS_REGION}" ] && [ -n "${AWS_ACCESS_KEY_ID}" ] && [ -n "${AWS_SECRET_ACCESS_KEY}" ]; then
echo "Configuring AWS with region: ${AWS_REGION}"
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."
else
echo "AWS configuration variables are not fully set. Skipping AWS configuration."
fi

# Update kubeconfig if a 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}"
fi

# Install specified Flux version if provided
if [[ -n "$FLUX_VERSION" ]]; then
echo "Installing Flux version: ${FLUX_VERSION}"
curl -s https://fluxcd.io/install.sh | bash || error_exit "Failed to install Flux version: ${FLUX_VERSION}"
fi

# Conditionally install Node.js version if NODE_VERSION is set
if [ -n "${NODE_VERSION}" ]; then
echo "NODE_VERSION is set to ${NODE_VERSION}. Installing Node.js version: ${NODE_VERSION}"
export NVM_DIR="/usr/local/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
nvm install $NODE_VERSION
else
echo "NODE_VERSION is not set. Skipping Node.js installation."
fi

# Authenticate GitHub CLI if GITHUB_TOKEN is provided
if [ -n "${GH_CLI_TOKEN}" ]; then
echo "Authenticating GitHub CLI..."
gh auth login --with-token <<< $GH_CLI_TOKEN
gh auth setup-git
else
echo "GH_CLI_TOKEN is not set. Skipping GitHub CLI authentication."
fi

# Install specified Flux version if provided
if [[ -n "$FLUX_VERSION" ]]; then
echo "Installing Flux version: ${FLUX_VERSION}"
curl -s https://fluxcd.io/install.sh | bash || error_exit "Failed to install Flux version: ${FLUX_VERSION}"
fi

# Exit to a bash prompt
Expand Down

0 comments on commit ba833b4

Please sign in to comment.