Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

Commit

Permalink
style: standardize feature installation code (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbraun89 authored Jan 4, 2024
1 parent b4a8660 commit 53e255a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
11 changes: 6 additions & 5 deletions src/devcontainers-cli/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

# devcontainers CLI (devcontainers-cli)
# devcontainers CLI (via npm) (devcontainers-cli)

The [devcontainers CLI](https://github.com/devcontainers/cli), which configures devcontainers from `devcontainer.json`. Requires the Docker client and access to a container engine daemon socket (e.g. by mounting the host's Docker socket into the container)
The devcontainers CLI, which configures devcontainers from devcontainer.json. Requires the Docker client and access to a Docker engine socket (e.g. by mounting the host's Docker socket into the container)

## Example devcontainers CLI Usage
## Example DevContainer Usage

```json
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
"ghcr.io/devcontainers-contrib/features/devcontainers-cli:1": {}
}
```
Expand All @@ -16,4 +15,6 @@ The [devcontainers CLI](https://github.com/devcontainers/cli), which configures

| Options Id | Description | Type | Default Value |
|-----|-----|-----|-----|
| version | Specify the version to install | string | latest |
| version | Select the version to install. | string | latest |


5 changes: 3 additions & 2 deletions src/devcontainers-cli/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"id": "devcontainers-cli",
"version": "1.0.0",
"name": "devcontainers CLI",
"name": "devcontainers CLI (via npm)",
"documentationURL": "http://github.com/devcontainers-contrib/features/tree/main/src/devcontainers-cli",
"description": "The devcontainers CLI, which configures devcontainers from devcontainer.json. Requires the Docker client and access to a Docker engine socket (e.g. by mounting the host's Docker socket into the container)",
"options": {
"version": {
"default": "latest",
"description": "Specify the version to install",
"description": "Select the version to install.",
"proposals": [
"latest"
],
Expand All @@ -16,6 +16,7 @@
},
"installsAfter": [
"ghcr.io/devcontainers-contrib/features/npm-package",
"ghcr.io/devcontainers/features/node",
"ghcr.io/devcontainers/features/docker-outside-of-docker"
]
}
6 changes: 3 additions & 3 deletions src/devcontainers-cli/install.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash -i

set -e

source ./library_scripts.sh
. ./library_scripts.sh

# nanolayer is a cli utility which keeps container layers as small as possible
# source code: https://github.com/devcontainers-contrib/nanolayer
# `ensure_nanolayer` is a bash function that will find any existing nanolayer installations,
# and if missing - will download a temporary copy that automatically get deleted at the end
# of the script
ensure_nanolayer nanolayer_location "v0.4.39"
ensure_nanolayer nanolayer_location "v0.5.5"


$nanolayer_location \
install \
Expand Down
45 changes: 19 additions & 26 deletions src/devcontainers-cli/library_scripts.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/bash -i


clean_download() {
Expand All @@ -10,13 +9,13 @@ clean_download() {
# The above steps will minimize the leftovers being created while installing the downloader
# Supported distros:
# debian/ubuntu/alpine

url=$1
output_location=$2
tempdir=$(mktemp -d)
downloader_installed=""

function _apt_get_install() {
_apt_get_install() {
tempdir=$1

# copy current state of apt list - in order to revert back later (minimize contianer layer size)
Expand All @@ -25,7 +24,7 @@ clean_download() {
apt-get -y install --no-install-recommends wget ca-certificates
}

function _apt_get_cleanup() {
_apt_get_cleanup() {
tempdir=$1

echo "removing wget"
Expand All @@ -36,15 +35,15 @@ clean_download() {
rm -r /var/lib/apt/lists && mv $tempdir/lists /var/lib/apt/lists
}

function _apk_install() {
_apk_install() {
tempdir=$1
# copy current state of apk cache - in order to revert back later (minimize contianer layer size)
cp -p -R /var/cache/apk $tempdir

apk add --no-cache wget
}

function _apk_cleanup() {
_apk_cleanup() {
tempdir=$1

echo "removing wget"
Expand Down Expand Up @@ -100,45 +99,39 @@ ensure_nanolayer() {
local variable_name=$1

local required_version=$2
# normalize version
if ! [[ $required_version == v* ]]; then
required_version=v$required_version
fi

local nanolayer_location=""
local __nanolayer_location=""

# If possible - try to use an already installed nanolayer
if [[ -z "${NANOLAYER_FORCE_CLI_INSTALLATION}" ]]; then
if [[ -z "${NANOLAYER_CLI_LOCATION}" ]]; then
if [ -z "${NANOLAYER_FORCE_CLI_INSTALLATION}" ]; then
if [ -z "${NANOLAYER_CLI_LOCATION}" ]; then
if type nanolayer >/dev/null 2>&1; then
echo "Found a pre-existing nanolayer in PATH"
nanolayer_location=nanolayer
__nanolayer_location=nanolayer
fi
elif [ -f "${NANOLAYER_CLI_LOCATION}" ] && [ -x "${NANOLAYER_CLI_LOCATION}" ] ; then
nanolayer_location=${NANOLAYER_CLI_LOCATION}
echo "Found a pre-existing nanolayer which were given in env variable: $nanolayer_location"
__nanolayer_location=${NANOLAYER_CLI_LOCATION}
echo "Found a pre-existing nanolayer which were given in env variable: $__nanolayer_location"
fi

# make sure its of the required version
if ! [[ -z "${nanolayer_location}" ]]; then
if ! [ -z "${__nanolayer_location}" ]; then
local current_version
current_version=$($nanolayer_location --version)
if ! [[ $current_version == v* ]]; then
current_version=v$current_version
fi
current_version=$($__nanolayer_location --version)


if ! [ $current_version == $required_version ]; then
echo "skipping usage of pre-existing nanolayer. (required version $required_version does not match existing version $current_version)"
nanolayer_location=""
__nanolayer_location=""
fi
fi

fi

# If not previuse installation found, download it temporarly and delete at the end of the script
if [[ -z "${nanolayer_location}" ]]; then
if [ -z "${__nanolayer_location}" ]; then

if [ "$(uname -sm)" == "Linux x86_64" ] || [ "$(uname -sm)" == "Linux aarch64" ]; then
if [ "$(uname -sm)" = 'Linux x86_64' ] || [ "$(uname -sm)" = "Linux aarch64" ]; then
tmp_dir=$(mktemp -d -t nanolayer-XXXXXXXXXX)

clean_up () {
Expand All @@ -162,7 +155,7 @@ ensure_nanolayer() {

tar xfzv $tmp_dir/$tar_filename -C "$tmp_dir"
chmod a+x $tmp_dir/nanolayer
nanolayer_location=$tmp_dir/nanolayer
__nanolayer_location=$tmp_dir/nanolayer


else
Expand All @@ -172,7 +165,7 @@ ensure_nanolayer() {
fi

# Expose outside the resolved location
declare -g ${variable_name}=$nanolayer_location
export ${variable_name}=$__nanolayer_location

}

Expand Down
Empty file modified test/devcontainers-cli/test.sh
100644 → 100755
Empty file.

0 comments on commit 53e255a

Please sign in to comment.