fix: only use git to update local catalogue

See coop-cloud/organising#321.
This commit is contained in:
decentral1se 2023-07-25 21:13:04 +02:00
parent 4f22228aab
commit ab64eb2e8d
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
1 changed files with 4 additions and 84 deletions

View File

@ -2,7 +2,6 @@ package recipe
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
@ -11,7 +10,6 @@ import (
"sort"
"strconv"
"strings"
"time"
"coopcloud.tech/abra/pkg/catalogue"
"coopcloud.tech/abra/pkg/compose"
@ -660,51 +658,6 @@ func EnsureUpToDate(recipeName string, conf *runtime.Config) error {
return nil
}
type CatalogueOfflineError struct {
msg string
}
func (e *CatalogueOfflineError) Error() string {
return fmt.Sprintf("catalogue offline: %s", e.msg)
}
// recipeCatalogueFSIsLatest checks whether the recipe catalogue stored locally
// is up to date.
func recipeCatalogueFSIsLatest() (bool, error) {
httpClient := web.NewHTTPRetryClient()
res, err := httpClient.Head(RecipeCatalogueURL)
if err != nil {
return false, &CatalogueOfflineError{err.Error()}
}
lastModified := res.Header["Last-Modified"][0]
parsed, err := time.Parse(time.RFC1123, lastModified)
if err != nil {
return false, err
}
info, err := os.Stat(config.RECIPES_JSON)
if err != nil {
if os.IsNotExist(err) {
logrus.Debugf("no recipe catalogue found in file system cache")
return false, nil
}
return false, err
}
localModifiedTime := info.ModTime().Unix()
remoteModifiedTime := parsed.Unix()
if localModifiedTime < remoteModifiedTime {
logrus.Debug("file system cached recipe catalogue is out-of-date")
return false, nil
}
logrus.Debug("file system cached recipe catalogue is up-to-date")
return true, nil
}
// ReadRecipeCatalogue reads the recipe catalogue.
func ReadRecipeCatalogue() (RecipeCatalogue, error) {
recipes := make(RecipeCatalogue)
@ -713,23 +666,8 @@ func ReadRecipeCatalogue() (RecipeCatalogue, error) {
return nil, err
}
recipeFSIsLatest, err := recipeCatalogueFSIsLatest()
if err != nil {
var offlineErr *CatalogueOfflineError
if errors.As(err, &offlineErr) {
logrus.Error(err)
logrus.Error("unable to retrieve catalogue from internet, using local copy.")
recipeFSIsLatest = true
} else {
return nil, err
}
}
if !recipeFSIsLatest {
if err := readRecipeCatalogueWeb(&recipes); err != nil {
return nil, err
}
return recipes, nil
if err := catalogue.EnsureUpToDate(); err != nil {
return nil, err
}
if err := readRecipeCatalogueFS(&recipes); err != nil {
@ -755,26 +693,6 @@ func readRecipeCatalogueFS(target interface{}) error {
return nil
}
// readRecipeCatalogueWeb reads the catalogue from the web.
func readRecipeCatalogueWeb(target interface{}) error {
if err := web.ReadJSON(RecipeCatalogueURL, &target); err != nil {
return err
}
recipesJSON, err := json.MarshalIndent(target, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(config.RECIPES_JSON, recipesJSON, 0764); err != nil {
return err
}
logrus.Debugf("read recipe catalogue from web at %s", RecipeCatalogueURL)
return nil
}
// VersionsOfService lists the version of a service.
func VersionsOfService(recipe, serviceName string) ([]string, error) {
var versions []string
@ -1076,7 +994,9 @@ func GetRecipeCatalogueVersions(recipeName string, catl RecipeCatalogue) ([]stri
}
}
}
sortVersionStrings(versions)
return versions, nil
}