this repo has no description
1#!/usr/bin/env bash
2
3: "${BINARY_NAME:="egctl"}"
4: "${EGCTL_INSTALL_DIR:="/usr/local/bin"}"
5
6export VERSION
7
8HAS_CURL="$(type "curl" &> /dev/null && echo true || echo false)"
9HAS_WGET="$(type "wget" &> /dev/null && echo true || echo false)"
10HAS_GIT="$(type "git" &> /dev/null && echo true || echo false)"
11
12# initArch discovers the architecture for this system.
13initArch() {
14 ARCH=$(uname -m)
15 case $ARCH in
16 armv5*) ARCH="armv5";;
17 armv6*) ARCH="armv6";;
18 armv7*) ARCH="arm";;
19 aarch64) ARCH="arm64";;
20 x86) ARCH="386";;
21 x86_64) ARCH="amd64";;
22 i686) ARCH="386";;
23 i386) ARCH="386";;
24 esac
25}
26
27# initOS discovers the operating system for this system.
28initOS() {
29 OS="$(uname|tr '[:upper:]' '[:lower:]')"
30
31 case "$OS" in
32 # Minimalist GNU for Windows
33 mingw*|cygwin*) OS='windows';;
34 esac
35}
36
37# runs the given command as root (detects if we are root already)
38runAsRoot() {
39 if [ $EUID -ne 0 ]; then
40 sudo "${@}"
41 else
42 "${@}"
43 fi
44}
45
46# verifySupported checks that the os/arch combination is supported for
47# binary builds, as well whether or not necessary tools are present.
48verifySupported() {
49 local supported="darwin-amd64\ndarwin-arm64\nlinux-amd64\nlinux-arm64\n"
50 if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
51 echo "No prebuilt binary for ${OS}-${ARCH}."
52 echo "To build from source, go to https://github.com/envoyproxy/gateway"
53 exit 1
54 fi
55
56 if [ "${HAS_CURL}" != "true" ] && [ "${HAS_WGET}" != "true" ]; then
57 echo "Either curl or wget is required"
58 exit 1
59 fi
60
61 if [ "${HAS_GIT}" != "true" ]; then
62 echo "[WARNING] Could not find git. It is required for plugin installation."
63 fi
64}
65
66# checkDesiredVersion checks if the desired version is available.
67checkDesiredVersion() {
68 if [ "$VERSION" == "" ]; then
69 # Get tag from release URL
70 local latest_release_url="https://github.com/envoyproxy/gateway/releases"
71 if [ "${HAS_CURL}" == "true" ]; then
72 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)
73 elif [ "${HAS_WGET}" == "true" ]; then
74 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)
75 fi
76 fi
77}
78
79# checkEGCTLInstalledVersion checks which version of egctl is installed and
80# if it needs to be changed.
81checkEGCTLInstalledVersion() {
82 if [[ -f "${EGCTL_INSTALL_DIR}/${BINARY_NAME}" ]]; then
83 version=$("${EGCTL_INSTALL_DIR}/${BINARY_NAME}" version --remote=false | grep -Eo "v[0-9]+\.[0-9]+.*" )
84 if [[ "$version" == "$VERSION" ]]; then
85 echo "egctl ${version} is already ${VERSION:-latest}"
86 return 0
87 else
88 echo "egctl ${VERSION} is available. Changing from version ${version}."
89 return 1
90 fi
91 else
92 return 1
93 fi
94}
95
96# downloadFile downloads the latest binary package
97# for that binary.
98downloadFile() {
99 EGCTL_DIST="egctl_${VERSION}_${OS}_${ARCH}.tar.gz"
100 DOWNLOAD_URL="https://github.com/envoyproxy/gateway/releases/download/$VERSION/$EGCTL_DIST"
101 EGCTL_TMP_ROOT="$(mktemp -dt egctl-installer-XXXXXX)"
102 EGCTL_TMP_FILE="$EGCTL_TMP_ROOT/$EGCTL_DIST"
103 echo "Downloading $DOWNLOAD_URL"
104 if [ "${HAS_CURL}" == "true" ]; then
105 curl -SsL "$DOWNLOAD_URL" -o "$EGCTL_TMP_FILE"
106 elif [ "${HAS_WGET}" == "true" ]; then
107 wget -q -O "$EGCTL_TMP_FILE" "$DOWNLOAD_URL"
108 fi
109}
110
111# installFile installs the egctl binary.
112installFile() {
113 EGCTL_TMP="$EGCTL_TMP_ROOT/$BINARY_NAME"
114 mkdir -p "$EGCTL_TMP"
115 tar xf "$EGCTL_TMP_FILE" -C "$EGCTL_TMP"
116 EGCTL_TMP_BIN="$EGCTL_TMP/bin/$OS/$ARCH/egctl"
117 echo "Preparing to install $BINARY_NAME into ${EGCTL_INSTALL_DIR}"
118 runAsRoot cp "$EGCTL_TMP_BIN" "$EGCTL_INSTALL_DIR/$BINARY_NAME"
119 echo "$BINARY_NAME installed into $EGCTL_INSTALL_DIR/$BINARY_NAME"
120}
121
122# fail_trap is executed if an error occurs.
123fail_trap() {
124 result=$?
125 if [ "$result" != "0" ]; then
126 if [[ -n "$INPUT_ARGUMENTS" ]]; then
127 echo "Failed to install $BINARY_NAME with the arguments provided: $INPUT_ARGUMENTS"
128 else
129 echo "Failed to install $BINARY_NAME"
130 fi
131 echo -e "\tFor support, go to https://github.com/envoyproxy/gateway."
132 fi
133 cleanup
134 exit $result
135}
136
137# testVersion tests the installed client to make sure it is working.
138testVersion() {
139 set +e
140 if ! [ "$(command -v $BINARY_NAME)" ]; then
141 echo "$BINARY_NAME not found. Is $EGCTL_INSTALL_DIR on your PATH?"
142 exit 1
143 fi
144 set -e
145}
146
147# cleanup temporary files.
148cleanup() {
149 if [[ -d "${EGCTL_TMP_ROOT:-}" ]]; then
150 rm -rf "$EGCTL_TMP_ROOT"
151 fi
152}
153
154# Execution
155
156#Stop execution on any error
157trap "fail_trap" EXIT
158set -e
159
160initArch
161initOS
162verifySupported
163checkDesiredVersion
164if ! checkEGCTLInstalledVersion; then
165 downloadFile
166 installFile
167fi
168testVersion
169cleanup