Skip to content

Commit

Permalink
feat(upgrade): support upgrade qcadmin self
Browse files Browse the repository at this point in the history
support upgrade qcadmin self

Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed Jun 8, 2022
1 parent c0d22f9 commit d5986be
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 33 deletions.
40 changes: 20 additions & 20 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,26 @@ steps:
branch:
- master

- name: upload edge install
image: ysicing/drone-plugin-cos
privileged: true
pull: always
settings:
debug: true
region: ap-shanghai
autotime: false
bucket:
from_secret: s3-bucket
accesskey:
from_secret: s3-access-key
secretkey:
from_secret: s3-secret-key
source: ./hack/scripts/get.sh
target:
from_secret: s3-edge-path
when:
branch:
- master
# - name: upload edge install
# image: ysicing/drone-plugin-cos
# privileged: true
# pull: always
# settings:
# debug: true
# region: ap-shanghai
# autotime: false
# bucket:
# from_secret: s3-bucket
# accesskey:
# from_secret: s3-access-key
# secretkey:
# from_secret: s3-secret-key
# source: ./hack/scripts/get.sh
# target:
# from_secret: s3-edge-path
# when:
# branch:
# - master

- name: release stable version
image: ysicing/drone-plugin-rv
Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
[![Releases](https://img.shields.io/github/release-pre/easysoft/qucheng_cli.svg)](https://github.com/easysoft/qucheng_cli/releases)
[![docs](https://img.shields.io/badge/docs-done-green)](https://www.qucheng.cn/)

compatibility:

- [x] 100% support `Debian 11+`

## Quick start

```bash
Expand All @@ -21,3 +25,34 @@ q init
make generate
make build
```

## Install

### Building From Source

`qcadmin(q)` is currently using go v1.16 or above. In order to build ergo from source you must:

```bash
# Clone the repo
# Build and run the executable
make generate
make build
```

### Linux Binary

Downloaded from pre-compiled binaries

```bash
# stable / tag
curl https://pkg.qucheng.com/qucheng/cli/stable/get.sh | sh -
# edge / master (Recommended)
curl https://pkg.qucheng.com/qucheng/cli/edge/get.sh | sh -
```

## Upgrade

```bash
# upgrade self
q upgrade q
```
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.0.1
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 1.0.1

- [x] 支持升级cli

## 1.0.0

- [x] 支持配置应用市场版本
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func BuildRoot(f factory.Factory) *cobra.Command {
rootCmd.AddCommand(newCmdUninstall())
rootCmd.AddCommand(newCmdStatus())
rootCmd.AddCommand(newCmdServe())
rootCmd.AddCommand(newCmdUpgrade())
// Add plugin commands
rootCmd.AddCommand(newCmdPlugin())

Expand Down
22 changes: 22 additions & 0 deletions cmd/upgrade.go
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
}
61 changes: 61 additions & 0 deletions cmd/upgrade/q.go
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)
}
16 changes: 11 additions & 5 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,25 @@ const (
)

type versionGet struct {
Version string `json:"version"`
BuildDate string `json:"build_date"`
Code int `json:"code"`
Data struct {
Name string `json:"name"`
Version string `json:"version"`
Sync time.Time `json:"sync"`
} `json:"data"`
Message string `json:"message"`
Timestamp int `json:"timestamp"`
}

// PreCheckLatestVersion 检查最新版本
func PreCheckLatestVersion() (string, error) {
lastVersion := &versionGet{}
client := req.C().SetUserAgent(common.GetUG()).SetTimeout(time.Second * 5)
_, err := client.R().SetResult(lastVersion).Get("http://release.metrics.qucheng.com/last/qcadmin?type=none")
_, err := client.R().SetResult(lastVersion).Get("https://api.qucheng.com/api/release/last/qcadmin")
if err != nil {
return "", err
}
return lastVersion.Version, nil
return lastVersion.Data.Version, nil
}

func ShowVersion() {
Expand All @@ -74,7 +80,7 @@ func ShowVersion() {
nowversion, _ := gv.New(common.Version)
needupgrade := nowversion.LT(gv.MustParse(lastversion))
if needupgrade {
log.Flog.Infof("当前最新版本 %s, 可以使用 %s 将版本升级到最新版本", color.SGreen(lastversion), color.SGreen("ergo upgrade"))
log.Flog.Infof("当前最新版本 %s, 可以使用 %s 将版本升级到最新版本", color.SGreen(lastversion), color.SGreen("q upgrade q"))
return
}
}
Expand Down
155 changes: 155 additions & 0 deletions hack/scripts/get.edge.sh
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
}
Loading

0 comments on commit d5986be

Please sign in to comment.