seaweedfs/weed/command/update.go

372 lines
8.7 KiB
Go
Raw Normal View History

2022-06-14 19:01:57 +08:00
package command
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/util"
2022-06-14 19:01:57 +08:00
"golang.org/x/net/context/ctxhttp"
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
2022-06-14 19:01:57 +08:00
)
//copied from https://github.com/restic/restic/tree/master/internal/selfupdate
2022-06-14 19:01:57 +08:00
// Release collects data about a single release on GitHub.
type Release struct {
Name string `json:"name"`
TagName string `json:"tag_name"`
Draft bool `json:"draft"`
PreRelease bool `json:"prerelease"`
PublishedAt time.Time `json:"published_at"`
Assets []Asset `json:"assets"`
Version string `json:"-"` // set manually in the code
}
// Asset is a file uploaded and attached to a release.
type Asset struct {
ID int `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
}
2022-06-15 15:26:04 +08:00
const githubAPITimeout = 30 * time.Second
// githubError is returned by the GitHub API, e.g. for rate-limiting.
type githubError struct {
Message string
}
2022-09-15 14:06:44 +08:00
// default version is not full version
var isFullVersion = false
2022-06-14 19:01:57 +08:00
var (
updateOpt UpdateOptions
)
type UpdateOptions struct {
dir *string
name *string
2022-06-16 11:19:49 +08:00
Version *string
2022-06-14 19:01:57 +08:00
}
func init() {
path, _ := os.Executable()
_, name := filepath.Split(path)
updateOpt.dir = cmdUpdate.Flag.String("dir", filepath.Dir(path), "directory to save new weed.")
2022-09-15 00:13:59 +08:00
updateOpt.name = cmdUpdate.Flag.String("name", name, "name of new weed. On windows, name shouldn't be same to the original name.")
updateOpt.Version = cmdUpdate.Flag.String("version", "0", "specific version of weed you want to download. If not specified, get the latest version.")
2022-06-14 19:01:57 +08:00
cmdUpdate.Run = runUpdate
}
var cmdUpdate = &Command{
UsageLine: "update [-dir=/path/to/dir] [-name=name] [-version=x.xx]",
Short: "get latest or specific version from https://github.com/seaweedfs/seaweedfs",
Long: `get latest or specific version from https://github.com/seaweedfs/seaweedfs`,
2022-06-14 19:01:57 +08:00
}
func runUpdate(cmd *Command, args []string) bool {
path, _ := os.Executable()
_, name := filepath.Split(path)
if *updateOpt.dir != "" {
if err := util.TestFolderWritable(util.ResolvePath(*updateOpt.dir)); err != nil {
glog.Fatalf("Check Folder(-dir) Writable %s : %s", *updateOpt.dir, err)
2022-06-14 19:01:57 +08:00
return false
}
} else {
*updateOpt.dir = filepath.Dir(path)
}
if *updateOpt.name == "" {
*updateOpt.name = name
}
2022-06-16 16:52:57 +08:00
target := filepath.Join(*updateOpt.dir, *updateOpt.name)
if runtime.GOOS == "windows" {
2022-06-16 16:52:57 +08:00
if target == path {
2022-09-15 00:13:59 +08:00
glog.Fatalf("On windows, name of the new weed shouldn't be same to the original name.")
2022-06-14 19:01:57 +08:00
return false
}
}
glog.V(0).Infof("new weed will be saved to %s", target)
_, err := downloadRelease(context.Background(), target, *updateOpt.Version)
2022-06-14 19:01:57 +08:00
if err != nil {
2022-06-16 11:19:49 +08:00
glog.Errorf("unable to download weed: %v", err)
2022-06-14 19:01:57 +08:00
return false
}
return true
}
2022-06-16 11:19:49 +08:00
func downloadRelease(ctx context.Context, target string, ver string) (version string, err error) {
2022-06-14 19:01:57 +08:00
currentVersion := util.VERSION_NUMBER
2022-07-30 14:39:11 +08:00
rel, err := GitHubLatestRelease(ctx, ver, "seaweedfs", "seaweedfs")
2022-06-14 19:01:57 +08:00
if err != nil {
return "", err
}
if rel.Version == currentVersion {
2022-06-16 11:19:49 +08:00
if ver == "0" {
glog.V(0).Infof("weed is up to date")
} else {
glog.V(0).Infof("no need to download the same version of weed ")
}
2022-06-14 19:01:57 +08:00
return currentVersion, nil
}
2022-06-16 11:19:49 +08:00
glog.V(0).Infof("download version: %s", rel.Version)
2022-06-14 19:01:57 +08:00
2022-06-15 15:26:04 +08:00
largeDiskSuffix := ""
if util.VolumeSizeLimitGB == 8000 {
largeDiskSuffix = "_large_disk"
}
fullSuffix := ""
if isFullVersion {
fullSuffix = "_full"
}
2022-06-14 19:01:57 +08:00
ext := "tar.gz"
if runtime.GOOS == "windows" {
ext = "zip"
}
suffix := fmt.Sprintf("%s_%s%s%s.%s", runtime.GOOS, runtime.GOARCH, fullSuffix, largeDiskSuffix, ext)
2022-06-14 19:01:57 +08:00
md5Filename := fmt.Sprintf("%s.md5", suffix)
_, md5Val, err := getGithubDataFile(ctx, rel.Assets, md5Filename)
if err != nil {
return "", err
}
downloadFilename, buf, err := getGithubDataFile(ctx, rel.Assets, suffix)
if err != nil {
return "", err
}
md5Ctx := md5.New()
md5Ctx.Write(buf)
binaryMd5 := md5Ctx.Sum(nil)
if hex.EncodeToString(binaryMd5) != string(md5Val[0:32]) {
glog.Errorf("md5:'%s' '%s'", hex.EncodeToString(binaryMd5), string(md5Val[0:32]))
2022-06-15 15:26:04 +08:00
err = fmt.Errorf("binary md5sum doesn't match")
2022-06-14 19:01:57 +08:00
return "", err
}
err = extractToFile(buf, downloadFilename, target)
if err != nil {
return "", err
} else {
glog.V(0).Infof("successfully updated weed to version %v\n", rel.Version)
2022-06-14 19:01:57 +08:00
}
return rel.Version, nil
}
2022-06-16 11:19:49 +08:00
// GitHubLatestRelease uses the GitHub API to get information about the specific
2022-06-14 19:01:57 +08:00
// release of a repository.
2022-06-16 11:19:49 +08:00
func GitHubLatestRelease(ctx context.Context, ver string, owner, repo string) (Release, error) {
2022-06-14 19:01:57 +08:00
ctx, cancel := context.WithTimeout(ctx, githubAPITimeout)
defer cancel()
2022-06-16 11:19:49 +08:00
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", owner, repo)
2022-06-14 19:01:57 +08:00
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return Release{}, err
}
// pin API version 3
req.Header.Set("Accept", "application/vnd.github.v3+json")
res, err := ctxhttp.Do(ctx, http.DefaultClient, req)
if err != nil {
return Release{}, err
}
defer util_http.CloseResponse(res)
2022-06-14 19:01:57 +08:00
if res.StatusCode != http.StatusOK {
content := res.Header.Get("Content-Type")
if strings.Contains(content, "application/json") {
// try to decode error message
var msg githubError
jerr := json.NewDecoder(res.Body).Decode(&msg)
if jerr == nil {
return Release{}, fmt.Errorf("unexpected status %v (%v) returned, message:\n %v", res.StatusCode, res.Status, msg.Message)
}
}
return Release{}, fmt.Errorf("unexpected status %v (%v) returned", res.StatusCode, res.Status)
}
buf, err := io.ReadAll(res.Body)
2022-06-14 19:01:57 +08:00
if err != nil {
return Release{}, err
}
var release Release
2022-06-16 11:19:49 +08:00
var releaseList []Release
err = json.Unmarshal(buf, &releaseList)
2022-06-14 19:01:57 +08:00
if err != nil {
return Release{}, err
}
2022-06-16 11:19:49 +08:00
if ver == "0" {
release = releaseList[0]
glog.V(0).Infof("latest version is %v\n", release.TagName)
} else {
for _, r := range releaseList {
if r.TagName == ver {
release = r
break
}
}
}
2022-06-14 19:01:57 +08:00
if release.TagName == "" {
2022-06-16 11:19:49 +08:00
return Release{}, fmt.Errorf("can not find the specific version")
2022-06-14 19:01:57 +08:00
}
release.Version = release.TagName
return release, nil
}
func getGithubData(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
// request binary data
req.Header.Set("Accept", "application/octet-stream")
res, err := ctxhttp.Do(ctx, http.DefaultClient, req)
if err != nil {
return nil, err
}
defer util_http.CloseResponse(res)
2022-06-14 19:01:57 +08:00
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status %v (%v) returned", res.StatusCode, res.Status)
}
buf, err := io.ReadAll(res.Body)
2022-06-14 19:01:57 +08:00
if err != nil {
return nil, err
}
return buf, nil
}
func getGithubDataFile(ctx context.Context, assets []Asset, suffix string) (filename string, data []byte, err error) {
var url string
for _, a := range assets {
if strings.HasSuffix(a.Name, suffix) {
url = a.URL
filename = a.Name
break
}
}
if url == "" {
return "", nil, fmt.Errorf("unable to find file with suffix %v", suffix)
}
glog.V(0).Infof("download %v\n", filename)
data, err = getGithubData(ctx, url)
if err != nil {
return "", nil, err
}
return filename, data, nil
}
func extractToFile(buf []byte, filename, target string) error {
var rd io.Reader = bytes.NewReader(buf)
switch filepath.Ext(filename) {
case ".gz":
gr, err := gzip.NewReader(rd)
if err != nil {
return err
}
defer gr.Close()
trd := tar.NewReader(gr)
hdr, terr := trd.Next()
if terr != nil {
glog.Errorf("uncompress file(%s) failed:%s", hdr.Name, terr)
return terr
}
rd = trd
case ".zip":
zrd, err := zip.NewReader(bytes.NewReader(buf), int64(len(buf)))
if err != nil {
return err
}
if len(zrd.File) != 1 {
2022-06-15 15:26:04 +08:00
return fmt.Errorf("ZIP archive contains more than one file")
2022-06-14 19:01:57 +08:00
}
file, err := zrd.File[0].Open()
if err != nil {
return err
}
defer func() {
_ = file.Close()
}()
rd = file
}
// Write everything to a temp file
dir := filepath.Dir(target)
new, err := os.CreateTemp(dir, "weed")
2022-06-14 19:01:57 +08:00
if err != nil {
return err
}
n, err := io.Copy(new, rd)
if err != nil {
_ = new.Close()
_ = os.Remove(new.Name())
return err
}
if err = new.Sync(); err != nil {
return err
}
if err = new.Close(); err != nil {
return err
}
mode := os.FileMode(0755)
// attempt to find the original mode
if fi, err := os.Lstat(target); err == nil {
mode = fi.Mode()
}
// Rename the temp file to the final location atomically.
if err := os.Rename(new.Name(), target); err != nil {
return err
}
glog.V(0).Infof("saved %d bytes in %v\n", n, target)
return os.Chmod(target, mode)
}