mirror of
https://github.com/mudler/luet.git
synced 2025-09-01 15:18:28 +00:00
Add SimpleDocker backend
This commit is contained in:
28
pkg/compiler/backend/backend_suite_test.go
Normal file
28
pkg/compiler/backend/backend_suite_test.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License along
|
||||||
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package backend_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSolver(t *testing.T) {
|
||||||
|
RegisterFailHandler(Fail)
|
||||||
|
RunSpecs(t, "Backend Suite")
|
||||||
|
}
|
94
pkg/compiler/backend/simpledocker.go
Normal file
94
pkg/compiler/backend/simpledocker.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License along
|
||||||
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package backend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/mudler/luet/pkg/compiler"
|
||||||
|
. "github.com/mudler/luet/pkg/logger"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SimpleDocker struct{}
|
||||||
|
|
||||||
|
func NewSimpleDockerBackend() compiler.CompilerBackend {
|
||||||
|
return &SimpleDocker{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Missing still: labels, and build args expansion
|
||||||
|
func (*SimpleDocker) BuildImage(opts compiler.CompilerBackendOptions) error {
|
||||||
|
name := opts.ImageName
|
||||||
|
path := opts.SourcePath
|
||||||
|
dockerfileName := opts.DockerFileName
|
||||||
|
buildarg := []string{"build", "-f", dockerfileName, "-t", name, "."}
|
||||||
|
Spinner(22)
|
||||||
|
Debug("Building image "+name+" - running docker with: ", buildarg)
|
||||||
|
cmd := exec.Command("docker", buildarg...)
|
||||||
|
cmd.Dir = path
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Failed building image: "+string(out))
|
||||||
|
}
|
||||||
|
SpinnerStop()
|
||||||
|
Info(string(out))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*SimpleDocker) RemoveImage(opts compiler.CompilerBackendOptions) error {
|
||||||
|
name := opts.ImageName
|
||||||
|
buildarg := []string{"rmi", name}
|
||||||
|
Spinner(22)
|
||||||
|
out, err := exec.Command("docker", buildarg...).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Failed removing image: "+string(out))
|
||||||
|
}
|
||||||
|
SpinnerStop()
|
||||||
|
Info(string(out))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SimpleDocker) ImageDefinitionToTar(opts compiler.CompilerBackendOptions) error {
|
||||||
|
if err := s.BuildImage(opts); err != nil {
|
||||||
|
return errors.Wrap(err, "Failed building image")
|
||||||
|
}
|
||||||
|
if err := s.ExportImage(opts); err != nil {
|
||||||
|
return errors.Wrap(err, "Failed exporting image")
|
||||||
|
}
|
||||||
|
if err := s.RemoveImage(opts); err != nil {
|
||||||
|
return errors.Wrap(err, "Failed removing image")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*SimpleDocker) ExportImage(opts compiler.CompilerBackendOptions) error {
|
||||||
|
name := opts.ImageName
|
||||||
|
path := opts.Destination
|
||||||
|
|
||||||
|
buildarg := []string{"save", name, "-o", path}
|
||||||
|
Spinner(22)
|
||||||
|
Debug("Saving image "+name+" - running docker with: ", buildarg)
|
||||||
|
out, err := exec.Command("docker", buildarg...).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Failed exporting image: "+string(out))
|
||||||
|
}
|
||||||
|
SpinnerStop()
|
||||||
|
Info(string(out))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Use container-diff (https://github.com/GoogleContainerTools/container-diff) for checking out layer diffs
|
98
pkg/compiler/backend/simpledocker_test.go
Normal file
98
pkg/compiler/backend/simpledocker_test.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License along
|
||||||
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package backend_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/mudler/luet/pkg/compiler"
|
||||||
|
. "github.com/mudler/luet/pkg/compiler/backend"
|
||||||
|
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
helpers "github.com/mudler/luet/pkg/helpers"
|
||||||
|
pkg "github.com/mudler/luet/pkg/package"
|
||||||
|
"github.com/mudler/luet/pkg/tree"
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("Docker backend", func() {
|
||||||
|
Context("Simple Docker backend satisfies main interface functionalities", func() {
|
||||||
|
It("Builds and generate tars", func() {
|
||||||
|
generalRecipe := tree.NewGeneralRecipe()
|
||||||
|
|
||||||
|
err := generalRecipe.Load("../../../tests/fixtures/buildtree")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(generalRecipe.Tree()).ToNot(BeNil()) // It should be populated back at this point
|
||||||
|
|
||||||
|
Expect(len(generalRecipe.Tree().GetPackageSet().GetPackages())).To(Equal(1))
|
||||||
|
|
||||||
|
compiler := NewLuetCompiler(nil, generalRecipe.Tree())
|
||||||
|
spec, err := compiler.FromPackage(&pkg.DefaultPackage{Name: "enman", Category: "app-admin", Version: "1.4.0"})
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
lspec, ok := spec.(*LuetCompilationSpec)
|
||||||
|
Expect(ok).To(BeTrue())
|
||||||
|
|
||||||
|
Expect(lspec.Steps).To(Equal([]string{"echo foo > /test", "echo bar > /test2"}))
|
||||||
|
Expect(lspec.Image).To(Equal("luet/base"))
|
||||||
|
Expect(lspec.Seed).To(Equal("alpine"))
|
||||||
|
tmpdir, err := ioutil.TempDir(os.TempDir(), "tree")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(tmpdir) // clean up
|
||||||
|
|
||||||
|
err = lspec.WriteBuildImageDefinition(filepath.Join(tmpdir, "Dockerfile"))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
dockerfile, err := helpers.Read(filepath.Join(tmpdir, "Dockerfile"))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(dockerfile).To(Equal(`
|
||||||
|
FROM alpine
|
||||||
|
COPY . /luetbuild
|
||||||
|
WORKDIR /luetbuild
|
||||||
|
`))
|
||||||
|
b := NewSimpleDockerBackend()
|
||||||
|
opts := CompilerBackendOptions{
|
||||||
|
ImageName: "luet/base",
|
||||||
|
SourcePath: tmpdir,
|
||||||
|
DockerFileName: "Dockerfile",
|
||||||
|
Destination: filepath.Join(tmpdir, "output1"),
|
||||||
|
}
|
||||||
|
Expect(b.ImageDefinitionToTar(opts)).ToNot(HaveOccurred())
|
||||||
|
Expect(helpers.Exists(filepath.Join(tmpdir, "output1"))).To(BeTrue())
|
||||||
|
Expect(b.BuildImage(opts)).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
err = lspec.WriteStepImageDefinition(lspec.Image, filepath.Join(tmpdir, "LuetDockerfile"))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
dockerfile, err = helpers.Read(filepath.Join(tmpdir, "LuetDockerfile"))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(dockerfile).To(Equal(`
|
||||||
|
FROM luet/base
|
||||||
|
RUN echo foo > /test
|
||||||
|
RUN echo bar > /test2`))
|
||||||
|
opts = CompilerBackendOptions{
|
||||||
|
ImageName: "test",
|
||||||
|
SourcePath: tmpdir,
|
||||||
|
DockerFileName: "LuetDockerfile",
|
||||||
|
Destination: filepath.Join(tmpdir, "output2"),
|
||||||
|
}
|
||||||
|
Expect(b.ImageDefinitionToTar(opts)).ToNot(HaveOccurred())
|
||||||
|
Expect(helpers.Exists(filepath.Join(tmpdir, "output2"))).To(BeTrue())
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user