Skip to content

Commit d1a5c67

Browse files
committed
refactor: improve flag interpretation for install.sh (#9554)
1 parent 869d040 commit d1a5c67

File tree

2 files changed

+159
-27
lines changed

2 files changed

+159
-27
lines changed

docs/install/install.sh.md

+81
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,87 @@ manually via `coder server` or as a system package.
2727
By default, the Coder server runs on `http://127.0.0.1:3000` and uses a
2828
[public tunnel](../admin/configure.md#tunnel) for workspace connections.
2929

30+
## `PATH` conflicts
31+
32+
It's possible to end up in situations where you have multiple `coder` binaries
33+
in your `PATH`, and your system may use a version that you don't intend. Your
34+
`PATH` is a variable that tells your shell where to look for programs to run.
35+
36+
You can check where all of the versions are by running `which -a coder`.
37+
38+
For example, a common conflict on macOS might be between a version installed by
39+
Homebrew, and a version installed manually to the /usr/local/bin directory.
40+
41+
```console
42+
$ which -a coder
43+
/usr/local/bin/coder
44+
/opt/homebrew/bin/coder
45+
```
46+
47+
Whichever binary comes first in this list will be used when running `coder`
48+
commands.
49+
50+
### Reordering your `PATH`
51+
52+
If you use bash or zsh, you can update your `PATH` like this:
53+
54+
```shell
55+
# You might want to add this line to the end of your ~/.bashrc or ~/.zshrc file!
56+
export PATH="/opt/homebrew/bin:$PATH"
57+
```
58+
59+
If you use fish, you can update your `PATH` like this:
60+
61+
```shell
62+
# You might want to add this line to the end of your ~/.config/fish/config.fish file!
63+
fish_add_path "/opt/homebrew/bin"
64+
```
65+
66+
> ℹ If you ran install.sh with a `--prefix` flag, you can replace
67+
> `/opt/homebrew` with whatever value you used there. Make sure to leave the
68+
> `/bin` at the end!
69+
70+
Now we can observe that the order has changed:
71+
72+
```console
73+
$ which -a coder
74+
/opt/homebrew/bin/coder
75+
/usr/local/bin/coder
76+
```
77+
78+
### Removing unneeded binaries
79+
80+
If you want to uninstall a version of `coder` that you installed with a package
81+
manager, you can run whichever one of these commands applies:
82+
83+
```shell
84+
# On macOS, with Homebrew installed
85+
brew uninstall coder
86+
```
87+
88+
```shell
89+
# On Debian/Ubuntu based systems
90+
sudo dpkg -r coder
91+
```
92+
93+
```shell
94+
# On Fedora/RHEL-like systems
95+
sudo rpm -e coder
96+
```
97+
98+
```shell
99+
# On Alpine
100+
sudo apk del coder
101+
```
102+
103+
If the conflicting binary is not installed by your system package manager, you
104+
can just delete it.
105+
106+
```shell
107+
# You might not need `sudo`, depending on the location
108+
sudo rm /usr/local/bin/coder
109+
```
110+
30111
## Next steps
31112

32113
- [Configuring Coder](../admin/configure.md)

install.sh

+78-27
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,29 @@ Standalone release has been installed into $STANDALONE_INSTALL_PREFIX/bin/$STAND
104104
105105
EOF
106106

107-
if [ "$STANDALONE_INSTALL_PREFIX" != /usr/local ]; then
107+
CODER_COMMAND="$(command -v "$STANDALONE_BINARY_NAME")"
108+
109+
if [ ! "$CODER_COMMAND" ]; then
108110
cath <<EOF
109111
Extend your path to use Coder:
110-
PATH="$STANDALONE_INSTALL_PREFIX/bin:\$PATH"
112+
113+
$ PATH="$STANDALONE_INSTALL_PREFIX/bin:\$PATH"
111114
112115
EOF
113-
fi
114-
cath <<EOF
115-
Run Coder:
116-
$STANDALONE_BINARY_NAME server
116+
elif [ "$CODER_COMMAND" != "$STANDALONE_BINARY_LOCATION" ]; then
117+
echo_path_conflict "$CODER_COMMAND" "$STANDALONE_INSTALL_PREFIX"
118+
else
119+
cath <<EOF
120+
To run a Coder server:
121+
122+
$ $STANDALONE_BINARY_NAME server
123+
124+
To connect to a Coder deployment:
125+
126+
$ $STANDALONE_BINARY_NAME login <deployment url>
117127
118128
EOF
129+
fi
119130
}
120131

121132
echo_brew_postinstall() {
@@ -124,8 +135,15 @@ echo_brew_postinstall() {
124135
return
125136
fi
126137

138+
CODER_COMMAND="$(command -v "coder")"
139+
BREW_PREFIX="$(brew --prefix)"
140+
141+
if [ "$CODER_COMMAND" != "$BREW_PREFIX/bin/coder" ]; then
142+
echo_path_conflict "$CODER_COMMAND" "$BREW_PREFIX"
143+
fi
144+
127145
cath <<EOF
128-
brew formula has been installed.
146+
Homebrew formula has been installed.
129147
130148
To run a Coder server:
131149
@@ -157,7 +175,6 @@ To run a Coder server:
157175
# Or just run the server directly
158176
$ coder server
159177
160-
Default URL: http://127.0.0.1:3000
161178
Configuring Coder: https://coder.com/docs/v2/latest/admin/configure
162179
163180
To connect to a Coder deployment:
@@ -169,28 +186,45 @@ EOF
169186

170187
echo_dryrun_postinstall() {
171188
cath <<EOF
172-
173189
Dry-run complete.
174190
175191
To install Coder, re-run this script without the --dry-run flag.
192+
193+
EOF
194+
}
195+
196+
echo_path_conflict() {
197+
cath <<EOF
198+
There is another binary in your PATH that conflicts with the binary we've installed.
199+
200+
$1
201+
202+
This is likely because of an existing installation of Coder. See our documentation for suggests on how to resolve this.
203+
204+
https://coder.com/docs/v2/latest/install/install.sh#path-conflicts
205+
176206
EOF
177207
}
178208

179209
main() {
180210
TERRAFORM_VERSION="1.3.4"
211+
181212
if [ "${TRACE-}" ]; then
182213
set -x
183214
fi
215+
184216
unset \
185217
DRY_RUN \
186218
METHOD \
187219
OPTIONAL \
188220
ALL_FLAGS \
189221
RSH_ARGS \
190222
EDGE \
191-
RSH
223+
RSH \
224+
WITH_TERRAFORM
192225

193226
ALL_FLAGS=""
227+
194228
while [ "$#" -gt 0 ]; do
195229
case "$1" in
196230
-*)
@@ -245,7 +279,7 @@ main() {
245279
exit 0
246280
;;
247281
--with-terraform)
248-
METHOD=with_terraform
282+
WITH_TERRAFORM=1
249283
;;
250284
--)
251285
shift
@@ -275,8 +309,29 @@ main() {
275309
return
276310
fi
277311

312+
# These can be overridden for testing but shouldn't normally be used as it can
313+
# result in a broken coder.
314+
OS=${OS:-$(os)}
315+
ARCH=${ARCH:-$(arch)}
316+
TERRAFORM_ARCH=${TERRAFORM_ARCH:-$(terraform_arch)}
317+
318+
# We can't reasonably support installing specific versions of Coder through
319+
# Homebrew, so if we're on macOS and the `--version` flag was set, we should
320+
# "detect" standalone to be the appropriate installation method. This check
321+
# needs to occur before we set `VERSION` to a default of the latest release.
322+
if [ "$OS" = "darwin" ] && [ "${VERSION-}" ]; then
323+
METHOD=standalone
324+
fi
325+
326+
# If we've been provided a flag which is specific to the standalone installation
327+
# method, we should "detect" standalone to be the appropriate installation method.
328+
# This check needs to occur before we set these variables with defaults.
329+
if [ "${STANDALONE_INSTALL_PREFIX-}" ] || [ "${STANDALONE_BINARY_NAME-}" ]; then
330+
METHOD=standalone
331+
fi
332+
278333
METHOD="${METHOD-detect}"
279-
if [ "$METHOD" != detect ] && [ "$METHOD" != with_terraform ] && [ "$METHOD" != standalone ]; then
334+
if [ "$METHOD" != detect ] && [ "$METHOD" != standalone ]; then
280335
echoerr "Unknown install method \"$METHOD\""
281336
echoerr "Run with --help to see usage."
282337
exit 1
@@ -285,15 +340,10 @@ main() {
285340
# These are used by the various install_* functions that make use of GitHub
286341
# releases in order to download and unpack the right release.
287342
CACHE_DIR=$(echo_cache_dir)
288-
STANDALONE_INSTALL_PREFIX=${STANDALONE_INSTALL_PREFIX:-/usr/local}
289343
TERRAFORM_INSTALL_PREFIX=${TERRAFORM_INSTALL_PREFIX:-/usr/local}
344+
STANDALONE_INSTALL_PREFIX=${STANDALONE_INSTALL_PREFIX:-/usr/local}
290345
STANDALONE_BINARY_NAME=${STANDALONE_BINARY_NAME:-coder}
291346
VERSION=${VERSION:-$(echo_latest_version)}
292-
# These can be overridden for testing but shouldn't normally be used as it can
293-
# result in a broken coder.
294-
OS=${OS:-$(os)}
295-
ARCH=${ARCH:-$(arch)}
296-
TERRAFORM_ARCH=${TERRAFORM_ARCH:-$(terraform_arch)}
297347

298348
distro_name
299349

@@ -302,6 +352,11 @@ main() {
302352
echoh
303353
fi
304354

355+
# Start by installing Terraform, if requested
356+
if [ "${WITH_TERRAFORM-}" = 1 ]; then
357+
with_terraform
358+
fi
359+
305360
# Standalone installs by pulling pre-built releases from GitHub.
306361
if [ "$METHOD" = standalone ]; then
307362
if has_standalone; then
@@ -313,10 +368,6 @@ main() {
313368
exit 1
314369
fi
315370
fi
316-
if [ "$METHOD" = with_terraform ]; then
317-
# Install terraform then continue the script
318-
with_terraform
319-
fi
320371

321372
# DISTRO can be overridden for testing but shouldn't normally be used as it
322373
# can result in a broken coder.
@@ -429,7 +480,7 @@ with_terraform() {
429480
install_macos() {
430481
# If there is no `brew` binary available, just default to installing standalone
431482
if command_exists brew; then
432-
echoh "Installing v$VERSION of the coder formula from coder/coder."
483+
echoh "Installing coder with Homebrew from the coder/coder tap."
433484
echoh
434485

435486
sh_c brew install coder/coder/coder
@@ -505,16 +556,16 @@ install_standalone() {
505556
"$sh_c" unzip -d "$CACHE_DIR" -o "$CACHE_DIR/coder_${VERSION}_${OS}_${ARCH}.zip"
506557
fi
507558

508-
COPY_LOCATION="$STANDALONE_INSTALL_PREFIX/bin/$STANDALONE_BINARY_NAME"
559+
STANDALONE_BINARY_LOCATION="$STANDALONE_INSTALL_PREFIX/bin/$STANDALONE_BINARY_NAME"
509560

510561
# Remove the file if it already exists to
511562
# avoid https://github.com/coder/coder/issues/2086
512-
if [ -f "$COPY_LOCATION" ]; then
513-
"$sh_c" rm "$COPY_LOCATION"
563+
if [ -f "$STANDALONE_BINARY_LOCATION" ]; then
564+
"$sh_c" rm "$STANDALONE_BINARY_LOCATION"
514565
fi
515566

516567
# Copy the binary to the correct location.
517-
"$sh_c" cp "$CACHE_DIR/coder" "$COPY_LOCATION"
568+
"$sh_c" cp "$CACHE_DIR/coder" "$STANDALONE_BINARY_LOCATION"
518569

519570
echo_standalone_postinstall
520571
}

0 commit comments

Comments
 (0)