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

cli: improve flags and commands

docs: add README.md and examples
centos/rhel: fix dracut initramfs

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2022-04-21 18:28:50 +02:00
parent c240aac13d
commit 5b8cad6176
22 changed files with 550 additions and 56 deletions

View File

@ -15,6 +15,8 @@
package main
import (
"strings"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -25,11 +27,12 @@ import (
)
var (
file = "Dockerfile"
tag = uuid.New().String()
buildCmd = &cobra.Command{
file = "Dockerfile"
tag = uuid.New().String()
buildArgs []string
buildCmd = &cobra.Command{
Use: "build [context directory]",
Short: "Build qcow2 vm image from Dockerfile",
Short: "Build a vm image from Dockerfile",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
size, err := parseSize(size)
@ -40,10 +43,14 @@ var (
exec.Run = exec.RunStdout
}
logrus.Infof("building docker image from %s", file)
if err := docker.Cmd(cmd.Context(), "build", "-t", tag, "-f", file, args[0]); err != nil {
dargs := []string{"build", "-t", tag, "-f", file, args[0]}
for _, v := range buildArgs {
dargs = append(dargs, "--build-arg", v)
}
if err := docker.Cmd(cmd.Context(), dargs...); err != nil {
return err
}
return docker2vm.Convert(cmd.Context(), tag, size, password, output)
return d2vm.Convert(cmd.Context(), tag, size, password, output, format)
},
}
)
@ -52,11 +59,12 @@ func init() {
rootCmd.AddCommand(buildCmd)
buildCmd.Flags().StringVarP(&file, "file", "f", "Dockerfile", "Name of the Dockerfile (Default is 'PATH/Dockerfile')")
buildCmd.Flags().StringVarP(&tag, "tag", "t", tag, "Name and optionally a tag in the 'name:tag' format")
buildCmd.Flags().StringArrayVar(&buildArgs, "build-arg", nil, "Set build-time variables")
buildCmd.Flags().StringVarP(&output, "output", "o", output, "The output qcow2 image")
buildCmd.Flags().StringVarP(&format, "output-format", "O", format, "The output image format, supported formats: "+strings.Join(d2vm.OutputFormats(), " "))
buildCmd.Flags().StringVarP(&output, "output", "o", output, "The output image")
buildCmd.Flags().StringVarP(&password, "password", "p", "root", "Root user password")
buildCmd.Flags().StringVarP(&size, "size", "s", "1G", "The output image size")
buildCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size")
buildCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
buildCmd.Flags().BoolVar(&force, "force", false, "Override output qcow2 image")
buildCmd.Flags().BoolVar(&force, "force", false, "Override output image")
}

View File

@ -17,6 +17,7 @@ package main
import (
"fmt"
"os"
"strings"
"github.com/c2h5oh/datasize"
"github.com/sirupsen/logrus"
@ -28,13 +29,19 @@ import (
)
var (
pull = false
convertCmd = &cobra.Command{
Use: "convert [docker image]",
Short: "Convert Docker image to qcow2 vm image",
Short: "Convert Docker image to vm image",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
img := args[0]
tag := "latest"
if parts := strings.Split(img, ":"); len(parts) > 1 {
img, tag = parts[0], parts[1]
}
size, err := parseSize(size)
if err != nil {
return err
@ -52,11 +59,24 @@ var (
return fmt.Errorf("%s already exists", output)
}
}
logrus.Infof("pulling image %s", img)
if err := docker.Cmd(cmd.Context(), "image", "pull", img); err != nil {
return err
found := false
if !pull {
o, _, err := docker.CmdOut(cmd.Context(), "image", "ls", "--format={{ .Repository }}:{{ .Tag }}", img)
if err != nil {
return err
}
found = strings.TrimSuffix(o, "\n") == fmt.Sprintf("%s:%s", img, tag)
if found {
logrus.Infof("using local image %s:%s", img, tag)
}
}
return docker2vm.Convert(cmd.Context(), img, size, password, output)
if pull || !found {
logrus.Infof("pulling image %s", img)
if err := docker.Cmd(cmd.Context(), "image", "pull", img); err != nil {
return err
}
}
return d2vm.Convert(cmd.Context(), img, size, password, output, format)
},
}
)
@ -70,9 +90,11 @@ func parseSize(s string) (int64, error) {
}
func init() {
convertCmd.Flags().StringVarP(&output, "output", "o", output, "The output qcow2 image")
convertCmd.Flags().BoolVar(&pull, "pull", false, "Always pull docker image")
convertCmd.Flags().StringVarP(&format, "output-format", "O", format, "The output image format, supported formats: "+strings.Join(d2vm.OutputFormats(), " "))
convertCmd.Flags().StringVarP(&output, "output", "o", output, "The output image")
convertCmd.Flags().StringVarP(&password, "password", "p", "root", "The Root user password")
convertCmd.Flags().StringVarP(&size, "size", "s", "1G", "The output image size")
convertCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size")
convertCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
convertCmd.Flags().BoolVarP(&force, "force", "f", false, "Override output qcow2 image")
rootCmd.AddCommand(convertCmd)

View File

@ -26,9 +26,11 @@ var (
password = "root"
force = false
debug = false
format = "qcow2"
rootCmd = &cobra.Command{
Use: "d2vm",
Use: "d2vm",
SilenceUsage: true,
}
)