Skip to content

Commit

Permalink
Add postgresql-client Feature (#1)
Browse files Browse the repository at this point in the history
## Description

Adds a Feature that installs a version of the `postgresql-client`
libraries (notably, `psql`) for interacting with a PostgreSQL server.

## Commits

- Formatting [skip ci]
- Update metadata and README [skip ci]
- Add PostgreSQL client feature
  • Loading branch information
jgarber623-cargosense authored Feb 28, 2024
2 parents f5438ca + 0726f20 commit 99a6854
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CargoSense/devcontainer-features

🐳 📦 **Reusable features for [Dev Containers](https://code.visualstudio.com/docs/devcontainers/containers) and [GitHub Codespaces](https://github.com/features/codespaces).**
🐳 📦 **Reusable Features for [Dev Containers](https://code.visualstudio.com/docs/devcontainers/containers) and [GitHub Codespaces](https://github.com/features/codespaces).**

> [!TIP]
> If you're new to Dev Containers, check out [the VisualStudio Code Dev Containers tutorial](https://code.visualstudio.com/docs/devcontainers/tutorial).
Expand Down
2 changes: 1 addition & 1 deletion src/actionlint/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# actionlint

Install [actionlint](https://github.com/rhysd/actionlint), a static checker for GitHub Actions workflow files.
Install [actionlint](https://github.com/rhysd/actionlint), a static checker for [GitHub Actions](https://github.com/features/actions) workflow files.

## Usage

Expand Down
1 change: 1 addition & 0 deletions src/actionlint/devcontainer-feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "actionlint",
"id": "actionlint",
"version": "1.0.0",
"description": "Install actionlint, a static checker for GitHub Actions workflow files.",
"options": {
"version": {
"type": "string",
Expand Down
21 changes: 21 additions & 0 deletions src/postgresql-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# postgresql-client

Install client and administrative programs for [PostgreSQL](https://www.postgresql.org).

## Usage

```json
"features": {
"ghcr.io/CargoSense/devcontainer-features/postgresql-client:1": {}
}
```

## Options

| Option ID | Description | Type | Default Value |
|:--------------|:------------------------------------------|:-------|:--------------|
| `version` | The postgresql-client version to install. | string | `os-provided` |

## OS Support

This Feature should work on recent versions of Debian/Ubuntu and Linux distributions using the [apt](https://wiki.debian.org/AptCLI) management tool.
21 changes: 21 additions & 0 deletions src/postgresql-client/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "PostgreSQL Client",
"id": "postgresql-client",
"version": "1.0.0",
"description": "Install client and administrative programs for PostgreSQL.",
"options": {
"version": {
"type": "string",
"enum": [
"15",
"16",
"os-provided"
],
"default": "os-provided",
"description": "Select or enter a PostgreSQL version."
}
},
"installsAfter": [
"ghcr.io/devcontainers/features/common-utils"
]
}
50 changes: 50 additions & 0 deletions src/postgresql-client/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env sh

set -e

POSTGRESQL_VERSION="${VERSION:-"os-provided"}"
VERSION_CODENAME="$(sed -nr 's/VERSION_CODENAME=(.+)/\1/p' /etc/os-release)"

if [ "$(id -u)" -ne 0 ]; then
printf 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi

if [ "${POSTGRESQL_VERSION}" = "os-provided" ]; then
apt update --yes
apt install --no-install-recommends --yes postgresql-client

exit 0
fi

curl_installed=""
gpg_installed=""

if ! type curl >/dev/null 2>&1; then
apt update --yes
apt install --no-install-recommends --yes curl ca-certificates

curl_installed="true"
fi

if ! type gpg >/dev/null 2>&1; then
apt update --yes
apt install --no-install-recommends --yes gpg

gpg_installed="true"
fi

install -d /usr/share/postgresql-common/pgdg
curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt '${VERSION_CODENAME}'-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

apt update --yes
apt install --no-install-recommends --yes postgresql-client-${POSTGRESQL_VERSION}

if ! [ -z $curl_installed ]; then
apt purge curl --autoremove --yes
fi

if ! [ -z $gpg_installed ]; then
apt purge gpg --autoremove --yes
fi
46 changes: 46 additions & 0 deletions test/postgresql-client/postgresql-client-15.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# This test file will be executed against an auto-generated devcontainer.json that
# includes the 'postgresql-client' Feature with no options.
#
# For more information, see: https://github.com/devcontainers/cli/blob/main/docs/features/test.md
#
# Eg:
# {
# "image": "<..some-base-image...>",
# "features": {
# "postgresql-client": {}
# },
# "remoteUser": "root"
# }
#
# Thus, the value of all options will fall back to the default value in the
# Feature's 'devcontainer-feature.json'.
#
# These scripts are run as 'root' by default. Although that can be changed
# with the '--remote-user' flag.
#
# This test can be run with the following command:
#
# devcontainer features test \
# --features postgresql-client \
# --remote-user root \
# --skip-scenarios \
# --base-image mcr.microsoft.com/devcontainers/base:ubuntu \
# /path/to/this/repo

set -e

# Optional: Import test library bundled with the devcontainer CLI
# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib
# Provides the 'check' and 'reportResults' commands.
source dev-container-features-test-lib

# Feature-specific tests
# The 'check' command comes from the dev-container-features-test-lib. Syntax is...
# check <LABEL> <cmd> [args...]
check "version" bash -c "psql --version | grep -E 'psql \(PostgreSQL\) 15\..+'"

# Report result
# If any of the checks above exited with a non-zero exit code, the test will fail.
reportResults
10 changes: 10 additions & 0 deletions test/postgresql-client/scenarios.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"postgresql-client-15": {
"image": "debian:latest",
"features": {
"postgresql-client": {
"version": "15"
}
}
}
}
47 changes: 47 additions & 0 deletions test/postgresql-client/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

# This test file will be executed against an auto-generated devcontainer.json that
# includes the 'postgresql-client' Feature with no options.
#
# For more information, see: https://github.com/devcontainers/cli/blob/main/docs/features/test.md
#
# Eg:
# {
# "image": "<..some-base-image...>",
# "features": {
# "postgresql-client": {}
# },
# "remoteUser": "root"
# }
#
# Thus, the value of all options will fall back to the default value in the
# Feature's 'devcontainer-feature.json'.
#
# These scripts are run as 'root' by default. Although that can be changed
# with the '--remote-user' flag.
#
# This test can be run with the following command:
#
# devcontainer features test \
# --features postgresql-client \
# --remote-user root \
# --skip-scenarios \
# --base-image mcr.microsoft.com/devcontainers/base:ubuntu \
# /path/to/this/repo

set -e

# Optional: Import test library bundled with the devcontainer CLI
# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib
# Provides the 'check' and 'reportResults' commands.
source dev-container-features-test-lib

# Feature-specific tests
# The 'check' command comes from the dev-container-features-test-lib. Syntax is...
# check <LABEL> <cmd> [args...]
check "version" psql --version
check "which psql" bash -c "which psql | grep /usr/bin/psql"

# Report result
# If any of the checks above exited with a non-zero exit code, the test will fail.
reportResults

0 comments on commit 99a6854

Please sign in to comment.