sources: Simplify Run command signature

Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
This commit is contained in:
Thomas Hipp 2018-03-16 12:58:14 +01:00
parent 0612992b66
commit dd35a5830b
No known key found for this signature in database
GPG Key ID: 993408D1137B7D51
7 changed files with 36 additions and 24 deletions

View File

@ -188,8 +188,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
} }
// Download the root filesystem // Download the root filesystem
err = downloader.Run(*c.definition, c.definition.Image.Release, err = downloader.Run(*c.definition, c.sourceDir)
c.definition.Image.MappedArchitecture, c.sourceDir)
if err != nil { if err != nil {
return fmt.Errorf("Error while downloading source: %s", err) return fmt.Errorf("Error while downloading source: %s", err)
} }

View File

@ -22,10 +22,12 @@ func NewAlpineLinuxHTTP() *AlpineLinuxHTTP {
} }
// Run downloads an Alpine Linux mini root filesystem. // Run downloads an Alpine Linux mini root filesystem.
func (s *AlpineLinuxHTTP) Run(definition shared.Definition, release, arch, rootfsDir string) error { func (s *AlpineLinuxHTTP) Run(definition shared.Definition, rootfsDir string) error {
fname := fmt.Sprintf("alpine-minirootfs-%s-%s.tar.gz", release, arch) 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, 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) url, err := url.Parse(tarball)
if err != nil { if err != nil {

View File

@ -22,9 +22,11 @@ func NewArchLinuxHTTP() *ArchLinuxHTTP {
} }
// Run downloads an Arch Linux tarball. // Run downloads an Arch Linux tarball.
func (s *ArchLinuxHTTP) Run(definition shared.Definition, release, arch, rootfsDir string) error { func (s *ArchLinuxHTTP) Run(definition shared.Definition, rootfsDir string) error {
fname := fmt.Sprintf("archlinux-bootstrap-%s-x86_64.tar.gz", release) fname := fmt.Sprintf("archlinux-bootstrap-%s-%s.tar.gz",
tarball := fmt.Sprintf("%s/%s/%s", definition.Source.URL, release, fname) definition.Image.Release, definition.Image.MappedArchitecture)
tarball := fmt.Sprintf("%s/%s/%s", definition.Source.URL,
definition.Image.Release, fname)
url, err := url.Parse(tarball) url, err := url.Parse(tarball)
if err != nil { 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 // Move everything inside 'root.x86_64' (which was is the tarball) to its
// parent directory // 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 { if err != nil {
return err 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))
} }

View File

@ -27,10 +27,13 @@ func NewCentOSHTTP() *CentOSHTTP {
} }
// Run downloads the tarball and unpacks it. // Run downloads the tarball and unpacks it.
func (s *CentOSHTTP) Run(definition shared.Definition, release, arch, rootfsDir string) error { func (s *CentOSHTTP) Run(definition shared.Definition, rootfsDir string) error {
baseURL := fmt.Sprintf("%s/%s/isos/%s/", definition.Source.URL, strings.Split(release, ".")[0], arch) 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 == "" { if s.fname == "" {
return fmt.Errorf("Couldn't get name of iso") return fmt.Errorf("Couldn't get name of iso")
} }

View File

@ -18,7 +18,7 @@ func NewDebootstrap() *Debootstrap {
} }
// Run runs 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 var args []string
os.RemoveAll(rootfsDir) os.RemoveAll(rootfsDir)
@ -27,8 +27,8 @@ func (s *Debootstrap) Run(definition shared.Definition, release, arch, rootfsDir
args = append(args, "--variant", definition.Source.Variant) args = append(args, "--variant", definition.Source.Variant)
} }
if arch != "" { if definition.Image.MappedArchitecture != "" {
args = append(args, "--arch", arch) args = append(args, "--arch", definition.Image.MappedArchitecture)
} }
if len(definition.Source.Keys) > 0 { 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, "--keyring", keyring)
} }
args = append(args, release, rootfsDir) args = append(args, definition.Image.Release, rootfsDir)
if definition.Source.URL != "" { if definition.Source.URL != "" {
args = append(args, 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 // If definition.Source.Suite is set, create a symlink in /usr/share/debootstrap/scripts
// pointing release to definition.Source.Suite. // pointing release to definition.Source.Suite.
if 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) err := os.Symlink(definition.Source.Suite, link)
if err != nil { if err != nil {
return err return err

View File

@ -4,7 +4,7 @@ import "github.com/lxc/distrobuilder/shared"
// A Downloader represents a source downloader. // A Downloader represents a source downloader.
type Downloader interface { type Downloader interface {
Run(shared.Definition, string, string, string) error Run(shared.Definition, string) error
} }
// Get returns a Downloader. // Get returns a Downloader.

View File

@ -28,14 +28,17 @@ func NewUbuntuHTTP() *UbuntuHTTP {
} }
// Run downloads the tarball and unpacks it. // Run downloads the tarball and unpacks it.
func (s *UbuntuHTTP) Run(definition shared.Definition, release, arch, rootfsDir string) error { func (s *UbuntuHTTP) Run(definition shared.Definition, rootfsDir string) error {
baseURL := fmt.Sprintf("%s/releases/%s/release/", definition.Source.URL, release) baseURL := fmt.Sprintf("%s/releases/%s/release/", definition.Source.URL,
definition.Image.Release)
if strings.ContainsAny(release, "0123456789") { if strings.ContainsAny(definition.Image.Release, "0123456789") {
s.fname = fmt.Sprintf("ubuntu-base-%s-base-%s.tar.gz", release, arch) s.fname = fmt.Sprintf("ubuntu-base-%s-base-%s.tar.gz",
definition.Image.Release, definition.Image.MappedArchitecture)
} else { } else {
// if release is non-numerical, find the latest release // 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 == "" { if s.fname == "" {
return fmt.Errorf("Couldn't find latest release") return fmt.Errorf("Couldn't find latest release")
} }