Skip to content

Commit

Permalink
Merge pull request #9894 from baude/machinesshfix
Browse files Browse the repository at this point in the history
Remove --execute from podman machine ssh
  • Loading branch information
openshift-merge-robot authored Apr 1, 2021
2 parents 12881ab + f6438d3 commit 8b599c5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 35 deletions.
53 changes: 32 additions & 21 deletions cmd/podman/machine/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
Expand All @@ -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 {
Expand Down
28 changes: 18 additions & 10 deletions docs/source/markdown/podman-machine-ssh.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ type ListResponse struct {
}

type SSHOptions struct {
Execute bool
Args []string
Args []string
}
type StartOptions struct{}

Expand Down
21 changes: 19 additions & 2 deletions pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 8b599c5

Please sign in to comment.