`scripts/Dockerfile.base` and `install.sh --with-terraform` download the
bundled Terraform zip from `releases.hashicorp.com` and use it without
checking it. This verifies that zip against a checksum file kept in the
Coder repo. The only thing trusted is the Coder repo, which is where the
`Dockerfile` and `install.sh` themselves come from. Everything fetched
from HashiCorp is checked against it.
The other download paths are already covered:
`provisioner/terraform/install.go` uses `hc-install`, which checks the
GPG-signed SHA256SUMS, and `mise.lock` / `flake.nix` already carry
per-platform hashes.
## Source of truth
`provisioner/terraform/terraform_SHA256SUMS` is a verbatim copy of
HashiCorp's `terraform_1.17.2_SHA256SUMS`, the version `main` ships,
verified against their signing key (`C874 011F 0AB4 0511 0D02 1055 3436
5D94 72D7 468F`). Being verbatim, you can diff it against the published
file or check it with the published `.sig`. Its hashes match the
Terraform entries in `mise.lock` on `main`. Both the Terraform version
and the checksums are read from it, so neither consumer pins a version.
## Consumers
* `scripts/Dockerfile.base` copies the file in and reads the version and
its architecture's checksum from it. It checks the zip with `sha256sum
-c` before unzipping. The build fails if there's no entry for the
architecture or the download doesn't match. All three build contexts
include the file: `docker-base.yaml`, `release.yaml` and
`scripts/build_docker.sh`. `docker-base.yaml` also runs when the file
changes.
* `install.sh` no longer has a `TERRAFORM_VERSION` or any hashes.
`--with-terraform` downloads `terraform_SHA256SUMS` from the Coder repo
at `refs/tags/v${VERSION}`, the tag of the release being installed. Only
if that tag doesn't have the file (a 404, for releases from before it
existed) does it fall back to `refs/heads/main`, with a note in the
output. Any other error fails. Fully qualified refs mean a branch named
like the tag, or a tag named `main`, can't be read instead. It then:
* only accepts lines with exactly two fields: 64 lowercase hex
characters and `terraform_X.Y.Z_<os>_<arch>.zip`. It picks the line for
`${OS}_${TERRAFORM_ARCH}`, validates the Terraform version and builds
the zip name from validated parts rather than using the one in the file.
It refuses to install if there's no such line;
* downloads the zip from HashiCorp and checks it with `sha256sum`,
`shasum -a 256` or `sha256`, including a zip reused from the cache. On a
mismatch it deletes the zip so a rerun downloads it again;
* skips the check under `--dry-run`, since nothing is downloaded. The
checksum file is still fetched so the version can be printed.
* `install.sh` **command handling** (security review): `sh_c` runs its
arguments directly instead of re-parsing them with `sh -c`, and
`sudo_sh_c` passes them to `sudo` or `doas` as-is. The `su` fallback
single-quotes every argument (`quote_args`). The resolved Coder version
must be `X.Y.Z` or `X.Y.Z-rc.N` before it's used in URLs, file names or
commands. As a side effect, install prefixes and cache paths with spaces
now work.
* `provisioner/terraform/checksums_test.go` fails if the file isn't in
SHA256SUMS format, is for a different version than `TerraformVersion`,
or lacks the `linux_{amd64,arm64,arm}` entries that `Dockerfile.base`
needs.
Also fixes `install.sh` unzipping `${ARCH}` instead of
`${TERRAFORM_ARCH}`, which broke `--with-terraform` on armv7 (`armv7` vs
`arm`).
## Bumping after this
Replace `terraform_SHA256SUMS` with the new verified file and update
`TerraformVersion` (plus `mise.toml`, `mise.lock`, `flake.nix` and the
airgap doc, as today). `Dockerfile.base` and `install.sh` need no edits.
`scripts/update_terraform_version.sh` (coder/coder#31463) does all of
this, and the `TerraformVersion` note in `install.go` points at it.
`main` is merged into this branch, including coder/coder#31438
(Terraform 1.17.1) and coder/coder#32110 (Terraform 1.17.2), so the
checksum file and `TerraformVersion` are at 1.17.2.
## Testing
After merging `main` (Terraform 1.17.2), I re-ran the Go test, `go vet`,
`gofmt`, `shellcheck`, `shfmt` and `actionlint`, and checked that the
`Dockerfile.base` extraction resolves 1.17.2 for amd64, arm64 and arm.
The `docker-base` build and the `install.sh` and `Dockerfile.base` runs
below were done before that merge, against 1.16.7 and 1.17.1, and I
haven't repeated them at 1.17.2. The `docker-base` workflow rebuilds the
image from the 1.17.2 file in CI.
* The `docker-base` workflow built all three platforms from the checksum
file: `terraform_1.16.7_linux_{amd64,arm64,arm}.zip: OK`, then Terraform
v1.16.7 on each.
* Ran the `Dockerfile.base` Terraform step under BusyBox 1.35 `sh`. It
fails with a changed hash or no entry for the architecture. With the
verified 1.17.1 file it installs 1.17.1 with no Dockerfile edit.
* `install.sh` `with_terraform` under `dash` on linux/amd64:
* fetched the real file over `raw.githubusercontent.com` from this
branch (standing in for `main`), then verified and installed 1.16.7;
* with the 1.17.1 file it installs 1.17.1;
* a changed hash is rejected and the cached zip is deleted;
* an unsupported platform and a garbage response are both refused;
* `--dry-run` prints the commands.
* Today neither the release tags nor `main` have the file, so
`--with-terraform` fails with "Could not download the Terraform
checksums" until this merges. After that, older releases use `main`'s
file.
* Security review fixes, under `dash` and `bash --posix`:
* checksum lines with `$(...)`, backticks, `;`, `../`, an extra field,
an uppercase or 65-character hash, or a malformed Terraform version are
refused before any command runs. The previous version of the script ran
the `$(...)` one. A file with an injected line followed by the real ones
installs only the real zip;
* with a `curl` stub, a tag hit reads `refs/tags/v2.39.0`, a 404 falls
back to `refs/heads/main`, and a 500 or a network error fails without
trying `main`;
* `sh_c`, and `sudo_sh_c` through `sudo`, `doas` and `su` stubs, pass
arguments with spaces, quotes, `;` and `$(...)` through unchanged and
run nothing else. `--dry-run` runs nothing;
* `--version v2.28.0`, `2.28.0;id` and `../../x` are refused. `--version
2.39.0`, `--version 2.33.0-rc.1`, `--stable` and the default mainline
still work;
* real installs of Terraform into a prefix with a space (no sudo) and
into a root-owned prefix (through `sudo`), and of Coder 2.39.0 with
`--method standalone` into a prefix with a space.
* `go test ./provisioner/terraform -run
TestTerraformChecksumFileMatchesVersion` passes with the 1.17.2 file,
and fails with the 1.17.1 file in place (`expected: "1.17.2", actual:
"1.17.1"`). `go vet` and `gofmt` are clean.
* `shellcheck --external-sources` (v0.10.0) and `shfmt -d` (v3.12.0) are
clean on `install.sh` and `scripts/build_docker.sh`, and `actionlint`
(v1.7.7) is clean on the workflows. Local git hooks were skipped.
## How coder/coder#31600, coder/coder#31463 and coder/coder#31461 fit
together
* coder/coder#31600 makes `provisioner/terraform/terraform_SHA256SUMS`
(a verbatim copy of HashiCorp's signed file) the one place the bundled
Terraform checksums live. `Dockerfile.base` reads it, and `install.sh`
downloads it from the Coder repo at install time.
* coder/coder#31463 adds `scripts/update_terraform_version.sh`. On
branches with the checksum file it just replaces that file. On older
branches it edits `install.sh` and `Dockerfile.base` directly.
* coder/coder#31461 (stacked on coder/coder#31463) runs the script
weekly for `main` and the supported release branches. It needed no
changes for the checksum file.
None of the three touch the same files, and merging all three onto
`main` is conflict-free. On that combined tree, the bump script (run
before `main` moved to 1.17.2) took 1.16.7 to 1.17.1 by replacing the
checksum file, the new Go test passes, and `actionlint` and `shellcheck`
pass. They can merge in any order. Merge coder/coder#31461 after
coder/coder#31463, retargeted to `main`.
Opened by Superblink on behalf of @droopy4096.
---------
Co-authored-by: coder[bot] <137810501+coder[bot]@users.noreply.github.com>
Co-authored-by: Dmytro Makovey <dmitry@coder.com>
Co-authored-by: Lisa Fiander <lisa@coder.com>
Co-authored-by: Dmitry Makovey <dmakovey@yahoo.com>
995 lines
26 KiB
Bash
Executable File
995 lines
26 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
# Coder's automatic install script.
|
|
# See https://github.com/coder/coder#install
|
|
#
|
|
# To run:
|
|
# curl -fsSL https://coder.com/install.sh | sh
|
|
|
|
usage() {
|
|
arg0="$0"
|
|
if [ "$0" = sh ]; then
|
|
arg0="curl -fsSL https://coder.com/install.sh | sh -s --"
|
|
else
|
|
not_curl_usage="The latest script is available at https://coder.com/install.sh
|
|
"
|
|
fi
|
|
|
|
cath <<EOF
|
|
Installs Coder.
|
|
It tries to use the system package manager if possible.
|
|
After successful installation it explains how to start Coder.
|
|
|
|
Pass in user@host to install Coder on user@host over ssh.
|
|
The remote host must have internet access.
|
|
${not_curl_usage-}
|
|
Usage:
|
|
|
|
${arg0} [--dry-run] [--mainline | --stable | --version X.X.X] [--method detect] \
|
|
[--prefix ~/.local] [--rsh ssh] [user@host]
|
|
|
|
--dry-run
|
|
Echo the commands for the install process without running them.
|
|
|
|
--mainline
|
|
Install the latest mainline version (default).
|
|
|
|
--stable
|
|
Install the latest stable version instead of the latest mainline version.
|
|
|
|
--version X.X.X
|
|
Install a specific version instead of the latest.
|
|
|
|
--method [detect | standalone]
|
|
Choose the installation method. Defaults to detect.
|
|
- detect detects the system package manager and tries to use it.
|
|
Full reference on the process is further below.
|
|
- standalone installs a standalone release archive into /usr/local/bin
|
|
|
|
--prefix <dir>
|
|
Sets the prefix used by standalone release archives. Defaults to /usr/local
|
|
and the binary is copied into /usr/local/bin
|
|
To install in \$HOME, pass --prefix=\$HOME/.local
|
|
|
|
--binary-name <name>
|
|
Sets the name for the CLI in standalone release archives. Defaults to "coder"
|
|
To use the CLI as coder2, pass --binary-name=coder2
|
|
Note: in-product documentation will always refer to the CLI as "coder"
|
|
|
|
--rsh <bin>
|
|
Specifies the remote shell for remote installation. Defaults to ssh.
|
|
|
|
--with-terraform
|
|
Installs the Terraform binary bundled with the Coder release alongside coder.
|
|
The version and its SHA256 checksum come from the Coder repository, and the
|
|
binary is downloaded from https://releases.hashicorp.com/terraform/ and verified.
|
|
This is great for if you are having issues with Coder installing terraform, or if you
|
|
just want it on your base system aswell.
|
|
This supports most systems, however if you are unsure yours is supported you can check
|
|
the link above.
|
|
--net-admin
|
|
Adds \`CAP_NET_ADMIN\` to the installed binary. This allows Coder to
|
|
increase network speeds, but has security implications.
|
|
See: https://man7.org/linux/man-pages/man7/capabilities.7.html
|
|
This only works on Linux based systems.
|
|
|
|
|
|
The detection method works as follows:
|
|
- Debian, Ubuntu, Raspbian: install the deb package from GitHub.
|
|
- Fedora, CentOS, RHEL, openSUSE: install the rpm package from GitHub.
|
|
- Alpine: install the apk package from GitHub.
|
|
- macOS: if \`brew\` is available, install from the coder/coder Homebrew tap.
|
|
- Otherwise, download from GitHub and install into \`--prefix\`.
|
|
|
|
We build releases on GitHub for amd64, armv7, and arm64 on Windows, Linux, and macOS.
|
|
|
|
When the detection method tries to pull a release from GitHub it will
|
|
fall back to installing standalone when there is no matching release for
|
|
the system's operating system and architecture.
|
|
|
|
The installer will cache all downloaded assets into ~/.cache/coder
|
|
EOF
|
|
}
|
|
|
|
echo_latest_stable_version() {
|
|
url="https://github.com/coder/coder/releases/latest"
|
|
# https://gist.github.com/lukechilds/a83e1d7127b78fef38c2914c4ececc3c#gistcomment-2758860
|
|
response=$(curl -sSLI -o /dev/null -w "\n%{http_code} %{url_effective}" ${url})
|
|
status_code=$(echo "$response" | tail -n1 | cut -d' ' -f1)
|
|
version=$(echo "$response" | tail -n1 | cut -d' ' -f2-)
|
|
body=$(echo "$response" | sed '$d')
|
|
|
|
if [ "$status_code" != "200" ]; then
|
|
echoerr "GitHub API returned status code: ${status_code}"
|
|
echoerr "URL: ${url}"
|
|
exit 1
|
|
fi
|
|
|
|
version="${version#https://github.com/coder/coder/releases/tag/v}"
|
|
echo "${version}"
|
|
}
|
|
|
|
echo_latest_mainline_version() {
|
|
# Fetch the releases from the GitHub API, sort by version number,
|
|
# and take the first result. Note that we're sorting by space-
|
|
# separated numbers and without utilizing the sort -V flag for the
|
|
# best compatibility.
|
|
url="https://api.github.com/repos/coder/coder/releases"
|
|
response=$(curl -sSL -w "\n%{http_code}" ${url})
|
|
status_code=$(echo "$response" | tail -n1)
|
|
body=$(echo "$response" | sed '$d')
|
|
|
|
if [ "$status_code" != "200" ]; then
|
|
echoerr "GitHub API returned status code: ${status_code}"
|
|
echoerr "URL: ${url}"
|
|
echoerr "Response body: ${body}"
|
|
exit 1
|
|
fi
|
|
|
|
# Filter to strict semver (MAJOR.MINOR.PATCH) to exclude
|
|
# pre-release tags like RC builds from version resolution.
|
|
echo "$body" |
|
|
awk -F'"' '/"tag_name"/ {print $4}' |
|
|
tr -d v |
|
|
grep '^[0-9]\+\.[0-9]\+\.[0-9]\+$' |
|
|
tr . ' ' |
|
|
sort -k1,1nr -k2,2nr -k3,3nr |
|
|
head -n1 |
|
|
tr ' ' .
|
|
}
|
|
|
|
echo_standalone_postinstall() {
|
|
if [ "${DRY_RUN-}" ]; then
|
|
echo_dryrun_postinstall
|
|
return
|
|
fi
|
|
|
|
channel=
|
|
advisory="To install our stable release (v${STABLE_VERSION}), use the --stable flag. "
|
|
if [ "${STABLE}" = 1 ]; then
|
|
channel="stable "
|
|
advisory=""
|
|
fi
|
|
if [ "${MAINLINE}" = 1 ]; then
|
|
channel="mainline "
|
|
fi
|
|
|
|
cath <<EOF
|
|
|
|
Coder ${channel}release v${VERSION} installed. ${advisory}See our releases documentation or GitHub for more information on versioning.
|
|
|
|
The Coder binary has been placed in the following location:
|
|
|
|
$STANDALONE_INSTALL_PREFIX/bin/$STANDALONE_BINARY_NAME
|
|
|
|
EOF
|
|
|
|
CODER_COMMAND="$(command -v "$STANDALONE_BINARY_NAME" || true)"
|
|
|
|
if [ -z "${CODER_COMMAND}" ]; then
|
|
cath <<EOF
|
|
Extend your path to use Coder:
|
|
|
|
$ PATH="$STANDALONE_INSTALL_PREFIX/bin:\$PATH"
|
|
|
|
EOF
|
|
elif [ "$CODER_COMMAND" != "$STANDALONE_BINARY_LOCATION" ]; then
|
|
echo_path_conflict "$CODER_COMMAND"
|
|
else
|
|
cath <<EOF
|
|
To run a Coder server:
|
|
|
|
$ $STANDALONE_BINARY_NAME server
|
|
|
|
To connect to a Coder deployment:
|
|
|
|
$ $STANDALONE_BINARY_NAME login <deployment url>
|
|
|
|
EOF
|
|
fi
|
|
}
|
|
|
|
echo_brew_postinstall() {
|
|
if [ "${DRY_RUN-}" ]; then
|
|
echo_dryrun_postinstall
|
|
return
|
|
fi
|
|
|
|
BREW_PREFIX="$(brew --prefix)"
|
|
|
|
cath <<EOF
|
|
|
|
Coder has been installed to
|
|
|
|
$BREW_PREFIX/bin/coder
|
|
|
|
EOF
|
|
|
|
CODER_COMMAND="$(command -v "coder" || true)"
|
|
|
|
if [ "$CODER_COMMAND" != "$BREW_PREFIX/bin/coder" ]; then
|
|
echo_path_conflict "$CODER_COMMAND"
|
|
fi
|
|
|
|
cath <<EOF
|
|
To run a Coder server:
|
|
|
|
$ coder server
|
|
|
|
To connect to a Coder deployment:
|
|
|
|
$ coder login <deployment url>
|
|
|
|
EOF
|
|
}
|
|
|
|
echo_systemd_postinstall() {
|
|
if [ "${DRY_RUN-}" ]; then
|
|
echo_dryrun_postinstall
|
|
return
|
|
fi
|
|
|
|
echoh
|
|
cath <<EOF
|
|
$1 package has been installed.
|
|
|
|
To run a Coder server:
|
|
|
|
# Start Coder now and on reboot
|
|
$ sudo systemctl enable --now coder
|
|
$ journalctl -u coder.service -b
|
|
|
|
# Or just run the server directly
|
|
$ coder server
|
|
|
|
Configuring Coder: https://coder.com/docs/admin/setup
|
|
|
|
To connect to a Coder deployment:
|
|
|
|
$ coder login <deployment url>
|
|
|
|
EOF
|
|
}
|
|
|
|
echo_dryrun_postinstall() {
|
|
cath <<EOF
|
|
Dry-run complete.
|
|
|
|
To install Coder, re-run this script without the --dry-run flag.
|
|
|
|
EOF
|
|
}
|
|
|
|
echo_path_conflict() {
|
|
cath <<EOF
|
|
There is another binary in your PATH that conflicts with the binary we've installed.
|
|
|
|
$1
|
|
|
|
This is likely because of an existing installation of Coder in your \$PATH.
|
|
|
|
Run \`which -a coder\` to view all installations.
|
|
|
|
EOF
|
|
}
|
|
|
|
main() {
|
|
MAINLINE=1
|
|
STABLE=0
|
|
|
|
if [ "${TRACE-}" ]; then
|
|
set -x
|
|
fi
|
|
|
|
unset \
|
|
DRY_RUN \
|
|
METHOD \
|
|
OPTIONAL \
|
|
ALL_FLAGS \
|
|
RSH_ARGS \
|
|
RSH \
|
|
WITH_TERRAFORM \
|
|
CAP_NET_ADMIN
|
|
|
|
ALL_FLAGS=""
|
|
|
|
while [ "$#" -gt 0 ]; do
|
|
case "$1" in
|
|
-*)
|
|
ALL_FLAGS="${ALL_FLAGS} $1"
|
|
;;
|
|
esac
|
|
|
|
case "$1" in
|
|
--dry-run)
|
|
DRY_RUN=1
|
|
;;
|
|
--method)
|
|
METHOD="$(parse_arg "$@")"
|
|
shift
|
|
;;
|
|
--method=*)
|
|
METHOD="$(parse_arg "$@")"
|
|
;;
|
|
--prefix)
|
|
STANDALONE_INSTALL_PREFIX="$(parse_arg "$@")"
|
|
shift
|
|
;;
|
|
--prefix=*)
|
|
STANDALONE_INSTALL_PREFIX="$(parse_arg "$@")"
|
|
;;
|
|
--binary-name)
|
|
STANDALONE_BINARY_NAME="$(parse_arg "$@")"
|
|
shift
|
|
;;
|
|
--binary-name=*)
|
|
STANDALONE_BINARY_NAME="$(parse_arg "$@")"
|
|
;;
|
|
--version)
|
|
VERSION="$(parse_arg "$@")"
|
|
MAINLINE=0
|
|
STABLE=0
|
|
shift
|
|
;;
|
|
--version=*)
|
|
VERSION="$(parse_arg "$@")"
|
|
MAINLINE=0
|
|
STABLE=0
|
|
;;
|
|
# Support edge for backward compatibility.
|
|
--mainline | --edge)
|
|
VERSION=
|
|
MAINLINE=1
|
|
STABLE=0
|
|
;;
|
|
--stable)
|
|
VERSION=
|
|
MAINLINE=0
|
|
STABLE=1
|
|
;;
|
|
--rsh)
|
|
RSH="$(parse_arg "$@")"
|
|
shift
|
|
;;
|
|
--rsh=*)
|
|
RSH="$(parse_arg "$@")"
|
|
;;
|
|
-h | --h | -help | --help)
|
|
usage
|
|
exit 0
|
|
;;
|
|
--with-terraform)
|
|
WITH_TERRAFORM=1
|
|
;;
|
|
--net-admin)
|
|
CAP_NET_ADMIN=1
|
|
;;
|
|
--)
|
|
shift
|
|
# We remove the -- added above.
|
|
ALL_FLAGS="${ALL_FLAGS% --}"
|
|
RSH_ARGS="$*"
|
|
break
|
|
;;
|
|
-*)
|
|
echoerr "Unknown flag $1"
|
|
echoerr "Run with --help to see usage."
|
|
exit 1
|
|
;;
|
|
*)
|
|
RSH_ARGS="$*"
|
|
break
|
|
;;
|
|
esac
|
|
|
|
shift
|
|
done
|
|
|
|
if [ "${RSH_ARGS-}" ]; then
|
|
RSH="${RSH-ssh}"
|
|
echoh "Installing remotely with $RSH $RSH_ARGS"
|
|
curl -fsSL https://coder.com/install.sh | prefix "$RSH_ARGS" "$RSH" "$RSH_ARGS" sh -s -- "$ALL_FLAGS"
|
|
return
|
|
fi
|
|
|
|
# These can be overridden for testing but shouldn't normally be used as it can
|
|
# result in a broken coder.
|
|
OS=${OS:-$(os)}
|
|
ARCH=${ARCH:-$(arch)}
|
|
TERRAFORM_ARCH=${TERRAFORM_ARCH:-$(terraform_arch)}
|
|
|
|
# If we've been provided a flag which is specific to the standalone installation
|
|
# method, we should "detect" standalone to be the appropriate installation method.
|
|
# This check needs to occur before we set these variables with defaults.
|
|
if [ "${STANDALONE_INSTALL_PREFIX-}" ] || [ "${STANDALONE_BINARY_NAME-}" ]; then
|
|
METHOD=standalone
|
|
fi
|
|
|
|
METHOD="${METHOD-detect}"
|
|
if [ "$METHOD" != detect ] && [ "$METHOD" != standalone ]; then
|
|
echoerr "Unknown install method \"$METHOD\""
|
|
echoerr "Run with --help to see usage."
|
|
exit 1
|
|
fi
|
|
|
|
# We can't reasonably support installing specific versions of Coder through
|
|
# Homebrew, so if we're on macOS and the `--version` flag or the `--stable`
|
|
# flag (our tap follows mainline) was set, we should "detect" standalone to
|
|
# be the appropriate installation method. This check needs to occur before we
|
|
# set `VERSION` to a default of the latest release.
|
|
if [ "$OS" = "darwin" ] && { [ "${VERSION-}" ] || [ "${STABLE}" = 1 ]; }; then
|
|
METHOD=standalone
|
|
fi
|
|
|
|
# These are used by the various install_* functions that make use of GitHub
|
|
# releases in order to download and unpack the right release.
|
|
CACHE_DIR=$(echo_cache_dir)
|
|
TERRAFORM_INSTALL_PREFIX=${TERRAFORM_INSTALL_PREFIX:-/usr/local}
|
|
STANDALONE_INSTALL_PREFIX=${STANDALONE_INSTALL_PREFIX:-/usr/local}
|
|
STANDALONE_BINARY_NAME=${STANDALONE_BINARY_NAME:-coder}
|
|
STABLE_VERSION=$(echo_latest_stable_version)
|
|
if [ "${MAINLINE}" = 1 ]; then
|
|
VERSION=$(echo_latest_mainline_version)
|
|
echoh "Resolved mainline version: v${VERSION}"
|
|
elif [ "${STABLE}" = 1 ]; then
|
|
VERSION=${STABLE_VERSION}
|
|
echoh "Resolved stable version: v${VERSION}"
|
|
fi
|
|
if ! is_version "${VERSION-}" rc; then
|
|
echoerr "Invalid Coder version \"${VERSION-}\"."
|
|
echoerr "Expected a version like 2.28.0, without a leading v."
|
|
exit 1
|
|
fi
|
|
|
|
distro_name
|
|
|
|
if [ "${DRY_RUN-}" ]; then
|
|
echoh "Running with --dry-run; the following are the commands that would be run if this were a real installation:"
|
|
echoh
|
|
fi
|
|
|
|
# Start by installing Terraform, if requested
|
|
if [ "${WITH_TERRAFORM-}" ]; then
|
|
with_terraform
|
|
fi
|
|
|
|
# If the version is the same as the stable version, we're installing
|
|
# the stable version.
|
|
if [ "${MAINLINE}" = 1 ] && [ "${VERSION}" = "${STABLE_VERSION}" ]; then
|
|
echoh "The latest mainline version has been promoted to stable, selecting stable."
|
|
MAINLINE=0
|
|
STABLE=1
|
|
fi
|
|
# If the manually specified version is stable, mark it as such.
|
|
if [ "${MAINLINE}" = 0 ] && [ "${STABLE}" = 0 ] && [ "${VERSION}" = "${STABLE_VERSION}" ]; then
|
|
STABLE=1
|
|
fi
|
|
|
|
# Standalone installs by pulling pre-built releases from GitHub.
|
|
if [ "$METHOD" = standalone ]; then
|
|
if has_standalone; then
|
|
install_standalone
|
|
exit 0
|
|
else
|
|
echoerr "There are no standalone releases for $ARCH"
|
|
echoerr "Please try again without '--method standalone'"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# DISTRO can be overridden for testing but shouldn't normally be used as it
|
|
# can result in a broken coder.
|
|
DISTRO=${DISTRO:-$(distro)}
|
|
|
|
case $DISTRO in
|
|
darwin) install_macos ;;
|
|
# The .deb and .rpm files are pulled from GitHub.
|
|
debian) install_deb ;;
|
|
fedora | opensuse) install_rpm ;;
|
|
# We don't have GitHub releases that work on Alpine or FreeBSD so we have no
|
|
# choice but to use npm here.
|
|
alpine) install_apk ;;
|
|
# For anything else we'll try to install standalone but fall back to npm if
|
|
# we don't have releases for the architecture.
|
|
*)
|
|
echoh "Unsupported package manager."
|
|
echoh "Falling back to standalone installation."
|
|
install_standalone
|
|
;;
|
|
esac
|
|
|
|
if [ "${CAP_NET_ADMIN:-}" ]; then
|
|
cap_net_admin
|
|
fi
|
|
}
|
|
|
|
cap_net_admin() {
|
|
if ! command_exists setcap && command_exists capsh; then
|
|
echo "Package 'libcap' not found. See install instructions for your distro: https://command-not-found.com/setcap"
|
|
return
|
|
fi
|
|
|
|
# Make sure we'e allowed to add CAP_NET_ADMIN.
|
|
if sudo_sh_c capsh --has-p=CAP_NET_ADMIN; then
|
|
sudo_sh_c setcap CAP_NET_ADMIN=+ep "$(command -v coder)" || true
|
|
|
|
# Unable to escalate perms, notify the user.
|
|
else
|
|
echo "Unable to setcap agent binary. Ensure the root user has CAP_NET_ADMIN permissions."
|
|
fi
|
|
}
|
|
|
|
parse_arg() {
|
|
case "$1" in
|
|
*=*)
|
|
# Remove everything after first equal sign.
|
|
opt="${1%%=*}"
|
|
# Remove everything before first equal sign.
|
|
optarg="${1#*=}"
|
|
if [ ! "$optarg" ] && [ ! "${OPTIONAL-}" ]; then
|
|
echoerr "$opt requires an argument"
|
|
echoerr "Run with --help to see usage."
|
|
exit 1
|
|
fi
|
|
echo "$optarg"
|
|
return
|
|
;;
|
|
esac
|
|
|
|
case "${2-}" in
|
|
"" | -*)
|
|
if [ ! "${OPTIONAL-}" ]; then
|
|
echoerr "$1 requires an argument"
|
|
echoerr "Run with --help to see usage."
|
|
exit 1
|
|
fi
|
|
;;
|
|
*)
|
|
echo "$2"
|
|
return
|
|
;;
|
|
esac
|
|
}
|
|
|
|
fetch() {
|
|
URL="$1"
|
|
FILE="$2"
|
|
|
|
if [ -e "$FILE" ]; then
|
|
echoh "+ Reusing $FILE"
|
|
return
|
|
fi
|
|
|
|
sh_c mkdir -p "$CACHE_DIR"
|
|
sh_c curl \
|
|
-#fL \
|
|
-o "$FILE.incomplete" \
|
|
-C - \
|
|
"$URL"
|
|
sh_c mv "$FILE.incomplete" "$FILE"
|
|
}
|
|
|
|
with_terraform() {
|
|
# Check if the unzip package is installed. If not error peacefully.
|
|
if ! (command_exists unzip); then
|
|
echoh
|
|
echoerr "This script needs the unzip package to run."
|
|
echoerr "Please install unzip to use this function"
|
|
exit 1
|
|
fi
|
|
# The Terraform version and its checksum come from the Coder repository, the
|
|
# same source as this script and the Coder release, so the Terraform download
|
|
# from HashiCorp is verified against a checksum we publish.
|
|
if ! TERRAFORM_SHA256SUMS=$(fetch_terraform_sha256sums); then
|
|
echoh
|
|
echoerr "Could not download the Terraform checksums from the Coder repository."
|
|
exit 1
|
|
fi
|
|
# Each line looks like "<sha256> terraform_<version>_<os>_<arch>.zip".
|
|
# Only well-formed lines are accepted, so the file can't smuggle another
|
|
# file name or version into the download and install commands below.
|
|
TERRAFORM_SUM=$(echo "$TERRAFORM_SHA256SUMS" | awk -v suffix="_${OS}_${TERRAFORM_ARCH}.zip" '
|
|
NF == 2 && length($1) == 64 && $1 !~ /[^0-9a-f]/ &&
|
|
$2 ~ /^terraform_[0-9]+[.][0-9]+[.][0-9]+_[a-z0-9]+_[a-z0-9]+[.]zip$/ &&
|
|
substr($2, length($2) - length(suffix) + 1) == suffix { print $1, $2; exit }
|
|
')
|
|
if [ ! "$TERRAFORM_SUM" ]; then
|
|
echoh
|
|
echoerr "The Coder repository has no Terraform checksum for ${OS}_${TERRAFORM_ARCH}."
|
|
echoerr "Refusing to install an unverified Terraform binary."
|
|
exit 1
|
|
fi
|
|
TERRAFORM_SHA256=${TERRAFORM_SUM%% *}
|
|
TERRAFORM_VERSION=$(echo "${TERRAFORM_SUM#* }" | cut -d_ -f2)
|
|
if ! is_version "$TERRAFORM_VERSION"; then
|
|
echoh
|
|
echoerr "The Coder repository has an invalid Terraform version \"$TERRAFORM_VERSION\"."
|
|
exit 1
|
|
fi
|
|
# Build the file name from validated parts instead of using it verbatim.
|
|
TERRAFORM_ZIP="terraform_${TERRAFORM_VERSION}_${OS}_${TERRAFORM_ARCH}.zip"
|
|
echoh "Installing Terraform version $TERRAFORM_VERSION $TERRAFORM_ARCH from the HashiCorp release repository."
|
|
echoh
|
|
|
|
# Download from official source and save it to cache
|
|
fetch "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/${TERRAFORM_ZIP}" \
|
|
"$CACHE_DIR/$TERRAFORM_ZIP"
|
|
if [ ! "${DRY_RUN-}" ]; then
|
|
verify_sha256 "$CACHE_DIR/$TERRAFORM_ZIP" "$TERRAFORM_SHA256"
|
|
fi
|
|
|
|
sh_c mkdir -p "$TERRAFORM_INSTALL_PREFIX" 2>/dev/null || true
|
|
|
|
sh_c="sh_c"
|
|
if [ ! -w "$TERRAFORM_INSTALL_PREFIX" ]; then
|
|
sh_c="sudo_sh_c"
|
|
fi
|
|
# Prepare /usr/local/bin/ and the binary for copying
|
|
"$sh_c" mkdir -p "$TERRAFORM_INSTALL_PREFIX/bin"
|
|
"$sh_c" unzip -d "$CACHE_DIR" -o "$CACHE_DIR/$TERRAFORM_ZIP"
|
|
COPY_LOCATION="$TERRAFORM_INSTALL_PREFIX/bin/terraform"
|
|
|
|
# Remove the file if it already exists to
|
|
# avoid https://github.com/coder/coder/issues/2086
|
|
if [ -f "$COPY_LOCATION" ]; then
|
|
"$sh_c" rm "$COPY_LOCATION"
|
|
fi
|
|
|
|
# Copy the binary to the correct location.
|
|
"$sh_c" cp "$CACHE_DIR/terraform" "$COPY_LOCATION"
|
|
}
|
|
|
|
# fetch_terraform_sha256sums prints provisioner/terraform/terraform_SHA256SUMS
|
|
# from the Coder repository: HashiCorp's checksum file for the Terraform version
|
|
# bundled with Coder. It reads the file at the tag of the Coder release being
|
|
# installed, and falls back to main only if that tag doesn't have the file
|
|
# (releases from before it existed). Fully qualified refs make sure a branch
|
|
# named like the tag, or a tag named main, can't be read instead.
|
|
fetch_terraform_sha256sums() {
|
|
sums_path=provisioner/terraform/terraform_SHA256SUMS
|
|
sums_response=$(curl -sSL -w "\n%{http_code}" "https://raw.githubusercontent.com/coder/coder/refs/tags/v${VERSION}/${sums_path}") || return 1
|
|
sums_status=$(echo "$sums_response" | tail -n1)
|
|
if [ "$sums_status" = 200 ]; then
|
|
echo "$sums_response" | sed '$d'
|
|
return 0
|
|
fi
|
|
if [ "$sums_status" != 404 ]; then
|
|
echoerr "Downloading the Terraform checksums for Coder v${VERSION} returned status code: ${sums_status}"
|
|
return 1
|
|
fi
|
|
echoh "Coder v${VERSION} doesn't pin Terraform checksums; using the ones from main." >&2
|
|
curl -fsSL "https://raw.githubusercontent.com/coder/coder/refs/heads/main/${sums_path}"
|
|
}
|
|
|
|
# sha256_file prints the SHA256 checksum of a file.
|
|
sha256_file() {
|
|
if command_exists sha256sum; then
|
|
sha256sum "$1" | awk '{ print $1 }'
|
|
elif command_exists shasum; then
|
|
shasum -a 256 "$1" | awk '{ print $1 }'
|
|
elif command_exists sha256; then
|
|
sha256 -q "$1"
|
|
else
|
|
echoerr "This script needs sha256sum, shasum or sha256 to verify downloads."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# verify_sha256 exits if a file doesn't match the expected SHA256 checksum.
|
|
# The file is removed so a rerun downloads it again instead of reusing it.
|
|
verify_sha256() {
|
|
actual=$(sha256_file "$1")
|
|
if [ "$actual" != "$2" ]; then
|
|
rm -f "$1"
|
|
echoh
|
|
echoerr "SHA256 checksum mismatch for $1"
|
|
echoerr " expected: $2"
|
|
echoerr " actual: $actual"
|
|
exit 1
|
|
fi
|
|
echoh "+ Verified SHA256 checksum of $1"
|
|
}
|
|
|
|
install_macos() {
|
|
# If there is no `brew` binary available, just default to installing standalone
|
|
if command_exists brew; then
|
|
echoh "Installing coder with Homebrew from the coder/coder tap."
|
|
echoh
|
|
|
|
sh_c brew install coder/coder/coder
|
|
|
|
echo_brew_postinstall
|
|
return
|
|
fi
|
|
|
|
echoh "Homebrew is not available."
|
|
echoh "Falling back to standalone installation."
|
|
install_standalone
|
|
}
|
|
|
|
install_deb() {
|
|
echoh "Installing v$VERSION of the $ARCH deb package from GitHub."
|
|
echoh
|
|
|
|
fetch "https://github.com/coder/coder/releases/download/v$VERSION/coder_${VERSION}_${OS}_${ARCH}.deb" \
|
|
"$CACHE_DIR/coder_${VERSION}_$ARCH.deb"
|
|
sudo_sh_c dpkg --force-confdef --force-confold -i "$CACHE_DIR/coder_${VERSION}_$ARCH.deb"
|
|
|
|
echo_systemd_postinstall deb
|
|
}
|
|
|
|
install_rpm() {
|
|
echoh "Installing v$VERSION of the $ARCH rpm package from GitHub."
|
|
echoh
|
|
|
|
fetch "https://github.com/coder/coder/releases/download/v$VERSION/coder_${VERSION}_${OS}_${ARCH}.rpm" \
|
|
"$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.rpm"
|
|
sudo_sh_c rpm -U "$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.rpm"
|
|
|
|
echo_systemd_postinstall rpm
|
|
}
|
|
|
|
install_apk() {
|
|
echoh "Installing v$VERSION of the $ARCH apk package from GitHub."
|
|
echoh
|
|
|
|
fetch "https://github.com/coder/coder/releases/download/v$VERSION/coder_${VERSION}_${OS}_${ARCH}.apk" \
|
|
"$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.apk"
|
|
sudo_sh_c apk add --allow-untrusted "$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.apk"
|
|
|
|
echo_systemd_postinstall apk
|
|
}
|
|
|
|
install_standalone() {
|
|
echoh "Installing v$VERSION of the $ARCH release from GitHub."
|
|
echoh
|
|
|
|
# macOS releases are packaged as .zip
|
|
case $OS in
|
|
darwin) STANDALONE_ARCHIVE_FORMAT=zip ;;
|
|
*) STANDALONE_ARCHIVE_FORMAT=tar.gz ;;
|
|
esac
|
|
fetch "https://github.com/coder/coder/releases/download/v$VERSION/coder_${VERSION}_${OS}_${ARCH}.$STANDALONE_ARCHIVE_FORMAT" \
|
|
"$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.$STANDALONE_ARCHIVE_FORMAT"
|
|
|
|
# -w only works if the directory exists so try creating it first. If this
|
|
# fails we can ignore the error as the -w check will then swap us to sudo.
|
|
sh_c mkdir -p "$STANDALONE_INSTALL_PREFIX" 2>/dev/null || true
|
|
|
|
sh_c mkdir -p "$CACHE_DIR/tmp"
|
|
if [ "$STANDALONE_ARCHIVE_FORMAT" = tar.gz ]; then
|
|
sh_c tar -C "$CACHE_DIR/tmp" -xzf "$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.tar.gz"
|
|
else
|
|
sh_c unzip -d "$CACHE_DIR/tmp" -o "$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.zip"
|
|
fi
|
|
|
|
STANDALONE_BINARY_LOCATION="$STANDALONE_INSTALL_PREFIX/bin/$STANDALONE_BINARY_NAME"
|
|
|
|
sh_c="sh_c"
|
|
if [ ! -w "$STANDALONE_INSTALL_PREFIX" ]; then
|
|
sh_c="sudo_sh_c"
|
|
fi
|
|
|
|
"$sh_c" mkdir -p "$STANDALONE_INSTALL_PREFIX/bin"
|
|
|
|
# Remove the file if it already exists to
|
|
# avoid https://github.com/coder/coder/issues/2086
|
|
if [ -f "$STANDALONE_BINARY_LOCATION" ]; then
|
|
"$sh_c" rm "$STANDALONE_BINARY_LOCATION"
|
|
fi
|
|
|
|
# Copy the binary to the correct location.
|
|
"$sh_c" cp "$CACHE_DIR/tmp/coder" "$STANDALONE_BINARY_LOCATION"
|
|
|
|
# Clean up the extracted files (note, not using sudo: $sh_c -> sh_c).
|
|
sh_c rm -rv "$CACHE_DIR/tmp"
|
|
|
|
echo_standalone_postinstall
|
|
}
|
|
|
|
# Determine if we have standalone releases on GitHub for the system's arch.
|
|
has_standalone() {
|
|
case $ARCH in
|
|
amd64) return 0 ;;
|
|
arm64) return 0 ;;
|
|
armv7)
|
|
[ "$(distro)" != darwin ]
|
|
return
|
|
;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
os() {
|
|
uname="$(uname)"
|
|
case $uname in
|
|
Linux) echo linux ;;
|
|
Darwin) echo darwin ;;
|
|
FreeBSD) echo freebsd ;;
|
|
*) echo "$uname" ;;
|
|
esac
|
|
}
|
|
|
|
# Print the detected Linux distro, otherwise print the OS name.
|
|
#
|
|
# Example outputs:
|
|
# - darwin -> darwin
|
|
# - freebsd -> freebsd
|
|
# - ubuntu, raspbian, debian ... -> debian
|
|
# - amzn, centos, rhel, fedora, ... -> fedora
|
|
# - opensuse-{leap,tumbleweed} -> opensuse
|
|
# - alpine -> alpine
|
|
# - arch -> arch
|
|
#
|
|
# Inspired by https://github.com/docker/docker-install/blob/26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c/install.sh#L111-L120.
|
|
distro() {
|
|
if [ "$OS" = "darwin" ] || [ "$OS" = "freebsd" ]; then
|
|
echo "$OS"
|
|
return
|
|
fi
|
|
|
|
if [ -f /etc/os-release ]; then
|
|
(
|
|
# shellcheck disable=SC1091
|
|
. /etc/os-release
|
|
if [ "${ID_LIKE-}" ]; then
|
|
for id_like in $ID_LIKE; do
|
|
case "$id_like" in debian | fedora | opensuse)
|
|
echo "$id_like"
|
|
return
|
|
;;
|
|
esac
|
|
done
|
|
fi
|
|
|
|
echo "$ID"
|
|
)
|
|
return
|
|
fi
|
|
}
|
|
|
|
# Print a human-readable name for the OS/distro.
|
|
distro_name() {
|
|
if [ "$(uname)" = "Darwin" ]; then
|
|
echo "macOS v$(sw_vers -productVersion)"
|
|
return
|
|
fi
|
|
|
|
if [ -f /etc/os-release ]; then
|
|
(
|
|
# shellcheck disable=SC1091
|
|
. /etc/os-release
|
|
echo "$PRETTY_NAME"
|
|
)
|
|
return
|
|
fi
|
|
|
|
# Prints something like: Linux 4.19.0-9-amd64
|
|
uname -sr
|
|
}
|
|
|
|
arch() {
|
|
uname_m=$(uname -m)
|
|
case $uname_m in
|
|
aarch64) echo arm64 ;;
|
|
x86_64) echo amd64 ;;
|
|
armv7l) echo armv7 ;;
|
|
*) echo "$uname_m" ;;
|
|
esac
|
|
}
|
|
|
|
# The following is to change the naming, that way people with armv7 won't receive a error
|
|
# List of binaries can be found here: https://releases.hashicorp.com/terraform/
|
|
terraform_arch() {
|
|
uname_m=$(uname -m)
|
|
case $uname_m in
|
|
aarch64) echo arm64 ;;
|
|
x86_64) echo amd64 ;;
|
|
armv7l) echo arm ;;
|
|
*) echo "$uname_m" ;;
|
|
esac
|
|
}
|
|
|
|
command_exists() {
|
|
if [ ! "$1" ]; then return 1; fi
|
|
command -v "$@" >/dev/null
|
|
}
|
|
|
|
# sh_c prints and runs a command. The arguments are run as-is, never re-parsed
|
|
# by a shell, so a path or version can't turn into extra shell commands.
|
|
sh_c() {
|
|
echoh "+ $*"
|
|
if [ ! "${DRY_RUN-}" ]; then
|
|
"$@"
|
|
fi
|
|
}
|
|
|
|
# sudo_sh_c runs a command as root with sh_c.
|
|
sudo_sh_c() {
|
|
if [ "$(id -u)" = 0 ]; then
|
|
sh_c "$@"
|
|
elif command_exists sudo; then
|
|
sh_c sudo "$@"
|
|
elif command_exists doas; then
|
|
sh_c doas "$@"
|
|
elif command_exists su; then
|
|
# su only takes a command string, so quote every argument for its shell.
|
|
sh_c su - -c "$(quote_args "$@")"
|
|
else
|
|
echoh
|
|
echoerr "This script needs to run the following command as root."
|
|
echoerr " $*"
|
|
echoerr "Please install sudo, su, or doas."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# quote_args prints its arguments as one shell command line, with every
|
|
# argument single-quoted so the shell runs it verbatim.
|
|
quote_args() {
|
|
quoted_args=
|
|
for quote_arg in "$@"; do
|
|
# Turn each ' into '\'' (end the quote, add a literal ', reopen it). The x
|
|
# keeps $(...) from dropping trailing newlines.
|
|
quote_arg=$(printf '%sx' "$quote_arg" | sed "s/'/'\\\\''/g")
|
|
quoted_args="$quoted_args '${quote_arg%x}'"
|
|
done
|
|
printf '%s\n' "${quoted_args# }"
|
|
}
|
|
|
|
# is_version reports whether $1 is a MAJOR.MINOR.PATCH version. With "rc" as
|
|
# $2, a Coder release candidate (MAJOR.MINOR.PATCH-rc.N) is accepted too.
|
|
# Versions end up in URLs, file names and commands, so nothing else is allowed.
|
|
is_version() {
|
|
case $1 in
|
|
"" | *[!0-9a-z.-]*) return 1 ;;
|
|
esac
|
|
if [ "${2-}" = rc ]; then
|
|
printf '%s\n' "$1" | grep -Eq '^[0-9]+[.][0-9]+[.][0-9]+(-rc[.][0-9]+)?$'
|
|
else
|
|
printf '%s\n' "$1" | grep -Eq '^[0-9]+[.][0-9]+[.][0-9]+$'
|
|
fi
|
|
}
|
|
|
|
echo_cache_dir() {
|
|
if [ "${XDG_CACHE_HOME-}" ]; then
|
|
echo "$XDG_CACHE_HOME/coder"
|
|
elif [ "${HOME-}" ]; then
|
|
echo "$HOME/.cache/coder"
|
|
else
|
|
echo "/tmp/coder-cache"
|
|
fi
|
|
}
|
|
|
|
echoh() {
|
|
echo "$@" | humanpath
|
|
}
|
|
|
|
cath() {
|
|
humanpath
|
|
}
|
|
|
|
echoerr() {
|
|
echoh "$@" >&2
|
|
}
|
|
|
|
# humanpath replaces all occurrences of " $HOME" with " ~"
|
|
# and all occurrences of '"$HOME' with the literal '"$HOME'.
|
|
humanpath() {
|
|
sed "s# $HOME# ~#g; s#\"$HOME#\"\$HOME#g"
|
|
}
|
|
|
|
# We need to make sure we exit with a non zero exit if the command fails.
|
|
# /bin/sh does not support -o pipefail unfortunately.
|
|
prefix() {
|
|
PREFIX="$1"
|
|
shift
|
|
fifo="$(mktemp -d)/fifo"
|
|
mkfifo "$fifo"
|
|
sed -e "s#^#$PREFIX: #" "$fifo" &
|
|
"$@" >"$fifo" 2>&1
|
|
}
|
|
|
|
main "$@"
|