Skip to content

Commit

Permalink
Merge branch 'main' into chaps-env-vars
Browse files Browse the repository at this point in the history
  • Loading branch information
vertism committed Jan 16, 2024
2 parents 5414bff + abb64d2 commit 1fd194c
Show file tree
Hide file tree
Showing 65 changed files with 3,060 additions and 338 deletions.
14 changes: 14 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ bash ../../../scripts/member-local-plan.sh
bash ../../../scripts/member-local-plan.sh -r modernisation-platform-sandbox
```

## Support

As this is a community supported feature, help is offered on a best endeavour basis.

If you do need help, please post in [`#devcontainer`](https://moj.enterprise.slack.com/archives/C06DZ4F04JZ)

## Contribution Guidelines

- Check that an existing feature doesn't cover what you're trying to add

- Where possible reuse the existing practices from other features, utilising the shared library `devcontainer-utils`

- If you are creating a feature, add it to the feature testing matrix in the GitHub Actions workflow and ensure appropiate tests exist

## Maintainers

- [@jacobwoffenden](https://github.com/jacobwoffenden)
Expand Down
10 changes: 9 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
"name": "modernisation-platform-environments",
"image": "mcr.microsoft.com/vscode/devcontainers/base:ubuntu",
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"configureZshAsDefaultShell": true
},
"./features/src/base": {},
"./features/src/aws": {},
"./features/src/terraform": {}
}
},
"overrideFeatureInstallOrder": [
"ghcr.io/devcontainers/features/common-utils",
"./features/src/base"
]
}
3 changes: 2 additions & 1 deletion .devcontainer/features/src/aws/devcontainer-feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"proposals": ["latest"],
"default": "latest"
}
}
},
"installsAfter": ["./features/src/base"]
}
37 changes: 13 additions & 24 deletions .devcontainer/features/src/aws/install-aws-cli.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
#!/usr/bin/env bash

set -euo pipefail
set -e

VERSION=${AWSCLIVERSION:-"latest"}
source /usr/local/bin/devcontainer-utils

get_system_architecture

VERSION="${AWSCLIVERSION:-"latest"}"

if [[ "${VERSION}" == "latest" ]]; then
ARTEFACT="awscli-exe-linux-$( uname -m ).zip"
ARTEFACT="awscli-exe-linux-$(uname -m).zip"
else
ARTEFACT="awscli-exe-linux-$( uname -m )-${VERSION}.zip"
ARTEFACT="awscli-exe-linux-$(uname -m)-${VERSION}.zip"
fi

# Install

apt-get update --yes

apt-get -y install --no-install-recommends \
ca-certificates \
curl \
unzip
curl --fail-with-body --location "https://awscli.amazonaws.com/${ARTEFACT}" \
--output "${ARTEFACT}"

curl https://awscli.amazonaws.com/${ARTEFACT} \
--output ${ARTEFACT}

unzip ${ARTEFACT}
unzip "${ARTEFACT}"

bash ./aws/install

rm --force --recursive aws ${ARTEFACT}

# Configure

echo "complete -C '/usr/local/bin/aws_completer' aws" >> /home/vscode/.bashrc

# Cleanup
rm --recursive --force aws "${ARTEFACT}"

rm --force --recursive /var/lib/apt/lists/*
install --owner=vscode --group=vscode --mode=775 "$(dirname "${0}")"/src/home/vscode/.devcontainer/feature-completion/aws.sh /home/vscode/.devcontainer/feature-completion/aws.sh
41 changes: 17 additions & 24 deletions .devcontainer/features/src/aws/install-aws-sso-cli.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
#!/usr/bin/env bash

set -euo pipefail
set -e

VERSION=${AWSSSOCLIVERSION:-"latest"}
source /usr/local/bin/devcontainer-utils

get_system_architecture

case "$( uname -m )" in
x86_64)
export ARCHITECTURE="amd64" ;;
aarch64 | armv8*)
export ARCHITECTURE="arm64" ;;
*)
echo "(!) Architecture $( uname -m ) unsupported"; exit 1 ;;
esac
GITHUB_REPOSITORY="synfinatic/aws-sso-cli"
VERSION=${AWSSSOCLIVERSION:-"latest"}

if [[ "${VERSION}" == "latest" ]]; then
VERSION=$(curl --silent "https://api.github.com/repos/synfinatic/aws-sso-cli/releases/latest" | jq -r '.tag_name')
VERSION_STRIP_V=$(echo "${VERSION}" | sed 's/v//')
get_github_latest_tag "${GITHUB_REPOSITORY}"
VERSION="${GITHUB_LATEST_TAG}"
VERSION_STRIP_V="${GITHUB_LATEST_TAG_STRIP_V}"
else
VERSION_STRIP_V="${VERSION#v}"
fi

# Install

curl --location https://github.com/synfinatic/aws-sso-cli/releases/download/${VERSION}/aws-sso-${VERSION_STRIP_V}-linux-${ARCHITECTURE} \
--output /usr/local/bin/aws-sso

chmod +x /usr/local/bin/aws-sso

mkdir --parents /home/vscode/.aws-sso
curl --location https://github.com/${GITHUB_REPOSITORY}/releases/download/${VERSION}/aws-sso-${VERSION_STRIP_V}-linux-${ARCHITECTURE} \
--output "aws-sso"

cp $( dirname $0 )/src/home/vscode/.aws-sso/config.yaml /home/vscode/.aws-sso/config.yaml
install --owner=vscode --group=vscode --mode=775 aws-sso /usr/local/bin/aws-sso

chown --recursive vscode:vscode /home/vscode/.aws-sso
install --directory --owner=vscode --group=vscode /home/vscode/.aws-sso

# Configure
install --owner=vscode --group=vscode --mode=775 "$(dirname "${0}")"/src/home/vscode/.aws-sso/config.yaml /home/vscode/.aws-sso/config.yaml

echo "export AWS_SSO_FILE_PASSWORD=\"aws_sso_123456789\"" >> /home/vscode/.bashrc
install --owner=vscode --group=vscode --mode=775 "$(dirname "${0}")"/src/home/vscode/.devcontainer/feature-completion/aws-sso.sh /home/vscode/.devcontainer/feature-completion/aws-sso.sh
10 changes: 8 additions & 2 deletions .devcontainer/features/src/aws/install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/usr/bin/env bash

bash $( dirname $0 )/install-aws-cli.sh
set -e

bash $( dirname $0 )/install-aws-sso-cli.sh
source /usr/local/bin/devcontainer-utils

logger "info" "Installing AWS CLI (version: ${AWSCLIVERSION})"
bash "$(dirname "${0}")"/install-aws-cli.sh

logger "info" "Installing AWS SSO CLI (version: ${AWSSSOCLIVERSION})"
bash "$(dirname "${0}")"/install-aws-sso-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

export AWS_SSO_FILE_PASSWORD="aws_sso_123456789"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

complete -C '/usr/local/bin/aws_completer' aws
12 changes: 12 additions & 0 deletions .devcontainer/features/src/base/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "base",
"version": "1.0.0",
"name": "base",
"description": "Base",
"customizations": {
"vscode": {
"extensions": ["GitHub.vscode-pull-request-github", "GitHub.vscode-github-actions", "ms-vsliveshare.vsliveshare"]
}
},
"installsAfter": ["ghcr.io/devcontainers/features/common-utils"]
}
21 changes: 21 additions & 0 deletions .devcontainer/features/src/base/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -e

source "$(dirname "${0}")"/src/usr/local/bin/devcontainer-utils

install --owner=vscode --group=vscode --mode=775 "$(dirname "${0}")"/src/usr/local/bin/devcontainer-utils /usr/local/bin/devcontainer-utils

install --owner=vscode --group=vscode --mode=755 "$(dirname "${0}")"/src/usr/local/etc/vscode-dev-containers/first-run-notice.txt /usr/local/etc/vscode-dev-containers/first-run-notice.txt

install --owner=vscode --group=vscode --mode=755 "$(dirname "${0}")"/src/home/vscode/.oh-my-zsh/custom/themes/devcontainers.zsh-theme /home/vscode/.oh-my-zsh/custom/themes/devcontainers.zsh-theme

install --directory --owner=vscode --group=vscode /home/vscode/.devcontainer/feature-completion

cat <<EOF >> /home/vscode/.zshrc
# dev container feature completion scripts
for file in "\${HOME}"/.devcontainer/feature-completion/*.sh; do
source "\${file}"
done
EOF
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Oh My Zsh! theme - partly inspired by https://github.com/ohmyzsh/ohmyzsh/blob/master/themes/robbyrussell.zsh-theme
# Source: https://github.com/devcontainers/features/blob/main/src/common-utils/scripts/devcontainers.zsh-theme

__zsh_prompt() {
local prompt_username
if [ ! -z "${GITHUB_USER}" ]; then
prompt_username="@${GITHUB_USER}"
else
prompt_username="%n"
fi
PROMPT="%{$fg[green]%}${prompt_username} %(?:%{$reset_color%}➜ :%{$fg_bold[red]%}➜ )" # User/exit code arrow
PROMPT+='%{$fg_bold[blue]%}%(5~|%-1~/…/%3~|%4~)%{$reset_color%} ' # cwd
PROMPT+='`\
if [ "$(git config --get devcontainers-theme.hide-status 2>/dev/null)" != 1 ] && [ "$(git config --get codespaces-theme.hide-status 2>/dev/null)" != 1 ]; then \
export BRANCH=$(git --no-optional-locks symbolic-ref --short HEAD 2>/dev/null || git --no-optional-locks rev-parse --short HEAD 2>/dev/null); \
if [ "${BRANCH}" != "" ]; then \
echo -n "%{$fg_bold[cyan]%}(%{$fg_bold[red]%}${BRANCH}" \
&& if [ "$(git config --get devcontainers-theme.show-dirty 2>/dev/null)" = 1 ] && \
git --no-optional-locks ls-files --error-unmatch -m --directory --no-empty-directory -o --exclude-standard ":/*" > /dev/null 2>&1; then \
echo -n " %{$fg_bold[yellow]%}✗"; \
fi \
&& echo -n "%{$fg_bold[cyan]%})%{$reset_color%} "; \
fi; \
fi`'

# Terraform
if command -v terraform &> /dev/null; then
PROMPT+='`\
terraformVersion=$(terraform -version | grep Terraform | cut -d " " -f 2 | sed "s/v//"); \
echo -n "[ terraform: %{$fg[blue]%}${terraformVersion}%{$reset_color%} ] " \
`'
fi

# AWS SSO
if command -v aws-sso &> /dev/null; then
PROMPT+='`\
if [[ ${AWS_SSO_PROFILE} == *"development"* || ${AWS_SSO_PROFILE} == *"test"* ]]; then \
echo -n "[ aws: %{$fg[green]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \
elif [[ ${AWS_SSO_PROFILE} == *"preproduction"* ]]; then \
echo -n "[ aws: %{$fg[yellow]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \
elif [[ ${AWS_SSO_PROFILE} == *"production"* ]]; then \
echo -n "[ aws: %{$fg[red]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \
elif [[ ! -z ${AWS_SSO_PROFILE} ]]; then \
echo -n "[ aws: %{$fg[blue]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \
fi`'
fi

PROMPT+='%{$fg[white]%}$ %{$reset_color%}'
unset -f __zsh_prompt
}
__zsh_prompt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash

##################################################
# Environment
##################################################

export DEBIAN_FRONTEND="noninteractive"

##################################################
# Function
##################################################

logger() {
local type="${1}"
local message="${2}"
timestamp=$(date --rfc-3339=seconds)
local timestamp

case "${type}" in
err | error)
echo "${timestamp} [ERROR] ${message}"
;;
info | information)
echo "${timestamp} [INFO] ${message}"
;;
warn | warning)
echo "${timestamp} [WARN] ${message}"
;;
esac
}

get_system_architecture() {
systemArchitecture="$(uname -m)"
export systemArchitecture

case ${systemArchitecture} in
x86_64)
logger "info" "Architecture is x86_64"
export ARCHITECTURE="amd64"
;;
aarch64 | armv8*)
logger "info" "Architecture is aarch64 or armv8"
export ARCHITECTURE="arm64"
;;
*)
logger "error" "Architecture ${systemArchitecture} is not supported"
exit 1
;;
esac
}

get_github_latest_tag() {
local repository="${1}"

repositoryLatestTag="$(curl --silent https://api.github.com/repos/"${repository}"/releases/latest | jq -r '.tag_name')"
export repositoryLatestTag

repositoryLatestTagStripV=${repositoryLatestTag//v/}

logger "info" "GitHub latest tag for ${repository} is ${repositoryLatestTag}"
export GITHUB_LATEST_TAG="${repositoryLatestTag}"
export GITHUB_LATEST_TAG_STRIP_V="${repositoryLatestTagStripV}"
}

apt_install() {
local packages="${1}"

apt-get update --yes

apt-get install --yes --no-install-recommends "${packages}"

apt-get clean

rm --force --recursive /var/lib/apt/lists/*
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
👋 Welcome! You are using the Modernisation Platform Environments dev container image.

🆘 If you need help or assistance, please post in #devcontainer (https://moj.enterprise.slack.com/archives/C06DZ4F04JZ)

🔍 To explore VS Code to its fullest, search using the Command Palette (Cmd/Ctrl + Shift + P or F1)
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"editor.defaultFormatter": "hashicorp.terraform"
}
}
}
},
"installsAfter": ["./features/src/base"]
}
Loading

0 comments on commit 1fd194c

Please sign in to comment.