Skip to content

Commit be54d14

Browse files
committed
Initial commit
0 parents  commit be54d14

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

Diff for: .gitpod.Dockerfile

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# syntax=docker/dockerfile:1.4
2+
FROM gitpod/workspace-full
3+
4+
USER root
5+
6+
COPY --from=amazon/aws-cli:latest /usr/local/aws-cli/ /usr/local/aws-cli/
7+
8+
# make sure we have these handy utilities
9+
RUN apt install -y inetutils-ping traceroute curl wget
10+
11+
RUN chmod +x /usr/local/aws-cli/v2/current/bin/aws && ln -s /usr/local/aws-cli/v2/current/bin/aws /usr/local/bin/aws
12+
13+
RUN chmod -R +x /usr/local/bin
14+
15+
USER gitpod
16+
17+
# below is an inline bash script to populate this container with a helper script to run at login
18+
# this is really a terrible idea and why aws_init.sh is used instead
19+
RUN <<'EOF' bash
20+
set -e
21+
22+
# install ECR credential helper:
23+
ECR_LATEST=$(curl -s https://api.github.com/repos/awslabs/amazon-ecr-credential-helper/releases/latest | jq -r ".tag_name")
24+
curl -o docker-credential-ecr-login -fSsL "https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/${ECR_LATEST##*v}/linux-amd64/docker-credential-ecr-login"
25+
sudo mv docker-credential-ecr-login /usr/local/bin/docker-credential-ecr-login
26+
sudo chmod +x /usr/local/bin/docker-credential-ecr-login
27+
28+
mkdir -p /home/gitpod/.aws/
29+
30+
cat <<'AWSINIT' > /home/gitpod/.aws/init.sh
31+
#!/bin/bash
32+
set -e
33+
34+
# create the config for SSO login
35+
# This assumes the below variables have been configured for this repo in gitpod
36+
# https://www.gitpod.io/docs/environment-variables#using-the-account-settings
37+
cat <<- AWSFILE > /home/gitpod/.aws/config
38+
[default]
39+
sso_start_url = ${AWS_SSO_URL}
40+
sso_region = ${AWS_SSO_REGION}
41+
sso_account_id = ${AWS_ACCOUNT_ID}
42+
sso_role_name = ${AWS_ROLE_NAME}
43+
region = ${AWS_REGION}
44+
AWSFILE
45+
46+
# update docker config to use ecr-login
47+
48+
# make sure we have ecr-login
49+
if [ ! -f /usr/local/bin/docker-credential-ecr-login ]; then
50+
ECR_LATEST=$(curl -s https://api.github.com/repos/awslabs/amazon-ecr-credential-helper/releases/latest | jq -r ".tag_name")
51+
curl -o docker-credential-ecr-login -fSsL "https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/${ECR_LATEST##*v}/linux-arm64/docker-credential-ecr-login"
52+
sudo mv docker-credential-ecr-login /usr/local/bin/docker-credential-ecr-login
53+
sudo chmod +x /usr/local/bin/docker-credential-ecr-login
54+
fi
55+
56+
# if we don't have a .docker/config.json, create:
57+
58+
if [ ! -d /home/gitpod/.docker ]; then
59+
mkdir -p /home/gitpod/.docker && echo '{}' > /home/gitpod/.docker/config.json
60+
elif [ ! -f /home/gitpod/.docker/config.json ]; then
61+
echo '{}' > /home/gitpod/.docker/config.json
62+
fi
63+
64+
jq '.credHelpers["public.ecr.aws"]="ecr-login"' /home/gitpod/.docker/config.json > /home/gitpod/.docker/config_tmp.json
65+
jq ".credHelpers[\"${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com\"]=\"ecr-login\"" /home/gitpod/.docker/config_tmp.json > /home/gitpod/.docker/config.json
66+
67+
AWSINIT
68+
69+
chmod +x /home/gitpod/.aws/init.sh
70+
71+
EOF
72+

Diff for: .gitpod.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# image:
2+
# file: .gitpod.Dockerfile
3+
4+
# multi-repo
5+
# additionalRepositories:
6+
# - url: https://github.com/<redacted>/something
7+
# checkoutLocation: something
8+
9+
# List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
10+
tasks:
11+
- before: |
12+
curl -fsSL https://openpgpkey.sneezingdog.com/.well-known/openpgpkey/sneezingdog.com/hu/jexrgxceciag7yppi57igp7m3shxc98r | gpg --import
13+
14+
- command: |
15+
# this will install all AWS tools and then open a browser window to authenticate your AWS session
16+
bash $GITPOD_REPO_ROOT/aws_init.sh
17+
# put things you want to do with AWS after this line
18+
19+
# List the ports to expose. Learn more https://www.gitpod.io/docs/config-ports/
20+
# ports:
21+
# - port: 3000
22+
# onOpen: open-preview

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Gitpod
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Gitpod with AWS
2+
3+
If your software project is comprised of multiple source control repositories it is possible to configure Gitpod to clone these additional repositories through the configuration keys of `additionalRepositories` and `mainConfiguration` in the [.gitpod.yml](https://www.gitpod.io/docs/references/gitpod-yml) file which removes the need to run multiple workspaces, and makes it easier to configure services which need to be aware of each other.
4+
5+
Learn more about cloning additional repositories and delegation at https://www.gitpod.io/docs/multi-repo-workspaces
6+
7+
## Demo
8+
9+
This repository uses `mainConfiguration` to delegate the configuration of Gitpod to https://github.com/gitpod-io/demo-multi-repo-frontend and makes it possible to open the same workspace from any issue, branch or other context URL.
10+
11+
<a href="https://gitpod.io/#https://github.com/gitpod-io/demo-gitpod-with-aws"><img src="https://gitpod-staging.com/button/open-in-gitpod.svg"/></a>
12+
13+
```bash
14+
$ cd /workspaces
15+
$ ls -ltr
16+
drwxr-xr-x 3 gitpod gitpod 69 Jun 22 02:37 demo-multi-repo-frontend
17+
drwxr-xr-x 3 gitpod gitpod 69 Jun 22 02:37 backend
18+
```

Diff for: aws_init.sh

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/bin/bash
2+
set -e
3+
4+
#update AWS
5+
OLD_DIR="$PWD"
6+
TMP_DIR="$(mktemp -d)"
7+
echo "Updating AWS"
8+
cd "${TMP_DIR}" || exit 1
9+
10+
curl -fSsl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
11+
unzip -qq awscliv2.zip
12+
sudo ./aws/install --update
13+
14+
cd "${OLD_DIR}" || exit 1
15+
rm -rf "${TMP_DIR}"
16+
17+
# make sure we have ecr-login
18+
if [ ! -f /usr/local/bin/docker-credential-ecr-login ]; then
19+
echo "Installing ecr-login helper"
20+
OLD_DIR="$PWD"
21+
TMP_DIR="$(mktemp -d)"
22+
cd "${TMP_DIR}" || exit 1
23+
ECR_LATEST=$(curl -s https://api.github.com/repos/awslabs/amazon-ecr-credential-helper/releases/latest | jq -r ".tag_name")
24+
curl -o docker-credential-ecr-login -fSsL "https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/${ECR_LATEST##*v}/linux-amd64/docker-credential-ecr-login"
25+
curl -o docker-credential-ecr-login.sha256 -fSsL "https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/${ECR_LATEST##*v}/linux-amd64/docker-credential-ecr-login.sha256"
26+
sha256sum -c docker-credential-ecr-login.sha256
27+
sudo mv docker-credential-ecr-login /usr/local/bin/docker-credential-ecr-login
28+
sudo chmod +x /usr/local/bin/docker-credential-ecr-login
29+
cd "${OLD_DIR}" || exit 1
30+
rm -rf "${TMP_DIR}"
31+
fi
32+
33+
# This should be moved to the workspace image, but we can shave that yak later.
34+
if ! command -v session-manager-plugin; then
35+
echo "Installing AWS session manager plugin"
36+
37+
TMP_DIR="$(mktemp -d)"
38+
cd "$TMP_DIR" || exit 1
39+
40+
curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"
41+
sudo dpkg -i "session-manager-plugin.deb"
42+
43+
cd "$OLD_DIR"
44+
rm -rf "$TMP_DIR"
45+
fi
46+
47+
48+
AWS_VARS=(AWS_SSO_URL AWS_SSO_REGION AWS_ACCOUNT_ID AWS_ROLE_NAME AWS_REGION)
49+
50+
for AWS_VAR in "${AWS_VARS[@]}"; do
51+
echo "$AWS_VAR is ${!AWS_VAR}"
52+
if [[ -z "${!AWS_VAR}" ]]; then
53+
echo "Error: AWS variable \"$AWS_VAR\" is unset"
54+
AWS_VAR_UNSET=true
55+
fi
56+
done
57+
58+
if ! [[ -z "$AWS_VAR_UNSET" ]]; then
59+
SCRIPT=$(realpath "$0")
60+
echo "AWS Variables are not set, skipping autoconfig of files"
61+
echo "Re-run ${SCRIPT} when AWS_ variables are set"
62+
exit 1
63+
fi
64+
65+
66+
# create the config for SSO login
67+
# This assumes the below variables have been configured for this repo in gitpod
68+
# https://www.gitpod.io/docs/environment-variables#using-the-account-settings
69+
echo "Forcing AWS config to just use SSO credentials"
70+
[[ -d /home/gitpod/.aws ]] || mkdir /home/gitpod/.aws
71+
cat <<- AWSFILE > /home/gitpod/.aws/config
72+
[default]
73+
sso_start_url = ${AWS_SSO_URL}
74+
sso_region = ${AWS_SSO_REGION}
75+
sso_account_id = ${AWS_ACCOUNT_ID}
76+
sso_role_name = ${AWS_ROLE_NAME}
77+
region = ${AWS_REGION}
78+
AWSFILE
79+
80+
# update docker config to use ecr-login
81+
# if we don't have a .docker/config.json, create:
82+
83+
if [ ! -d /home/gitpod/.docker ]; then
84+
mkdir -p /home/gitpod/.docker && echo '{}' > /home/gitpod/.docker/config.json
85+
elif [ ! -f /home/gitpod/.docker/config.json ]; then
86+
echo '{}' > /home/gitpod/.docker/config.json
87+
fi
88+
89+
echo "Ensuring Docker Config uses ecr-login for ECR repositories"
90+
91+
cp /home/gitpod/.docker/config.json /home/gitpod/.docker/config_bak.json
92+
jq '.credHelpers["public.ecr.aws"]="ecr-login"' /home/gitpod/.docker/config.json > /home/gitpod/.docker/config_tmp.json
93+
jq ".credHelpers[\"${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com\"]=\"ecr-login\"" /home/gitpod/.docker/config_tmp.json > /home/gitpod/.docker/config.json
94+
rm /home/gitpod/.docker/config_tmp.json
95+
96+
echo "Start an AWS SSO login session"
97+
98+
BROWSER="/ide/bin/helpers/browser.sh" aws sso login

0 commit comments

Comments
 (0)