Drop dir source and do http and docker source

Signed-off-by: Itxaka <itxaka@kairos.io>
This commit is contained in:
Itxaka
2025-04-03 16:46:59 +02:00
parent adb6aa2450
commit bcb6c40f5d
2 changed files with 21 additions and 39 deletions

View File

@@ -1099,12 +1099,12 @@ func validateSourceSysext(source string) error {
return nil return nil
} }
r, err := regexp.Compile(`^oci:|^dir:|^file:|^http:|^https:`) r, err := regexp.Compile(`^oci:|^file:|^http:|^https:`)
if err != nil { if err != nil {
return err return err
} }
if !r.MatchString(source) { if !r.MatchString(source) {
return fmt.Errorf("source %s does not match any of oci:, dir:, file: or http(s): ", source) return fmt.Errorf("source %s does not match any of oci:, file: or http(s): ", source)
} }
return nil return nil

View File

@@ -4,8 +4,10 @@ import (
"fmt" "fmt"
"github.com/distribution/reference" "github.com/distribution/reference"
"github.com/kairos-io/kairos-agent/v2/pkg/config" "github.com/kairos-io/kairos-agent/v2/pkg/config"
http2 "github.com/kairos-io/kairos-agent/v2/pkg/http"
"github.com/kairos-io/kairos-sdk/types"
"github.com/kairos-io/kairos-sdk/utils"
"io" "io"
"net/http"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@@ -249,6 +251,8 @@ func RemoveSystemExtension(cfg *config.Config, extension string) error {
return nil return nil
} }
// ParseURI parses a URI and returns a SourceDownload
// implementation based on the scheme of the URI
func parseURI(uri string) (SourceDownload, error) { func parseURI(uri string) (SourceDownload, error) {
u, err := url.Parse(uri) u, err := url.Parse(uri)
if err != nil { if err != nil {
@@ -268,12 +272,11 @@ func parseURI(uri string) (SourceDownload, error) {
value += ":latest" value += ":latest"
} }
return &dockerSource{value}, nil return &dockerSource{value}, nil
case "dir":
return &dirSource{value}, nil
case "file": case "file":
return &fileSource{value}, nil return &fileSource{value}, nil
case "http", "https": case "http", "https":
return &httpSource{value}, nil // Pass the full uri including the protocol
return &httpSource{uri}, nil
default: default:
return nil, fmt.Errorf("invalid URI reference %s", uri) return nil, fmt.Errorf("invalid URI reference %s", uri)
} }
@@ -333,39 +336,8 @@ type httpSource struct {
func (h httpSource) Download(s string) error { func (h httpSource) Download(s string) error {
// Download the file from the URI // Download the file from the URI
// and save it to the destination path // and save it to the destination path
get, err := http.Get(h.uri) client := http2.NewClient()
if err != nil { return client.GetURL(types.NewNullLogger(), h.uri, filepath.Join(s, filepath.Base(h.uri)))
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(get.Body)
if get.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download file %s: %s", h.uri, get.Status)
}
out, err := os.Create(filepath.Join(s, filepath.Base(h.uri)))
if err != nil {
return fmt.Errorf("failed to create file %s: %w", out.Name(), err)
}
defer func(out *os.File) {
_ = out.Close()
}(out)
if _, err := io.Copy(out, get.Body); err != nil {
return fmt.Errorf("failed to copy file %s to %s: %w", h.uri, out.Name(), err)
}
if err := out.Sync(); err != nil {
return fmt.Errorf("failed to sync file %s: %w", out.Name(), err)
}
return nil
}
type dirSource struct {
uri string
}
func (d dirSource) Download(s string) error {
return nil
} }
type dockerSource struct { type dockerSource struct {
@@ -373,5 +345,15 @@ type dockerSource struct {
} }
func (d dockerSource) Download(s string) error { func (d dockerSource) Download(s string) error {
// Download the file from the URI
// and save it to the destination path
image, err := utils.GetImage(d.uri, "", nil, nil)
if err != nil {
return err
}
err = utils.ExtractOCIImage(image, s)
if err != nil {
return err
}
return nil return nil
} }