-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(upgrade): support upgrade qcadmin self
support upgrade qcadmin self Signed-off-by: ysicing <[email protected]>
- Loading branch information
Showing
13 changed files
with
317 additions
and
33 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.0 | ||
1.0.1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# CHANGELOG | ||
|
||
## 1.0.1 | ||
|
||
- [x] 支持升级cli | ||
|
||
## 1.0.0 | ||
|
||
- [x] 支持配置应用市场版本 | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2021-2022 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.cn) 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. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/easysoft/qcadmin/cmd/upgrade" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newCmdUpgrade() *cobra.Command { | ||
up := &cobra.Command{ | ||
Use: "upgrade", | ||
Short: "upgrade version", | ||
Aliases: []string{"ug", "ugc"}, | ||
} | ||
up.AddCommand(upgrade.NewUpgradeQ()) | ||
return up | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2021-2022 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.cn) 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. | ||
|
||
package upgrade | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/easysoft/qcadmin/cmd/version" | ||
"github.com/easysoft/qcadmin/common" | ||
"github.com/easysoft/qcadmin/internal/pkg/util/log" | ||
"github.com/easysoft/qcadmin/pkg/selfupdate" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewUpgradeQ() *cobra.Command { | ||
upq := &cobra.Command{ | ||
Use: "q", | ||
Aliases: []string{"qcadmin"}, | ||
Short: "upgrade qcadmin(q) to the newest version", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
DoQcadmin() | ||
}, | ||
} | ||
return upq | ||
} | ||
|
||
func DoQcadmin() { | ||
log.Flog.StartWait("fetch latest version from remote...") | ||
lastversion, err := version.PreCheckLatestVersion() | ||
log.Flog.StopWait() | ||
if err != nil { | ||
log.Flog.Errorf("fetch latest version err, reason: %v", err) | ||
return | ||
} | ||
if lastversion == "" || lastversion == common.Version { | ||
log.Flog.Infof("The current version %s is the latest version", common.Version) | ||
return | ||
} | ||
cmdPath, err := os.Executable() | ||
if err != nil { | ||
log.Flog.Errorf("q executable err:%v", err) | ||
return | ||
} | ||
log.Flog.StartWait(fmt.Sprintf("downloading version %s...", lastversion)) | ||
assetURL := fmt.Sprintf("https://pkg.qucheng.com/qucheng/cli/stable/qcadmin_%s_%s", runtime.GOOS, runtime.GOARCH) | ||
err = selfupdate.UpdateTo(assetURL, cmdPath) | ||
log.Flog.StopWait() | ||
if err != nil { | ||
log.Flog.Errorf("upgrade failed, reason: %v", err) | ||
return | ||
} | ||
log.Flog.Donef("Successfully updated ergo to version %s", lastversion) | ||
log.Flog.Infof("Release note: \n\trelease %s ", lastversion) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
#!/bin/sh | ||
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/qucheng/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}/q-ro-test && rm -rf ${BIN_DIR}/q-ro-test"; then | ||
if [ -d /opt/bin ]; then | ||
BIN_DIR=/opt/bin | ||
fi | ||
fi | ||
} | ||
|
||
# --- verify an executable qcadmin binary is installed --- | ||
verify_qcadmin_is_executable() { | ||
if [ ! -x ${BIN_DIR}/q ]; then | ||
fatal "Executable qcadmin binary not found at ${BIN_DIR}/qcadmin" | ||
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 qcadmin-install.XXXXXXXXXX) | ||
TMP_HASH=${TMP_DIR}/qcadmin.hash | ||
TMP_BIN=${TMP_DIR}/qcadmin.bin | ||
cleanup() { | ||
code=$? | ||
set +e | ||
trap - EXIT | ||
rm -rf ${TMP_DIR} | ||
exit $code | ||
} | ||
trap cleanup INT EXIT | ||
} | ||
|
||
# --- use desired qcadmin version if defined or find version from channel --- | ||
get_release_version() { | ||
VERSION="edge" | ||
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} # qcadmin_linux_amd64 | ||
info "Downloading binary ${COS_URL}/${VERSION}/q" | ||
download ${TMP_BIN} ${BIN_URL} | ||
} | ||
|
||
# --- setup permissions and move binary to system directory --- | ||
setup_binary() { | ||
chmod 755 ${TMP_BIN} | ||
info "Installing qcadmin to ${BIN_DIR}/qcadmin" | ||
$SUDO chown root:root ${TMP_BIN} | ||
$SUDO mv -f ${TMP_BIN} ${BIN_DIR}/qcadmin | ||
[ -f "${BIN_DIR}/q" ] && ( | ||
$SUDO rm -f ${BIN_DIR}/q | ||
) | ||
info "Create qcadmin soft link ${BIN_DIR}/q" | ||
$SUDO ln -s ${BIN_DIR}/qcadmin ${BIN_DIR}/q | ||
info "Installation is complete. Use q --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 | ||
get_release_version | ||
# Skip download if qcadmin binary exists, support upgrade | ||
download_binary | ||
setup_binary | ||
} | ||
|
||
# --- run the install process -- | ||
{ | ||
setup_env "$@" | ||
download_and_verify | ||
} |
Oops, something went wrong.