From 811a0b0e9c0ad5262baafbf5b5ea4c7c311fba97 Mon Sep 17 00:00:00 2001 From: healthjyk Date: Fri, 22 Sep 2023 17:37:02 +0800 Subject: [PATCH 1/3] feat: update install.sh for MacOs&linux, and add uninstall.sh --- ...kusion.sh => deprecated-install-kusion.sh} | 0 static/scripts/deprecated-install.sh | 33 ++ static/scripts/install.sh | 363 +++++++++++++++++- static/scripts/uninstall.sh | 178 +++++++++ 4 files changed, 559 insertions(+), 15 deletions(-) rename static/scripts/{install-kusion.sh => deprecated-install-kusion.sh} (100%) create mode 100755 static/scripts/deprecated-install.sh mode change 100755 => 100644 static/scripts/install.sh create mode 100644 static/scripts/uninstall.sh diff --git a/static/scripts/install-kusion.sh b/static/scripts/deprecated-install-kusion.sh similarity index 100% rename from static/scripts/install-kusion.sh rename to static/scripts/deprecated-install-kusion.sh diff --git a/static/scripts/deprecated-install.sh b/static/scripts/deprecated-install.sh new file mode 100755 index 00000000..b658b170 --- /dev/null +++ b/static/scripts/deprecated-install.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# ------------------------------------------------------------ +# Copyright 2022 The KusionStack Authors +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Reference: https://github.com/dapr/cli/tree/master/install +# ------------------------------------------------------------ + +# KCLVM installaltion script +INSTALL_KCLVM_SCRIPT_URL="https://kcl-lang.io/script/install.sh" + +# kusion installaltion script +INSTALL_KUSION_SCRIPT_URL="https://kusionstack.io/scripts/install-kusion.sh" + +if type "curl" >/dev/null; then + curl -fsSL $INSTALL_KCLVM_SCRIPT_URL | /bin/bash + curl -fsSL $INSTALL_KUSION_SCRIPT_URL | /bin/bash +elif type "wget" >/dev/null; then + wget -q $INSTALL_KCLVM_SCRIPT_URL -O - | /bin/bash + wget -q $INSTALL_KUSION_SCRIPT_URL -O - | /bin/bash +else + warn "Either curl or wget is required" + exit 1 +fi diff --git a/static/scripts/install.sh b/static/scripts/install.sh old mode 100755 new mode 100644 index b658b170..90f7ef7b --- a/static/scripts/install.sh +++ b/static/scripts/install.sh @@ -15,19 +15,352 @@ # Reference: https://github.com/dapr/cli/tree/master/install # ------------------------------------------------------------ -# KCLVM installaltion script -INSTALL_KCLVM_SCRIPT_URL="https://kcl-lang.io/script/install.sh" - -# kusion installaltion script -INSTALL_KUSION_SCRIPT_URL="https://kusionstack.io/scripts/install-kusion.sh" - -if type "curl" >/dev/null; then - curl -fsSL $INSTALL_KCLVM_SCRIPT_URL | /bin/bash - curl -fsSL $INSTALL_KUSION_SCRIPT_URL | /bin/bash -elif type "wget" >/dev/null; then - wget -q $INSTALL_KCLVM_SCRIPT_URL -O - | /bin/bash - wget -q $INSTALL_KUSION_SCRIPT_URL -O - | /bin/bash -else - warn "Either curl or wget is required" +set -o errexit +set -o nounset +set -o pipefail + +# Sudo is required to copy binary to KUSION_HOME_DIR +USE_SUDO=${USE_SUDO:-"false"} + +# Specified profile +PROFILE=${PROFILE:-""} + +# specifed Kusion version to install +KUSION_VERSION=${1:-""} + +# Kusion location +KUSION_HOME_DIR=${KUSION_HOME_DIR:-"$HOME/.kusion"} + +# If set "true", then will not source kusion env in detected profile +SKIP_SOURCE_KUSION_ENV=${SKIP_SOURCE_KUSION_ENV:-"false"} + +# Http request CLI +KUSION_HTTP_REQUEST_CLI="curl" + +# Kusion binary file name +KUSION_CLI="kusion" + +# Kusion env file name +KUSION_ENV_FILE=".env" + +# Kusion binary path +# todo: update to "${KUSION_HOME_DIR}/bin/${KUSION_CLI}" in the future, should update .env file and kusion_tmp_cli_path +KUSION_CLI_FILE_PATH="${KUSION_HOME_DIR}/${KUSION_CLI}" + +# Kusion github basic information +GITHUB_ORG=KusionStack +GITHUB_REPO=kusion + +# Download path of Kusion installation package +DOWNLOAD_BASE="https://github.com/${GITHUB_ORG}/${GITHUB_REPO}/releases/download" + +# Kusion release URL +RELEASE_URL="https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/releases" + +info() { + command printf '\033[1;32mInfo\033[0m: %s\n' "$1" 1>&2 +} + +warn() { + command printf '\033[1;33mWarn\033[0m: %s\n' "$1" 1>&2 +} + +error() { + command printf '\033[1;31mError\033[0m: %s\n' "$1" 1>&2 +} + +runAsRoot() { + local CMD="$*" + + if [ $EUID -ne 0 ] && [ $USE_SUDO = "true" ]; then + CMD="sudo $CMD" + fi + + $CMD +} + +echoFexists() { + [ -f "$1" ] && echo "$1" +} + +getSystemInfo() { + ARCH=$(uname -m) + case $ARCH in + armv7*) ARCH="arm" ;; + aarch64) ARCH="arm64" ;; + x86_64) ARCH="amd64" ;; + esac + + OS=$(echo "$(uname)" | tr '[:upper:]' '[:lower:]') +} + +verifySupported() { + local supported=(darwin-amd64 linux-amd64 darwin-arm64) + local current_os_arch="${OS}-${ARCH}" + + for osarch in "${supported[@]}"; do + if [ "$osarch" == "$current_os_arch" ]; then + info "Your system is ${OS}_${ARCH}." + return + fi + done + + error "No prebuilt installation package for ${current_os_arch}" exit 1 -fi +} + +checkHttpRequestCLI() { + if type "curl" >/dev/null; then + KUSION_HTTP_REQUEST_CLI=curl + elif type "wget" >/dev/null; then + KUSION_HTTP_REQUEST_CLI=wget + else + error "Either curl or wget is required to download installation package." + exit 1 + fi +} + +toInstallVersion() { + if [ -z "$KUSION_VERSION" ]; then + info "Getting the latest Kusion version..." + getLatestReleaseVersion + else + echo "$KUSION_VERSION" + fi +} + +# todo: only "grep -v rc" is not correct, only va.b.c is formal release tag +getLatestReleaseVersion() { + local KusionReleaseURL="${RELEASE_URL}" + local latest_release="" + + if [ "$KUSION_HTTP_REQUEST_CLI" == "curl" ]; then + latest_release=$(curl -s $KusionReleaseURL | grep \"tag_name\" | grep -v rc | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p') + else + latest_release=$(wget -q --header="Accept: application/json" -O - $KusionReleaseURL | grep \"tag_name\" | grep -v rc | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p') + fi + + echo "${latest_release:1}" +} + +download() { + local kusion_version="$1" + local kusion_tag="v${kusion_version}" + KUSION_CLI_ARTIFACT="${KUSION_CLI}_${kusion_version}_${OS}_${ARCH}.tar.gz" + DOWNLOAD_URL="${DOWNLOAD_BASE}/${kusion_tag}/${KUSION_CLI_ARTIFACT}" + + # Create the temp directory + KUSION_TMP_ROOT=$(mktemp -dt kusion-install-XXXXXX) + ARTIFACT_TMP_FILE="$KUSION_TMP_ROOT/$KUSION_CLI_ARTIFACT" + + info "Downloading installation package from $DOWNLOAD_URL..." + if [ "$KUSION_HTTP_REQUEST_CLI" == "curl" ]; then + curl -SL "$DOWNLOAD_URL" -o "$ARTIFACT_TMP_FILE" + else + wget -O "$ARTIFACT_TMP_FILE" "$DOWNLOAD_URL" + fi + + if [ ! -f "$ARTIFACT_TMP_FILE" ]; then + error "Failed to download installation package." + return 1 + fi +} + +checkExistingKusion() { + if [ -f "$KUSION_CLI_FILE_PATH" ]; then + info "Existing kusion is detected:" + $KUSION_CLI_FILE_PATH version + info "Reinstalling kusion into $KUSION_HOME_DIR..." + else + info "Installing kusion into $KUSION_HOME_DIR..." + fi +} + +install() { + # decompress + info "Decompressing installation package $KUSION_CLI_ARTIFACT..." + tar xf "$ARTIFACT_TMP_FILE" -C "$KUSION_TMP_ROOT" + rm -f "$ARTIFACT_TMP_FILE" + kusion_tmp_cli_path="$KUSION_TMP_ROOT/$KUSION_CLI" + if [ ! -f "$kusion_tmp_cli_path" ]; then + error "Failed to decompress installation package." + return 1 + fi + + # detect profile + detected_profile=$(detectProfile "$(basename $SHELL)" "$(uname -s)") + if [ -z "${detected_profile-}" ]; then + error "No supported user profile found. Already tried \$PROFILE ($PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, ~/.profile, and ~/.config/fish/config.fish." + return 1 + fi + + # build env file + info "Building kusion env file..." + install_dir=$(echo "$KUSION_HOME_DIR" | sed "s:^$HOME:\$HOME:") + kusion_tmp_env_path="$KUSION_TMP_ROOT/$KUSION_ENV_FILE" + buildEnvFile "$detected_profile" "$install_dir" "$kusion_tmp_env_path" + + # clean kusion home + runAsRoot rm -rf "$KUSION_HOME_DIR" + if [ -d "$KUSION_HOME_DIR" ]; then + error "Failed to remove existing kusion in $KUSION_HOME_DIR." + return 1 + fi + + # move from tmp dir to kusion home + runAsRoot mv "$KUSION_TMP_ROOT" "$KUSION_HOME_DIR" + if [ ! -f "$KUSION_CLI_FILE_PATH" ]; then + error "Failed to move binary from tmp folder, $KUSION_CLI_FILE_PATH does not exists." + return 1 + fi + + # update profile + if [ $SKIP_SOURCE_KUSION_ENV = "true" ]; then + info "Skip editing user profile ($detected_profile), cause SKIP_SOURCE_KUSION_ENV is true. Please write source $install_dir/.env in user profile ($detected_profile) manually." + else + env_file_path="$install_dir/$KUSION_ENV_FILE" + updateProfile "$detected_profile" "$env_file_path" + fi +} + +detectProfile() { + local shell_name="$1" + local uname="$2" + + if [ -f "$PROFILE" ]; then + info "Current profile: $PROFILE" + return + fi + + # try to detect the current shell + case "$shell_name" in + bash) + # Shells on macOS default to opening with a login shell, while Linuxes + # default to a *non*-login shell, so if this is macOS we look for + # `.bash_profile` first; if it's Linux, we look for `.bashrc` first. The + # `*` fallthrough covers more than just Linux: it's everything that is not + # macOS (Darwin). It can be made narrower later if need be. + case $uname in + Darwin) + echoFexists "$HOME/.bash_profile" || echoFexists "$HOME/.bashrc" + ;; + *) + echoFexists "$HOME/.bashrc" || echoFexists "$HOME/.bash_profile" + ;; + esac + ;; + zsh) + echo "$HOME/.zshrc" + ;; + fish) + echo "$HOME/.config/fish/config.fish" + ;; + *) + # Fall back to checking for profile file existence. Once again, the order + # differs between macOS and everything else. + local profiles + case $uname in + Darwin) + profiles=(.profile .bash_profile .bashrc .zshrc .config/fish/config.fish) + ;; + *) + profiles=(.profile .bashrc .bash_profile .zshrc .config/fish/config.fish) + ;; + esac + + for profile in "${profiles[@]}"; do + echoFexists "$HOME/$profile" && break + done + ;; + esac +} + +buildEnvFile() { + local profile="$1" + local install_dir="$2" + local env_file_path="$3" + + env_content="$(buildEnvContent "$detected_profile" "$install_dir")" + command printf "$env_content" >>"$env_file_path" + chmod a-wx "$env_file_path" +} + +buildEnvContent() { + local profile="$1" + local install_dir="$2" + + if [[ $profile =~ \.fish$ ]]; then + # fish uses a little different syntax to modify the PATH + cat <>"$profile" + fi +} + +buildSourceEnvContent() { + local env_file_path="$1" + cat <&2 +} + +warn() { + command printf '\033[1;33mWarn\033[0m: %s\n' "$1" 1>&2 +} + +error() { + command printf '\033[1;31mError\033[0m: %s\n' "$1" 1>&2 +} + +runAsRoot() { + local CMD="$*" + + if [ $EUID -ne 0 ] && [ $USE_SUDO = "true" ]; then + CMD="sudo $CMD" + fi + + $CMD +} + +echoFexists() { + [ -f "$1" ] && echo "$1" +} + +removeInstallationDir() { + info "Removing kusion installation dir $KUSION_HOME_DIR..." + runAsRoot rm -rf "$KUSION_HOME_DIR" + if [ -d "$KUSION_HOME_DIR" ]; then + error "Removing kusion installation dir failed." + return 1 + fi +} + +clearProfileSource() { + if [ $SKIP_CLEAR_SOURCE_KUSION_ENV = true ]; then + info "Skip clearing kusion env in user profile." + return 0 + fi + + detected_profile=$(detectProfile "$(basename $SHELL)" "$(uname -s)") + if [ -z "${detected_profile-}" ]; then + warn "No supported user profile found. Already tried \$PROFILE ($PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, ~/.profile, and ~/.config/fish/config.fish. Skip clearing kusion env in user profile." + return 0 + fi + + info "Clearing kusion env in profile $detected_profile..." + deleteProfileSourceContent "$detected_profile" +} + +detectProfile() { + local shell_name="$1" + local uname="$2" + + if [ -f "$PROFILE" ]; then + info "Current profile: $PROFILE" + return + fi + + # try to detect the current shell + case "$shell_name" in + bash) + # Shells on macOS default to opening with a login shell, while Linuxes + # default to a *non*-login shell, so if this is macOS we look for + # `.bash_profile` first; if it's Linux, we look for `.bashrc` first. The + # `*` fallthrough covers more than just Linux: it's everything that is not + # macOS (Darwin). It can be made narrower later if need be. + case $uname in + Darwin) + echoFexists "$HOME/.bash_profile" || echoFexists "$HOME/.bashrc" + ;; + *) + echoFexists "$HOME/.bashrc" || echoFexists "$HOME/.bash_profile" + ;; + esac + ;; + zsh) + echo "$HOME/.zshrc" + ;; + fish) + echo "$HOME/.config/fish/config.fish" + ;; + *) + # Fall back to checking for profile file existence. Once again, the order + # differs between macOS and everything else. + local profiles + case $uname in + Darwin) + profiles=(.profile .bash_profile .bashrc .zshrc .config/fish/config.fish) + ;; + *) + profiles=(.profile .bashrc .bash_profile .zshrc .config/fish/config.fish) + ;; + esac + + for profile in "${profiles[@]}"; do + echoFexists "$HOME/$profile" && break + done + ;; + esac +} + +deleteProfileSourceContent() { + local profile="$1" + local source_line_pattern="^$SOURCE_KUSION_CONTENT$" + local source_line_annotation_pattern="^$SOURCE_KUSION_ANNOTATION_CONTENT$" + + if grep -qc "$source_line_pattern" "$profile"; then + line_number=$(grep -n "$source_line_pattern" "$profile" | cut -d: -f1) + sed -i '' ''"$line_number"'d' "$profile" + fi + if grep -q "$source_line_pattern" "$profile"; then + warn "Failed to delete $SOURCE_KUSION_CONTENT in $profile, please delete it manually." + else + info "Delete $SOURCE_KUSION_CONTENT in $profile succeeded." + fi + + if grep -q "$source_line_annotation_pattern" "$profile"; then + annotation_line_number=$(grep -n "$source_line_annotation_pattern" "$profile" | cut -d: -f1) + sed -i '' ''"$annotation_line_number"'d' "$profile" + fi +} + +exit_trap() { + result=$? + if [ "$result" != "0" ]; then + # todo: update to install faq page when determined + error "Failed to uninstall kusion. Please go to https://kusionstack.io for more support." + else + info "Uninstall kusion succeeded. Hope you can use kusion again, visit https://kusionstack.io for more information." + fi + + exit $result +} + +# ----------------------------------------------------------------------------- +# main +# ----------------------------------------------------------------------------- +trap "exit_trap" EXIT + +removeInstallationDir +clearProfileSource \ No newline at end of file From 673ed5a167ec3a8d97277746109072c0e628b5a2 Mon Sep 17 00:00:00 2001 From: healthjyk Date: Fri, 22 Sep 2023 17:50:57 +0800 Subject: [PATCH 2/3] fix: add executing permission for install.sh and uninstall.sh --- static/scripts/install.sh | 0 static/scripts/uninstall.sh | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 static/scripts/install.sh mode change 100644 => 100755 static/scripts/uninstall.sh diff --git a/static/scripts/install.sh b/static/scripts/install.sh old mode 100644 new mode 100755 diff --git a/static/scripts/uninstall.sh b/static/scripts/uninstall.sh old mode 100644 new mode 100755 From 14cd9d61cfe1ef00ebb7a01a75b9e84cdc5e4d46 Mon Sep 17 00:00:00 2001 From: healthjyk Date: Fri, 22 Sep 2023 18:18:14 +0800 Subject: [PATCH 3/3] fix: delete incorrect stack --- .github/workflows/install.yml | 33 +++++++++++++------------ static/stack/ci-test/settings.yaml | 0 static/stack/ci-test/stdout.golden.yaml | 25 ------------------- static/stack/kcl.yaml | 3 --- static/stack/main.k | 23 ----------------- static/stack/project.yaml | 1 - static/stack/stack.yaml | 1 - 7 files changed, 17 insertions(+), 69 deletions(-) delete mode 100644 static/stack/ci-test/settings.yaml delete mode 100644 static/stack/ci-test/stdout.golden.yaml delete mode 100644 static/stack/kcl.yaml delete mode 100644 static/stack/main.k delete mode 100644 static/stack/project.yaml delete mode 100644 static/stack/stack.yaml diff --git a/.github/workflows/install.yml b/.github/workflows/install.yml index f251400d..28a5a836 100644 --- a/.github/workflows/install.yml +++ b/.github/workflows/install.yml @@ -1,3 +1,4 @@ +# todo: Should check the installation is correct or not! Do it in the kusion repo. name: installation check on: push: @@ -17,27 +18,27 @@ jobs: - uses: actions/checkout@v3 - name: check install script run: static/scripts/install.sh - - name: check kusion run - run: sudo kusion compile -w static/stack -o stdout - shell: bash -leo pipefail {0} + #- name: check kusion run + # run: sudo kusion compile -w static/stack -o stdout + # shell: bash -leo pipefail {0} check-brew-on-macos: runs-on: macos-latest steps: - uses: actions/checkout@v3 - name: brew install kusion run: brew install KusionStack/tap/kusion - - name: check kusion run - run: kusion compile -w static/stack -o stdout - shell: bash -leo pipefail {0} + #- name: check kusion run + # run: kusion compile -w static/stack -o stdout + # shell: bash -leo pipefail {0} check-scripts-on-linux: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: check install script run: static/scripts/install.sh - - name: check kusion run - run: kusion compile -w static/stack -o stdout - shell: bash -ieo pipefail {0} + #- name: check kusion run + # run: kusion compile -w static/stack -o stdout + # shell: bash -ieo pipefail {0} check-brew-on-linux: runs-on: ubuntu-latest steps: @@ -46,9 +47,9 @@ jobs: - name: brew install kusion run: brew install KusionStack/tap/kusion shell: bash -ieo pipefail {0} - - name: check kusion run - run: kusion compile -w static/stack -o stdout - shell: bash -ieo pipefail {0} + #- name: check kusion run + # run: kusion compile -w static/stack -o stdout + # shell: bash -ieo pipefail {0} check-powershell-on-windows: runs-on: windows-latest steps: @@ -56,9 +57,9 @@ jobs: - name: check install script run: static/scripts/install.ps1 shell: powershell - - name: check kusion run - run: C:\kusion\kusion.exe compile -w static/stack -o stdout - shell: powershell + #- name: check kusion run + # run: C:\kusion\kusion.exe compile -w static/stack -o stdout + # shell: powershell check-scoop-on-windows: runs-on: windows-latest steps: @@ -68,5 +69,5 @@ jobs: iex "& {$(irm get.scoop.sh)} -RunAsAdmin" scoop bucket add KusionStack https://github.com/KusionStack/scoop-bucket.git scoop install KusionStack/kusion - kusion compile -w static/stack -o stdout + # kusion compile -w static/stack -o stdout shell: powershell diff --git a/static/stack/ci-test/settings.yaml b/static/stack/ci-test/settings.yaml deleted file mode 100644 index e69de29b..00000000 diff --git a/static/stack/ci-test/stdout.golden.yaml b/static/stack/ci-test/stdout.golden.yaml deleted file mode 100644 index 7ecc068c..00000000 --- a/static/stack/ci-test/stdout.golden.yaml +++ /dev/null @@ -1,25 +0,0 @@ -id: apps/v1:Deployment:default:nginx -type: Kubernetes -attributes: - apiVersion: apps/v1 - kind: Deployment - metadata: - name: nginx - namespace: default - labels: - app: nginx - spec: - replicas: 3 - selector: - matchLabels: - app: nginx - template: - metadata: - labels: - app: nginx - spec: - containers: - - name: nginx - image: nginx:1.14.2 - ports: - - containerPort: 80 diff --git a/static/stack/kcl.yaml b/static/stack/kcl.yaml deleted file mode 100644 index 1219ea7b..00000000 --- a/static/stack/kcl.yaml +++ /dev/null @@ -1,3 +0,0 @@ -kcl_cli_configs: - files: - - main.k \ No newline at end of file diff --git a/static/stack/main.k b/static/stack/main.k deleted file mode 100644 index 6256d804..00000000 --- a/static/stack/main.k +++ /dev/null @@ -1,23 +0,0 @@ -id = "apps/v1:Deployment:default:nginx" -$type = "Kubernetes" -attributes = { - apiVersion = "apps/v1" - kind = "Deployment" - metadata = { - name = "nginx" - namespace = "default" - labels.app = "nginx" - } - spec = { - replicas = 3 - selector.matchLabels.app = "nginx" - template.metadata.labels.app = "nginx" - template.spec.containers = [ - { - name = "nginx" - image = "nginx:1.14.2" - ports = [{ containerPort = 80 }] - } - ] - } -} \ No newline at end of file diff --git a/static/stack/project.yaml b/static/stack/project.yaml deleted file mode 100644 index 948a7b10..00000000 --- a/static/stack/project.yaml +++ /dev/null @@ -1 +0,0 @@ -name: project \ No newline at end of file diff --git a/static/stack/stack.yaml b/static/stack/stack.yaml deleted file mode 100644 index 8051f541..00000000 --- a/static/stack/stack.yaml +++ /dev/null @@ -1 +0,0 @@ -name: stack \ No newline at end of file