Skip to content

Commit 5a4c18e

Browse files
authored
Install under .getmesh/bin and update profile accordingly in install.sh (#57)
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent dded900 commit 5a4c18e

File tree

2 files changed

+114
-38
lines changed

2 files changed

+114
-38
lines changed

e2e/e2e_test.go

+15-30
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"fmt"
2121
"io/ioutil"
2222
"log"
23-
"net/http"
24-
"net/http/httptest"
2523
"os"
2624
"os/exec"
2725
"os/user"
@@ -44,19 +42,6 @@ func TestMain(m *testing.M) {
4442
log.Fatal(err)
4543
}
4644

47-
// set up download shell
48-
downloadShell, err := ioutil.ReadFile("site/install.sh")
49-
if err != nil {
50-
log.Fatal(err)
51-
}
52-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
53-
_, _ = w.Write(downloadShell)
54-
}))
55-
defer ts.Close()
56-
if err := os.Setenv("GETMESH_TEST_DOWNLOAD_SHELL_URL", ts.URL); err != nil {
57-
log.Fatal(err)
58-
}
59-
6045
// set up manifest
6146
if err := os.Setenv("GETMESH_TEST_MANIFEST_PATH", "site/manifest.json"); err != nil {
6247
log.Fatal(err)
@@ -81,6 +66,21 @@ func Test_E2E(t *testing.T) {
8166
t.Run("config-validate", configValidate)
8267
}
8368

69+
func getmeshInstall(t *testing.T) {
70+
cmd := exec.Command("bash", "site/install.sh")
71+
cmd.Stdout = os.Stdout
72+
cmd.Stderr = os.Stderr
73+
require.NoError(t, cmd.Run())
74+
75+
// check directory
76+
u, err := user.Current()
77+
require.NoError(t, err)
78+
_, err = os.Stat(filepath.Join(u.HomeDir, ".getmesh", "bin", "getmesh"))
79+
require.NoError(t, err)
80+
_, err = os.Stat(filepath.Join(u.HomeDir, ".getmesh", "istio"))
81+
require.NoError(t, err)
82+
}
83+
8484
func securityPatchChecker(t *testing.T) {
8585
m := &api.Manifest{
8686
IstioDistributions: []*api.IstioDistribution{
@@ -112,21 +112,6 @@ func securityPatchChecker(t *testing.T) {
112112
require.Contains(t, buf.String(), `[WARNING] The locally installed minor version 1.9-tetrate has a latest version 1.9.1000000000000-tetrate-v0 including security patches. We strongly recommend you to download 1.9.1000000000000-tetrate-v0 by "getmesh fetch".`)
113113
}
114114

115-
func getmeshInstall(t *testing.T) {
116-
cmd := exec.Command("bash", "site/install.sh")
117-
cmd.Stdout = os.Stdout
118-
cmd.Stderr = os.Stderr
119-
require.NoError(t, cmd.Run())
120-
121-
// check directory
122-
u, err := user.Current()
123-
require.NoError(t, err)
124-
_, err = os.Stat(filepath.Join("bin/getmesh"))
125-
require.NoError(t, err)
126-
_, err = os.Stat(filepath.Join(u.HomeDir, ".getmesh", "istio"))
127-
require.NoError(t, err)
128-
}
129-
130115
func enfOfLife(t *testing.T) {
131116
h, err := util.GetmeshHomeDir()
132117
require.NoError(t, err)

site/install.sh

+99-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ usage() {
2323
$this: download go binaries for tetratelabs/getmesh
2424
2525
Usage: $this [-b] bindir [-d] [tag]
26-
-b sets bindir or installation directory, Defaults to ./bin
26+
-b sets bindir or installation directory, Defaults to {HOME}/.getmesh/bin
2727
-d turns on debug logging
2828
[tag] is a tag from
2929
https://github.com/tetratelabs/getmesh/releases
@@ -37,10 +37,10 @@ EOF
3737
}
3838

3939
parse_args() {
40-
#BINDIR is ./bin unless set be ENV
40+
#BINDIR is ${HOME}/.getmesh/bin unless set be ENV
4141
# over-ridden by flag below
4242

43-
BINDIR=${BINDIR:-./bin}
43+
BINDIR=${BINDIR:-.getmesh/bin}
4444
while getopts "b:dh?x" arg; do
4545
case "$arg" in
4646
b) BINDIR="$OPTARG" ;;
@@ -64,13 +64,13 @@ execute() {
6464
hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
6565
srcdir="${tmpdir}"
6666
(cd "${tmpdir}" && untar "${TARBALL}")
67-
test ! -d "${BINDIR}" && install -d "${BINDIR}"
67+
test ! -d "${HOME}/${BINDIR}" && install -d "${HOME}/${BINDIR}"
6868
for binexe in $BINARIES; do
6969
if [ "$OS" = "windows" ]; then
7070
binexe="${binexe}.exe"
7171
fi
72-
install "${srcdir}/${binexe}" "${BINDIR}/"
73-
log_info "installed $(pwd)/${BINDIR}/${binexe}"
72+
install "${srcdir}/${binexe}" "${HOME}/${BINDIR}/"
73+
log_info "installed ${HOME}/${BINDIR}/${binexe}"
7474
done
7575
rm -rf "${tmpdir}"
7676
}
@@ -125,6 +125,9 @@ EOF
125125
is_command() {
126126
command -v "$1" >/dev/null
127127
}
128+
echo_fexists() {
129+
[ -f "$1" ] && echo "$1"
130+
}
128131
echoerr() {
129132
echo "$@" 1>&2
130133
}
@@ -383,8 +386,96 @@ TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
383386
CHECKSUM=getmesh_${VERSION}_checksums.txt
384387
CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
385388

386-
389+
# Install
387390
execute
388391

389392
# Aa a sanity check, install the latest default Istio.
390-
$(pwd)/bin/getmesh fetch
393+
${HOME}/.getmesh/bin/getmesh fetch >/dev/null
394+
395+
# Updating profile - originally copied from https://wasmtime.dev/install.sh with some modifications
396+
detect_profile() {
397+
local shellname="$1"
398+
local uname="$2"
399+
400+
if [ -f "$PROFILE" ]; then
401+
echo "$PROFILE"
402+
return
403+
fi
404+
405+
# try to detect the current shell
406+
case "$shellname" in
407+
bash)
408+
# based on Ubuntu 20.04 tests - the sequence of the profiles processing
409+
# is the same for both Linux and Mac - .bash_profile first and then
410+
# bashrc, also confirmed here:
411+
# https://askubuntu.com/questions/161249/bashrc-not-executed-when-opening-new-terminal
412+
echo_fexists "$HOME/.bash_profile" || echo_fexists "$HOME/.bashrc"
413+
;;
414+
zsh)
415+
echo "$HOME/.zshrc"
416+
;;
417+
fish)
418+
echo "$HOME/.config/fish/config.fish"
419+
;;
420+
*)
421+
# Fall back to checking for profile file existence. Once again, the order
422+
# differs between macOS and everything else.
423+
local profiles
424+
425+
profiles=( .profile .bash_profile .bashrc .zshrc .config/fish/config.fish )
426+
;;
427+
*)
428+
429+
for profile in "${profiles[@]}"; do
430+
echo_fexists "$HOME/$profile" && break
431+
done
432+
;;
433+
esac
434+
}
435+
436+
# generate shell code to source the loading script and modify the path for the input profile
437+
build_path_str() {
438+
local profile="$1"
439+
local profile_install_dir="$2"
440+
441+
if [[ $profile =~ \.fish$ ]]; then
442+
# fish uses a little different syntax to modify the PATH
443+
cat <<END_FISH_SCRIPT
444+
set -gx GETMESH_HOME "$profile_install_dir"
445+
string match -r ".getistio" "\$PATH" > /dev/null; or set -gx PATH "\$GETMESH_HOME/bin" \$PATH
446+
END_FISH_SCRIPT
447+
else
448+
# bash and zsh
449+
cat <<END_BASH_SCRIPT
450+
export GETMESH_HOME="$profile_install_dir"
451+
export PATH="\$GETMESH_HOME/bin:\$PATH"
452+
END_BASH_SCRIPT
453+
fi
454+
}
455+
456+
update_profile() {
457+
local install_dir="$1"
458+
459+
local profile_install_dir=$(echo "$install_dir" | sed "s:^$HOME:\$HOME:")
460+
local detected_profile="$(detect_profile $(basename "/$SHELL") $(uname -s) )"
461+
local path_str="$(build_path_str "$detected_profile" "$profile_install_dir")"
462+
463+
if [ -z "${detected_profile-}" ] ; then
464+
log_err "no user profile found."
465+
log_err "tried \$PROFILE ($PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, ~/.profile, and ~/.config/fish/config.fish."
466+
log_err ''
467+
log_err "you can either create one of these and try again or add these lines to the appropriate file:"
468+
printf "\n$path_str\n"
469+
return 1
470+
else
471+
if ! command grep -qc 'GETMESH_HOME' "$detected_profile"; then
472+
log_info "updating user profile ($detected_profile)..."
473+
log_info "the following two lines are added into your profile ($detected_profile):"
474+
printf "\n$path_str\n"
475+
command printf "$path_str" >> "$detected_profile"
476+
printf "\nFinished installation. Open a new terminal to start using getmesh!\n"
477+
fi
478+
fi
479+
}
480+
481+
update_profile ${HOME}/.getmesh

0 commit comments

Comments
 (0)