Skip to content

Commit 05757e2

Browse files
authored
feat(backends install): allow to specify name and alias during manual installation (#5971)
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 240b790 commit 05757e2

File tree

4 files changed

+96
-72
lines changed

4 files changed

+96
-72
lines changed

core/application/startup.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ func New(opts ...config.AppOption) (*Application, error) {
5959
log.Error().Err(err).Msg("error installing models")
6060
}
6161

62-
if err := coreStartup.InstallExternalBackends(options.BackendGalleries, options.BackendsPath, nil, options.ExternalBackends...); err != nil {
63-
log.Error().Err(err).Msg("error installing external backends")
62+
for _, backend := range options.ExternalBackends {
63+
if err := coreStartup.InstallExternalBackends(options.BackendGalleries, options.BackendsPath, nil, backend, "", ""); err != nil {
64+
log.Error().Err(err).Msg("error installing external backend")
65+
}
6466
}
6567

6668
configLoaderOpts := options.ToConfigLoaderOptions()

core/cli/backends.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ type BackendsList struct {
2323
}
2424

2525
type BackendsInstall struct {
26-
BackendArgs []string `arg:"" optional:"" name:"backends" help:"Backend configuration URLs to load"`
26+
BackendArgs string `arg:"" optional:"" name:"backend" help:"Backend configuration URL to load"`
27+
Name string `arg:"" optional:"" name:"name" help:"Name of the backend"`
28+
Alias string `arg:"" optional:"" name:"alias" help:"Alias of the backend"`
2729

2830
BackendsCMDFlags `embed:""`
2931
}
@@ -66,27 +68,25 @@ func (bi *BackendsInstall) Run(ctx *cliContext.Context) error {
6668
log.Error().Err(err).Msg("unable to load galleries")
6769
}
6870

69-
for _, backendName := range bi.BackendArgs {
70-
71-
progressBar := progressbar.NewOptions(
72-
1000,
73-
progressbar.OptionSetDescription(fmt.Sprintf("downloading backend %s", backendName)),
74-
progressbar.OptionShowBytes(false),
75-
progressbar.OptionClearOnFinish(),
76-
)
77-
progressCallback := func(fileName string, current string, total string, percentage float64) {
78-
v := int(percentage * 10)
79-
err := progressBar.Set(v)
80-
if err != nil {
81-
log.Error().Err(err).Str("filename", fileName).Int("value", v).Msg("error while updating progress bar")
82-
}
83-
}
84-
85-
err := startup.InstallExternalBackends(galleries, bi.BackendsPath, progressCallback, backendName)
71+
progressBar := progressbar.NewOptions(
72+
1000,
73+
progressbar.OptionSetDescription(fmt.Sprintf("downloading backend %s", bi.BackendArgs)),
74+
progressbar.OptionShowBytes(false),
75+
progressbar.OptionClearOnFinish(),
76+
)
77+
progressCallback := func(fileName string, current string, total string, percentage float64) {
78+
v := int(percentage * 10)
79+
err := progressBar.Set(v)
8680
if err != nil {
87-
return err
81+
log.Error().Err(err).Str("filename", fileName).Int("value", v).Msg("error while updating progress bar")
8882
}
8983
}
84+
85+
err := startup.InstallExternalBackends(galleries, bi.BackendsPath, progressCallback, bi.BackendArgs, bi.Name, bi.Alias)
86+
if err != nil {
87+
return err
88+
}
89+
9090
return nil
9191
}
9292

core/startup/backend_preload.go

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package startup
22

33
import (
4-
"errors"
54
"fmt"
65
"path/filepath"
76
"strings"
@@ -13,49 +12,68 @@ import (
1312
"github.com/rs/zerolog/log"
1413
)
1514

16-
func InstallExternalBackends(galleries []config.Gallery, backendPath string, downloadStatus func(string, string, string, float64), backends ...string) error {
17-
var errs error
15+
func InstallExternalBackends(galleries []config.Gallery, backendPath string, downloadStatus func(string, string, string, float64), backend, name, alias string) error {
1816
systemState, err := system.GetSystemState()
1917
if err != nil {
2018
return fmt.Errorf("failed to get system state: %w", err)
2119
}
22-
for _, backend := range backends {
23-
uri := downloader.URI(backend)
24-
switch {
25-
case uri.LooksLikeDir():
26-
name := filepath.Base(backend)
27-
log.Info().Str("backend", backend).Str("name", name).Msg("Installing backend from path")
28-
if err := gallery.InstallBackend(backendPath, &gallery.GalleryBackend{
29-
Metadata: gallery.Metadata{
30-
Name: name,
31-
},
32-
URI: backend,
33-
}, downloadStatus); err != nil {
34-
errs = errors.Join(err, fmt.Errorf("error installing backend %s", backend))
35-
}
36-
case uri.LooksLikeOCI():
37-
name, err := uri.FilenameFromUrl()
38-
if err != nil {
39-
return fmt.Errorf("failed to get filename from URL: %w", err)
40-
}
41-
// strip extension if any
42-
name = strings.TrimSuffix(name, filepath.Ext(name))
20+
uri := downloader.URI(backend)
21+
switch {
22+
case uri.LooksLikeDir():
23+
if name == "" { // infer it from the path
24+
name = filepath.Base(backend)
25+
}
26+
log.Info().Str("backend", backend).Str("name", name).Msg("Installing backend from path")
27+
if err := gallery.InstallBackend(backendPath, &gallery.GalleryBackend{
28+
Metadata: gallery.Metadata{
29+
Name: name,
30+
},
31+
Alias: alias,
32+
URI: backend,
33+
}, downloadStatus); err != nil {
34+
return fmt.Errorf("error installing backend %s: %w", backend, err)
35+
}
36+
case uri.LooksLikeOCI() && !uri.LooksLikeOCIFile():
37+
if name == "" {
38+
return fmt.Errorf("specifying a name is required for OCI images")
39+
}
40+
log.Info().Str("backend", backend).Str("name", name).Msg("Installing backend from OCI image")
41+
if err := gallery.InstallBackend(backendPath, &gallery.GalleryBackend{
42+
Metadata: gallery.Metadata{
43+
Name: name,
44+
},
45+
Alias: alias,
46+
URI: backend,
47+
}, downloadStatus); err != nil {
48+
return fmt.Errorf("error installing backend %s: %w", backend, err)
49+
}
50+
case uri.LooksLikeOCIFile():
51+
name, err := uri.FilenameFromUrl()
52+
if err != nil {
53+
return fmt.Errorf("failed to get filename from URL: %w", err)
54+
}
55+
// strip extension if any
56+
name = strings.TrimSuffix(name, filepath.Ext(name))
4357

44-
log.Info().Str("backend", backend).Str("name", name).Msg("Installing backend from OCI image")
45-
if err := gallery.InstallBackend(backendPath, &gallery.GalleryBackend{
46-
Metadata: gallery.Metadata{
47-
Name: name,
48-
},
49-
URI: backend,
50-
}, downloadStatus); err != nil {
51-
errs = errors.Join(err, fmt.Errorf("error installing backend %s", backend))
52-
}
53-
default:
54-
err := gallery.InstallBackendFromGallery(galleries, systemState, backend, backendPath, downloadStatus, true)
55-
if err != nil {
56-
errs = errors.Join(err, fmt.Errorf("error installing backend %s", backend))
57-
}
58+
log.Info().Str("backend", backend).Str("name", name).Msg("Installing backend from OCI image")
59+
if err := gallery.InstallBackend(backendPath, &gallery.GalleryBackend{
60+
Metadata: gallery.Metadata{
61+
Name: name,
62+
},
63+
Alias: alias,
64+
URI: backend,
65+
}, downloadStatus); err != nil {
66+
return fmt.Errorf("error installing backend %s: %w", backend, err)
67+
}
68+
default:
69+
if name != "" || alias != "" {
70+
return fmt.Errorf("specifying a name or alias is not supported for this backend")
71+
}
72+
err := gallery.InstallBackendFromGallery(galleries, systemState, backend, backendPath, downloadStatus, true)
73+
if err != nil {
74+
return fmt.Errorf("error installing backend %s: %w", backend, err)
5875
}
5976
}
60-
return errs
77+
78+
return nil
6179
}

pkg/downloader/uri.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,33 +98,33 @@ func (uri URI) DownloadWithAuthorizationAndCallback(basePath string, authorizati
9898
}
9999

100100
func (u URI) FilenameFromUrl() (string, error) {
101-
f, err := filenameFromUrl(string(u))
102-
if err != nil || f == "" {
103-
f = utils.MD5(string(u))
104-
if strings.HasSuffix(string(u), ".yaml") || strings.HasSuffix(string(u), ".yml") {
105-
f = f + ".yaml"
106-
}
107-
err = nil
101+
if f := filenameFromUrl(string(u)); f != "" {
102+
return f, nil
103+
}
104+
105+
f := utils.MD5(string(u))
106+
if strings.HasSuffix(string(u), ".yaml") || strings.HasSuffix(string(u), ".yml") {
107+
f = f + ".yaml"
108108
}
109109

110-
return f, err
110+
return f, nil
111111
}
112112

113-
func filenameFromUrl(urlstr string) (string, error) {
113+
func filenameFromUrl(urlstr string) string {
114114
// strip anything after @
115115
if strings.Contains(urlstr, "@") {
116116
urlstr = strings.Split(urlstr, "@")[0]
117117
}
118118

119119
u, err := url.Parse(urlstr)
120120
if err != nil {
121-
return "", fmt.Errorf("error due to parsing url: %w", err)
121+
return ""
122122
}
123123
x, err := url.QueryUnescape(u.EscapedPath())
124124
if err != nil {
125-
return "", fmt.Errorf("error due to escaping: %w", err)
125+
return ""
126126
}
127-
return filepath.Base(x), nil
127+
return filepath.Base(x)
128128
}
129129

130130
func (u URI) LooksLikeURL() bool {
@@ -158,6 +158,10 @@ func (s URI) LooksLikeOCI() bool {
158158
strings.HasPrefix(string(s), "docker.io")
159159
}
160160

161+
func (s URI) LooksLikeOCIFile() bool {
162+
return strings.HasPrefix(string(s), OCIFilePrefix)
163+
}
164+
161165
func (s URI) ResolveURL() string {
162166
switch {
163167
case strings.HasPrefix(string(s), GithubURI2):

0 commit comments

Comments
 (0)