Skip to content

Commit

Permalink
Detect 404 not found error when downloading using curl (#657)
Browse files Browse the repository at this point in the history
* Detect 404 not found error when downloading using curl

Signed-off-by: Kimmo Lehto <[email protected]>

* Add hint about version validity

Signed-off-by: Kimmo Lehto <[email protected]>

---------

Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke authored Feb 15, 2024
1 parent 3a57765 commit c6da051
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions configurer/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,37 @@ func (l *Linux) TempDir(h os.Host) (string, error) {
return h.ExecOutput("mktemp -d")
}

var trailingNumberRegex = regexp.MustCompile(`(\d+)$`)

func trailingNumber(s string) (int, bool) {
match := trailingNumberRegex.FindStringSubmatch(s)
if len(match) > 0 {
i, err := strconv.Atoi(match[1])
if err == nil {
return i, true
}
}
return 0, false
}

// DownloadURL performs a download from a URL on the host
func (l *Linux) DownloadURL(h os.Host, url, destination string, opts ...exec.Option) error {
return h.Exec(fmt.Sprintf(`curl -sSLf -o %s %s`, shellescape.Quote(destination), shellescape.Quote(url)), opts...)
err := h.Exec(fmt.Sprintf(`curl -sSLf -o %s %s`, shellescape.Quote(destination), shellescape.Quote(url)), opts...)
if err != nil {
if exitCode, ok := trailingNumber(err.Error()); ok && exitCode == 22 {
return fmt.Errorf("download failed: http 404 - not found: %w", err)
}
return fmt.Errorf("download failed: %w", err)
}
return nil
}

// DownloadK0s performs k0s binary download from github on the host
func (l *Linux) DownloadK0s(h os.Host, path string, version *version.Version, arch string) error {
v := strings.ReplaceAll(strings.TrimPrefix(version.String(), "v"), "+", "%2B")
url := fmt.Sprintf("https://github.com/k0sproject/k0s/releases/download/v%[1]s/k0s-v%[1]s-%[2]s", v, arch)
if err := l.DownloadURL(h, url, path); err != nil {
return fmt.Errorf("download k0s: %w", err)
return fmt.Errorf("failed to download k0s - check connectivity and k0s version validity: %w", err)
}

return nil
Expand Down

0 comments on commit c6da051

Please sign in to comment.