diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index 14661215..ddb6a5ba 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -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 }