Skip to content

Commit

Permalink
update: devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenion1987 committed Jun 26, 2024
1 parent 543257d commit b30caf1
Show file tree
Hide file tree
Showing 5 changed files with 493 additions and 167 deletions.
55 changes: 10 additions & 45 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,53 +1,18 @@
FROM python:slim-bookworm

# Set Variables
ARG LINUX_USER=vscode
ARG TF_VERSION=1.6.6
ARG TFDOCS_VERSION=latest
ARG TFLINT_VERSION=latest
ARG TFTRIVY_VERSION=latest
ARG TERRAGRUNT_VERSION=latest
ARG LINUX_USER
ARG TERRAFORM_VERSION
ARG TERRAFORMER_VERSION
ARG TFDOCS_VERSION
ARG TFLINT_VERSION
ARG TFTRIVY_VERSION
ARG TERRAGRUNT_VERSION

# Copy setup script
COPY ./scripts/setup-docker-container.sh /tmp
COPY requirements.txt /tmp

# Install all applications
RUN /tmp/setup-docker-container.sh install all ${TF_VERSION} ${TFDOCS_VERSION} ${TFLINT_VERSION} ${TFTRIVY_VERSION} ${TERRAGRUNT_VERSION}

# # Install OS requirements
# RUN /tmp/setup-docker-container.sh install os-requirements

# # Install pip
# RUN /tmp/setup-docker-container.sh install pip

# # Install pip requirements
# RUN /tmp/setup-docker-container.sh install pip-requirements

# # Install Terraform and Terraform tools
# RUN /tmp/setup-docker-container.sh install terraform ${TF_VERSION} \
# && /tmp/setup-docker-container.sh install terraform-docs ${TFDOCS_VERSION} \
# && /tmp/setup-docker-container.sh install terragrunt ${TERRAGRUNT_VERSION} \
# && /tmp/setup-docker-container.sh install tflint ${TFLINT_VERSION} \
# && /tmp/setup-docker-container.sh install trivy ${TFTRIVY_VERSION}


# # Install Pre-Commit
# RUN /tmp/setup-docker-container.sh install pre-commit

# # Setup python-argcomplete
# RUN /tmp/setup-docker-container.sh setup python-argcomplete

# # Setup default user
# RUN /tmp/setup-docker-container.sh setup user ${LINUX_USER}

# # Setup default user id_rsa ssh keypair
# RUN /tmp/setup-docker-container.sh setup ssh-key ${LINUX_USER}

# # Set default user
# USER ${LINUX_USER}
# WORKDIR /home/${LINUX_USER}

# # Setup oh-my-bash
# RUN /tmp/setup-docker-container.sh setup oh-my-bash

# HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD [ "pgrep bash" ]
RUN chmod +x /tmp/setup-docker-container.sh && \
/tmp/setup-docker-container.sh install all terraform ${TERRAFORM_VERSION} ${TERRAFORMER_VERSION} ${TFDOCS_VERSION} ${TFLINT_VERSION} ${TFTRIVY_VERSION} ${TERRAGRUNT_VERSION}
162 changes: 91 additions & 71 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,78 +1,98 @@
{
"name": "terraform-playground",
"build": {
"dockerfile": "./Dockerfile",
"args": {
"-t": "local/terraform-playground"
},
"context": "."
},
"runArgs": [
"--name=terraform-playground",
"--hostname=terraform"
],
"initializeCommand": "bash ./.devcontainer/scripts/initialize-command.sh",
"onCreateCommand": "bash ./.devcontainer/scripts/on-create-command.sh",
"postCreateCommand": "bash ./.devcontainer/scripts/post-create-command.sh",
"remoteUser": "vscode",
// "remoteEnv": {
// "PATH": "${containerEnv:PATH}:/home/vscode/.local/bin"
"name": "terraform",
// "image": "git.dogado.net:5000/mcs/eaa/docker-image-terraform:latest",
"build": {
// "args": {
// "LINUX_USER": "",
// "TERRAFORM_VERSION": "",
// "TERRAFORMER_VERSION": "",
// "TFDOCS_VERSION": "",
// "TFLINT_VERSION": "",
// "TFTRIVY_VERSION": "",
// "TERRAGRUNT_VERSION": ""
// },
"features": {
// https://github.com/devcontainers/features/tree/main/src/common-utils
"ghcr.io/devcontainers/features/common-utils:2": {
// "upgradePackages": "true",
"installZsh": "false",
"configureZshAsDefaultShell": "false",
// "userUid": "1000",
// "userGid": "1000",
"username": "vscode"
"dockerfile": "Dockerfile"
},
"runArgs": [
"--hostname=terraform"
],
"initializeCommand": "bash ./.devcontainer/scripts/initialize-command.sh",
"onCreateCommand": "bash ./.devcontainer/scripts/on-create-command.sh",
"postCreateCommand": "bash ./.devcontainer/scripts/post-create-command.sh",
"containerEnv": {
"TZ": "Europe/Berlin"
},
"remoteUser": "vscode",
"remoteEnv": {
"PATH": "${containerEnv:PATH}:/home/vscode/.local/bin:/workspaces/terraform-playground/.devcontainer/scripts"
},
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": "false",
"configureZshAsDefaultShell": "false",
"username": "vscode",
// "userUid": "1000",
// "userGid": "1000",
"upgradePackages": "false"
}
},
"customizations": {
"vscode": {
"settings": {
"telemetry.telemetryLevel": "off",
"files.eol": "\n",
"files.insertFinalNewline": true,
"files.autoSave": "off",
"editor.formatOnSave": true,
"editor.tabSize": 2,
"git.autorefresh": true,
"git.autofetch": true,
"git.autofetchPeriod": 90,
"git.pullTags": false,
"git.confirmSync": false,
"terminal.integrated.scrollback": 10000,
"[markdown]": {
"editor.defaultFormatter": "yzhang.markdown-all-in-one",
"editor.tabSize": 3
},
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform"
},
// https://github.com/devcontainers/features/tree/main/src/docker-outside-of-docker
"ghcr.io/devcontainers/features/docker-outside-of-docker": {
// "dockerDashComposeVersion": "v2",
"installDockerBuildx": false
"[terraform-vars]": {
"editor.defaultFormatter": "hashicorp.terraform"
}
},
"extensions": [
"sleistner.vscode-fileutils",
"yzhang.markdown-all-in-one",
"HashiCorp.terraform",
"hashicorp.hcl",
"mhutchie.git-graph"
]
}
},
"mounts": [
// OPTIONAL: Mount your local docker socket into the devcontainer for using proivider 'docker'
// {
// "source": "/var/run/docker.sock",
// "target": "/var/run/docker.sock",
// "type": "bind"
// },
// OPTIONAL: Mount your local workspace directory into the devcontainer
{
"source": "${localEnv:HOME}${localEnv:USERPROFILE}/.aws",
"target": "/home/vscode/.aws",
"type": "bind"
},
"customizations": {
"vscode": {
"settings": {
"telemetry.telemetryLevel": "off",
"files.eol": "\n",
"files.insertFinalNewline": true,
"files.autoSave": "off",
"editor.formatOnSave": true,
"git.autorefresh": true,
"git.autofetch": true,
"git.autofetchPeriod": 90,
"git.pullTags": false,
"git.confirmSync": false
},
"extensions": [
"sleistner.vscode-fileutils",
"esbenp.prettier-vscode",
"yzhang.markdown-all-in-one",
"HashiCorp.terraform"
]
}
{
"source": "${localEnv:HOME}${localEnv:USERPROFILE}/.config/openstack",
"target": "/home/vscode/.config/openstack",
"type": "bind"
},
"mounts": [
// OPTIONAL: Mount your local docker socket into the devcontainer for using proivider 'docker'
// {
// "source": "/var/run/docker.sock",
// "target": "/var/run/docker.sock",
// "type": "bind"
// },
// OPTIONAL: Mount your local workspace directory into the devcontainer
// {
// "source": "${localEnv:HOME}${localEnv:USERPROFILE}/workspace",
// "target": "/home/vscode/workspace",
// "type": "bind"
// },
{
"source": "devcontainer-history",
"target": "/home/vscode/.history_export",
"type": "volume"
}
]
{
"source": "devcontainer-history",
"target": "/home/vscode/.history_export",
"type": "volume"
}
]
}
1 change: 1 addition & 0 deletions .devcontainer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jinja2
90 changes: 83 additions & 7 deletions .devcontainer/scripts/post-create-command.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/usr/bin/env bash

# OPTIONAL: Setup oh-my-bash
# if .devcontainer/scripts/setup-docker-container.sh setup oh-my-bash; then
# .devcontainer/scripts/on-create-command.sh
# fi
# Add newline to PS1
sed -r -i 's/}\\\$ "$/}\\n\\$ "/' ~/".${SHELL##*/}rc"

# Install terraform autocomplete
terraform -install-autocomplete
terragrunt --install-autocomplete

# Install terraform-docs autocomplete
if ! grep -q 'terraform-docs completion' ~/".${SHELL##*/}rc"; then
Expand All @@ -20,10 +19,87 @@ if ! grep -q 'trivy completion' ~/".${SHELL##*/}rc"; then
echo -e 'source <(trivy completion "${SHELL##*/}")' >>~/".${SHELL##*/}rc"
fi

# Re-source profile
# shellcheck source=/dev/null
source ~/".${SHELL##*/}rc"
if ! git status &>/dev/null; then
git config --global --add safe.directory "${PWD}"
fi
pre-commit install

# # Create silent ssh config file
# [[ -d ~/.ssh ]] || mkdir ~/.ssh
# chmod 700 ~/.ssh
# cat << _EOF > ~/.ssh/config
# StrictHostKeyChecking no
# UserKnownHostsFile /dev/null
# LogLevel ERROR
# _EOF

# Add functions to source aws and openstack credentials
if [[ ! $(grep -qo source-aws-credentials ~/".${SHELL##*/}rc") == "source-aws-credentials" ]]; then
cat << _EOF >> ~/".${SHELL##*/}rc"
function source-aws-credentials() {
export AWS_DEFAULT_REGION=\$(awk -F' = ' '/region/ {print \$NF}' ~/.aws/config)
export AWS_ENDPOINT_URL=\$(awk -F' = ' '/endpoint/ {print \$NF}' ~/.aws/config)
export AWS_ACCESS_KEY_ID=\$(awk -F' = ' '/aws_access_key_id/ {print \$NF}' ~/.aws/credentials)
export AWS_SECRET_ACCESS_KEY=\$(awk -F' = ' '/aws_secret_access_key/ {print \$NF}' ~/.aws/credentials)
grep -E '^AWS' < <(env | sort)
}
_EOF
fi
if [[ ! $(grep -qo source-openstack-credentials ~/".${SHELL##*/}rc") == "source-openstack-credentials" ]]; then
cat << _EOF >> ~/".${SHELL##*/}rc"
function source-openstack-credentials() {
. ~/.config/openstack/groupone-mcs-openstack.sh \\
&& grep "OS_" < <(env | sort)
}
_EOF
fi
if ! grep -qo 'function sc' ~/".${SHELL##*/}rc"; then
cat << _EOF >> ~/".${SHELL##*/}rc"
# Source AWS and OS credentials and print them safely
function sc() {
local o
o=\$(source-aws-credentials)
o+=\$'\n'
o+=\$(source-openstack-credentials)
while read -r v; do
export \$v
done <<<"\${o}"
sed -E \\
-e 's/^(.*ACCESS_KEY.*)=(.*)$/\1=***/' \\
-e 's/^(OS_PASSWORD=)(.*)$/\1***/' <<<"\${o}" | \\
column -s '=' -t
}
_EOF
fi
if ! grep -qo 'function cc' ~/".${SHELL##*/}rc"; then
cat << _EOF >> ~/".${SHELL##*/}rc"
# Check environment for AWS and OS credentials and print them safely
function cc() {
local o
o=\$(grep -E -e "^AWS_" -e "^OS_"< <(env | sort))
sed -E \\
-e 's/^(.*ACCESS_KEY.*)=(.*)$/\1=***/' \\
-e 's/^(OS_PASSWORD=)(.*)$/\1***/' <<<"\${o}" | \\
column -s '=' -t
}
_EOF
fi
if ! grep -qo 'eval sc' ~/".${SHELL##*/}rc"; then
cat << _EOF >> ~/".${SHELL##*/}rc"
eval sc
_EOF
fi

if command -v openstack &>/dev/null; then
if ! grep -qo 'openstack complete' ~/".${SHELL##*/}rc"; then
cat << _EOF >> ~/".${SHELL##*/}rc"
source <(openstack complete)
_EOF
fi
fi
Loading

0 comments on commit b30caf1

Please sign in to comment.