Skip to content

Commit 768f3f9

Browse files
committed
add cases for windows to install.sh
1 parent 7e23081 commit 768f3f9

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

install.sh

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ Usage:
6464
Installs Terraform binary from https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/ source
6565
alongside coder.
6666
This is great for if you are having issues with Coder installing terraform, or if you
67-
just want it on your base system aswell.
67+
just want it on your base system as well.
6868
This supports most systems, however if you are unsure yours is supported you can check
6969
the link above.
70+
7071
--net-admin
7172
Adds \`CAP_NET_ADMIN\` to the installed binary. This allows Coder to
7273
increase network speeds, but has security implications.
@@ -157,11 +158,11 @@ Coder ${channel}release v${VERSION} installed. ${advisory}See our releases docum
157158
158159
The Coder binary has been placed in the following location:
159160
160-
$STANDALONE_INSTALL_PREFIX/bin/$STANDALONE_BINARY_NAME
161+
$STANDALONE_INSTALL_PREFIX/bin/$STANDALONE_PLATFORM_BINARY_NAME
161162
162163
EOF
163164

164-
CODER_COMMAND="$(command -v "$STANDALONE_BINARY_NAME" || true)"
165+
CODER_COMMAND="$(command -v "$STANDALONE_PLATFORM_BINARY_NAME" || true)"
165166

166167
if [ -z "${CODER_COMMAND}" ]; then
167168
cath <<EOF
@@ -176,11 +177,11 @@ EOF
176177
cath <<EOF
177178
To run a Coder server:
178179
179-
$ $STANDALONE_BINARY_NAME server
180+
$ $STANDALONE_PLATFORM_BINARY_NAME server
180181
181182
To connect to a Coder deployment:
182183
183-
$ $STANDALONE_BINARY_NAME login <deployment url>
184+
$ $STANDALONE_PLATFORM_BINARY_NAME login <deployment url>
184185
185186
EOF
186187
fi
@@ -396,6 +397,10 @@ main() {
396397
ARCH=${ARCH:-$(arch)}
397398
TERRAFORM_ARCH=${TERRAFORM_ARCH:-$(terraform_arch)}
398399

400+
# The above OS detection usually fails on Windows because the OS environment variable
401+
# is unpredictable. The is_windows function should account for this.
402+
if is_windows ; then OS="windows"; fi
403+
399404
# If we've been provided a flag which is specific to the standalone installation
400405
# method, we should "detect" standalone to be the appropriate installation method.
401406
# This check needs to occur before we set these variables with defaults.
@@ -424,7 +429,8 @@ main() {
424429
CACHE_DIR=$(echo_cache_dir)
425430
TERRAFORM_INSTALL_PREFIX=${TERRAFORM_INSTALL_PREFIX:-/usr/local}
426431
STANDALONE_INSTALL_PREFIX=${STANDALONE_INSTALL_PREFIX:-/usr/local}
427-
STANDALONE_BINARY_NAME=${STANDALONE_BINARY_NAME:-coder}
432+
STANDALONE_PLATFORM_BINARY_NAME=$( format_platform_filename "${STANDALONE_BINARY_NAME:-coder}" )
433+
428434
STABLE_VERSION=$(echo_latest_stable_version)
429435
if [ "${MAINLINE}" = 1 ]; then
430436
VERSION=$(echo_latest_mainline_version)
@@ -482,6 +488,7 @@ main() {
482488
# We don't have GitHub releases that work on Alpine or FreeBSD so we have no
483489
# choice but to use npm here.
484490
alpine) install_apk ;;
491+
windows) install_windows ;;
485492
# For anything else we'll try to install standalone but fall back to npm if
486493
# we don't have releases for the architecture.
487494
*)
@@ -496,6 +503,11 @@ main() {
496503
fi
497504
}
498505

506+
is_windows() {
507+
# shellcheck disable=SC3028
508+
[ "$OSTYPE" = "msys" ] || [ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "win32" ] || [ "$COMSPEC" != "" ]
509+
}
510+
499511
cap_net_admin() {
500512
if ! command_exists setcap && command_exists capsh; then
501513
echo "Package 'libcap' not found. See install instructions for your distro: https://command-not-found.com/setcap"
@@ -570,7 +582,7 @@ with_terraform() {
570582
echoerr "Please install unzip to use this function"
571583
exit 1
572584
fi
573-
echoh "Installing Terraform version $TERRAFORM_VERSION $TERRAFORM_ARCH from the HashiCorp release repository."
585+
echoh "Installing Terraform version $TERRAFORM_VERSION for $OS/$TERRAFORM_ARCH from the HashiCorp release repository."
574586
echoh
575587

576588
# Download from official source and save it to cache
@@ -586,7 +598,8 @@ with_terraform() {
586598
# Prepare /usr/local/bin/ and the binary for copying
587599
"$sh_c" mkdir -p "$TERRAFORM_INSTALL_PREFIX/bin"
588600
"$sh_c" unzip -d "$CACHE_DIR" -o "$CACHE_DIR/terraform_${TERRAFORM_VERSION}_${OS}_${ARCH}.zip"
589-
COPY_LOCATION="$TERRAFORM_INSTALL_PREFIX/bin/terraform"
601+
TERRAFORM_PLATFORM_BINARY_NAME=$( format_platform_filename "terraform" )
602+
COPY_LOCATION="$TERRAFORM_INSTALL_PREFIX/bin/$TERRAFORM_PLATFORM_BINARY_NAME"
590603

591604
# Remove the file if it already exists to
592605
# avoid https://github.com/coder/coder/issues/2086
@@ -595,9 +608,21 @@ with_terraform() {
595608
fi
596609

597610
# Copy the binary to the correct location.
598-
"$sh_c" cp "$CACHE_DIR/terraform" "$COPY_LOCATION"
611+
"$sh_c" cp "$CACHE_DIR/${TERRAFORM_PLATFORM_BINARY_NAME}" "$COPY_LOCATION"
612+
}
613+
614+
format_platform_filename() {
615+
IN_FILENAME="$1"
616+
PLATFORM_SUFFIX=""
617+
if is_windows ; then PLATFORM_SUFFIX=".exe"; fi
618+
printf "%s%s" "$IN_FILENAME" "$PLATFORM_SUFFIX"
619+
}
620+
621+
install_windows() {
622+
install_standalone
599623
}
600624

625+
601626
install_macos() {
602627
# If there is no `brew` binary available, just default to installing standalone
603628
if command_exists brew; then
@@ -649,12 +674,12 @@ install_apk() {
649674
}
650675

651676
install_standalone() {
652-
echoh "Installing v$VERSION of the $ARCH release from GitHub."
677+
echoh "Installing v$VERSION of the $OS/$ARCH release from GitHub."
653678
echoh
654679

655680
# macOS releases are packaged as .zip
656681
case $OS in
657-
darwin) STANDALONE_ARCHIVE_FORMAT=zip ;;
682+
darwin | windows) STANDALONE_ARCHIVE_FORMAT=zip ;;
658683
*) STANDALONE_ARCHIVE_FORMAT=tar.gz ;;
659684
esac
660685

@@ -672,7 +697,7 @@ install_standalone() {
672697
sh_c unzip -d "$CACHE_DIR/tmp" -o "$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.zip"
673698
fi
674699

675-
STANDALONE_BINARY_LOCATION="$STANDALONE_INSTALL_PREFIX/bin/$STANDALONE_BINARY_NAME"
700+
STANDALONE_PLATFORM_BINARY_LOCATION="$STANDALONE_INSTALL_PREFIX/bin/$STANDALONE_PLATFORM_BINARY_NAME"
676701

677702
sh_c="sh_c"
678703
if [ ! -w "$STANDALONE_INSTALL_PREFIX" ]; then
@@ -683,12 +708,14 @@ install_standalone() {
683708

684709
# Remove the file if it already exists to
685710
# avoid https://github.com/coder/coder/issues/2086
686-
if [ -f "$STANDALONE_BINARY_LOCATION" ]; then
687-
"$sh_c" rm "$STANDALONE_BINARY_LOCATION"
711+
if [ -f "$STANDALONE_PLATFORM_BINARY_LOCATION" ]; then
712+
"$sh_c" rm "$STANDALONE_PLATFORM_BINARY_LOCATION"
688713
fi
689714

715+
SOURCE_PLATFORM_BINARY_NAME=$( format_platform_filename "coder" )
716+
690717
# Copy the binary to the correct location.
691-
"$sh_c" cp "$CACHE_DIR/tmp/coder" "$STANDALONE_BINARY_LOCATION"
718+
"$sh_c" cp "$CACHE_DIR/tmp/$SOURCE_PLATFORM_BINARY_NAME" "$STANDALONE_PLATFORM_BINARY_LOCATION"
692719

693720
# Clean up the extracted files (note, not using sudo: $sh_c -> sh_c).
694721
sh_c rm -rv "$CACHE_DIR/tmp"
@@ -710,6 +737,7 @@ has_standalone() {
710737
}
711738

712739
os() {
740+
if is_windows; then echo windows; return; fi
713741
uname="$(uname)"
714742
case $uname in
715743
Linux) echo linux ;;
@@ -732,10 +760,12 @@ os() {
732760
#
733761
# Inspired by https://github.com/docker/docker-install/blob/26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c/install.sh#L111-L120.
734762
distro() {
735-
if [ "$OS" = "darwin" ] || [ "$OS" = "freebsd" ]; then
763+
case "$OS" in
764+
darwin | freebsd | windows)
736765
echo "$OS"
737766
return
738-
fi
767+
;;
768+
esac
739769

740770
if [ -f /etc/os-release ]; then
741771
(
@@ -764,6 +794,11 @@ distro_name() {
764794
return
765795
fi
766796

797+
if is_windows; then
798+
echo "Windows"
799+
return
800+
fi
801+
767802
if [ -f /etc/os-release ]; then
768803
(
769804
# shellcheck disable=SC1091

0 commit comments

Comments
 (0)