From dd35a5830b356a98fde6104e320a5cba187d6958 Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Fri, 16 Mar 2018 12:58:14 +0100 Subject: [PATCH] sources: Simplify Run command signature Signed-off-by: Thomas Hipp --- distrobuilder/main.go | 3 +-- sources/alpine-http.go | 8 +++++--- sources/archlinux-http.go | 14 +++++++++----- sources/centos-http.go | 9 ++++++--- sources/debootstrap.go | 11 ++++++----- sources/source.go | 2 +- sources/ubuntu-http.go | 13 ++++++++----- 7 files changed, 36 insertions(+), 24 deletions(-) diff --git a/distrobuilder/main.go b/distrobuilder/main.go index 8e20b49..26d479c 100644 --- a/distrobuilder/main.go +++ b/distrobuilder/main.go @@ -188,8 +188,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error { } // Download the root filesystem - err = downloader.Run(*c.definition, c.definition.Image.Release, - c.definition.Image.MappedArchitecture, c.sourceDir) + err = downloader.Run(*c.definition, c.sourceDir) if err != nil { return fmt.Errorf("Error while downloading source: %s", err) } diff --git a/sources/alpine-http.go b/sources/alpine-http.go index a9f2c3d..e52354c 100644 --- a/sources/alpine-http.go +++ b/sources/alpine-http.go @@ -22,10 +22,12 @@ func NewAlpineLinuxHTTP() *AlpineLinuxHTTP { } // Run downloads an Alpine Linux mini root filesystem. -func (s *AlpineLinuxHTTP) Run(definition shared.Definition, release, arch, rootfsDir string) error { - fname := fmt.Sprintf("alpine-minirootfs-%s-%s.tar.gz", release, arch) +func (s *AlpineLinuxHTTP) Run(definition shared.Definition, rootfsDir string) error { + fname := fmt.Sprintf("alpine-minirootfs-%s-%s.tar.gz", definition.Image.Release, + definition.Image.MappedArchitecture) tarball := fmt.Sprintf("%s/v%s/releases/%s/%s", definition.Source.URL, - strings.Join(strings.Split(release, ".")[0:2], "."), arch, fname) + strings.Join(strings.Split(definition.Image.Release, ".")[0:2], "."), + definition.Image.MappedArchitecture, fname) url, err := url.Parse(tarball) if err != nil { diff --git a/sources/archlinux-http.go b/sources/archlinux-http.go index 33c4cbf..df96cea 100644 --- a/sources/archlinux-http.go +++ b/sources/archlinux-http.go @@ -22,9 +22,11 @@ func NewArchLinuxHTTP() *ArchLinuxHTTP { } // Run downloads an Arch Linux tarball. -func (s *ArchLinuxHTTP) Run(definition shared.Definition, release, arch, rootfsDir string) error { - fname := fmt.Sprintf("archlinux-bootstrap-%s-x86_64.tar.gz", release) - tarball := fmt.Sprintf("%s/%s/%s", definition.Source.URL, release, fname) +func (s *ArchLinuxHTTP) Run(definition shared.Definition, rootfsDir string) error { + fname := fmt.Sprintf("archlinux-bootstrap-%s-%s.tar.gz", + definition.Image.Release, definition.Image.MappedArchitecture) + tarball := fmt.Sprintf("%s/%s/%s", definition.Source.URL, + definition.Image.Release, fname) url, err := url.Parse(tarball) if err != nil { @@ -65,7 +67,8 @@ func (s *ArchLinuxHTTP) Run(definition shared.Definition, release, arch, rootfsD // Move everything inside 'root.x86_64' (which was is the tarball) to its // parent directory - files, err := filepath.Glob(fmt.Sprintf("%s/*", filepath.Join(rootfsDir, "root.x86_64"))) + files, err := filepath.Glob(fmt.Sprintf("%s/*", filepath.Join(rootfsDir, + "root", definition.Image.MappedArchitecture))) if err != nil { return err } @@ -77,5 +80,6 @@ func (s *ArchLinuxHTTP) Run(definition shared.Definition, release, arch, rootfsD } } - return os.RemoveAll(filepath.Join(rootfsDir, "root.x86_64")) + return os.RemoveAll(filepath.Join(rootfsDir, "root", + definition.Image.MappedArchitecture)) } diff --git a/sources/centos-http.go b/sources/centos-http.go index 1065f68..9c3f7c3 100644 --- a/sources/centos-http.go +++ b/sources/centos-http.go @@ -27,10 +27,13 @@ func NewCentOSHTTP() *CentOSHTTP { } // Run downloads the tarball and unpacks it. -func (s *CentOSHTTP) Run(definition shared.Definition, release, arch, rootfsDir string) error { - baseURL := fmt.Sprintf("%s/%s/isos/%s/", definition.Source.URL, strings.Split(release, ".")[0], arch) +func (s *CentOSHTTP) Run(definition shared.Definition, rootfsDir string) error { + baseURL := fmt.Sprintf("%s/%s/isos/%s/", definition.Source.URL, + strings.Split(definition.Image.Release, ".")[0], + definition.Image.MappedArchitecture) - s.fname = getRelease(definition.Source.URL, release, definition.Source.Variant, arch) + s.fname = getRelease(definition.Source.URL, definition.Image.Release, + definition.Source.Variant, definition.Image.MappedArchitecture) if s.fname == "" { return fmt.Errorf("Couldn't get name of iso") } diff --git a/sources/debootstrap.go b/sources/debootstrap.go index 9cf188f..468bda0 100644 --- a/sources/debootstrap.go +++ b/sources/debootstrap.go @@ -18,7 +18,7 @@ func NewDebootstrap() *Debootstrap { } // Run runs debootstrap. -func (s *Debootstrap) Run(definition shared.Definition, release, arch, rootfsDir string) error { +func (s *Debootstrap) Run(definition shared.Definition, rootfsDir string) error { var args []string os.RemoveAll(rootfsDir) @@ -27,8 +27,8 @@ func (s *Debootstrap) Run(definition shared.Definition, release, arch, rootfsDir args = append(args, "--variant", definition.Source.Variant) } - if arch != "" { - args = append(args, "--arch", arch) + if definition.Image.MappedArchitecture != "" { + args = append(args, "--arch", definition.Image.MappedArchitecture) } if len(definition.Source.Keys) > 0 { @@ -41,7 +41,7 @@ func (s *Debootstrap) Run(definition shared.Definition, release, arch, rootfsDir args = append(args, "--keyring", keyring) } - args = append(args, release, rootfsDir) + args = append(args, definition.Image.Release, rootfsDir) if definition.Source.URL != "" { args = append(args, definition.Source.URL) @@ -50,7 +50,8 @@ func (s *Debootstrap) Run(definition shared.Definition, release, arch, rootfsDir // If definition.Source.Suite is set, create a symlink in /usr/share/debootstrap/scripts // pointing release to definition.Source.Suite. if definition.Source.Suite != "" { - link := filepath.Join("/usr/share/debootstrap/scripts", release) + link := filepath.Join("/usr/share/debootstrap/scripts", + definition.Image.Release) err := os.Symlink(definition.Source.Suite, link) if err != nil { return err diff --git a/sources/source.go b/sources/source.go index bd57943..ccdc686 100644 --- a/sources/source.go +++ b/sources/source.go @@ -4,7 +4,7 @@ import "github.com/lxc/distrobuilder/shared" // A Downloader represents a source downloader. type Downloader interface { - Run(shared.Definition, string, string, string) error + Run(shared.Definition, string) error } // Get returns a Downloader. diff --git a/sources/ubuntu-http.go b/sources/ubuntu-http.go index 65a2f2b..14cb6e2 100644 --- a/sources/ubuntu-http.go +++ b/sources/ubuntu-http.go @@ -28,14 +28,17 @@ func NewUbuntuHTTP() *UbuntuHTTP { } // Run downloads the tarball and unpacks it. -func (s *UbuntuHTTP) Run(definition shared.Definition, release, arch, rootfsDir string) error { - baseURL := fmt.Sprintf("%s/releases/%s/release/", definition.Source.URL, release) +func (s *UbuntuHTTP) Run(definition shared.Definition, rootfsDir string) error { + baseURL := fmt.Sprintf("%s/releases/%s/release/", definition.Source.URL, + definition.Image.Release) - if strings.ContainsAny(release, "0123456789") { - s.fname = fmt.Sprintf("ubuntu-base-%s-base-%s.tar.gz", release, arch) + if strings.ContainsAny(definition.Image.Release, "0123456789") { + s.fname = fmt.Sprintf("ubuntu-base-%s-base-%s.tar.gz", + definition.Image.Release, definition.Image.MappedArchitecture) } else { // if release is non-numerical, find the latest release - s.fname = getLatestRelease(definition.Source.URL, release, arch) + s.fname = getLatestRelease(definition.Source.URL, + definition.Image.Release, definition.Image.MappedArchitecture) if s.fname == "" { return fmt.Errorf("Couldn't find latest release") }