Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
fleetctl: properly run local and remote commands
Browse files Browse the repository at this point in the history
Remote commands need to be escaped because they are being interpretted
by a shell. Local commands, however, cannot be because they are being
exec'd. Additionally, splitting an opaque command by space is very error
prone (e.g. `ls "dumb filename"`).
  • Loading branch information
crawford committed Jun 18, 2015
1 parent cb6eb8c commit 2d613e3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
10 changes: 5 additions & 5 deletions fleetctl/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"fmt"
"strconv"

"github.com/coreos/fleet/job"
)
Expand Down Expand Up @@ -71,15 +71,15 @@ func runJournal(args []string) (exit int) {
return 1
}

command := fmt.Sprintf("journalctl --unit %s --no-pager -n %d", name, flagLines)
cmd := []string{"journalctl", "--unit", name, "--no-pager", "-n", strconv.Itoa(flagLines)}

if flagSudo {
command = "sudo " + command
cmd = append([]string{"sudo"}, cmd...)
}

if flagFollow {
command += " -f"
cmd = append(cmd, "-f")
}

return runCommand(command, u.MachineID)
return runCommand(u.MachineID, cmd[0], cmd[1:]...)
}
20 changes: 12 additions & 8 deletions fleetctl/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ func findAddressInRunningUnits(name string) (string, bool, error) {

// runCommand will attempt to run a command on a given machine. It will attempt
// to SSH to the machine if it is identified as being remote.
func runCommand(cmd string, machID string) (retcode int) {
func runCommand(machID string, cmd string, args ...string) (retcode int) {
var err error
if machine.IsLocalMachineID(machID) {
err, retcode = runLocalCommand(cmd)
err, retcode = runLocalCommand(cmd, args...)
if err != nil {
stderr("Error running local command: %v", err)
}
Expand All @@ -227,7 +227,7 @@ func runCommand(cmd string, machID string) (retcode int) {
stderr("Error getting machine IP: %v", err)
} else {
addr := findSSHPort(ms.PublicIP)
err, retcode = runRemoteCommand(cmd, addr)
err, retcode = runRemoteCommand(addr, cmd, args...)
if err != nil {
stderr("Error running remote command: %v", err)
}
Expand All @@ -237,9 +237,8 @@ func runCommand(cmd string, machID string) (retcode int) {
}

// runLocalCommand runs the given command locally and returns any error encountered and the exit code of the command
func runLocalCommand(cmd string) (error, int) {
cmdSlice := strings.Split(cmd, " ")
osCmd := exec.Command(cmdSlice[0], cmdSlice[1:]...)
func runLocalCommand(cmd string, args ...string) (error, int) {
osCmd := exec.Command(cmd, args...)
osCmd.Stderr = os.Stderr
osCmd.Stdout = os.Stdout
osCmd.Start()
Expand All @@ -259,7 +258,7 @@ func runLocalCommand(cmd string) (error, int) {

// runRemoteCommand runs the given command over SSH on the given IP, and returns
// any error encountered and the exit status of the command
func runRemoteCommand(cmd string, addr string) (err error, exit int) {
func runRemoteCommand(addr string, cmd string, args ...string) (err error, exit int) {
var sshClient *ssh.SSHForwardingClient
timeout := getSSHTimeoutFlag()
if tun := getTunnelFlag(); tun != "" {
Expand All @@ -271,7 +270,12 @@ func runRemoteCommand(cmd string, addr string) (err error, exit int) {
return err, -1
}

cmdargs := cmd
for _, arg := range args {
cmdargs += fmt.Sprintf(" %q", arg)
}

defer sshClient.Close()

return ssh.Execute(sshClient, cmd)
return ssh.Execute(sshClient, cmdargs)
}
3 changes: 1 addition & 2 deletions fleetctl/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func runStatusUnits(args []string) (exit int) {
fmt.Printf("\n")
}

cmd := fmt.Sprintf("systemctl status -l %q", name)
if exit = runCommand(cmd, uMap[name].MachineID); exit != 0 {
if exit = runCommand(uMap[name].MachineID, "systemctl", "status", "-l", name); exit != 0 {
break
}
}
Expand Down

0 comments on commit 2d613e3

Please sign in to comment.