Last active
March 14, 2022 18:41
-
-
Save ilyam8/4e66e7339ec7b670a6383385c06eba29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
NETDATA_CONF_URL="http://127.0.0.1:19999/netdata.conf" | |
DASHBOARD_RELEASE_TAG="${1}" | |
DASHBOARD_LATEST_RELEASE_URL="https://api.github.com/repos/netdata/dashboard/releases/latest" | |
DASHBOARD_DOWNLOAD_URL="https://github.com/netdata/dashboard/releases/download/TAG/dashboard.tar.gz" | |
DASHBOARD_FILE="dashboard.tar.gz" | |
RCol='\e[0m' | |
BRed='\e[1;31m' | |
BGre='\e[1;32m' | |
BYel='\e[1;33m' | |
info() { | |
printf "%b[info]%b %s\n" "$BGre" "$RCol" "${@}" | |
} | |
warn() { | |
printf "%b[warn]%b %s\n" "$BYel" "$RCol" "${@}" | |
} | |
fatal() { | |
printf "%b[fatal]%b %s\n" "$BRed" "$RCol" "${@}" | |
exit 1 | |
} | |
info "using Netdata REST API to fetch web files configuration" | |
netdata_conf=$(curl -sS --max-time 5 "$NETDATA_CONF_URL") | |
[ -z "$netdata_conf" ] && fatal "failed to fetch web files configuration from $NETDATA_CONF_URL" | |
WEB_DIR=$(echo "$netdata_conf" | grep -m 1 "web files directory" | awk '{print $NF}' ORS='') | |
WEB_OWNER=$(echo "$netdata_conf" | grep -m 1 "web files owner" | awk '{print $NF}' ORS='') | |
WEB_GROUP=$(echo "$netdata_conf" | grep -m 1 "web files group" | awk '{print $NF}' ORS='') | |
[ -z "$WEB_DIR" ] && fatal "failed to extract 'web files directory'" | |
if [ -z "$DASHBOARD_RELEASE_TAG" ]; then | |
warn "using GitHub API to fetch dashboard latest tag" | |
warn "for unauthenticated requests, the rate limit allows for up to 60 requests per hour" | |
warn "provide a specific tag if you bump into the limit (see https://github.com/netdata/dashboard/releases)" | |
if ! DASHBOARD_RELEASE_TAG=$(curl -sS --max-time 5 $DASHBOARD_LATEST_RELEASE_URL | grep "tag_name" | cut -d'"' -f4) || | |
[ -z "$DASHBOARD_RELEASE_TAG" ]; then | |
fatal "failed to fetch dashboard release tag from ${DASHBOARD_LATEST_RELEASE_URL}" | |
fi | |
fi | |
DASHBOARD_DOWNLOAD_URL=${DASHBOARD_DOWNLOAD_URL//"TAG"/$DASHBOARD_RELEASE_TAG} | |
info "you are about to:" | |
info " - download dashboard ${DASHBOARD_RELEASE_TAG} (${DASHBOARD_DOWNLOAD_URL})" | |
info " - extract it to the ${WEB_DIR}" | |
[ -n "$WEB_OWNER" ] && [ -n "$WEB_GROUP" ] && info " - change ${WEB_DIR} files ownership to ${WEB_OWNER}:${WEB_GROUP}" | |
info "procced? (press CTRL-C to exit)" | |
read -rn 1 | |
if [[ $(id -u) -ne 0 ]]; then | |
fatal "please run as root" | |
fi | |
DASHBOARD_FILE="dashboard.tar.gz" | |
[ -f "$DASHBOARD_FILE" ] && rm "$DASHBOARD_FILE" | |
if ! curl -sSfLJ --max-time 30 -o "$DASHBOARD_FILE" "$DASHBOARD_DOWNLOAD_URL"; then | |
fatal "failed to download dashboard from $DASHBOARD_DOWNLOAD_URL" | |
fi | |
if ! tar -xf "$DASHBOARD_FILE" --strip-components=1 -C "${WEB_DIR}/"; then | |
fatal "failed to extract dashboard.tar.gz into ${WEB_DIR}" | |
fi | |
[ -f "$DASHBOARD_FILE" ] && rm "$DASHBOARD_FILE" | |
if [ -n "$WEB_OWNER" ] && [ -n "$WEB_GROUP" ]; then | |
if ! chown -R "${WEB_OWNER}:${WEB_GROUP}" "${WEB_DIR}/"; then | |
fatal "failed to chown ${WEB_DIR}/ content" | |
fi | |
fi | |
info "dashboard ${DASHBOARD_RELEASE_TAG} is successfully installed!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment