mirror of
https://github.com/containers/podman.git
synced 2025-07-03 17:27:18 +08:00
Remove --execute from podman machine ssh
The --execute flag ended up serving no purpose. It was removed and documentation was updated. Fixed a panic when no VM name was provided. [NO TESTS NEEDED] Signed-off-by: baude <bbaude@redhat.com>
This commit is contained in:
@ -13,14 +13,12 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
sshCmd = &cobra.Command{
|
sshCmd = &cobra.Command{
|
||||||
Use: "ssh [options] [MACHINE] [COMMAND [ARG ...]]",
|
Use: "ssh [NAME] [COMMAND [ARG ...]]",
|
||||||
Short: "SSH into a virtual machine",
|
Short: "SSH into a virtual machine",
|
||||||
Long: "SSH into a virtual machine ",
|
Long: "SSH into a virtual machine ",
|
||||||
RunE: ssh,
|
RunE: ssh,
|
||||||
Args: cobra.MaximumNArgs(1),
|
|
||||||
Example: `podman machine ssh myvm
|
Example: `podman machine ssh myvm
|
||||||
podman machine ssh -e myvm echo hello`,
|
podman machine ssh -e myvm echo hello`,
|
||||||
|
|
||||||
ValidArgsFunction: autocompleteMachineSSH,
|
ValidArgsFunction: autocompleteMachineSSH,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -30,36 +28,49 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
sshCmd.Flags().SetInterspersed(false)
|
||||||
registry.Commands = append(registry.Commands, registry.CliCommand{
|
registry.Commands = append(registry.Commands, registry.CliCommand{
|
||||||
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
||||||
Command: sshCmd,
|
Command: sshCmd,
|
||||||
Parent: machineCmd,
|
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 {
|
func ssh(cmd *cobra.Command, args []string) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
|
validVM bool
|
||||||
vm machine.VM
|
vm machine.VM
|
||||||
vmType string
|
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
|
// Set the VM to default
|
||||||
if !sshOpts.Execute && len(sshOpts.Args) > 0 {
|
vmName := defaultMachineName
|
||||||
return errors.New("too many args: to execute commands via ssh, use -e flag")
|
// 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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
// 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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch vmType {
|
switch vmType {
|
||||||
|
@ -4,36 +4,44 @@
|
|||||||
podman\-machine\-ssh - SSH into a virtual machine
|
podman\-machine\-ssh - SSH into a virtual machine
|
||||||
|
|
||||||
## SYNOPSIS
|
## SYNOPSIS
|
||||||
**podman machine ssh** [*options*] [*name*] [*command* [*arg* ...]]
|
**podman machine ssh** [*name*] [*command* [*arg* ...]]
|
||||||
|
|
||||||
## DESCRIPTION
|
## 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
|
## OPTIONS
|
||||||
|
|
||||||
#### **\-\-execute**, **-e**
|
|
||||||
|
|
||||||
Execute the given command on the VM
|
|
||||||
|
|
||||||
#### **\-\-help**
|
#### **\-\-help**
|
||||||
|
|
||||||
Print usage statement.
|
Print usage statement.
|
||||||
|
|
||||||
## EXAMPLES
|
## EXAMPLES
|
||||||
|
|
||||||
|
To get an interactive session with the default virtual machine:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ podman machine ssh
|
||||||
|
```
|
||||||
|
|
||||||
To get an interactive session with a VM called `myvm`:
|
To get an interactive session with a VM called `myvm`:
|
||||||
```
|
```
|
||||||
$ podman machine ssh 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`:
|
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
|
## SEE ALSO
|
||||||
|
@ -56,7 +56,6 @@ type ListResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SSHOptions struct {
|
type SSHOptions struct {
|
||||||
Execute bool
|
|
||||||
Args []string
|
Args []string
|
||||||
}
|
}
|
||||||
type StartOptions struct{}
|
type StartOptions struct{}
|
||||||
|
@ -400,7 +400,7 @@ func (v *MachineVM) SSH(name string, opts machine.SSHOptions) error {
|
|||||||
port := strconv.Itoa(v.Port)
|
port := strconv.Itoa(v.Port)
|
||||||
|
|
||||||
args := []string{"-i", v.IdentityPath, "-p", port, sshDestination}
|
args := []string{"-i", v.IdentityPath, "-p", port, sshDestination}
|
||||||
if opts.Execute {
|
if len(opts.Args) > 0 {
|
||||||
args = append(args, opts.Args...)
|
args = append(args, opts.Args...)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Connecting to vm %s. To close connection, use `~.` or `exit`\n", v.Name)
|
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
|
// 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)
|
vmConfigDir, err := machine.GetConfDir(vmtype)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -493,3 +497,16 @@ func List(opts machine.ListOptions) ([]*machine.ListResponse, error) {
|
|||||||
}
|
}
|
||||||
return listed, err
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user