Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit c858742

Browse files
authored
AWS CLI feature (#1326)
1 parent fb3be42 commit c858742

File tree

8 files changed

+303
-20
lines changed

8 files changed

+303
-20
lines changed

script-library/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Some scripts have special installation instructions (like `desktop-lite-debian.s
1414

1515
| Document | Script | Maintainers |
1616
|----------|--------|------------|
17+
| [AWS CLI Install Script](docs/awscli.md) | `awscli-debian.sh` | VS Code and GitHub Codespaces teams |
1718
| [Azure CLI Install Script](docs/azcli.md) | `azcli-debian.sh` | VS Code and GitHub Codespaces teams |
1819
| [Common Script](docs/common.md) | `common-debian.sh`<br />`common-alpine.sh`<br />`common-redhat.sh` (Community) | VS Code and GitHub Codespaces teams |
1920
| [Desktop (Lightweight) Install Script](docs/desktop-lite.md) | `desktop-lite-debian.sh` | VS Code and GitHub Codespaces teams|
@@ -97,7 +98,7 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
9798
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*
9899
```
99100

100-
As before, the last line is technically optional, but minimizes the size of the layer by removing temporary contents.
101+
As before, the last line is technically optional, but minimizes the size of the layer by removing temporary contents.
101102

102103
You can also use `wget`:
103104

@@ -153,4 +154,3 @@ See [CONTRIBUTING.md](../CONTRIBUTING.md) for details on contributing definition
153154
Copyright (c) Microsoft Corporation. All rights reserved.
154155

155156
Licensed under the MIT License. See [LICENSE](https://github.com/microsoft/vscode-dev-containers/blob/main/LICENSE)
156-

script-library/awscli-debian.sh

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
#
7+
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/awscli.md
8+
# Maintainer: The VS Code and Codespaces Teams
9+
#
10+
# Syntax: ./awscli-debian.sh [AWS CLI version]
11+
12+
set -e
13+
14+
AWSCLI_VERSION=${1:-"latest"}
15+
AWSCLI_GPG_KEY=FB5DB77FD5C118B80511ADA8A6310ACC4672475C
16+
AWSCLI_GPG_KEY_MATERIAL="-----BEGIN PGP PUBLIC KEY BLOCK-----
17+
mQINBF2Cr7UBEADJZHcgusOJl7ENSyumXh85z0TRV0xJorM2B/JL0kHOyigQluUG
18+
ZMLhENaG0bYatdrKP+3H91lvK050pXwnO/R7fB/FSTouki4ciIx5OuLlnJZIxSzx
19+
PqGl0mkxImLNbGWoi6Lto0LYxqHN2iQtzlwTVmq9733zd3XfcXrZ3+LblHAgEt5G
20+
TfNxEKJ8soPLyWmwDH6HWCnjZ/aIQRBTIQ05uVeEoYxSh6wOai7ss/KveoSNBbYz
21+
gbdzoqI2Y8cgH2nbfgp3DSasaLZEdCSsIsK1u05CinE7k2qZ7KgKAUIcT/cR/grk
22+
C6VwsnDU0OUCideXcQ8WeHutqvgZH1JgKDbznoIzeQHJD238GEu+eKhRHcz8/jeG
23+
94zkcgJOz3KbZGYMiTh277Fvj9zzvZsbMBCedV1BTg3TqgvdX4bdkhf5cH+7NtWO
24+
lrFj6UwAsGukBTAOxC0l/dnSmZhJ7Z1KmEWilro/gOrjtOxqRQutlIqG22TaqoPG
25+
fYVN+en3Zwbt97kcgZDwqbuykNt64oZWc4XKCa3mprEGC3IbJTBFqglXmZ7l9ywG
26+
EEUJYOlb2XrSuPWml39beWdKM8kzr1OjnlOm6+lpTRCBfo0wa9F8YZRhHPAkwKkX
27+
XDeOGpWRj4ohOx0d2GWkyV5xyN14p2tQOCdOODmz80yUTgRpPVQUtOEhXQARAQAB
28+
tCFBV1MgQ0xJIFRlYW0gPGF3cy1jbGlAYW1hem9uLmNvbT6JAlQEEwEIAD4WIQT7
29+
Xbd/1cEYuAURraimMQrMRnJHXAUCXYKvtQIbAwUJB4TOAAULCQgHAgYVCgkICwIE
30+
FgIDAQIeAQIXgAAKCRCmMQrMRnJHXJIXEAChLUIkg80uPUkGjE3jejvQSA1aWuAM
31+
yzy6fdpdlRUz6M6nmsUhOExjVIvibEJpzK5mhuSZ4lb0vJ2ZUPgCv4zs2nBd7BGJ
32+
MxKiWgBReGvTdqZ0SzyYH4PYCJSE732x/Fw9hfnh1dMTXNcrQXzwOmmFNNegG0Ox
33+
au+VnpcR5Kz3smiTrIwZbRudo1ijhCYPQ7t5CMp9kjC6bObvy1hSIg2xNbMAN/Do
34+
ikebAl36uA6Y/Uczjj3GxZW4ZWeFirMidKbtqvUz2y0UFszobjiBSqZZHCreC34B
35+
hw9bFNpuWC/0SrXgohdsc6vK50pDGdV5kM2qo9tMQ/izsAwTh/d/GzZv8H4lV9eO
36+
tEis+EpR497PaxKKh9tJf0N6Q1YLRHof5xePZtOIlS3gfvsH5hXA3HJ9yIxb8T0H
37+
QYmVr3aIUes20i6meI3fuV36VFupwfrTKaL7VXnsrK2fq5cRvyJLNzXucg0WAjPF
38+
RrAGLzY7nP1xeg1a0aeP+pdsqjqlPJom8OCWc1+6DWbg0jsC74WoesAqgBItODMB
39+
rsal1y/q+bPzpsnWjzHV8+1/EtZmSc8ZUGSJOPkfC7hObnfkl18h+1QtKTjZme4d
40+
H17gsBJr+opwJw/Zio2LMjQBOqlm3K1A4zFTh7wBC7He6KPQea1p2XAMgtvATtNe
41+
YLZATHZKTJyiqA==
42+
=vYOk
43+
-----END PGP PUBLIC KEY BLOCK-----"
44+
45+
if [ "$(id -u)" -ne 0 ]; then
46+
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
47+
exit 1
48+
fi
49+
50+
# Get central common setting
51+
get_common_setting() {
52+
if [ "${common_settings_file_loaded}" != "true" ]; then
53+
curl -sfL "https://aka.ms/vscode-dev-containers/script-library/settings.env" 2>/dev/null -o /tmp/vsdc-settings.env || echo "Could not download settings file. Skipping."
54+
common_settings_file_loaded=true
55+
fi
56+
if [ -f "/tmp/vsdc-settings.env" ]; then
57+
local multi_line=""
58+
if [ "$2" = "true" ]; then multi_line="-z"; fi
59+
local result="$(grep ${multi_line} -oP "$1=\"?\K[^\"]+" /tmp/vsdc-settings.env | tr -d '\0')"
60+
if [ ! -z "${result}" ]; then declare -g $1="${result}"; fi
61+
fi
62+
echo "$1=${!1}"
63+
}
64+
65+
# Function to run apt-get if needed
66+
apt_get_update_if_needed()
67+
{
68+
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
69+
echo "Running apt-get update..."
70+
apt-get update
71+
else
72+
echo "Skipping apt-get update."
73+
fi
74+
}
75+
76+
# Checks if packages are installed and installs them if not
77+
check_packages() {
78+
if ! dpkg -s "$@" > /dev/null 2>&1; then
79+
apt_get_update_if_needed
80+
apt-get -y install --no-install-recommends "$@"
81+
fi
82+
}
83+
84+
export DEBIAN_FRONTEND=noninteractive
85+
86+
check_packages curl ca-certificates gnupg2 dirmngr
87+
88+
verify_aws_cli_gpg_signature() {
89+
local filePath=$1
90+
local sigFilePath=$2
91+
92+
get_common_setting AWSCLI_GPG_KEY
93+
get_common_setting AWSCLI_GPG_KEY_MATERIAL
94+
local awsCliPublicKeyFile=aws-cli-public-key.pem
95+
echo "${AWSCLI_GPG_KEY_MATERIAL}" > "${awsCliPublicKeyFile}"
96+
gpg --quiet --import "${awsCliPublicKeyFile}"
97+
98+
gpg --batch --quiet --verify "${sigFilePath}" "${filePath}"
99+
local status=$?
100+
101+
gpg --batch --quiet --delete-keys "${AWSCLI_GPG_KEY}"
102+
rm "${awsCliPublicKeyFile}"
103+
104+
return ${status}
105+
}
106+
107+
install() {
108+
local scriptZipFile=awscli.zip
109+
local scriptSigFile=awscli.sig
110+
111+
# See Linux install docs at https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
112+
if [ "${AWSCLI_VERSION}" != "latest" ]; then
113+
local versionStr=-${AWSCLI_VERSION}
114+
fi
115+
architecture=$(dpkg --print-architecture)
116+
case "${architecture}" in
117+
amd64) architectureStr=x86_64 ;;
118+
arm64) architectureStr=aarch64 ;;
119+
*)
120+
echo "AWS CLI does not support machine architecture '$architecture'. Please use an x86-64 or ARM64 machine."
121+
exit 1
122+
esac
123+
local scriptUrl=https://awscli.amazonaws.com/awscli-exe-linux-${architectureStr}${versionStr}.zip
124+
curl "${scriptUrl}" -o "${scriptZipFile}"
125+
curl "${scriptUrl}.sig" -o "${scriptSigFile}"
126+
127+
verify_aws_cli_gpg_signature "$scriptZipFile" "$scriptSigFile"
128+
if (( $? > 0 )); then
129+
echo "Could not verify GPG signature of AWS CLI install script. Make sure you provided a valid version."
130+
exit 1
131+
fi
132+
133+
unzip "${scriptZipFile}"
134+
./aws/install
135+
136+
rm -rf ./aws
137+
}
138+
139+
echo "(*) Installing AWS CLI..."
140+
141+
install
142+
143+
echo "Done!"

script-library/container-features/README.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ This folder includes some explorations around dynamic container feature injectio
88

99
**Registering a feature**
1010

11-
Create the install script in the [script-library](../../script-library/) directory with the naming convention `<lowercase-feature-name>-<target-os>.sh`. EG `python-debian.sh` or `common-alpine.sh`
11+
Create the install script in the [script-library](../../script-library/) directory with the naming convention `<lowercase-feature-name>-<target-os>.sh`. E.g., `python-debian.sh` or `common-alpine.sh`
1212

1313
Add a new object to the [devcontainer-features.json](../../script-library/container-features/src/devcontainer-features.json) file:
1414

1515
```json
1616
{
1717
"id": "<lowercase-feature-name>", // Must match the <lowercase-feature-name> used to name the install script.
1818
"name": "Display Name of Feature",
19-
"documentationURL": "https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/<lowercase-feature-name>.md",
19+
"documentationURL": "https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/<lowercase-feature-name>.md",
2020
"options": {
2121
"scriptArgument$1": {
2222
"type": "string", // Either "string" or "boolean"
@@ -31,7 +31,7 @@ Add a new object to the [devcontainer-features.json](../../script-library/contai
3131
}
3232
},
3333
"buildArg": "_VSC_INSTALL_<CAPITALIZED_ID>", // Must match the ENV VAR defined in the feature-scripts.env file.
34-
"extensions": [], // Array of VS Code extensions to install with this feature.
34+
"extensions": [], // Array of VS Code extensions to install with this feature.
3535
"include": [] // Array of base containers this script can be used on.
3636
}
3737
```
@@ -43,7 +43,7 @@ _VSC_INSTALL_<FEATURE>="<feature>-debian.sh ${_BUILD_ARG_<FEATURE>_<OPTION1>:-<o
4343
```
4444

4545
- Options declared in `devcontainer-features.json` are mapped using the naming convention `_BUILD_ARG_<FEATURE>_<OPTIONNAME>` and their default should match the declared default for that option.
46-
- EG `_VSC_INSTALL_AZURE_CLI="azcli-debian.sh ${_BUILD_ARG_AZURE_CLI_VERSION:-latest}"`
46+
- E.g., `_VSC_INSTALL_AZURE_CLI="azcli-debian.sh ${_BUILD_ARG_AZURE_CLI_VERSION:-latest}"`
4747

4848
**Feature testing**
4949

@@ -59,21 +59,25 @@ _VSC_INSTALL_<FEATURE>="<feature>-debian.sh ${_BUILD_ARG_<FEATURE>_<OPTION1>:-<o
5959
Repeat as needed to iterate from a clean workspace.
6060

6161
*Unit tests*
62+
6263
- Add your feature to the [run-scripts.sh](../../script-library/test/regression/run-scripts.sh) file to ensure it is included in CI tests.
6364

6465
- Your addition should take the form `runScript <feature> <non-default-args>`.
6566

66-
EG
67+
E.g.:
68+
6769
```sh
6870
runScript dotnet "3.1 true ${USERNAME} false /opt/dotnet dotnet"
6971
```
7072

7173
- If your script takes the installation user as an argument, be sure to specify it as ${USERNAME} in the tests for programatic testing.
7274

7375
*Regression tests*
76+
7477
- Add your feature to the [test-features.env](../../script-library/container-features/test-features.env) file to include it in regression tests of the container-feature functionality. By setting the `_VSC_INSTALL_<FEATURE>` ENV VAR to true and adding the expected _BUILD_ARG options for your feature.
7578

76-
EG
79+
E.g.:
80+
7781
```
7882
_VSC_INSTALL_DOTNET=true
7983
_BUILD_ARG_DOTNET_VERSION=latest
@@ -82,11 +86,12 @@ EG
8286

8387
**Feature documentation**
8488

85-
Add your new feature to the list of scripts in the [script-library README.md](https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/README.md#scripts).
89+
Add your new feature to the list of scripts in the [script-library README.md](../../script-library/README.md#scripts).
90+
91+
Add documentation for your new feature script to the [script-library/docs](../../script-library/docs) directory.
8692

87-
Add documentation for your new feature script to the [script-library/docs](https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs) directory.
93+
Documentation should include:
8894

89-
Documentation should include:
9095
- the status of the script, supported operating systems, and maintainer.
9196
- the syntax expected to run as a feature or script
9297
- a description of the script arguments
@@ -97,6 +102,7 @@ Feel free to use other scripts in that directory as inspiration.
97102
### Best practices for writing feature install scripts
98103

99104
- Decouple sections of the shellscript that handle user setup, helper functions, and feature installation. Doing so will apply a logical and natural flow to the script for future developers and maintainers to follow. One way to denote this distinction is to use in-line comments throughout the script.
105+
100106
```md
101107
# Logical flow recommended:
102108
1. File header and description.
@@ -109,6 +115,7 @@ Feel free to use other scripts in that directory as inspiration.
109115
```
110116

111117
- One way to make troubleshooting the script easier when writing a bash shell script is to echo error messages to `STDERR`. A possible way we implemented this in bash scripts is to create an `err()` function like so:
118+
112119
```sh
113120
# Setup STDERR.
114121
err() {
@@ -119,12 +126,14 @@ Feel free to use other scripts in that directory as inspiration.
119126
```
120127

121128
- If writing a bash shellscript, we recommend using double quotes and braces when referencing named variables:
129+
122130
```sh
123131
variable="My example var"
124132
echo "${variable}"
125133
```
126134

127135
- One method to to ensure the global space in a script is not too crowded with unnecessary variables is to assign return values from functions to a new variable, and use the keyword `local` for vars inside of functions. For example:
136+
128137
```sh
129138
test_function() {
130139
local test = "hello world!"
@@ -134,6 +143,7 @@ Feel free to use other scripts in that directory as inspiration.
134143
```
135144

136145
- If using temporary files within the script, we recommend removing all those files once they are no longer needed. One method for doing this is running a cleanup function with a `trap` method when the script exits:
146+
137147
```sh
138148
# Cleanup temporary directory and associated files when exiting the script.
139149
cleanup() {
@@ -150,7 +160,7 @@ Feel free to use other scripts in that directory as inspiration.
150160

151161
- Consider using [shellcheck](https://github.com/koalaman/shellcheck) or the [vscode-shellcheck extension](https://github.com/vscode-shellcheck/vscode-shellcheck) to apply linting and static code analysis to the bash script to ensure it is formatted correctly.
152162

153-
- Consider using common helper functions from [shared/utils.sh](../../script-library/shared/utils.sh) when managing common tasks (like updating PATH variables, or managing gpg keys) by copying them directly into your script.
163+
- Consider using common helper functions from [shared/utils.sh](../../script-library/shared/utils.sh) when managing common tasks (like updating PATH variables, or managing gpg keys) by copying them directly into your script.
154164
- NOTE: This is done to minimize the impact that any change can have on existing working scripts.
155165
- Similarly, if you add a helper function to your script that could benefit others in the future, consider adding it to the `shared/utils.sh` file as well.
156166

0 commit comments

Comments
 (0)