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

Skip to content

Commit 82d49db

Browse files
author
Admin
committed
- 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)
1 parent 4e14cc5 commit 82d49db

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

install.sh

Lines changed: 77 additions & 3 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+
--install-terraform
58+
Installs Terraform binary from https://releases.hashicorp.com/terraform/1.3.4/ 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.
@@ -212,6 +221,9 @@ main() {
212221
usage
213222
exit 0
214223
;;
224+
--install-terraform)
225+
METHOD=install_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" != install_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+
TERRA_ARCH=${TERRA_ARCH:-$(terra_arch)}
260274

261275
distro_name
262276

@@ -276,6 +290,10 @@ main() {
276290
exit 1
277291
fi
278292
fi
293+
if [ "$METHOD" = install_terraform ]; then
294+
# Install terraform then contine the script
295+
install_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,43 @@ fetch() {
351369
sh_c mv "$FILE.incomplete" "$FILE"
352370
}
353371

372+
install_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+
381+
echoh "Installing Terraform version 1.3.4 $TERRA_ARCH package from Hashicorp Source."
382+
echoh
383+
384+
# Download from offical source and save it to cache
385+
fetch "https://releases.hashicorp.com/terraform/1.3.4/terraform_1.3.4_${OS}_${TERRA_ARCH}.zip" \
386+
"$CACHE_DIR/terraform_1.3.4_${OS}_${TERRA_ARCH}.zip"
387+
388+
sh_c mkdir -p "$TERRAFORM_INSTALL_PREFIX" 2>/dev/null || true
389+
390+
sh_c="sh_c"
391+
if [ ! -w "$TERRAFORM_INSTALL_PREFIX" ]; then
392+
sh_c="sudo_sh_c"
393+
fi
394+
# Prepare /usr/local/bin/ and the binary for copying
395+
"$sh_c" mkdir -p "$TERRAFORM_INSTALL_PREFIX/bin"
396+
"$sh_c" unzip -d "$CACHE_DIR" -o "$CACHE_DIR/terraform_1.3.4_${OS}_${ARCH}.zip"
397+
COPY_LOCATION="$TERRAFORM_INSTALL_PREFIX/bin/terraform"
398+
399+
# Remove the file if it already exists to
400+
# avoid https://github.com/coder/coder/issues/2086
401+
if [ -f "$COPY_LOCATION" ]; then
402+
"$sh_c" rm "$COPY_LOCATION"
403+
fi
404+
405+
# Copy the binary to the correct location.
406+
"$sh_c" cp "$CACHE_DIR/terraform" "$COPY_LOCATION"
407+
}
408+
354409
install_deb() {
355410
echoh "Installing v$VERSION of the $ARCH deb package from GitHub."
356411
echoh
@@ -385,6 +440,13 @@ install_apk() {
385440
}
386441

387442
install_standalone() {
443+
# Check if the unzip package is installed. If not error peacefully.
444+
if !(command_exists unzip); then
445+
echoh
446+
echoerr "This script needs the unzip package to run."
447+
echoerr "Please install unzip to use this function"
448+
exit 1
449+
fi
388450
echoh "Installing v$VERSION of the $ARCH release from GitHub."
389451
echoh
390452

@@ -431,7 +493,7 @@ install_standalone() {
431493
has_standalone() {
432494
case $ARCH in
433495
amd64) return 0 ;;
434-
ard64) return 0 ;;
496+
arm64) return 0 ;;
435497
armv7)
436498
[ "$(distro)" != darwin ]
437499
return
@@ -516,6 +578,18 @@ arch() {
516578
esac
517579
}
518580

581+
# The following is to change the naming, that way people with armv7 won't recieve a error
582+
# List of binaries can be found here: https://releases.hashicorp.com/terraform/1.3.4/
583+
terra_arch() {
584+
uname_m=$(uname -m)
585+
case $uname_m in
586+
aarch64) echo arm64 ;;
587+
x86_64) echo amd64 ;;
588+
armv7l) echo arm ;;
589+
*) echo "$uname_m" ;;
590+
esac
591+
}
592+
519593
command_exists() {
520594
if [ ! "$1" ]; then return 1; fi
521595
command -v "$@" >/dev/null

0 commit comments

Comments
 (0)