linkerd2/bin/web

136 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
set -o errexit -o nounset -o pipefail
ROOT=$( cd "${BASH_SOURCE[0]%/*}"/.. && pwd )
DEV_PORT=8080
export NODE_ENV=${NODE_ENV:=development}
function -h {
cat <<USAGE
USAGE: web <command>
* build - build the assets
* dev - run a dev server (with live reloading). Options:
-p $DEV_PORT
* integration (cloud|local) - run the integration tests
Note: Sauce Connect tunnel must be open for cloud tests
* port-forward - setup a tunnel to a controller component and port
Example: port-forward controller 8085
* run - run a local server (sans. reloading)
* setup - get the environment setup for development
Note: any command line options are passed on to yarn
* test - run the unit tests
Note: any command line options are passed on to the test runner (jest)
USAGE
}; function --help { -h ;}
check-for-linkerd() {
controller_pod=$(get-pod controller)
grafana_pod=$(get-pod grafana)
if [[ -z "${controller_pod// }" ]]; then
err 'Controller is not running. Have you installed Linkerd?'
exit 1
fi
if [[ -z "${grafana_pod// }" ]]; then
err 'Controller is not running. Have you installed Linkerd?'
exit 1
fi
}
dev() {
while getopts "p:" opt; do
case "$opt" in
p) DEV_PORT=$OPTARG
esac
done
cd "$ROOT"/web/app && yarn webpack-dev-server --port $DEV_PORT &
run
}
build() {
cd "$ROOT"/web/app
yarn webpack
}
get-pod() {
if [ $# -ne 1 ]; then
echo "usage: bin/${0##*/} get-pod component-name" >&2
exit 1
fi
kubectl --namespace=linkerd get po \
--selector=linkerd.io/control-plane-component=$1 \
--field-selector='status.phase==Running' \
-o jsonpath='{.items[*].metadata.name}'
}
port-forward() {
if [ $# -ne 2 ]; then
echo "usage: bin/${0##*/} port-forward component-name port-number" >&2
exit 1
fi
nc -z localhost "$2" || \
kubectl --namespace=linkerd port-forward $(get-pod $1) "$2:$2"
}
run() {
# Stop everything in the process group (in the background) whenever the
# parent process experiences an error and exits.
trap 'exit' INT TERM
trap 'kill 0' EXIT
build
check-for-linkerd && (
port-forward controller 8085 &
port-forward grafana 3000 &
)
cd "$ROOT"/web && \
../bin/go-run . --addr=:7777 $*
}
setup() {
cd "$ROOT"/web/app
yarn $*
}
function test {
cd "$ROOT"/web/app
yarn jest "$*"
}
integration() {
if [[ $# -ne 1 || ($1 != "cloud" && $1 != "local") ]]; then
echo "usage: bin/${0##*/} integration (cloud|local)" >&2
exit 1
fi
if [ "$1" = cloud ]; then
cd "$ROOT"/web/app && \
./node_modules/.bin/wdio ./integration/wdio-sauce.conf.js
else
cd "$ROOT"/web/app && \
./node_modules/.bin/wdio ./integration/wdio.conf.js
fi
}
main() {
setup
build
}
msg() { out "$*" >&2 ;}
err() { local x=$? ; msg "$*" ; return $(( x == 0 ? 1 : x )) ;}
out() { printf '%s\n' "$*" ;}
if [[ ${1:-} ]] && declare -F | cut -d' ' -f3 | grep -Fqx -- "${1:-}"
then "$@"
else main "$@"
fi