Merge pull request #9894 from baude/machinesshfix

Remove --execute from podman machine ssh
This commit is contained in:
OpenShift Merge Robot 2021-04-01 17:14:44 +02:00 committed by GitHub
commit 8b599c5126
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 35 deletions

View file

@ -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 {

View file

@ -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

View file

@ -56,8 +56,7 @@ type ListResponse struct {
}
type SSHOptions struct {
Execute bool
Args []string
Args []string
}
type StartOptions struct{}

View file

@ -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
}