sources: Add AptSources

This allows the apt sources to be templated after the image has been
unpacked.

This resolves #31

Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
This commit is contained in:
Thomas Hipp 2018-03-07 12:14:24 +01:00
parent 81501c9220
commit b36650d5c9
No known key found for this signature in database
GPG Key ID: 993408D1137B7D51
3 changed files with 72 additions and 2 deletions

View File

@ -37,6 +37,7 @@ type DefinitionSource struct {
Keyserver string `yaml:"keyserver,omitempty"`
Variant string `yaml:"variant,omitempty"`
Suite string `yaml:"suite,omitempty"`
AptSources string `yaml:"apt_sources,omitempty"`
}
// A DefinitionTargetLXC represents LXC specific files as part of the metadata.

View File

@ -5,6 +5,8 @@ import (
"path"
"path/filepath"
"gopkg.in/flosch/pongo2.v3"
"github.com/lxc/distrobuilder/shared"
)
@ -57,5 +59,38 @@ func (s *Debootstrap) Run(source shared.DefinitionSource, release, arch, rootfsD
defer os.Remove(link)
}
return shared.RunCommand("debootstrap", args...)
err := shared.RunCommand("debootstrap", args...)
if err != nil {
return err
}
if source.AptSources != "" {
ctx := pongo2.Context{
"source": source,
// We use an anonymous struct instead of DefinitionImage because we
// need the mapped architecture, and Release is all one
// needs in the sources.list.
"image": struct {
Release string
}{
release,
},
}
out, err := shared.RenderTemplate(source.AptSources, ctx)
if err != nil {
return err
}
// Replace content of sources.list with the templated content.
file, err := os.Create(filepath.Join(rootfsDir, "etc", "apt", "sources.list"))
if err != nil {
return err
}
defer file.Close()
file.WriteString(out)
}
return nil
}

View File

@ -11,6 +11,7 @@ import (
"strings"
lxd "github.com/lxc/lxd/shared"
"gopkg.in/flosch/pongo2.v3"
"github.com/lxc/distrobuilder/shared"
)
@ -61,7 +62,40 @@ func (s *UbuntuHTTP) Run(source shared.DefinitionSource, release, arch, rootfsDi
return fmt.Errorf("Error downloading Ubuntu image: %s", err)
}
return s.unpack(filepath.Join(os.TempDir(), s.fname), rootfsDir)
err = s.unpack(filepath.Join(os.TempDir(), s.fname), rootfsDir)
if err != nil {
return err
}
if source.AptSources != "" {
ctx := pongo2.Context{
"source": source,
// We use an anonymous struct instead of DefinitionImage because we
// need the mapped architecture, and Release is all one needs in
// the sources.list.
"image": struct {
Release string
}{
release,
},
}
out, err := shared.RenderTemplate(source.AptSources, ctx)
if err != nil {
return err
}
// Replace content of sources.list with the templated content.
file, err := os.Create(filepath.Join(rootfsDir, "etc", "apt", "sources.list"))
if err != nil {
return err
}
defer file.Close()
file.WriteString(out)
}
return nil
}
func (s UbuntuHTTP) unpack(filePath, rootDir string) error {