summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorAshley Cui <acui@redhat.com>2021-03-04 15:29:12 -0500
committerbaude <bbaude@redhat.com>2021-03-25 08:41:11 -0500
commita861f6fd3ebe4fe0b63a1b550e6b99d7525228c0 (patch)
tree6d39e58da427d09e75ebbb440521d520342a0089 /cmd
parentaf91f27d85f4104267eea493f8588f8c6a2d01e3 (diff)
downloadpodman-a861f6fd3ebe4fe0b63a1b550e6b99d7525228c0.tar.gz
podman-a861f6fd3ebe4fe0b63a1b550e6b99d7525228c0.tar.bz2
podman-a861f6fd3ebe4fe0b63a1b550e6b99d7525228c0.zip
Podman machine CLI and interface stub
Podman machine will be a mac-only command that manages the VM where containers are run. Currently, only the CLI is written and the interface function for the VM management is stub for future developement The podman machine cli is only built on mac builds. Signed-off-by: Ashley Cui <acui@redhat.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/podman/machine/config.go38
-rw-r--r--cmd/podman/machine/create.go132
-rw-r--r--cmd/podman/machine/machine.go30
-rw-r--r--cmd/podman/machine/start.go34
-rw-r--r--cmd/podman/machine/stop.go34
-rw-r--r--cmd/podman/main.go1
6 files changed, 269 insertions, 0 deletions
diff --git a/cmd/podman/machine/config.go b/cmd/podman/machine/config.go
new file mode 100644
index 000000000..5fa6aa50d
--- /dev/null
+++ b/cmd/podman/machine/config.go
@@ -0,0 +1,38 @@
+package machine
+
+import "fmt"
+
+type CreateOptions struct {
+ CPUS uint64
+ Memory uint64
+ KernelPath string
+ Devices []VMDevices
+}
+
+type VMDevices struct {
+ Path string
+ ReadOnly bool
+}
+
+type VM interface {
+ Create(name string, opts CreateOptions) error
+ Start(name string) error
+ Stop(name string) error
+}
+
+type TestVM struct {
+}
+
+func (vm *TestVM) Create(name string, opts CreateOptions) error {
+ fmt.Printf("Created: %s\n", name)
+ return nil
+}
+
+func (vm *TestVM) Start(name string) error {
+ fmt.Printf("Started: %s\n", name)
+ return nil
+}
+func (vm *TestVM) Stop(name string) error {
+ fmt.Printf("Stopped: %s\n", name)
+ return nil
+}
diff --git a/cmd/podman/machine/create.go b/cmd/podman/machine/create.go
new file mode 100644
index 000000000..35f0677ad
--- /dev/null
+++ b/cmd/podman/machine/create.go
@@ -0,0 +1,132 @@
+package machine
+
+import (
+ "fmt"
+ "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/pkg/errors"
+ "github.com/spf13/cobra"
+)
+
+var (
+ createCmd = &cobra.Command{
+ Use: "create [options] NAME",
+ Short: "Create a vm",
+ Long: "Create a virtual machine for Podman to run on. Virtual machines are used to run Podman on Macs. ",
+ RunE: create,
+ Args: cobra.ExactArgs(1),
+ Example: `podman machine create myvm`,
+ ValidArgsFunction: completion.AutocompleteNone,
+ }
+)
+
+type CreateCLIOptions struct {
+ CPUS uint64
+ Memory uint64
+ KernelPath string
+ Devices []string
+}
+
+var (
+ createOpts = CreateCLIOptions{}
+)
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
+ Command: createCmd,
+ Parent: machineCmd,
+ })
+ flags := createCmd.Flags()
+
+ cpusFlagName := "cpus"
+ flags.Uint64Var(
+ &createOpts.CPUS,
+ cpusFlagName, 0,
+ "Number of CPUs. The default is 0.000 which means no limit",
+ )
+ _ = createCmd.RegisterFlagCompletionFunc(cpusFlagName, completion.AutocompleteNone)
+
+ memoryFlagName := "memory"
+ flags.Uint64VarP(
+ &createOpts.Memory,
+ memoryFlagName, "m", 0,
+ "Memory (in MB)",
+ )
+ _ = createCmd.RegisterFlagCompletionFunc(memoryFlagName, completion.AutocompleteNone)
+
+ kernelPathFlagName := "kernel-path"
+ flags.StringVar(
+ &createOpts.KernelPath,
+ kernelPathFlagName, "",
+ "Kernel path",
+ )
+ _ = createCmd.RegisterFlagCompletionFunc(kernelPathFlagName, completion.AutocompleteNone)
+
+ deviceFlagName := "device"
+ flags.StringSliceVar(
+ &createOpts.Devices,
+ deviceFlagName, []string{},
+ "Add a device",
+ )
+ _ = createCmd.RegisterFlagCompletionFunc(deviceFlagName, completion.AutocompleteDefault)
+}
+
+func create(cmd *cobra.Command, args []string) error {
+ vmOpts := CreateOptions{
+ CPUS: createOpts.CPUS,
+ Memory: createOpts.Memory,
+ KernelPath: createOpts.KernelPath,
+ }
+
+ if cmd.Flags().Changed("device") {
+ devices, err := cmd.Flags().GetStringSlice("device")
+ if err != nil {
+ return err
+ }
+ vmOpts.Devices, err = parseDevices(devices)
+ if err != nil {
+ return err
+ }
+ }
+
+ test := new(TestVM)
+ test.Create(args[0], vmOpts)
+
+ return nil
+}
+
+func parseDevices(devices []string) ([]VMDevices, error) {
+ vmDevices := make([]VMDevices, 0, len(devices))
+
+ for _, dev := range devices {
+ split := strings.Split(dev, ":")
+
+ if len(split) == 1 {
+ vmDevices = append(vmDevices, VMDevices{
+ Path: split[0],
+ ReadOnly: false,
+ })
+ } else if len(split) == 2 {
+ var readonly bool
+
+ switch split[1] {
+ case "ro", "readonly":
+ readonly = true
+ default:
+ return nil, errors.New(fmt.Sprintf("Invalid readonly value: %s", dev))
+ }
+
+ vmDevices = append(vmDevices, VMDevices{
+ Path: split[0],
+ ReadOnly: readonly,
+ })
+ } else {
+ return nil, errors.New(fmt.Sprintf("Invalid device format: %s", dev))
+ }
+ }
+ return vmDevices, nil
+}
diff --git a/cmd/podman/machine/machine.go b/cmd/podman/machine/machine.go
new file mode 100644
index 000000000..ce5a3b889
--- /dev/null
+++ b/cmd/podman/machine/machine.go
@@ -0,0 +1,30 @@
+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 on Macs.",
+ 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/start.go b/cmd/podman/machine/start.go
new file mode 100644
index 000000000..118ce74c0
--- /dev/null
+++ b/cmd/podman/machine/start.go
@@ -0,0 +1,34 @@
+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/spf13/cobra"
+)
+
+var (
+ startCmd = &cobra.Command{
+ Use: "start NAME",
+ Short: "Start an existing machine",
+ Long: "Start an existing machine ",
+ RunE: start,
+ Args: cobra.ExactArgs(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 {
+ test := new(TestVM)
+ test.Start(args[0])
+ return nil
+}
diff --git a/cmd/podman/machine/stop.go b/cmd/podman/machine/stop.go
new file mode 100644
index 000000000..d04b3a447
--- /dev/null
+++ b/cmd/podman/machine/stop.go
@@ -0,0 +1,34 @@
+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/spf13/cobra"
+)
+
+var (
+ stopCmd = &cobra.Command{
+ Use: "stop NAME",
+ Short: "Stop an existing machine",
+ Long: "Stop an existing machine ",
+ RunE: stop,
+ Args: cobra.ExactArgs(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,
+ })
+}
+
+func stop(cmd *cobra.Command, args []string) error {
+ test := new(TestVM)
+ test.Stop(args[0])
+ return nil
+}
diff --git a/cmd/podman/main.go b/cmd/podman/main.go
index 561459c31..5219da26d 100644
--- a/cmd/podman/main.go
+++ b/cmd/podman/main.go
@@ -9,6 +9,7 @@ import (
_ "github.com/containers/podman/v3/cmd/podman/generate"
_ "github.com/containers/podman/v3/cmd/podman/healthcheck"
_ "github.com/containers/podman/v3/cmd/podman/images"
+ _ "github.com/containers/podman/v3/cmd/podman/machine"
_ "github.com/containers/podman/v3/cmd/podman/manifest"
_ "github.com/containers/podman/v3/cmd/podman/networks"
_ "github.com/containers/podman/v3/cmd/podman/play"