Skip to content

Commit

Permalink
split ssh into ssh and exec commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed May 9, 2020
1 parent 6e38ea3 commit df9d88e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 22 deletions.
71 changes: 71 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"errors"
"fmt"
"github.com/mhewedy/vermin/vms"
"os"
"strings"

"github.com/spf13/cobra"
)

// execCmd represents the exec command
var execCmd = &cobra.Command{
Use: "exec",
Short: "Run a command in a running VM",
Long: `Run a command in a running VM
Examples:
Execute remote command:
$ vermin exec vm_09 cat /etc/passwd
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
var command string
if len(args) > 1 {
command = strings.Join(args[1:], " ")
}
err := vms.Exec(vmName, command)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("vm required")
}
return nil
},
ValidArgsFunction: listRunningVms,
}

func init() {
rootCmd.AddCommand(execCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// execCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
//execCmd.Flags().BoolP("purge", "p", false, "Purge the IP cache")
}
18 changes: 4 additions & 14 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import (
"errors"
"fmt"
"github.com/mhewedy/vermin/vms"
"os"
"strings"

"github.com/spf13/cobra"
"os"
)

// sshCmd represents the ssh command
Expand All @@ -33,25 +31,17 @@ var sshCmd = &cobra.Command{
Examples:
Open a terminal:
$ vermin ssh vm_02
Execute remote command:
$ vermin ssh vm_09 cat /etc/passwd
$ vermin ssh vm_02
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
var command string
if len(args) > 1 {
command = strings.Join(args[1:], " ")
}
err := vms.SecureShell(vmName, command)
if err != nil {
if err := vms.SecureShell(vmName); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
if len(args) != 1 {
return errors.New("vm required")
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions command/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func Execute(vmName string, cmd string) (string, error) {
return command.Ssh(ipAddr, "--", cmd).Call()
}

//Interact execute ssh in interactive mode
func Interact(vmName string, cmd string) error {
//ExecInteract execute ssh in interactive mode
func ExecInteract(vmName string, cmd string) error {
ipAddr, err := ip.Find(vmName, false)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion vms/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func provision(vmName string, script string) error {
if _, err := ssh.Execute(vmName, "chmod +x "+vmFile); err != nil {
return err
}
if err := ssh.Interact(vmName, vmFile); err != nil {
if err := ssh.ExecInteract(vmName, vmFile); err != nil {
return err
}

Expand Down
18 changes: 13 additions & 5 deletions vms/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Stop(vmName string) error {
return nil
}

func SecureShell(vmName string, cmd string) error {
func SecureShell(vmName string) error {
if err := checkRunningVM(vmName); err != nil {
return err
}
Expand All @@ -51,11 +51,19 @@ func SecureShell(vmName string, cmd string) error {
return err
}

if len(cmd) == 0 {
return ssh.OpenTerminal(vmName)
} else {
return ssh.Interact(vmName, cmd)
return ssh.OpenTerminal(vmName)
}

func Exec(vmName string, cmd string) error {
if err := checkRunningVM(vmName); err != nil {
return err
}

if err := ssh.EstablishConn(vmName); err != nil {
return err
}

return ssh.ExecInteract(vmName, cmd)
}

func Remove(vmName string, force bool) error {
Expand Down

0 comments on commit df9d88e

Please sign in to comment.