···11+#!/usr/bin/env bash
22+33+: "${BINARY_NAME:="egctl"}"
44+: "${EGCTL_INSTALL_DIR:="/usr/local/bin"}"
55+66+export VERSION
77+88+HAS_CURL="$(type "curl" &> /dev/null && echo true || echo false)"
99+HAS_WGET="$(type "wget" &> /dev/null && echo true || echo false)"
1010+HAS_GIT="$(type "git" &> /dev/null && echo true || echo false)"
1111+1212+# initArch discovers the architecture for this system.
1313+initArch() {
1414+ ARCH=$(uname -m)
1515+ case $ARCH in
1616+ armv5*) ARCH="armv5";;
1717+ armv6*) ARCH="armv6";;
1818+ armv7*) ARCH="arm";;
1919+ aarch64) ARCH="arm64";;
2020+ x86) ARCH="386";;
2121+ x86_64) ARCH="amd64";;
2222+ i686) ARCH="386";;
2323+ i386) ARCH="386";;
2424+ esac
2525+}
2626+2727+# initOS discovers the operating system for this system.
2828+initOS() {
2929+ OS="$(uname|tr '[:upper:]' '[:lower:]')"
3030+3131+ case "$OS" in
3232+ # Minimalist GNU for Windows
3333+ mingw*|cygwin*) OS='windows';;
3434+ esac
3535+}
3636+3737+# runs the given command as root (detects if we are root already)
3838+runAsRoot() {
3939+ if [ $EUID -ne 0 ]; then
4040+ sudo "${@}"
4141+ else
4242+ "${@}"
4343+ fi
4444+}
4545+4646+# verifySupported checks that the os/arch combination is supported for
4747+# binary builds, as well whether or not necessary tools are present.
4848+verifySupported() {
4949+ local supported="darwin-amd64\ndarwin-arm64\nlinux-amd64\nlinux-arm64\n"
5050+ if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
5151+ echo "No prebuilt binary for ${OS}-${ARCH}."
5252+ echo "To build from source, go to https://github.com/envoyproxy/gateway"
5353+ exit 1
5454+ fi
5555+5656+ if [ "${HAS_CURL}" != "true" ] && [ "${HAS_WGET}" != "true" ]; then
5757+ echo "Either curl or wget is required"
5858+ exit 1
5959+ fi
6060+6161+ if [ "${HAS_GIT}" != "true" ]; then
6262+ echo "[WARNING] Could not find git. It is required for plugin installation."
6363+ fi
6464+}
6565+6666+# checkDesiredVersion checks if the desired version is available.
6767+checkDesiredVersion() {
6868+ if [ "$VERSION" == "" ]; then
6969+ # Get tag from release URL
7070+ local latest_release_url="https://github.com/envoyproxy/gateway/releases"
7171+ if [ "${HAS_CURL}" == "true" ]; then
7272+ VERSION=$(curl -Ls $latest_release_url | grep 'href="/envoyproxy/gateway/releases/tag/v[0-9]*.[0-9]*.[0-9]*\"' | sed -E 's/.*\/envoyproxy\/gateway\/releases\/tag\/(v[0-9\.]+)".*/\1/g' | head -1)
7373+ elif [ "${HAS_WGET}" == "true" ]; then
7474+ VERSION=$(wget $latest_release_url -O - 2>&1 | grep 'href="/envoyproxy/gateway/releases/tag/v[0-9]*.[0-9]*.[0-9]*\"' | sed -E 's/.*\/envoyproxy\/gateway\/releases\/tag\/(v[0-9\.]+)".*/\1/g' | head -1)
7575+ fi
7676+ fi
7777+}
7878+7979+# checkEGCTLInstalledVersion checks which version of egctl is installed and
8080+# if it needs to be changed.
8181+checkEGCTLInstalledVersion() {
8282+ if [[ -f "${EGCTL_INSTALL_DIR}/${BINARY_NAME}" ]]; then
8383+ version=$("${EGCTL_INSTALL_DIR}/${BINARY_NAME}" version --remote=false | grep -Eo "v[0-9]+\.[0-9]+.*" )
8484+ if [[ "$version" == "$VERSION" ]]; then
8585+ echo "egctl ${version} is already ${VERSION:-latest}"
8686+ return 0
8787+ else
8888+ echo "egctl ${VERSION} is available. Changing from version ${version}."
8989+ return 1
9090+ fi
9191+ else
9292+ return 1
9393+ fi
9494+}
9595+9696+# downloadFile downloads the latest binary package
9797+# for that binary.
9898+downloadFile() {
9999+ EGCTL_DIST="egctl_${VERSION}_${OS}_${ARCH}.tar.gz"
100100+ DOWNLOAD_URL="https://github.com/envoyproxy/gateway/releases/download/$VERSION/$EGCTL_DIST"
101101+ EGCTL_TMP_ROOT="$(mktemp -dt egctl-installer-XXXXXX)"
102102+ EGCTL_TMP_FILE="$EGCTL_TMP_ROOT/$EGCTL_DIST"
103103+ echo "Downloading $DOWNLOAD_URL"
104104+ if [ "${HAS_CURL}" == "true" ]; then
105105+ curl -SsL "$DOWNLOAD_URL" -o "$EGCTL_TMP_FILE"
106106+ elif [ "${HAS_WGET}" == "true" ]; then
107107+ wget -q -O "$EGCTL_TMP_FILE" "$DOWNLOAD_URL"
108108+ fi
109109+}
110110+111111+# installFile installs the egctl binary.
112112+installFile() {
113113+ EGCTL_TMP="$EGCTL_TMP_ROOT/$BINARY_NAME"
114114+ mkdir -p "$EGCTL_TMP"
115115+ tar xf "$EGCTL_TMP_FILE" -C "$EGCTL_TMP"
116116+ EGCTL_TMP_BIN="$EGCTL_TMP/bin/$OS/$ARCH/egctl"
117117+ echo "Preparing to install $BINARY_NAME into ${EGCTL_INSTALL_DIR}"
118118+ runAsRoot cp "$EGCTL_TMP_BIN" "$EGCTL_INSTALL_DIR/$BINARY_NAME"
119119+ echo "$BINARY_NAME installed into $EGCTL_INSTALL_DIR/$BINARY_NAME"
120120+}
121121+122122+# fail_trap is executed if an error occurs.
123123+fail_trap() {
124124+ result=$?
125125+ if [ "$result" != "0" ]; then
126126+ if [[ -n "$INPUT_ARGUMENTS" ]]; then
127127+ echo "Failed to install $BINARY_NAME with the arguments provided: $INPUT_ARGUMENTS"
128128+ else
129129+ echo "Failed to install $BINARY_NAME"
130130+ fi
131131+ echo -e "\tFor support, go to https://github.com/envoyproxy/gateway."
132132+ fi
133133+ cleanup
134134+ exit $result
135135+}
136136+137137+# testVersion tests the installed client to make sure it is working.
138138+testVersion() {
139139+ set +e
140140+ if ! [ "$(command -v $BINARY_NAME)" ]; then
141141+ echo "$BINARY_NAME not found. Is $EGCTL_INSTALL_DIR on your PATH?"
142142+ exit 1
143143+ fi
144144+ set -e
145145+}
146146+147147+# cleanup temporary files.
148148+cleanup() {
149149+ if [[ -d "${EGCTL_TMP_ROOT:-}" ]]; then
150150+ rm -rf "$EGCTL_TMP_ROOT"
151151+ fi
152152+}
153153+154154+# Execution
155155+156156+#Stop execution on any error
157157+trap "fail_trap" EXIT
158158+set -e
159159+160160+initArch
161161+initOS
162162+verifySupported
163163+checkDesiredVersion
164164+if ! checkEGCTLInstalledVersion; then
165165+ downloadFile
166166+ installFile
167167+fi
168168+testVersion
169169+cleanup