From 41f22b9a79b8854c58a4a15475270f1682391ae5 Mon Sep 17 00:00:00 2001 From: Young Bu Park Date: Sat, 12 Oct 2019 20:19:36 -0700 Subject: [PATCH] Initial commit of macos and linux install script (#146) * Initial commit of linux installation script * sudo checking * add more helper functions * add licence * Add usage * use single commandline for macos and linux * Add more logs --- install/README.md | 12 +-- install/install.ps1 | 0 install/install.sh | 182 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+), 9 deletions(-) mode change 100644 => 100755 install/install.ps1 create mode 100755 install/install.sh diff --git a/install/README.md b/install/README.md index 5deb9f06..c0de5436 100644 --- a/install/README.md +++ b/install/README.md @@ -1,19 +1,13 @@ # Dapr CLI Installer -## Install Dapr CLI - -### Windows +## Windows ``` powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex" ``` -Note: Until the repo is public, please use the below command. +## MacOS/Linux ``` -powershell -Command "$Env:GITHUB_USER='your_github_id'; $Env:GITHUB_TOKEN='your_github_pat_token'; iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1?token=AC2QIRDUTKTPEPOE5W7HOH25VJ72W | iex" +curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash ``` - -### Linux/MacOS - -WIP. diff --git a/install/install.ps1 b/install/install.ps1 old mode 100644 new mode 100755 diff --git a/install/install.sh b/install/install.sh new file mode 100755 index 00000000..44242b05 --- /dev/null +++ b/install/install.sh @@ -0,0 +1,182 @@ +#!/usr/bin/env bash + +# ------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------------------------------ + +# Dapr CLI location +: ${DAPR_INSTALL_DIR:="/usr/local/bin"} + +# sudo is required to copy binary to DAPR_INSTALL_DIR for linux +: ${USE_SUDO:="false"} + +# Http request CLI +DAPR_HTTP_REQUEST_CLI=curl + +# GitHub Organization and repo name to download release +GITHUB_ORG=dapr +GITHUB_REPO=cli + +# Dapr CLI filename +DAPR_CLI_FILENAME=dapr + +DAPR_CLI_FILE="${DAPR_INSTALL_DIR}/${DAPR_CLI_FILENAME}" + +getSystemInfo() { + ARCH=$(uname -m) + case $ARCH in + armv7*) ARCH="arm";; + aarch64) ARCH="arm64";; + x86_64) ARCH="amd64";; + esac + + OS=$(echo `uname`|tr '[:upper:]' '[:lower:]') + + # Most linux distro needs root permission to copy the file to /usr/local/bin + if [ "$OS" == "linux" ] && [ "$DAPR_INSTALL_DIR" == "/usr/local/bin" ]; then + USE_SUDO="true" + fi +} + +verifySupported() { + local supported=(darwin-amd64 linux-amd64 linux-arm linux-arm64) + local current_osarch="${OS}-${ARCH}" + + for osarch in "${supported[@]}"; do + if [ "$osarch" == "$current_osarch" ]; then + echo "Your system is ${OS}_${ARCH}" + return + fi + done + + echo "No prebuilt binary for ${current_osarch}" + exit 1 +} + +runAsRoot() { + local CMD="$*" + + if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then + CMD="sudo $CMD" + fi + + $CMD +} + +checkHttpRequestCLI() { + if type "curl" > /dev/null; then + DAPR_HTTP_REQUEST_CLI=curl + elif type "wget" > /dev/null; then + DAPR_HTTP_REQUEST_CLI=wget + else + echo "Either curl or wget is required" + exit 1 + fi +} + +checkExistingDapr() { + if [ -f "$DAPR_CLI_FILE" ]; then + echo -e "\nDapr CLI is detected:" + $DAPR_CLI_FILE --version + echo -e "Reinstalling Dapr CLI - ${DAPR_CLI_FILE}...\n" + else + echo -e "Installing Dapr CLI...\n" + fi +} + +getLatestRelease() { + local daprReleaseUrl="https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/releases" + local latest_release="" + + if [ "$DAPR_HTTP_REQUEST_CLI" == "curl" ]; then + latest_release=$(curl -s $daprReleaseUrl | grep \"tag_name\" | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p') + else + latest_release=$(wget -q --header="Accept: application/json" -O - $daprReleaseUrl | grep \"tag_name\" | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p') + fi + + ret_val=$latest_release +} + +downloadFile() { + LATEST_RELEASE_TAG=$1 + + DAPR_CLI_ARTIFACT="${DAPR_CLI_FILENAME}_${OS}_${ARCH}.tar.gz" + DOWNLOAD_BASE="https://github.com/${GITHUB_ORG}/${GITHUB_REPO}/releases/download" + DOWNLOAD_URL="${DOWNLOAD_BASE}/${LATEST_RELEASE_TAG}/${DAPR_CLI_ARTIFACT}" + + # Create the temp directory + DAPR_TMP_ROOT=$(mktemp -dt dapr-install-XXXXXX) + ARTIFACT_TMP_FILE="$DAPR_TMP_ROOT/$DAPR_CLI_ARTIFACT" + + echo "Downloading $DOWNLOAD_URL ..." + if [ "$DAPR_HTTP_REQUEST_CLI" == "curl" ]; then + curl -SsL "$DOWNLOAD_URL" -o "$ARTIFACT_TMP_FILE" + else + wget -q -O "$ARTIFACT_TMP_FILE" "$DOWNLOAD_URL" + fi + + if [ ! -f "$ARTIFACT_TMP_FILE" ]; then + echo "failed to download $DOWNLOAD_URL ..." + exit 1 + fi +} + +installFile() { + tar xf "$ARTIFACT_TMP_FILE" -C "$DAPR_TMP_ROOT" + local tmp_root_dapr_cli="$DAPR_TMP_ROOT/$DAPR_CLI_FILENAME" + + if [ ! -f "$tmp_root_dapr_cli" ]; then + echo "Failed to unpack Dapr cli executable." + exit 1 + fi + + chmod o+x $tmp_root_dapr_cli + runAsRoot cp "$tmp_root_dapr_cli" "$DAPR_INSTALL_DIR" + + if [ -f "$DAPR_CLI_FILE" ]; then + echo "$DAPR_CLI_FILENAME installed into $DAPR_INSTALL_DIR successfully." + + $DAPR_CLI_FILE --version + else + echo "Failed to install $DAPR_CLI_FILENAME" + exit 1 + fi +} + +fail_trap() { + result=$? + if [ "$result" != "0" ]; then + echo "Failed to install Dapr CLI" + echo "For support, go to https://dapr.io" + fi + cleanup + exit $result +} + +cleanup() { + if [[ -d "${DAPR_TMP_ROOT:-}" ]]; then + rm -rf "$DAPR_TMP_ROOT" + fi +} + +installCompleted() { + echo -e "\nTo get started with Dapr, please visit https://github.com/dapr/docs/tree/master/getting-started" +} + +# ----------------------------------------------------------------------------- +# main +# ----------------------------------------------------------------------------- +trap "fail_trap" EXIT + +getSystemInfo +verifySupported +checkExistingDapr +checkHttpRequestCLI + +getLatestRelease +downloadFile $ret_val +installFile +cleanup + +installCompleted