-
Notifications
You must be signed in to change notification settings - Fork 0
/
margo.go
230 lines (169 loc) · 3.73 KB
/
margo.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bytes"
"code.google.com/p/go.crypto/ssh"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"sync"
)
type Config struct {
Jobs []Jobs
}
type Jobs struct {
Name string
Tasks []Task
}
type Task struct {
Hosts []string
Key string
Commands []Command
Options TaskOptions
}
type Command struct {
User, Exec string
}
type TaskOptions struct {
Hosts HostsOptions
Commands CommandsOptions
}
type HostsOptions struct {
Sync bool
}
type CommandsOptions struct {
Sync bool
}
type Keychain struct {
Keys []ssh.Signer
}
func errorHandler(x *error) {
if *x != nil {
panic(*x)
}
}
func (k *Keychain) Key(i int) (ssh.PublicKey, error) {
if i < 0 || i >= len(k.Keys) {
return nil, nil
}
return k.Keys[i].PublicKey(), nil
}
func (k *Keychain) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
return k.Keys[i].Sign(rand, data)
}
func (k *Keychain) LoadPEM(file string) (err error) {
buf, err := ioutil.ReadFile(file)
if err != nil {
return err
}
key, err := ssh.ParsePrivateKey(buf)
if err != nil {
return err
}
k.Keys = append(k.Keys, key)
return nil
}
func createClient(key *string, address *string, user *string) (client *ssh.ClientConn, err error) {
k := new(Keychain)
err = k.LoadPEM(*key)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: *user,
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(k),
},
}
client, err = ssh.Dial("tcp", *address, config)
if err != nil {
return nil, err
}
return client, nil
}
func parseConfig(path *string) (config *Config, err error) {
file, err := ioutil.ReadFile(*path)
if err != nil {
return nil, err
}
json.Unmarshal(file, &config)
return config, nil
}
func runTask(task *Task, tg *sync.WaitGroup) {
var hg sync.WaitGroup
hosts := task.Hosts
key := task.Key
commands := task.Commands
options := task.Options
for m, n := 0, len(hosts); m < n; m++ {
hg.Add(1)
if options.Hosts.Sync == true {
executeHostCommands(&hosts[m], &key, &options, commands[:], &hg)
} else {
go executeHostCommands(&hosts[m], &key, &options, commands[:], &hg)
}
}
hg.Wait()
tg.Done()
}
func executeHostCommands(host, key *string, options *TaskOptions, commands []Command, hg *sync.WaitGroup) {
var cg sync.WaitGroup
for o, p := 0, len(commands); o < p; o++ {
cg.Add(1)
user := commands[o].User
exec := commands[o].Exec
if options.Commands.Sync == true {
executeCommand(&user, &exec, host, key, &cg)
} else {
go executeCommand(&user, &exec, host, key, &cg)
}
}
cg.Wait()
hg.Done()
}
func executeCommand(user, exec, host, key *string, cg *sync.WaitGroup) {
client, err := createClient(key, host, user)
errorHandler(&err)
defer client.Close()
session, err := client.NewSession()
errorHandler(&err)
defer session.Close()
var buf bytes.Buffer
session.Stdout = &buf
err = session.Run(*exec)
errorHandler(&err)
fmt.Println(*exec + " - " + *user + "@" + *host + ":")
fmt.Println(buf.String())
cg.Done()
}
func main() {
var configFile, jobToRun string
flag.StringVar(&configFile, "config", "", "Location of config.json")
flag.StringVar(&jobToRun, "job", "", "Name of the job to run")
flag.Parse()
if configFile == "" {
err := errors.New("Missing config flag (ex: -config \"/tmp/config.json\")")
errorHandler(&err)
}
if jobToRun == "" {
err := errors.New("Missing job flag (ex: -job \"My Job\")")
errorHandler(&err)
}
config, err := parseConfig(&configFile)
errorHandler(&err)
var tg sync.WaitGroup
for i, j := 0, len(config.Jobs); i < j; i++ {
job := config.Jobs[i]
name := job.Name
if name == jobToRun {
tasks := job.Tasks
for k, l := 0, len(tasks); k < l; k++ {
tg.Add(1)
go runTask(&tasks[k], &tg)
}
}
}
tg.Wait()
}