2022-04-19 10:07:35 +00:00
|
|
|
package cwhub
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// Install, upgrade and remove items from the hub to the local configuration
|
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
import (
|
2023-11-24 14:57:32 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2022-04-19 10:07:35 +00:00
|
|
|
"fmt"
|
2023-11-24 14:57:32 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-05-24 13:46:48 +00:00
|
|
|
"path/filepath"
|
2022-04-19 10:07:35 +00:00
|
|
|
|
|
|
|
"github.com/enescakir/emoji"
|
|
|
|
)
|
|
|
|
|
2023-11-28 22:51:51 +00:00
|
|
|
// Upgrade downloads and applies the last version of the item from the hub.
|
|
|
|
func (i *Item) Upgrade(force bool) (bool, error) {
|
|
|
|
updated := false
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2023-12-05 12:38:52 +00:00
|
|
|
if i.State.IsLocal() {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Infof("not upgrading %s: local item", i.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
return false, nil
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if !i.State.Downloaded {
|
|
|
|
return false, fmt.Errorf("can't upgrade %s: not installed", i.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !i.State.Installed {
|
|
|
|
return false, fmt.Errorf("can't upgrade %s: downloaded but not installed", i.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.State.UpToDate {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Infof("%s: up-to-date", i.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
if err := i.DownloadDataIfNeeded(force); err != nil {
|
|
|
|
return false, fmt.Errorf("%s: download failed: %w", i.Name, err)
|
2022-10-25 12:10:51 +00:00
|
|
|
}
|
2023-10-03 09:20:56 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if !force {
|
|
|
|
// no upgrade needed
|
|
|
|
return false, nil
|
2022-05-24 13:46:48 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := i.downloadLatest(force, true); err != nil {
|
|
|
|
return false, fmt.Errorf("%s: download failed: %w", i.Name, err)
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if !i.State.UpToDate {
|
|
|
|
if i.State.Tainted {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Warningf("%v %s is tainted, --force to overwrite", emoji.Warning, i.Name)
|
2022-05-24 13:46:48 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
} else {
|
|
|
|
// a check on stdout is used while scripting to know if the hub has been upgraded
|
|
|
|
// and a configuration reload is required
|
|
|
|
// TODO: use a better way to communicate this
|
|
|
|
fmt.Printf("updated %s\n", i.Name)
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Infof("%v %s: updated", emoji.Package, i.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
updated = true
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2023-10-03 09:20:56 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
return updated, nil
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// downloadLatest downloads the latest version of the item to the hub directory.
|
|
|
|
func (i *Item) downloadLatest(overwrite bool, updateOnly bool) (string, error) {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Debugf("Downloading %s %s", i.Type, i.Name)
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
for _, sub := range i.SubItems() {
|
|
|
|
if !sub.State.Installed && updateOnly && sub.State.Downloaded {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Debugf("skipping upgrade of %s: not installed", i.Name)
|
2022-04-19 10:07:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Debugf("Download %s sub-item: %s %s (%t -> %t)", i.Name, sub.Type, sub.Name, i.State.Installed, updateOnly)
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// recurse as it's a collection
|
|
|
|
if sub.HasSubItems() {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Tracef("collection, recurse")
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if _, err := sub.downloadLatest(overwrite, updateOnly); err != nil {
|
|
|
|
return "", fmt.Errorf("while downloading %s: %w", sub.Name, err)
|
|
|
|
}
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
downloaded := sub.State.Downloaded
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if _, err := sub.download(overwrite); err != nil {
|
|
|
|
return "", fmt.Errorf("while downloading %s: %w", sub.Name, err)
|
|
|
|
}
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// We need to enable an item when it has been added to a collection since latest release of the collection.
|
|
|
|
// We check if sub.Downloaded is false because maybe the item has been disabled by the user.
|
|
|
|
if !sub.State.Installed && !downloaded {
|
|
|
|
if err := sub.enable(); err != nil {
|
|
|
|
return "", fmt.Errorf("enabling '%s': %w", sub.Name, err)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if !i.State.Installed && updateOnly && i.State.Downloaded {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Debugf("skipping upgrade of %s: not installed", i.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
ret, err := i.download(overwrite)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to download item: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
// FetchLatest downloads the latest item from the hub, verifies the hash and returns the content and the used url.
|
|
|
|
func (i *Item) FetchLatest() ([]byte, string, error) {
|
2023-11-24 14:57:32 +00:00
|
|
|
url, err := i.hub.remote.urlTo(i.RemotePath)
|
|
|
|
if err != nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
return nil, "", fmt.Errorf("failed to build request: %w", err)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := hubClient.Get(url)
|
|
|
|
if err != nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
return nil, "", err
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-12-15 14:27:22 +00:00
|
|
|
return nil, "", fmt.Errorf("bad http code %d", resp.StatusCode)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
return nil, "", err
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hash := sha256.New()
|
|
|
|
if _, err = hash.Write(body); err != nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
return nil, "", fmt.Errorf("while hashing %s: %w", i.Name, err)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
meow := hex.EncodeToString(hash.Sum(nil))
|
|
|
|
if meow != i.Versions[i.Version].Digest {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Errorf("Downloaded version doesn't match index, please 'hub update'")
|
|
|
|
i.hub.logger.Debugf("got %s, expected %s", meow, i.Versions[i.Version].Digest)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
return nil, "", fmt.Errorf("invalid download hash for %s", i.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
return body, url, nil
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// download downloads the item from the hub and writes it to the hub directory.
|
|
|
|
func (i *Item) download(overwrite bool) (string, error) {
|
2023-12-05 12:38:52 +00:00
|
|
|
if i.State.IsLocal() {
|
2023-11-28 22:51:51 +00:00
|
|
|
return "", fmt.Errorf("%s is local, can't download", i.Name)
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
// if user didn't --force, don't overwrite local, tainted, up-to-date files
|
|
|
|
if !overwrite {
|
|
|
|
if i.State.Tainted {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Debugf("%s: tainted, not updated", i.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
return "", nil
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if i.State.UpToDate {
|
|
|
|
// We still have to check if data files are present
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Debugf("%s: up-to-date, not updated", i.Name)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
body, url, err := i.FetchLatest()
|
2023-11-24 14:57:32 +00:00
|
|
|
if err != nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
return "", fmt.Errorf("while downloading %s: %w", url, err)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// all good, install
|
|
|
|
|
|
|
|
// ensure that target file is within target dir
|
|
|
|
finalPath, err := i.downloadPath()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
parentDir := filepath.Dir(finalPath)
|
|
|
|
|
|
|
|
if err = os.MkdirAll(parentDir, os.ModePerm); err != nil {
|
|
|
|
return "", fmt.Errorf("while creating %s: %w", parentDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check actual file
|
|
|
|
if _, err = os.Stat(finalPath); !os.IsNotExist(err) {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Warningf("%s: overwrite", i.Name)
|
|
|
|
i.hub.logger.Debugf("target: %s", finalPath)
|
2023-11-24 14:57:32 +00:00
|
|
|
} else {
|
2023-12-19 16:20:09 +00:00
|
|
|
i.hub.logger.Infof("%s: OK", i.Name)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.WriteFile(finalPath, body, 0o644); err != nil {
|
|
|
|
return "", fmt.Errorf("while writing %s: %w", finalPath, err)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
i.State.Downloaded = true
|
|
|
|
i.State.Tainted = false
|
|
|
|
i.State.UpToDate = true
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
if err = downloadDataSet(i.hub.local.InstallDataDir, overwrite, bytes.NewReader(body), i.hub.logger); err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return "", fmt.Errorf("while downloading data for %s: %w", i.FileName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return finalPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadDataIfNeeded downloads the data set for the item.
|
|
|
|
func (i *Item) DownloadDataIfNeeded(force bool) error {
|
|
|
|
itemFilePath, err := i.installPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
itemFile, err := os.Open(itemFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("while opening %s: %w", itemFilePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer itemFile.Close()
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
if err = downloadDataSet(i.hub.local.InstallDataDir, force, itemFile, i.hub.logger); err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return fmt.Errorf("while downloading data for %s: %w", itemFilePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|