diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 87785f1e..60765b62 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -8,7 +8,6 @@ package cmd import ( "github.com/easysoft/qcadmin/cmd/upgrade" - "github.com/easysoft/qcadmin/internal/app/config" "github.com/easysoft/qcadmin/internal/pkg/util/factory" "github.com/spf13/cobra" ) @@ -20,9 +19,7 @@ func newCmdUpgrade(f factory.Factory) *cobra.Command { Aliases: []string{"ug", "ugc"}, } up.AddCommand(upgrade.NewUpgradeQ(f)) - cfg, _ := config.LoadConfig() - if cfg == nil || !cfg.Quickon.DevOps { - up.AddCommand(upgrade.NewUpgradeQucheng(f)) - } + up.AddCommand(upgrade.NewUpgradeOperator(f)) + up.AddCommand(upgrade.NewUpgradePlatform(f)) return up } diff --git a/cmd/upgrade/operator.go b/cmd/upgrade/operator.go index 1faeac73..7c514474 100644 --- a/cmd/upgrade/operator.go +++ b/cmd/upgrade/operator.go @@ -9,6 +9,7 @@ package upgrade import ( "os" + "github.com/easysoft/qcadmin/common" qcexec "github.com/easysoft/qcadmin/internal/pkg/util/exec" "github.com/easysoft/qcadmin/internal/pkg/util/factory" "github.com/sirupsen/logrus" @@ -20,10 +21,9 @@ func NewUpgradeOperator(f factory.Factory) *cobra.Command { log: f.GetLog(), } upq := &cobra.Command{ - Use: "operator", - Aliases: []string{"cne-operator"}, - Short: "upgrade operator to the newest version", - Args: cobra.NoArgs, + Use: "operator", + Short: "upgrade operator to the newest version", + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { up.DoOperator() }, @@ -32,12 +32,13 @@ func NewUpgradeOperator(f factory.Factory) *cobra.Command { } func (up option) DoOperator() { - args := []string{"platform", "plugins", "upgrade", "cne-operator"} + args := []string{"exp", "helm", "upgrade", "--name", common.DefaultCneOperatorName, "--chart", common.DefaultCneOperatorName, "--repo", common.DefaultHelmRepoName, "-n", common.GetDefaultSystemNamespace(true)} if up.log.GetLevel() == logrus.DebugLevel { args = append(args, "--debug") } if err := qcexec.CommandRun(os.Args[0], args...); err != nil { - up.log.Errorf("upgrade plugin cne-operator err: %v", err) + up.log.Errorf("upgrade cne-operator failed, reason: %v", err) return } + up.log.Done("upgrade cne-operator success") } diff --git a/cmd/upgrade/quickon.go b/cmd/upgrade/quickon.go index 90338d6f..29d5cc1e 100644 --- a/cmd/upgrade/quickon.go +++ b/cmd/upgrade/quickon.go @@ -20,15 +20,20 @@ type Option struct { log log.Logger } -func NewUpgradeQucheng(f factory.Factory) *cobra.Command { +func NewUpgradePlatform(f factory.Factory) *cobra.Command { upcmd := &Option{ log: f.GetLog(), } up := &cobra.Command{ - Use: "quickon", - Aliases: []string{"qc", "qucheng"}, - Short: "Upgrades the QuCheng to the newest version", + Use: "platform", + Aliases: []string{"pt"}, + Short: "upgrade platform to the newest version", Args: cobra.NoArgs, + PreRunE: func(cmd *cobra.Command, args []string) error { + // 检查升级前的环境 + upcmd.log.Donef("check upgrade environment...") + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { return upcmd.Run() }, diff --git a/hack/scripts/devops/get.sh b/hack/scripts/devops/get.sh new file mode 100755 index 00000000..548d8304 --- /dev/null +++ b/hack/scripts/devops/get.sh @@ -0,0 +1,193 @@ +#!/bin/sh +# Copyright (c) 2021-2023 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved. +# Use of this source code is covered by the following dual licenses: +# (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2) +# (2) Affero General Public License 3.0 (AGPL 3.0) +# license that can be found in the LICENSE file. + +# Source code is available at https://github.com/easysoft/quickon_cli + +# SCRIPT_COMMIT_SHA="a77fcbaf862d7b2d1eb8b08c5920adde14edc4ca" +# SCRIPT_DATA="Fri Sep 8 09:46:36 AM CST 2023" + +# Usage: +# curl ... | ENV_VAR=... sh - +# or +# ENV_VAR=... ./install.sh +# +# Example: +# Installing Quickon with ZenTao: +# curl ... | DEVOPS_TYPE="" sh - +# - DEVOPS_TYPE +# Install Type when install Quickon. +# Defaults to '', support 'max', 'biz', 'ipd' +# - DEVOPS_VERSION +# Install Version when install Quickon. +# Defaults to '' +# - INSTALL_DOMAIN +# If not set default use gen default domain + +set -e +set -o noglob + +# --- helper functions for logs --- +info() +{ + echo '[INFO] ' "$@" +} +warn() +{ + echo '[WARN] ' "$@" >&2 +} +fatal() +{ + echo '[ERROR] ' "$@" >&2 + exit 1 +} + +COS_URL=https://pkg.qucheng.com/devops/cli + +# --- define needed environment variables --- +setup_env() { + # --- use sudo if we are not already root --- + SUDO=sudo + if [ $(id -u) -eq 0 ]; then + SUDO= + fi + BIN_DIR=/usr/local/bin + if ! $SUDO sh -c "touch ${BIN_DIR}/z-ro-test && rm -rf ${BIN_DIR}/z-ro-test"; then + if [ -d /opt/bin ]; then + BIN_DIR=/opt/bin + fi + fi +} + +# --- verify an executable z binary is installed --- +verify_z_is_executable() { + if [ ! -x ${BIN_DIR}/z ]; then + fatal "Executable z binary not found at ${BIN_DIR}/z" + fi +} + +# --- verify existence of network downloader executable --- +verify_downloader() { + # Return failure if it doesn't exist or is no executable + [ -x "$(command -v $1)" ] || return 1 + + # Set verified executable as our downloader program and return success + DOWNLOADER=$1 + return 0 +} + +# --- create temporary directory and cleanup when done --- +setup_tmp() { + TMP_DIR=$(mktemp -d -t z-install.XXXXXXXXXX) + TMP_HASH=${TMP_DIR}/z.hash + TMP_BIN=${TMP_DIR}/z.bin + cleanup() { + code=$? + set +e + trap - EXIT + rm -rf ${TMP_DIR} + exit $code + } + trap cleanup INT EXIT +} + +setup_quickon() { + [ -d "/opt/quickon/backup" ] || ( + mkdir -p /opt/quickon/backup + chmod 777 /opt/quickon/backup + ) +} + +# --- use desired qcadmin version if defined or find version from channel --- +get_release_version() { + if [ -z "$VERSION" ]; then + VERSION="stable" + fi + info "Using ${VERSION} as release" +} + +# --- set arch and suffix, fatal if architecture not supported --- +setup_verify_arch() { + if [ -z "$ARCH" ]; then + ARCH=$(uname -m) + fi + case $ARCH in + amd64|x86_64) + ARCH=amd64 + SUFFIX=${ARCH} + ;; + arm64|aarch64) + ARCH=arm64 + SUFFIX=${ARCH} + ;; + arm*) + ARCH=arm + SUFFIX=${ARCH}hf + ;; + *) + fatal "Unsupported architecture $ARCH" + esac +} + + +# --- download from url --- +download() { + [ $# -eq 2 ] || fatal 'download needs exactly 2 arguments' + + case $DOWNLOADER in + curl) + curl -o $1 -sfL $2 + ;; + wget) + wget -qO $1 $2 + ;; + *) + fatal "Incorrect executable '$DOWNLOADER'" + ;; + esac + + # Abort if download command failed + [ $? -eq 0 ] || fatal 'Download failed' +} + +# --- download binary from cos url --- +download_binary() { + BIN_URL=${COS_URL}/${VERSION}/qcadmin_linux_${SUFFIX} + info "Downloading binary" + download ${TMP_BIN} ${BIN_URL} +} + +# --- setup permissions and move binary to system directory --- +setup_binary() { + chmod 755 ${TMP_BIN} + info "Installing z to ${BIN_DIR}/z" + $SUDO chown root:root ${TMP_BIN} + $SUDO mv -f ${TMP_BIN} ${BIN_DIR}/qcadmin + [ -f "${BIN_DIR}/z" ] && ( + $SUDO rm -f ${BIN_DIR}/z + ) + info "Create soft link ${BIN_DIR}/z" + $SUDO ln -s ${BIN_DIR}/qcadmin ${BIN_DIR}/z + info "Installation is complete. Use z --help" +} + +# --- download and verify qcadmin --- +download_and_verify() { + setup_verify_arch + verify_downloader curl || verify_downloader wget || fatal 'Can not find curl or wget for downloading files' + setup_tmp + setup_quickon + get_release_version + # Skip download if qcadmin binary exists, support upgrade + download_binary + setup_binary +} + +# --- run the install process -- +{ + setup_env + download_and_verify +} diff --git a/pkg/qucheng/upgrade/upgrade.go b/pkg/qucheng/upgrade/upgrade.go index 2a71da46..6755cb02 100644 --- a/pkg/qucheng/upgrade/upgrade.go +++ b/pkg/qucheng/upgrade/upgrade.go @@ -9,20 +9,14 @@ package upgrade import ( "fmt" "os" - "time" "github.com/easysoft/qcadmin/common" "github.com/easysoft/qcadmin/internal/app/config" - suffixdomain "github.com/easysoft/qcadmin/pkg/qucheng/domain" "github.com/ergoapi/util/color" "github.com/cockroachdb/errors" - qcexec "github.com/easysoft/qcadmin/internal/pkg/util/exec" "github.com/easysoft/qcadmin/internal/pkg/util/helm" - "github.com/easysoft/qcadmin/internal/pkg/util/kutil" "github.com/easysoft/qcadmin/internal/pkg/util/log" - "github.com/ergoapi/util/exid" - "github.com/ergoapi/util/file" "github.com/ergoapi/util/version" ) @@ -111,52 +105,14 @@ func Upgrade(flagVersion string, log log.Logger) error { for _, cv := range qv.Components { if cv.CanUpgrade { defaultValue, _ := helmClient.GetValues(cv.Name) - if cv.Name == "qucheng" || cv.Name == "quickon" { - cfg, _ := config.LoadConfig() - domain := cfg.Domain - if kutil.IsLegalDomain(domain) { - log.Infof("load %s tls cert", domain) - defaultTLS := fmt.Sprintf("%s/tls-haogs-cn.yaml", common.GetDefaultCacheDir()) - if !file.CheckFileExists(defaultTLS) { - suffixdomain.UpgradeTLSDDomain("127.0.0.1", exid.GenUUID(), domain) - log.StartWait(fmt.Sprintf("start issuing domain %s certificate, may take 3-5min", domain)) - waittls := time.Now() - for { - if _, err := os.Stat(defaultTLS); err == nil { - log.StopWait() - log.Done("download tls cert success") - if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetKubeConfig()).Run(); err != nil { - log.Warnf("load default tls cert failed, reason: %v", err) - } else { - log.Done("load default tls cert success") - } - qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", "default", "--kubeconfig", common.GetKubeConfig()).Run() - args := []string{"ingress.tls.enabled=true", "ingress.tls.secretName=tls-haogs-cn"} - values, _ := helm.MergeValues(args) - defaultValue = helm.MergeMaps(defaultValue, values) - break - } - _, mainDomain := kutil.SplitDomain(domain) - qcexec.Command(os.Args[0], "experimental", "tools", "wget", "-t", fmt.Sprintf("https://pkg.qucheng.com/ssl/%s/%s/tls.yaml", mainDomain, domain), "-d", defaultTLS).Run() - log.Debug("wait for tls cert ready...") - time.Sleep(time.Second * 5) - trywaitsc := time.Now() - if trywaitsc.Sub(waittls) > time.Minute*3 { - // TODO timeout - log.Debugf("wait tls cert ready, timeout: %v", trywaitsc.Sub(waittls).Seconds()) - break - } - } - } - } - } - if _, err := helmClient.Upgrade(cv.Name, common.DefaultHelmRepoName, cv.Name, "", defaultValue); err != nil { log.Warnf("upgrade %s failed, reason: %v", cv.Name, err) } else { log.Donef("upgrade %s success", cv.Name) count++ } + } else { + log.Infof("%s current version is the latest", cv.Name) } } if count == 0 {