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

Skip to content

Commit 4ed8dd0

Browse files
authored
refactor: improve flag interpretation for install.sh (#9554)
1 parent b15bfa4 commit 4ed8dd0

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
@@ -34,6 +34,87 @@ manually via `coder server` or as a system package.
3434
By default, the Coder server runs on `http://127.0.0.1:3000` and uses a
3535
[public tunnel](../admin/configure.md#tunnel) for workspace connections.
3636

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

39120
- [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)