4
4
package exec
5
5
6
6
import (
7
+ "fmt"
7
8
gossh "github.com/coreos/fleet/Godeps/_workspace/src/golang.org/x/crypto/ssh"
9
+ "github.com/mgutz/ansi"
10
+ goexec "os/exec"
8
11
"sync"
9
12
"time"
10
13
@@ -13,14 +16,68 @@ import (
13
16
"github.com/namshi/godo/src/ssh"
14
17
)
15
18
19
+ // Checks whether a command needs
20
+ // to be run locally or remotely.
21
+ //
22
+ // If the only server is "local"
23
+ // then it means that we need to
24
+ // simply run the command on this
25
+ // local machine.
26
+ func isLocalCommand (servers map [string ]config.Server ) bool {
27
+ if len (servers ) == 1 {
28
+ if _ , ok := servers ["local" ]; ok {
29
+ return true
30
+ }
31
+ }
32
+
33
+ return false
34
+ }
35
+
36
+ // Executes the command on the given
37
+ // servers.
38
+ //
39
+ // The main goal of this method is to
40
+ // figure out whether this command needs
41
+ // to be executed locally or remotely,
42
+ // and go ahead with the proper execution
43
+ // strategy.
44
+ func ExecuteCommands (command string , servers map [string ]config.Server , cfg config.Config ) {
45
+ if isLocalCommand (servers ) {
46
+ executeLocalCommand (command )
47
+ } else {
48
+ executeRemoteCommands (command , servers , cfg )
49
+ }
50
+ }
51
+
52
+ // Executes a command locally.
53
+ func executeLocalCommand (command string ) {
54
+ cmd := goexec .Command (command )
55
+ stdout , stderr := log .GetRemoteLoggers ("local" )
56
+ cmd .Stdout = stdout
57
+ cmd .Stderr = stderr
58
+
59
+ err := cmd .Start ()
60
+
61
+ // failed to spawn new process
62
+ if err != nil {
63
+ fmt .Println (ansi .Color (err .Error (), "red+h" ))
64
+ }
65
+
66
+ // Failed to execute?
67
+ err = cmd .Wait ()
68
+ if err != nil {
69
+ fmt .Println (ansi .Color (err .Error (), "red+h" ))
70
+ }
71
+ }
72
+
16
73
// Executes the given command on a series
17
74
// of servers.
18
75
//
19
76
// We will launch N goroutins based on how
20
77
// many commands we need to remotely execute,
21
78
// and stop the execution once everyone is
22
79
// done.
23
- func ExecuteRemoteCommands (command string , servers map [string ]config.Server , cfg config.Config ) {
80
+ func executeRemoteCommands (command string , servers map [string ]config.Server , cfg config.Config ) {
24
81
var wg sync.WaitGroup
25
82
wg .Add (len (servers ))
26
83
@@ -29,7 +86,7 @@ func ExecuteRemoteCommands(command string, servers map[string]config.Server, cfg
29
86
c := & ssh.Config {Address : serverConfig .Address , Alias : server , Tunnel : serverConfig .Tunnel , User : serverConfig .User , Hostfile : cfg .Hostfile }
30
87
c .Timeout = time .Duration (cfg .Timeout ) * time .Second
31
88
session , _ := ssh .CreateClient (c ).NewSession ()
32
- ExecuteRemoteCommand (command , session , server )
89
+ executeRemoteCommand (command , session , server )
33
90
defer wg .Done ()
34
91
}(server , serverConfig )
35
92
}
@@ -39,7 +96,7 @@ func ExecuteRemoteCommands(command string, servers map[string]config.Server, cfg
39
96
40
97
// Executes the given command through SSH,
41
98
// connecting with the given config.
42
- func ExecuteRemoteCommand (command string , session * gossh.Session , server string ) {
99
+ func executeRemoteCommand (command string , session * gossh.Session , server string ) {
43
100
stdout , stderr := log .GetRemoteLoggers (server )
44
101
session .Stdout = stdout
45
102
session .Stderr = stderr
0 commit comments