2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2025-06-23 13:52:26 +00:00

remove -O option, use output extension instead

add run command to execute vm in qemu or virtualbox

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2022-05-20 16:36:36 +02:00
parent 29d953c14d
commit 62d8a1019d
17 changed files with 1320 additions and 38 deletions

View File

@ -18,12 +18,24 @@ import (
"bufio"
"context"
_ "embed"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/sirupsen/logrus"
"go.linka.cloud/d2vm/pkg/exec"
)
func dockerSocket() string {
if runtime.GOOS == "windows" {
return "//var/run/docker.sock"
}
return "/var/run/docker.sock"
}
func FormatImgName(name string) string {
s := strings.Replace(name, ":", "-", -1)
s = strings.Replace(s, "/", "_", -1)
@ -70,3 +82,34 @@ func ImageList(ctx context.Context, tag string) ([]string, error) {
func Pull(ctx context.Context, tag string) error {
return Cmd(ctx, "image", "pull", tag)
}
func RunInteractiveAndRemove(ctx context.Context, args ...string) error {
logrus.Tracef("running 'docker run --rm -i -t %s'", strings.Join(args, " "))
cmd := exec.CommandContext(ctx, "docker", append([]string{"run", "--rm", "-it"}, args...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func RunD2VM(ctx context.Context, image, version, cmd string, args ...string) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
if version == "" {
version = "latest"
}
a := []string{
"--privileged",
"-v",
fmt.Sprintf("%s:/var/run/docker.sock", dockerSocket()),
"-v",
fmt.Sprintf("%s:/d2vm", pwd),
"-w",
"/d2vm",
fmt.Sprintf("%s:%s", image, version),
cmd,
}
return RunInteractiveAndRemove(ctx, append(a, args...)...)
}