diff options
Diffstat (limited to 'cmd/podman/machine')
-rw-r--r-- | cmd/podman/machine/init.go | 96 | ||||
-rw-r--r-- | cmd/podman/machine/machine.go | 32 | ||||
-rw-r--r-- | cmd/podman/machine/machine_unsupported.go | 5 | ||||
-rw-r--r-- | cmd/podman/machine/rm.go | 92 | ||||
-rw-r--r-- | cmd/podman/machine/ssh.go | 74 | ||||
-rw-r--r-- | cmd/podman/machine/start.go | 52 | ||||
-rw-r--r-- | cmd/podman/machine/stop.go | 53 |
7 files changed, 404 insertions, 0 deletions
diff --git a/cmd/podman/machine/init.go b/cmd/podman/machine/init.go new file mode 100644 index 000000000..05474fd89 --- /dev/null +++ b/cmd/podman/machine/init.go @@ -0,0 +1,96 @@ +// +build amd64,linux amd64,darwin arm64,darwin + +package machine + +import ( + "github.com/containers/common/pkg/completion" + "github.com/containers/podman/v3/cmd/podman/registry" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/machine" + "github.com/containers/podman/v3/pkg/machine/qemu" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + initCmd = &cobra.Command{ + Use: "init [options] [NAME]", + Short: "initialize a vm", + Long: "initialize a virtual machine for Podman to run on. Virtual machines are used to run Podman.", + RunE: initMachine, + Args: cobra.MaximumNArgs(1), + Example: `podman machine init myvm`, + ValidArgsFunction: completion.AutocompleteNone, + } +) + +var ( + initOpts = machine.InitOptions{} + defaultMachineName string = "podman-machine-default" +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: initCmd, + Parent: machineCmd, + }) + flags := initCmd.Flags() + + cpusFlagName := "cpus" + flags.Uint64Var( + &initOpts.CPUS, + cpusFlagName, 1, + "Number of CPUs. The default is 1.", + ) + _ = initCmd.RegisterFlagCompletionFunc(cpusFlagName, completion.AutocompleteNone) + + diskSizeFlagName := "disk-size" + flags.Uint64Var( + &initOpts.DiskSize, + diskSizeFlagName, 10, + "Disk size in GB", + ) + + _ = initCmd.RegisterFlagCompletionFunc(diskSizeFlagName, completion.AutocompleteNone) + + memoryFlagName := "memory" + flags.Uint64VarP( + &initOpts.Memory, + memoryFlagName, "m", 2048, + "Memory (in MB)", + ) + _ = initCmd.RegisterFlagCompletionFunc(memoryFlagName, completion.AutocompleteNone) + + ImagePathFlagName := "image-path" + flags.StringVar(&initOpts.ImagePath, ImagePathFlagName, "", "Path to qcow image") + _ = initCmd.RegisterFlagCompletionFunc(ImagePathFlagName, completion.AutocompleteDefault) + + IgnitionPathFlagName := "ignition-path" + flags.StringVar(&initOpts.IgnitionPath, IgnitionPathFlagName, "", "Path to ignition file") + _ = initCmd.RegisterFlagCompletionFunc(IgnitionPathFlagName, completion.AutocompleteDefault) +} + +// TODO should we allow for a users to append to the qemu cmdline? +func initMachine(cmd *cobra.Command, args []string) error { + var ( + vm machine.VM + vmType string + err error + ) + initOpts.Name = defaultMachineName + if len(args) > 0 { + initOpts.Name = args[0] + } + switch vmType { + default: // qemu is the default + if _, err := qemu.LoadVMByName(initOpts.Name); err == nil { + return errors.Wrap(machine.ErrVMAlreadyExists, initOpts.Name) + } + vm, err = qemu.NewMachine(initOpts) + } + if err != nil { + return err + } + return vm.Init(initOpts) +} diff --git a/cmd/podman/machine/machine.go b/cmd/podman/machine/machine.go new file mode 100644 index 000000000..ed284ee10 --- /dev/null +++ b/cmd/podman/machine/machine.go @@ -0,0 +1,32 @@ +// +build amd64,linux amd64,darwin arm64,darwin + +package machine + +import ( + "github.com/containers/podman/v3/cmd/podman/registry" + "github.com/containers/podman/v3/cmd/podman/validate" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/spf13/cobra" +) + +var ( + noOp = func(cmd *cobra.Command, args []string) error { + return nil + } + // Command: podman _machine_ + machineCmd = &cobra.Command{ + Use: "machine", + Short: "Manage a virtual machine", + Long: "Manage a virtual machine. Virtual machines are used to run Podman.", + PersistentPreRunE: noOp, + PersistentPostRunE: noOp, + RunE: validate.SubCommandExists, + } +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: machineCmd, + }) +} diff --git a/cmd/podman/machine/machine_unsupported.go b/cmd/podman/machine/machine_unsupported.go new file mode 100644 index 000000000..cb1636419 --- /dev/null +++ b/cmd/podman/machine/machine_unsupported.go @@ -0,0 +1,5 @@ +// +build !amd64 arm64,linux amd64,windows + +package machine + +func init() {} diff --git a/cmd/podman/machine/rm.go b/cmd/podman/machine/rm.go new file mode 100644 index 000000000..cd2cc84f2 --- /dev/null +++ b/cmd/podman/machine/rm.go @@ -0,0 +1,92 @@ +// +build amd64,linux amd64,darwin arm64,darwin + +package machine + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/containers/common/pkg/completion" + "github.com/containers/podman/v3/cmd/podman/registry" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/machine" + "github.com/containers/podman/v3/pkg/machine/qemu" + "github.com/spf13/cobra" +) + +var ( + rmCmd = &cobra.Command{ + Use: "rm [options] [NAME]", + Short: "Remove an existing machine", + Long: "Remove an existing machine ", + RunE: rm, + Args: cobra.MaximumNArgs(1), + Example: `podman machine rm myvm`, + ValidArgsFunction: completion.AutocompleteNone, + } +) + +var ( + destoryOptions machine.RemoveOptions +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: rmCmd, + Parent: machineCmd, + }) + + flags := rmCmd.Flags() + formatFlagName := "force" + flags.BoolVar(&destoryOptions.Force, formatFlagName, false, "Do not prompt before rming") + + keysFlagName := "save-keys" + flags.BoolVar(&destoryOptions.SaveKeys, keysFlagName, false, "Do not delete SSH keys") + + ignitionFlagName := "save-ignition" + flags.BoolVar(&destoryOptions.SaveIgnition, ignitionFlagName, false, "Do not delete ignition file") + + imageFlagName := "save-image" + flags.BoolVar(&destoryOptions.SaveImage, imageFlagName, false, "Do not delete the image file") +} + +func rm(cmd *cobra.Command, args []string) error { + var ( + err error + vm machine.VM + vmType string + ) + vmName := defaultMachineName + if len(args) > 0 && len(args[0]) > 0 { + vmName = args[0] + } + switch vmType { + default: + vm, err = qemu.LoadVMByName(vmName) + } + if err != nil { + return err + } + confirmationMessage, remove, err := vm.Remove(vmName, machine.RemoveOptions{}) + if err != nil { + return err + } + + if !destoryOptions.Force { + // Warn user + fmt.Println(confirmationMessage) + reader := bufio.NewReader(os.Stdin) + fmt.Print("Are you sure you want to continue? [y/N] ") + answer, err := reader.ReadString('\n') + if err != nil { + return err + } + if strings.ToLower(answer)[0] != 'y' { + return nil + } + } + return remove() +} diff --git a/cmd/podman/machine/ssh.go b/cmd/podman/machine/ssh.go new file mode 100644 index 000000000..879122a14 --- /dev/null +++ b/cmd/podman/machine/ssh.go @@ -0,0 +1,74 @@ +// +build amd64,linux amd64,darwin arm64,darwin + +package machine + +import ( + "github.com/containers/podman/v3/cmd/podman/common" + "github.com/containers/podman/v3/cmd/podman/registry" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/machine" + "github.com/containers/podman/v3/pkg/machine/qemu" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + sshCmd = &cobra.Command{ + Use: "ssh [options] [NAME] [COMMAND [ARG ...]]", + Short: "SSH into a virtual machine", + Long: "SSH into a virtual machine ", + RunE: ssh, + Args: cobra.MaximumNArgs(1), + Example: `podman machine ssh myvm + podman machine ssh -e myvm echo hello`, + + ValidArgsFunction: common.AutocompleteMachineSSH, + } +) + +var ( + sshOpts machine.SSHOptions +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: sshCmd, + Parent: machineCmd, + }) + + flags := sshCmd.Flags() + executeFlagName := "execute" + flags.BoolVarP(&sshOpts.Execute, executeFlagName, "e", false, "Execute command from args") +} + +func ssh(cmd *cobra.Command, args []string) error { + var ( + err error + vm machine.VM + vmType string + ) + vmName := defaultMachineName + if len(args) > 0 && len(args[0]) > 1 { + vmName = args[0] + } + sshOpts.Args = args[1:] + + // Error if no execute but args given + if !sshOpts.Execute && len(sshOpts.Args) > 0 { + return errors.New("too many args: to execute commands via ssh, use -e flag") + } + // Error if execute but no args given + if sshOpts.Execute && len(sshOpts.Args) < 1 { + return errors.New("must proivde at least one command to execute") + } + + switch vmType { + default: + vm, err = qemu.LoadVMByName(vmName) + } + if err != nil { + return errors.Wrapf(err, "vm %s not found", args[0]) + } + return vm.SSH(vmName, sshOpts) +} diff --git a/cmd/podman/machine/start.go b/cmd/podman/machine/start.go new file mode 100644 index 000000000..80fd77102 --- /dev/null +++ b/cmd/podman/machine/start.go @@ -0,0 +1,52 @@ +// +build amd64,linux amd64,darwin arm64,darwin + +package machine + +import ( + "github.com/containers/common/pkg/completion" + "github.com/containers/podman/v3/cmd/podman/registry" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/machine" + "github.com/containers/podman/v3/pkg/machine/qemu" + "github.com/spf13/cobra" +) + +var ( + startCmd = &cobra.Command{ + Use: "start [NAME]", + Short: "Start an existing machine", + Long: "Start an existing machine ", + RunE: start, + Args: cobra.MaximumNArgs(1), + Example: `podman machine start myvm`, + ValidArgsFunction: completion.AutocompleteNone, + } +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: startCmd, + Parent: machineCmd, + }) +} + +func start(cmd *cobra.Command, args []string) error { + var ( + err error + vm machine.VM + vmType string + ) + vmName := defaultMachineName + if len(args) > 0 && len(args[0]) > 0 { + vmName = args[0] + } + switch vmType { + default: + vm, err = qemu.LoadVMByName(vmName) + } + if err != nil { + return err + } + return vm.Start(vmName, machine.StartOptions{}) +} diff --git a/cmd/podman/machine/stop.go b/cmd/podman/machine/stop.go new file mode 100644 index 000000000..4fcb065a3 --- /dev/null +++ b/cmd/podman/machine/stop.go @@ -0,0 +1,53 @@ +// +build amd64,linux amd64,darwin arm64,darwin + +package machine + +import ( + "github.com/containers/common/pkg/completion" + "github.com/containers/podman/v3/cmd/podman/registry" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/machine" + "github.com/containers/podman/v3/pkg/machine/qemu" + "github.com/spf13/cobra" +) + +var ( + stopCmd = &cobra.Command{ + Use: "stop [NAME]", + Short: "Stop an existing machine", + Long: "Stop an existing machine ", + RunE: stop, + Args: cobra.MaximumNArgs(1), + Example: `podman machine stop myvm`, + ValidArgsFunction: completion.AutocompleteNone, + } +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: stopCmd, + Parent: machineCmd, + }) +} + +// TODO Name shouldnt be required, need to create a default vm +func stop(cmd *cobra.Command, args []string) error { + var ( + err error + vm machine.VM + vmType string + ) + vmName := defaultMachineName + if len(args) > 0 && len(args[0]) > 0 { + vmName = args[0] + } + switch vmType { + default: + vm, err = qemu.LoadVMByName(vmName) + } + if err != nil { + return err + } + return vm.Stop(vmName, machine.StopOptions{}) +} |