From 4ab8a6f67eb9de0de40d478cb0cbec05b1b725c0 Mon Sep 17 00:00:00 2001 From: baude Date: Mon, 22 Mar 2021 13:29:25 -0500 Subject: Improvements for machine clean up ci failures and add appropriate arch,os exclusion tags Signed-off-by: baude --- cmd/podman/common/completion.go | 8 +++ cmd/podman/machine/create.go | 28 ++++------ cmd/podman/machine/destroy.go | 91 ------------------------------- cmd/podman/machine/machine.go | 4 +- cmd/podman/machine/machine_unsupported.go | 5 ++ cmd/podman/machine/remove.go | 88 ++++++++++++++++++++++++++++++ cmd/podman/machine/ssh.go | 9 +-- cmd/podman/machine/start.go | 2 + cmd/podman/machine/stop.go | 2 + 9 files changed, 123 insertions(+), 114 deletions(-) delete mode 100644 cmd/podman/machine/destroy.go create mode 100644 cmd/podman/machine/machine_unsupported.go create mode 100644 cmd/podman/machine/remove.go (limited to 'cmd/podman') diff --git a/cmd/podman/common/completion.go b/cmd/podman/common/completion.go index bc106263c..6bed5e0c6 100644 --- a/cmd/podman/common/completion.go +++ b/cmd/podman/common/completion.go @@ -1092,3 +1092,11 @@ func AutocompleteVolumeFilters(cmd *cobra.Command, args []string, toComplete str } return completeKeyValues(toComplete, kv) } + +// AutocompleteMachineSSH - Autocomplete machine ssh command. +func AutocompleteMachineSSH(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + return nil, cobra.ShellCompDirectiveDefault +} diff --git a/cmd/podman/machine/create.go b/cmd/podman/machine/create.go index 04c5e9e65..1da34327a 100644 --- a/cmd/podman/machine/create.go +++ b/cmd/podman/machine/create.go @@ -1,3 +1,5 @@ +// +build amd64,linux amd64,darwin arm64,darwin + package machine import ( @@ -6,17 +8,16 @@ import ( "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 ( createCmd = &cobra.Command{ - Use: "create [options] NAME", + 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. ", + Long: "Create a virtual machine for Podman to run on. Virtual machines are used to run Podman.", RunE: create, - Args: cobra.NoArgs, + Args: cobra.MaximumNArgs(1), Example: `podman machine create myvm`, ValidArgsFunction: completion.AutocompleteNone, } @@ -32,7 +33,8 @@ type CreateCLIOptions struct { } var ( - createOpts = CreateCLIOptions{} + createOpts = CreateCLIOptions{} + defaultMachineName string = "podman-machine-default" ) func init() { @@ -59,14 +61,6 @@ func init() { ) _ = createCmd.RegisterFlagCompletionFunc(memoryFlagName, completion.AutocompleteNone) - deviceFlagName := "name" - flags.StringVar( - &createOpts.Name, - deviceFlagName, "", - "set vm name", - ) - _ = createCmd.RegisterFlagCompletionFunc(deviceFlagName, completion.AutocompleteDefault) - ImagePathFlagName := "image-path" flags.StringVar(&createOpts.ImagePath, ImagePathFlagName, "", "Path to qcow image") _ = createCmd.RegisterFlagCompletionFunc(ImagePathFlagName, completion.AutocompleteDefault) @@ -78,9 +72,9 @@ func init() { // TODO should we allow for a users to append to the qemu cmdline? func create(cmd *cobra.Command, args []string) error { - // TODO add ability to create default, not name required - if len(createOpts.Name) < 1 { - return errors.New("required --name not provided") + createOpts.Name = defaultMachineName + if len(args) > 0 { + createOpts.Name = args[0] } vmOpts := machine.CreateOptions{ CPUS: createOpts.CPUS, @@ -95,8 +89,6 @@ func create(cmd *cobra.Command, args []string) error { err error ) switch vmType { - case "foobar": - // do nothing default: // qemu is the default vm, err = qemu.NewMachine(vmOpts) } diff --git a/cmd/podman/machine/destroy.go b/cmd/podman/machine/destroy.go deleted file mode 100644 index ab23d607d..000000000 --- a/cmd/podman/machine/destroy.go +++ /dev/null @@ -1,91 +0,0 @@ -package machine - -import ( - "bufio" - "fmt" - "os" - "strings" - - "github.com/containers/common/pkg/completion" - "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/spf13/cobra" -) - -var ( - destroyCmd = &cobra.Command{ - Use: "destroy [options] NAME", - Short: "Destroy an existing machine", - Long: "Destroy an existing machine ", - RunE: destroy, - Args: cobra.ExactArgs(1), - Example: `podman machine destroy myvm`, - ValidArgsFunction: completion.AutocompleteNone, - } -) - -var ( - destoryOptions machine.DestroyOptions -) - -func init() { - registry.Commands = append(registry.Commands, registry.CliCommand{ - Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, - Command: destroyCmd, - Parent: machineCmd, - }) - - flags := destroyCmd.Flags() - formatFlagName := "force" - flags.BoolVar(&destoryOptions.Force, formatFlagName, false, "Do not prompt before destroying") - _ = destroyCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteJSONFormat) - - keysFlagName := "save-keys" - flags.BoolVar(&destoryOptions.SaveKeys, keysFlagName, false, "Do not delete SSH keys") - _ = destroyCmd.RegisterFlagCompletionFunc(keysFlagName, common.AutocompleteJSONFormat) - - ignitionFlagName := "save-ignition" - flags.BoolVar(&destoryOptions.SaveIgnition, ignitionFlagName, false, "Do not delete ignition file") - _ = destroyCmd.RegisterFlagCompletionFunc(ignitionFlagName, common.AutocompleteJSONFormat) - - imageFlagName := "save-image" - flags.BoolVar(&destoryOptions.SaveImage, imageFlagName, false, "Do not delete the image file") - _ = destroyCmd.RegisterFlagCompletionFunc(imageFlagName, common.AutocompleteJSONFormat) -} - -func destroy(cmd *cobra.Command, args []string) error { - var ( - err error - vm machine.VM - vmType string - ) - switch vmType { - default: - vm, err = qemu.LoadVMByName(args[0]) - } - if err != nil { - return err - } - confirmationMessage, doIt, err := vm.Destroy(args[0], machine.DestroyOptions{}) - 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 doIt() -} diff --git a/cmd/podman/machine/machine.go b/cmd/podman/machine/machine.go index ce5a3b889..ed284ee10 100644 --- a/cmd/podman/machine/machine.go +++ b/cmd/podman/machine/machine.go @@ -1,3 +1,5 @@ +// +build amd64,linux amd64,darwin arm64,darwin + package machine import ( @@ -15,7 +17,7 @@ var ( machineCmd = &cobra.Command{ Use: "machine", Short: "Manage a virtual machine", - Long: "Manage a virtual machine. Virtual machines are used to run Podman on Macs.", + Long: "Manage a virtual machine. Virtual machines are used to run Podman.", PersistentPreRunE: noOp, PersistentPostRunE: noOp, RunE: validate.SubCommandExists, 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/remove.go b/cmd/podman/machine/remove.go new file mode 100644 index 000000000..f6ce9e326 --- /dev/null +++ b/cmd/podman/machine/remove.go @@ -0,0 +1,88 @@ +// +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 ( + removeCmd = &cobra.Command{ + Use: "remove [options] NAME", + Short: "Remove an existing machine", + Long: "Remove an existing machine ", + RunE: remove, + Args: cobra.ExactArgs(1), + Example: `podman machine remove 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: removeCmd, + Parent: machineCmd, + }) + + flags := removeCmd.Flags() + formatFlagName := "force" + flags.BoolVar(&destoryOptions.Force, formatFlagName, false, "Do not prompt before removeing") + + 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 remove(cmd *cobra.Command, args []string) error { + var ( + err error + vm machine.VM + vmType string + ) + switch vmType { + default: + vm, err = qemu.LoadVMByName(args[0]) + } + if err != nil { + return err + } + confirmationMessage, doIt, err := vm.Remove(args[0], 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 doIt() +} diff --git a/cmd/podman/machine/ssh.go b/cmd/podman/machine/ssh.go index 32483f731..a7111a195 100644 --- a/cmd/podman/machine/ssh.go +++ b/cmd/podman/machine/ssh.go @@ -1,7 +1,9 @@ +// +build amd64,linux amd64,darwin arm64,darwin + package machine import ( - "github.com/containers/common/pkg/completion" + "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" @@ -14,13 +16,13 @@ var ( sshCmd = &cobra.Command{ Use: "ssh [options] NAME [COMMAND [ARG ...]]", Short: "SSH into a virtual machine", - Long: "SSH into a podman-managed virtual machine ", + Long: "SSH into a virtual machine ", RunE: ssh, Args: cobra.MinimumNArgs(1), Example: `podman machine ssh myvm podman machine ssh -e myvm echo hello`, - ValidArgsFunction: completion.AutocompleteNone, + ValidArgsFunction: common.AutocompleteMachineSSH, } ) @@ -38,7 +40,6 @@ func init() { flags := sshCmd.Flags() executeFlagName := "execute" flags.BoolVarP(&sshOpts.Execute, executeFlagName, "e", false, "Execute command from args") - _ = sshCmd.RegisterFlagCompletionFunc(executeFlagName, completion.AutocompleteDefault) } func ssh(cmd *cobra.Command, args []string) error { diff --git a/cmd/podman/machine/start.go b/cmd/podman/machine/start.go index 762639358..44ade2850 100644 --- a/cmd/podman/machine/start.go +++ b/cmd/podman/machine/start.go @@ -1,3 +1,5 @@ +// +build amd64,linux amd64,darwin arm64,darwin + package machine import ( diff --git a/cmd/podman/machine/stop.go b/cmd/podman/machine/stop.go index b6585c296..35fd4ff95 100644 --- a/cmd/podman/machine/stop.go +++ b/cmd/podman/machine/stop.go @@ -1,3 +1,5 @@ +// +build amd64,linux amd64,darwin arm64,darwin + package machine import ( -- cgit v1.2.3-54-g00ecf