mirror of
https://github.com/linka-cloud/d2vm.git
synced 2024-11-26 09:36:25 +00:00
refactoring: explicit docker commands
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
parent
20ba409039
commit
085e57a07a
@ -39,15 +39,9 @@ var (
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if debug {
|
exec.SetDebug(debug)
|
||||||
exec.Run = exec.RunStdout
|
|
||||||
}
|
|
||||||
logrus.Infof("building docker image from %s", file)
|
logrus.Infof("building docker image from %s", file)
|
||||||
dargs := []string{"build", "-t", tag, "-f", file, args[0]}
|
if err := docker.Build(cmd.Context(), tag, file, args[0], buildArgs...); err != nil {
|
||||||
for _, v := range buildArgs {
|
|
||||||
dargs = append(dargs, "--build-arg", v)
|
|
||||||
}
|
|
||||||
if err := docker.Cmd(cmd.Context(), dargs...); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return d2vm.Convert(cmd.Context(), tag, size, password, output, format)
|
return d2vm.Convert(cmd.Context(), tag, size, password, output, format)
|
||||||
|
@ -51,9 +51,7 @@ var (
|
|||||||
return fmt.Errorf("%s already exists", output)
|
return fmt.Errorf("%s already exists", output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if debug {
|
exec.SetDebug(debug)
|
||||||
exec.Run = exec.RunStdout
|
|
||||||
}
|
|
||||||
if _, err := os.Stat(output); err == nil || !os.IsNotExist(err) {
|
if _, err := os.Stat(output); err == nil || !os.IsNotExist(err) {
|
||||||
if !force {
|
if !force {
|
||||||
return fmt.Errorf("%s already exists", output)
|
return fmt.Errorf("%s already exists", output)
|
||||||
@ -61,18 +59,18 @@ var (
|
|||||||
}
|
}
|
||||||
found := false
|
found := false
|
||||||
if !pull {
|
if !pull {
|
||||||
o, _, err := docker.CmdOut(cmd.Context(), "image", "ls", "--format={{ .Repository }}:{{ .Tag }}", img)
|
imgs, err := docker.ImageList(cmd.Context(), img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
found = strings.TrimSuffix(o, "\n") == fmt.Sprintf("%s:%s", img, tag)
|
found = len(imgs) == 1 && imgs[0] == fmt.Sprintf("%s:%s", img, tag)
|
||||||
if found {
|
if found {
|
||||||
logrus.Infof("using local image %s:%s", img, tag)
|
logrus.Infof("using local image %s:%s", img, tag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if pull || !found {
|
if pull || !found {
|
||||||
logrus.Infof("pulling image %s", img)
|
logrus.Infof("pulling image %s", img)
|
||||||
if err := docker.Cmd(cmd.Context(), "image", "pull", img); err != nil {
|
if err := docker.Pull(cmd.Context(), img); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,10 +56,10 @@ func Convert(ctx context.Context, img string, size int64, password string, outpu
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logrus.Infof("building kernel enabled image")
|
logrus.Infof("building kernel enabled image")
|
||||||
if err := docker.Cmd(ctx, "image", "build", "-t", imgUUID, "-f", p, dir); err != nil {
|
if err := docker.Build(ctx, imgUUID, p, dir); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer docker.Cmd(ctx, "image", "rm", imgUUID)
|
defer docker.Remove(ctx, imgUUID)
|
||||||
|
|
||||||
logrus.Infof("creating vm image")
|
logrus.Infof("creating vm image")
|
||||||
b, err := NewBuilder(ctx, tmpPath, imgUUID, "", size, r, format)
|
b, err := NewBuilder(ctx, tmpPath, imgUUID, "", size, r, format)
|
||||||
|
@ -132,7 +132,7 @@ COPY hostname /etc/
|
|||||||
RUN rm -rf /etc/apk
|
RUN rm -rf /etc/apk
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
exec.Run = exec.RunStdout
|
exec.SetDebug(true)
|
||||||
tmp := filepath.Join(os.TempDir(), "d2vm-tests", "image-flatten")
|
tmp := filepath.Join(os.TempDir(), "d2vm-tests", "image-flatten")
|
||||||
require.NoError(t, os.MkdirAll(tmp, perm))
|
require.NoError(t, os.MkdirAll(tmp, perm))
|
||||||
defer os.RemoveAll(tmp)
|
defer os.RemoveAll(tmp)
|
||||||
@ -140,12 +140,9 @@ RUN rm -rf /etc/apk
|
|||||||
require.NoError(t, os.WriteFile(filepath.Join(tmp, "hostname"), []byte("d2vm-flatten-test"), perm))
|
require.NoError(t, os.WriteFile(filepath.Join(tmp, "hostname"), []byte("d2vm-flatten-test"), perm))
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(tmp, "resolv.conf"), []byte("nameserver 8.8.8.8"), perm))
|
require.NoError(t, os.WriteFile(filepath.Join(tmp, "resolv.conf"), []byte("nameserver 8.8.8.8"), perm))
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte(dockerfile), perm))
|
require.NoError(t, os.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte(dockerfile), perm))
|
||||||
require.NoError(t, docker.Cmd(ctx, "image", "build", "-t", img, tmp))
|
require.NoError(t, docker.Build(ctx, img, "", tmp))
|
||||||
defer docker.Cmd(ctx, "image", "rm", img)
|
defer docker.Remove(ctx, img)
|
||||||
|
|
||||||
// imgTar := filepath.Join(tmp, img+".tar")
|
|
||||||
// require.NoError(t, docker.Cmd(ctx, "image", "save", img, "-o", imgTar))
|
|
||||||
//
|
|
||||||
imgTmp := filepath.Join(tmp, "image")
|
imgTmp := filepath.Join(tmp, "image")
|
||||||
|
|
||||||
i, err := NewImage(ctx, img, imgTmp)
|
i, err := NewImage(ctx, img, imgTmp)
|
||||||
|
@ -15,8 +15,10 @@
|
|||||||
package docker
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"go.linka.cloud/d2vm/pkg/exec"
|
"go.linka.cloud/d2vm/pkg/exec"
|
||||||
@ -35,3 +37,36 @@ func Cmd(ctx context.Context, args ...string) error {
|
|||||||
func CmdOut(ctx context.Context, args ...string) (string, string, error) {
|
func CmdOut(ctx context.Context, args ...string) (string, string, error) {
|
||||||
return exec.RunOut(ctx, "docker", args...)
|
return exec.RunOut(ctx, "docker", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Build(ctx context.Context, tag, dockerfile, dir string, buildArgs ...string) error {
|
||||||
|
if dockerfile == "" {
|
||||||
|
dockerfile = filepath.Join(dir, "Dockerfile")
|
||||||
|
}
|
||||||
|
args := []string{"image", "build", "-t", tag, "-f", dockerfile}
|
||||||
|
for _, v := range buildArgs {
|
||||||
|
args = append(args, "--build-arg", v)
|
||||||
|
}
|
||||||
|
args = append(args, dir)
|
||||||
|
return Cmd(ctx, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Remove(ctx context.Context, tag string) error {
|
||||||
|
return Cmd(ctx, "image", "rm", tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ImageList(ctx context.Context, tag string) ([]string, error) {
|
||||||
|
o, _, err := CmdOut(ctx, "image", "ls", "--format={{ .Repository }}:{{ .Tag }}", tag)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s := bufio.NewScanner(strings.NewReader(o))
|
||||||
|
var imgs []string
|
||||||
|
for s.Scan() {
|
||||||
|
imgs = append(imgs, s.Text())
|
||||||
|
}
|
||||||
|
return imgs, s.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Pull(ctx context.Context, tag string) error {
|
||||||
|
return Cmd(ctx, "image", "pull", tag)
|
||||||
|
}
|
||||||
|
@ -29,6 +29,14 @@ var (
|
|||||||
CommandContext = exec.CommandContext
|
CommandContext = exec.CommandContext
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func SetDebug(debug bool) {
|
||||||
|
if debug {
|
||||||
|
Run = RunStdout
|
||||||
|
} else {
|
||||||
|
Run = RunNoOut
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func RunStdout(ctx context.Context, c string, args ...string) error {
|
func RunStdout(ctx context.Context, c string, args ...string) error {
|
||||||
cmd := exec.CommandContext(ctx, c, args...)
|
cmd := exec.CommandContext(ctx, c, args...)
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
|
Loading…
Reference in New Issue
Block a user