Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update errors #468

Merged
merged 22 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9740f58
sources/almalinux: Update errors
monstermunchkin Jun 18, 2021
bf95992
sources/alpine: Update errors
monstermunchkin Jun 18, 2021
2105eea
sources/alt: Update errors
monstermunchkin Jun 21, 2021
b52a64c
sources/apertis: Update errors
monstermunchkin Jun 21, 2021
e46a0d7
sources/archlinux: Update errors
monstermunchkin Jun 21, 2021
5d30a88
sources/centos: Update errors
monstermunchkin Jun 21, 2021
67260dd
sources/debootstrap: Update errors
monstermunchkin Jun 21, 2021
307a5e3
sources/docker: Update errors
monstermunchkin Jun 21, 2021
91cdeb2
sources/fedora: Update errors
monstermunchkin Jun 21, 2021
5ff4252
sources/funtoo: Update errors
monstermunchkin Jun 21, 2021
b2ce7a3
sources/gentoo: Update errors
monstermunchkin Jun 21, 2021
268245a
sources/opensuse: Update errors
monstermunchkin Jun 21, 2021
faf79dd
sources/openwrt: Update errors
monstermunchkin Jun 21, 2021
3a58c7a
source/oraclelinux: Update errors
monstermunchkin Jun 21, 2021
a66a807
sources/plamolinux: Update errors
monstermunchkin Jun 21, 2021
e2eb573
sources/rhel-common: Update errors
monstermunchkin Jun 21, 2021
7e53482
sources/rocky: Update errors
monstermunchkin Jun 21, 2021
aa09e19
sources/rootfs: Update errors
monstermunchkin Jun 21, 2021
1252c64
sources/sabayon: Update errors
monstermunchkin Jun 21, 2021
b0fdeef
sources/springdalelinux: Update errors
monstermunchkin Jun 21, 2021
2dc4e7f
sources/ubuntu: Update errors
monstermunchkin Jun 21, 2021
f2a6663
sources/voidlinux: Update errors
monstermunchkin Jun 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 20 additions & 18 deletions sources/almalinux-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ type almalinux struct {

// Run downloads the tarball and unpacks it.
func (s *almalinux) Run() error {
var err error

s.majorVersion = strings.Split(s.definition.Image.Release, ".")[0]

baseURL := fmt.Sprintf("%s/%s/isos/%s/", s.definition.Source.URL,
strings.ToLower(s.definition.Image.Release),
s.definition.Image.ArchitectureMapped)
s.fname = s.getRelease(s.definition.Source.URL, s.definition.Image.Release,
s.fname, err = s.getRelease(s.definition.Source.URL, s.definition.Image.Release,
s.definition.Source.Variant, s.definition.Image.ArchitectureMapped)
if s.fname == "" {
return fmt.Errorf("Couldn't get name of iso")
if err != nil {
return errors.Wrap(err, "Failed to get release")
}

fpath := shared.GetTargetDir(s.definition.Image)
Expand All @@ -52,7 +54,7 @@ func (s *almalinux) Run() error {

url, err := url.Parse(baseURL)
if err != nil {
return err
return errors.Wrapf(err, "Failed to parse URL %q", baseURL)
}

checksumFile := ""
Expand All @@ -75,26 +77,26 @@ func (s *almalinux) Run() error {

fpath, err := shared.DownloadHash(s.definition.Image, baseURL+checksumFile, "", nil)
if err != nil {
return err
return errors.Wrapf(err, "Failed to download %q", baseURL+checksumFile)
}

// Only verify file if possible.
if strings.HasSuffix(checksumFile, ".asc") {
valid, err := shared.VerifyFile(filepath.Join(fpath, checksumFile), "",
s.definition.Source.Keys, s.definition.Source.Keyserver)
if err != nil {
return err
return errors.Wrapf(err, "Failed to verify %q", checksumFile)
}
if !valid {
return errors.New("Failed to verify tarball")
return errors.Errorf("Invalid signature for %q", filepath.Join(fpath, checksumFile))
}
}
}
}

_, err = shared.DownloadHash(s.definition.Image, baseURL+s.fname, checksumFile, sha256.New())
if err != nil {
return errors.Wrap(err, "Error downloading AlmaLinux image")
return errors.Wrapf(err, "Failed to download %q", baseURL+s.fname)
}

if strings.HasSuffix(s.fname, ".raw.xz") || strings.HasSuffix(s.fname, ".raw") {
Expand All @@ -117,7 +119,7 @@ func (s *almalinux) rawRunner() error {
rm -rf /rootfs/var/cache/yum
`, s.majorVersion))
if err != nil {
return err
return errors.Wrap(err, "Failed to run script")
}

return nil
Expand Down Expand Up @@ -244,36 +246,36 @@ yum ${yum_args} --installroot=/rootfs -y --releasever=%s --skip-broken install $
rm -rf /rootfs/var/cache/yum
`, gpgKeysPath, s.majorVersion))
if err != nil {
return err
return errors.Wrap(err, "Failed to run script")
}

return nil
}

func (s *almalinux) getRelease(URL, release, variant, arch string) string {
resp, err := http.Get(URL + path.Join("/", strings.ToLower(release), "isos", arch))
func (s *almalinux) getRelease(URL, release, variant, arch string) (string, error) {
fullURL := URL + path.Join("/", strings.ToLower(release), "isos", arch)

resp, err := http.Get(fullURL)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
return "", errors.Wrapf(err, "Failed to GET %q", fullURL)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
return "", errors.Wrap(err, "Failed to read body")
}

re := s.getRegexes(arch, variant, release)

for _, r := range re {
matches := r.FindAllString(string(body), -1)
if len(matches) > 0 {
return matches[len(matches)-1]
return matches[len(matches)-1], nil
}
}

return ""
return "", nil
}

func (s *almalinux) getRegexes(arch string, variant string, release string) []*regexp.Regexp {
Expand Down
22 changes: 11 additions & 11 deletions sources/alpine-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *alpineLinux) Run() error {

if s.definition.Image.Release == "edge" {
if s.definition.Source.SameAs == "" {
return fmt.Errorf("You can't use Alpine edge without setting same_as")
return errors.New("You can't use Alpine edge without setting same_as")
}

releaseFull = s.definition.Source.SameAs
Expand All @@ -38,7 +38,7 @@ func (s *alpineLinux) Run() error {
} else if len(releaseField) == 3 {
releaseShort = fmt.Sprintf("v%s.%s", releaseField[0], releaseField[1])
} else {
return fmt.Errorf("Bad Alpine release: %s", releaseFull)
return errors.Errorf("Bad Alpine release: %s", releaseFull)
}

fname := fmt.Sprintf("alpine-minirootfs-%s-%s.tar.gz", releaseFull, s.definition.Image.ArchitectureMapped)
Expand All @@ -47,7 +47,7 @@ func (s *alpineLinux) Run() error {

url, err := url.Parse(tarball)
if err != nil {
return err
return errors.Wrapf(err, "Failed to parse URL %q", tarball)
}

if !s.definition.Source.SkipVerification && url.Scheme != "https" &&
Expand All @@ -63,7 +63,7 @@ func (s *alpineLinux) Run() error {
fpath, err = shared.DownloadHash(s.definition.Image, tarball, tarball+".sha256", sha256.New())
}
if err != nil {
return err
return errors.Wrapf(err, "Failed to download %q", tarball)
}

// Force gpg checks when using http
Expand All @@ -75,37 +75,37 @@ func (s *alpineLinux) Run() error {
s.definition.Source.Keys,
s.definition.Source.Keyserver)
if err != nil {
return err
return errors.Wrapf(err, "Failed to download %q", tarball+".asc")
}
if !valid {
return errors.New("Failed to verify tarball")
return errors.Errorf("Invalid signature for %q", filepath.Join(fpath, fname))
}
}

// Unpack
err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil)
if err != nil {
return err
return errors.Wrapf(err, "Failed to unpack %q", fname)
}

// Handle edge builds
if s.definition.Image.Release == "edge" {
// Upgrade to edge
exitChroot, err := shared.SetupChroot(s.rootfsDir, s.definition.Environment, nil)
if err != nil {
return err
return errors.Wrap(err, "Failed to set up chroot")
}

err = shared.RunCommand("sed", "-i", "-e", "s/v[[:digit:]]\\.[[:digit:]]\\+/edge/g", "/etc/apk/repositories")
if err != nil {
exitChroot()
return err
return errors.Wrap(err, "Failed to edit apk repositories")
}

err = shared.RunCommand("apk", "upgrade", "--update-cache", "--available")
if err != nil {
exitChroot()
return err
return errors.Wrap(err, "Failed to upgrade edge build")
}

exitChroot()
Expand All @@ -114,7 +114,7 @@ func (s *alpineLinux) Run() error {
// Fix bad permissions in Alpine tarballs
err = os.Chmod(s.rootfsDir, 0755)
if err != nil {
return err
return errors.Wrapf(err, "Failed to chmod %q", s.rootfsDir)
}

return nil
Expand Down
17 changes: 10 additions & 7 deletions sources/alt-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *altLinux) Run() error {

url, err := url.Parse(baseURL)
if err != nil {
return err
return errors.Wrapf(err, "Failed to parse URL %q", baseURL)
}

checksumFile := ""
Expand All @@ -35,21 +35,24 @@ func (s *altLinux) Run() error {

fpath, err := shared.DownloadHash(s.definition.Image, checksumFile+".asc", "", nil)
if err != nil {
return err
return errors.Wrapf(err, "Failed to download %q", checksumFile+".asc")
}

shared.DownloadHash(s.definition.Image, checksumFile, "", nil)
_, err = shared.DownloadHash(s.definition.Image, checksumFile, "", nil)
if err != nil {
return errors.Wrapf(err, "Failed to download %q", checksumFile)
}

valid, err := shared.VerifyFile(
filepath.Join(fpath, "SHA256SUM"),
filepath.Join(fpath, "SHA256SUM.asc"),
s.definition.Source.Keys,
s.definition.Source.Keyserver)
if err != nil {
return err
return errors.Wrap(err, "Failed to verify file")
}
if !valid {
return fmt.Errorf("Failed to validate tarball")
return errors.Errorf("Invalid signature for %q", "SHA256SUM")
}
} else {
// Force gpg checks when using http
Expand All @@ -61,13 +64,13 @@ func (s *altLinux) Run() error {

fpath, err := shared.DownloadHash(s.definition.Image, baseURL+fname, checksumFile, sha256.New())
if err != nil {
return err
return errors.Wrapf(err, "Failed to download %q", baseURL+fname)
}

// Unpack
err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil)
if err != nil {
return err
return errors.Wrapf(err, "Failed to unpack %q", fname)
}

return nil
Expand Down
28 changes: 14 additions & 14 deletions sources/apertis-http.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package sources

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"

lxd "github.com/lxc/lxd/shared"
"github.com/pkg/errors"

"github.com/lxc/distrobuilder/shared"
)
Expand All @@ -31,7 +30,7 @@ func (s *apertis) Run() error {

resp, err := http.Head(baseURL)
if err != nil {
return err
return errors.Wrapf(err, "Failed to HEAD %q", baseURL)
}

if resp.StatusCode == http.StatusNotFound {
Expand All @@ -41,7 +40,10 @@ func (s *apertis) Run() error {
baseURL = fmt.Sprintf("%s/%s/%s",
s.definition.Source.URL, s.definition.Source.Variant, release)
} else {
exactRelease = s.getLatestRelease(baseURL, release)
exactRelease, err = s.getLatestRelease(baseURL, release)
if err != nil {
return errors.Wrap(err, "Failed to get latest release")
}
}

baseURL = fmt.Sprintf("%s/%s/%s/%s/",
Expand All @@ -51,7 +53,7 @@ func (s *apertis) Run() error {

url, err := url.Parse(baseURL)
if err != nil {
return err
return errors.Wrapf(err, "Failed to parse %q", baseURL)
}

// Force gpg checks when using http
Expand All @@ -61,38 +63,36 @@ func (s *apertis) Run() error {

fpath, err := shared.DownloadHash(s.definition.Image, baseURL+fname, "", nil)
if err != nil {
return err
return errors.Wrapf(err, "Failed to download %q", baseURL+fname)
}

// Unpack
err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil)
if err != nil {
return err
return errors.Wrapf(err, "Failed to unpack %q", fname)
}

return nil
}

func (s *apertis) getLatestRelease(baseURL, release string) string {
func (s *apertis) getLatestRelease(baseURL, release string) (string, error) {
resp, err := http.Get(baseURL)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
return "", errors.Wrapf(err, "Failed to GET %q", baseURL)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
return "", errors.Wrap(err, "Failed to ready body")
}

regex := regexp.MustCompile(fmt.Sprintf(">(%s\\.\\d+)/<", release))
releases := regex.FindAllStringSubmatch(string(body), -1)

if len(releases) > 0 {
return releases[len(releases)-1][1]
return releases[len(releases)-1][1], nil
}

return ""
return "", nil
}
4 changes: 3 additions & 1 deletion sources/apertis-http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func TestApertisHTTP_getLatestRelease(t *testing.T) {
}
for _, tt := range tests {
baseURL := fmt.Sprintf("https://images.apertis.org/release/%s", tt.release)
require.Equal(t, tt.want, s.getLatestRelease(baseURL, tt.release))
release, err := s.getLatestRelease(baseURL, tt.release)
require.NoError(t, err)
require.Equal(t, tt.want, release)
}
}