diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-04-01 17:14:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-01 17:14:44 +0200 |
commit | 8b599c51268dc141bd963d8b5d3f25feadfcbb0e (patch) | |
tree | 6f6ade9a91ba306b3d27f7b8ae09da83830da7bd | |
parent | 12881abc065afda5bf979accfead542d411f48d6 (diff) | |
parent | f6438d36f3e52eb721f4223e767fd67b4c274d08 (diff) | |
download | podman-8b599c51268dc141bd963d8b5d3f25feadfcbb0e.tar.gz podman-8b599c51268dc141bd963d8b5d3f25feadfcbb0e.tar.bz2 podman-8b599c51268dc141bd963d8b5d3f25feadfcbb0e.zip |
Merge pull request #9894 from baude/machinesshfix
Remove --execute from podman machine ssh
-rw-r--r-- | cmd/podman/machine/ssh.go | 53 | ||||
-rw-r--r-- | docs/source/markdown/podman-machine-ssh.1.md | 28 | ||||
-rw-r--r-- | pkg/machine/config.go | 3 | ||||
-rw-r--r-- | pkg/machine/qemu/machine.go | 21 |
4 files changed, 70 insertions, 35 deletions
diff --git a/cmd/podman/machine/ssh.go b/cmd/podman/machine/ssh.go index aec68d2a4..fc7c71992 100644 --- a/cmd/podman/machine/ssh.go +++ b/cmd/podman/machine/ssh.go @@ -13,14 +13,12 @@ import ( var ( sshCmd = &cobra.Command{ - Use: "ssh [options] [MACHINE] [COMMAND [ARG ...]]", + Use: "ssh [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: autocompleteMachineSSH, } ) @@ -30,36 +28,49 @@ var ( ) func init() { + sshCmd.Flags().SetInterspersed(false) 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 + err error + validVM bool + 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") + // Set the VM to default + vmName := defaultMachineName + // If len is greater than 0, it means we may have been + // provided the VM name. If so, we check. The VM name, + // if provided, must be in args[0]. + if len(args) > 0 { + switch vmType { + default: + validVM, err = qemu.IsValidVMName(args[0]) + if err != nil { + return err + } + if validVM { + vmName = args[0] + } else { + sshOpts.Args = append(sshOpts.Args, args[0]) + } + } } - // 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") + // If len is greater than 1, it means we might have been + // given a vmname and args or just args + if len(args) > 1 { + if validVM { + sshOpts.Args = args[1:] + } else { + sshOpts.Args = args + } } switch vmType { diff --git a/docs/source/markdown/podman-machine-ssh.1.md b/docs/source/markdown/podman-machine-ssh.1.md index fa78f876a..30cbaf4bd 100644 --- a/docs/source/markdown/podman-machine-ssh.1.md +++ b/docs/source/markdown/podman-machine-ssh.1.md @@ -4,36 +4,44 @@ podman\-machine\-ssh - SSH into a virtual machine ## SYNOPSIS -**podman machine ssh** [*options*] [*name*] [*command* [*arg* ...]] +**podman machine ssh** [*name*] [*command* [*arg* ...]] ## DESCRIPTION -SSH into a Podman-managed virtual machine. +SSH into a Podman-managed virtual machine and optionally execute a command +on the virtual machine. Unless using the default virtual machine, the +first argument must be the virtual machine name. The optional command to +execute can then follow. If no command is provided, an interactive session +with the virtual machine is established. -Podman on MacOS requires a virtual machine. This is because containers are Linux - -containers do not run on any other OS because containers' core functionality are -tied to the Linux kernel. ## OPTIONS -#### **\-\-execute**, **-e** - -Execute the given command on the VM - #### **\-\-help** Print usage statement. ## EXAMPLES +To get an interactive session with the default virtual machine: + +``` +$ podman machine ssh +``` + To get an interactive session with a VM called `myvm`: ``` $ podman machine ssh myvm ``` +To run a command on the default virtual machine: +``` +$ podman machine ssh rpm -q podman +``` + To run a command on a VM called `myvm`: ``` -$ podman machine ssh -e myvm -- rpm -q podman +$ podman machine ssh myvm rpm -q podman ``` ## SEE ALSO diff --git a/pkg/machine/config.go b/pkg/machine/config.go index 554ea7c97..32b3b5c2b 100644 --- a/pkg/machine/config.go +++ b/pkg/machine/config.go @@ -56,8 +56,7 @@ type ListResponse struct { } type SSHOptions struct { - Execute bool - Args []string + Args []string } type StartOptions struct{} diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index b48926524..2652ebc10 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -400,7 +400,7 @@ func (v *MachineVM) SSH(name string, opts machine.SSHOptions) error { port := strconv.Itoa(v.Port) args := []string{"-i", v.IdentityPath, "-p", port, sshDestination} - if opts.Execute { + if len(opts.Args) > 0 { args = append(args, opts.Args...) } else { fmt.Printf("Connecting to vm %s. To close connection, use `~.` or `exit`\n", v.Name) @@ -446,7 +446,11 @@ func getDiskSize(path string) (uint64, error) { } // List lists all vm's that use qemu virtualization -func List(opts machine.ListOptions) ([]*machine.ListResponse, error) { +func List(_ machine.ListOptions) ([]*machine.ListResponse, error) { + return GetVMInfos() +} + +func GetVMInfos() ([]*machine.ListResponse, error) { vmConfigDir, err := machine.GetConfDir(vmtype) if err != nil { return nil, err @@ -493,3 +497,16 @@ func List(opts machine.ListOptions) ([]*machine.ListResponse, error) { } return listed, err } + +func IsValidVMName(name string) (bool, error) { + infos, err := GetVMInfos() + if err != nil { + return false, err + } + for _, vm := range infos { + if vm.Name == name { + return true, nil + } + } + return false, nil +} |