@@ -13,51 +13,66 @@ import (
1313)
1414
1515// SSH opens an interactive shell to a machine in the cluster
16- func (c * FleetClient ) SSH (name string ) (err error ) {
17- var sshClient * ssh.SSHForwardingClient
16+ func (c * FleetClient ) SSH (name string ) error {
17+ sshClient , err := c .sshConnect (name )
18+ if err != nil {
19+ return err
20+ }
21+
22+ defer sshClient .Close ()
23+ err = ssh .Shell (sshClient )
24+ return err
25+ }
26+
27+ func (c * FleetClient ) SSHExec (name , cmd string ) error {
28+ fmt .Printf ("Excuting '%s' on container '%s'\n " , cmd , name )
29+
30+ conn , err := c .sshConnect (name )
31+ if err != nil {
32+ return err
33+ }
34+
35+ err , _ = ssh .Execute (conn , cmd )
36+ return err
37+ }
38+
39+ func (c * FleetClient ) sshConnect (name string ) (* ssh.SSHForwardingClient , error ) {
1840
1941 timeout := time .Duration (Flags .SSHTimeout * 1000 ) * time .Millisecond
2042
2143 ms , err := c .machineState (name )
2244 if err != nil {
23- return err
45+ return nil , err
2446 }
2547
2648 // If name isn't a machine ID, try it as a unit instead
2749 if ms == nil {
2850 units , err := c .Units (name )
2951
3052 if err != nil {
31- return err
53+ return nil , err
3254 }
3355
3456 machID , err := c .findUnit (units [0 ])
3557
3658 if err != nil {
37- return err
59+ return nil , err
3860 }
3961
4062 ms , err = c .machineState (machID )
4163
4264 if err != nil || ms == nil {
43- return err
65+ return nil , err
4466 }
4567 }
4668
4769 addr := ms .PublicIP
4870
4971 if tun := getTunnelFlag (); tun != "" {
50- sshClient , err = ssh .NewTunnelledSSHClient ("core" , tun , addr , getChecker (), false , timeout )
51- } else {
52- sshClient , err = ssh .NewSSHClient ("core" , addr , getChecker (), false , timeout )
53- }
54- if err != nil {
55- return err
72+ return ssh .NewTunnelledSSHClient ("core" , tun , addr , getChecker (), false , timeout )
5673 }
74+ return ssh .NewSSHClient ("core" , addr , getChecker (), false , timeout )
5775
58- defer sshClient .Close ()
59- err = ssh .Shell (sshClient )
60- return err
6176}
6277
6378// runCommand will attempt to run a command on a given machine. It will attempt
0 commit comments