Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b602f5

Browse files
authored
feat: Added --with-terraform argument to install coder and terraform together (#5586)
* - Added a `--install-terraform` argument - Added a unzip command check to the standalone function - Cleaner error and help redirect the user to a solution - Added help info for `--install-terraform` argument - Fixed standalone install typo (ard64 -> arm64) * - Corrected formatting errors, and renamed functions * - Fixed typos - Added recommend changes for consistency * Removed unzip check in standalone function * Fixed styling * Moved the TERRAFORM_VERSION Var up
1 parent 763147e commit 9b602f5

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

install.sh

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Usage:
4545
Sets the prefix used by standalone release archives. Defaults to /usr/local
4646
and the binary is copied into /usr/local/bin
4747
To install in \$HOME, pass ---prefix=\$HOME/.local
48-
48+
4949
--binary-name <name>
5050
Sets the name for the CLI in standalone release archives. Defaults to "coder"
5151
To use the CLI as coder2, pass --binary-name=coder2
@@ -54,6 +54,15 @@ Usage:
5454
--rsh <bin>
5555
Specifies the remote shell for remote installation. Defaults to ssh.
5656
57+
--with-terraform
58+
Installs Terraform binary from https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/ source
59+
alongside coder.
60+
This is great for if you are having issues with Coder installing terraform, or if you
61+
just want it on your base system aswell.
62+
This supports most systems, however if you are unsure yours is supported you can check
63+
the link above.
64+
65+
5766
The detection method works as follows:
5867
- Debian, Ubuntu, Raspbian: install the deb package from GitHub.
5968
- Fedora, CentOS, RHEL, openSUSE: install the rpm package from GitHub.
@@ -145,10 +154,10 @@ EOF
145154
}
146155

147156
main() {
157+
TERRAFORM_VERSION="1.3.4"
148158
if [ "${TRACE-}" ]; then
149159
set -x
150160
fi
151-
152161
unset \
153162
DRY_RUN \
154163
METHOD \
@@ -212,6 +221,9 @@ main() {
212221
usage
213222
exit 0
214223
;;
224+
--with-terraform)
225+
METHOD=with_terraform
226+
;;
215227
--)
216228
shift
217229
# We remove the -- added above.
@@ -241,7 +253,7 @@ main() {
241253
fi
242254

243255
METHOD="${METHOD-detect}"
244-
if [ "$METHOD" != detect ] && [ "$METHOD" != standalone ]; then
256+
if [ "$METHOD" != detect ] && [ "$METHOD" != with_terraform ] && [ "$METHOD" != standalone ]; then
245257
echoerr "Unknown install method \"$METHOD\""
246258
echoerr "Run with --help to see usage."
247259
exit 1
@@ -251,12 +263,14 @@ main() {
251263
# releases in order to download and unpack the right release.
252264
CACHE_DIR=$(echo_cache_dir)
253265
STANDALONE_INSTALL_PREFIX=${STANDALONE_INSTALL_PREFIX:-/usr/local}
266+
TERRAFORM_INSTALL_PREFIX=${TERRAFORM_INSTALL_PREFIX:-/usr/local}
254267
STANDALONE_BINARY_NAME=${STANDALONE_BINARY_NAME:-coder}
255268
VERSION=${VERSION:-$(echo_latest_version)}
256269
# These can be overridden for testing but shouldn't normally be used as it can
257270
# result in a broken coder.
258271
OS=${OS:-$(os)}
259272
ARCH=${ARCH:-$(arch)}
273+
TERRAFORM_ARCH=${TERRAFORM_ARCH:-$(terraform_arch)}
260274

261275
distro_name
262276

@@ -276,6 +290,10 @@ main() {
276290
exit 1
277291
fi
278292
fi
293+
if [ "$METHOD" = with_terraform ]; then
294+
# Install terraform then continue the script
295+
with_terraform
296+
fi
279297

280298
# DISTRO can be overridden for testing but shouldn't normally be used as it
281299
# can result in a broken coder.
@@ -351,6 +369,42 @@ fetch() {
351369
sh_c mv "$FILE.incomplete" "$FILE"
352370
}
353371

372+
with_terraform() {
373+
# Check if the unzip package is installed. If not error peacefully.
374+
if ! (command_exists unzip); then
375+
echoh
376+
echoerr "This script needs the unzip package to run."
377+
echoerr "Please install unzip to use this function"
378+
exit 1
379+
fi
380+
echoh "Installing Terraform version $TERRAFORM_VERSION $TERRAFORM_ARCH from the HashiCorp release repository."
381+
echoh
382+
383+
# Download from official source and save it to cache
384+
fetch "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_${OS}_${TERRAFORM_ARCH}.zip" \
385+
"$CACHE_DIR/terraform_${TERRAFORM_VERSION}_${OS}_${TERRAFORM_ARCH}.zip"
386+
387+
sh_c mkdir -p "$TERRAFORM_INSTALL_PREFIX" 2>/dev/null || true
388+
389+
sh_c="sh_c"
390+
if [ ! -w "$TERRAFORM_INSTALL_PREFIX" ]; then
391+
sh_c="sudo_sh_c"
392+
fi
393+
# Prepare /usr/local/bin/ and the binary for copying
394+
"$sh_c" mkdir -p "$TERRAFORM_INSTALL_PREFIX/bin"
395+
"$sh_c" unzip -d "$CACHE_DIR" -o "$CACHE_DIR/terraform_${TERRAFORM_VERSION}_${OS}_${ARCH}.zip"
396+
COPY_LOCATION="$TERRAFORM_INSTALL_PREFIX/bin/terraform"
397+
398+
# Remove the file if it already exists to
399+
# avoid https://github.com/coder/coder/issues/2086
400+
if [ -f "$COPY_LOCATION" ]; then
401+
"$sh_c" rm "$COPY_LOCATION"
402+
fi
403+
404+
# Copy the binary to the correct location.
405+
"$sh_c" cp "$CACHE_DIR/terraform" "$COPY_LOCATION"
406+
}
407+
354408
install_deb() {
355409
echoh "Installing v$VERSION of the $ARCH deb package from GitHub."
356410
echoh
@@ -431,7 +485,7 @@ install_standalone() {
431485
has_standalone() {
432486
case $ARCH in
433487
amd64) return 0 ;;
434-
ard64) return 0 ;;
488+
arm64) return 0 ;;
435489
armv7)
436490
[ "$(distro)" != darwin ]
437491
return
@@ -516,6 +570,18 @@ arch() {
516570
esac
517571
}
518572

573+
# The following is to change the naming, that way people with armv7 won't receive a error
574+
# List of binaries can be found here: https://releases.hashicorp.com/terraform/
575+
terraform_arch() {
576+
uname_m=$(uname -m)
577+
case $uname_m in
578+
aarch64) echo arm64 ;;
579+
x86_64) echo amd64 ;;
580+
armv7l) echo arm ;;
581+
*) echo "$uname_m" ;;
582+
esac
583+
}
584+
519585
command_exists() {
520586
if [ ! "$1" ]; then return 1; fi
521587
command -v "$@" >/dev/null

0 commit comments

Comments
 (0)