-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.go
86 lines (72 loc) · 1.89 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cli
import (
"context"
"crypto/tls"
"fmt"
"strings"
"time"
"github.com/alitto/pond"
"github.com/dpogorzelski/speedrun/pkg/speedrun/cloud"
portalpb "github.com/dpogorzelski/speedrun/proto/portal"
"storj.io/drpc/drpcconn"
"github.com/apex/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var runCmd = &cobra.Command{
Use: "run <command to run>",
Short: "Run a shell command on remote servers",
Example: " speedrun run whoami\n speedrun run whoami --target \"labels.foo = bar AND labels.environment = staging\"",
Args: cobra.MinimumNArgs(1),
RunE: run,
}
func init() {
runCmd.SetUsageTemplate(usage)
}
func run(cmd *cobra.Command, args []string) error {
command := strings.Join(args, " ")
s := strings.Split(command, " ")
usePrivateIP := viper.GetBool("portal.use-private-ip")
tlsConfig, err := cloud.SetupTLS()
if err != nil {
return err
}
target, err := cmd.Flags().GetString("target")
if err != nil {
return err
}
instances, err := cloud.GetInstances(target)
if err != nil {
return err
}
pool := pond.New(1000, 10000)
for _, p := range instances {
instance := p
pool.Submit(func() {
fields := log.Fields{
"host": instance.Name,
"address": instance.GetAddress(usePrivateIP),
}
log := log.WithFields(fields)
addr := fmt.Sprintf("%s:%d", instance.GetAddress(usePrivateIP), 1337)
rawconn, err := tls.Dial("tcp", addr, tlsConfig)
if err != nil {
log.Error(err.Error())
return
}
conn := drpcconn.New(rawconn)
defer conn.Close()
c := portalpb.NewDRPCPortalClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
r, err := c.RunCommand(ctx, &portalpb.CommandRequest{Name: s[0], Args: s[1:]})
if err != nil {
log.Error(err.Error())
return
}
log.WithField("state", r.GetState()).Info(r.GetMessage())
})
}
pool.StopAndWait()
return nil
}