/
repofinder.go
400 lines (315 loc) · 8.16 KB
/
repofinder.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package main
import (
"github.com/alexflint/go-arg"
"fmt"
"path/filepath"
"os"
"strings"
"golang.org/x/crypto/ssh"
"io/ioutil"
"io"
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"strconv"
"bytes"
"time"
)
type SSHClient struct {
Config *ssh.ClientConfig
Host string
Port int
ConnTimeout time.Duration
}
type SSHCommand struct {
Path string
Env []string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
var args struct {
Env string `arg:"-e,help:choose an environment to scan,required"`
Locations []string `arg:"-l,help:search location origin"`
Repo string `arg:"-r,help:pick a single repo to check"`
Debug bool `arg:"-d,help:turn on debug mode"`
Host string `args:"-H,help:URI of host to interogate,required"`
Port int `args:"-P,help:SSH port of host,required"`
File string `args:"-f,help:file containing commands to execute,required"`
}
type Context struct {
Prov []struct {
Env string `json:"env"`
System []struct {
Channel string `json:"channel"`
Commands []string `json:"commands"`
DNS string `json:"dns"`
Name string `json:"name"`
Port string `json:"port"`
} `json:"system"`
} `json:"prov"`
}
type BuildInfo struct {
Build_date string `json:"build.date"`
Build_version_full string `json:"build.version.full"`
}
type Versions struct {
Prov []Envs `json:"prov"`
}
type Envs struct {
Env string `json:"env"`
Vrs []Version `json:"versions"`
}
type Version struct {
System string `json:"system"`
Channel string `json:"channel"`
Version []string `json:"version"`
BuildInfo BuildInfo `json:"buildInfo"`
}
func (vers *Versions) AddEnvs(env Envs) []Envs {
vers.Prov = append(vers.Prov, env)
return vers.Prov
}
func (vers *Envs) AddVersion(version Version) []Version {
vers.Vrs = append(vers.Vrs, version)
return vers.Vrs
}
func (vers *Envs) AddEnv(env string) string {
vers.Env = env
return vers.Env
}
func (ver *Version) AddVer(vers []string) []string {
ver.Version = vers
return ver.Version
}
func (vers *Version) AddBuildInfo(buildInfo BuildInfo) BuildInfo {
vers.BuildInfo = buildInfo
return vers.BuildInfo
}
func init() {
arg.MustParse(&args)
}
type prov struct {
prov string
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/getAgg", AggVersions)
http.Handle("/", r)
// Create client config
fmt.Println("Listening...")
http.ListenAndServe(":3000", nil)
fmt.Printf("The environment you picked was --> %s\n ", args.Env)
for _, loc := range args.Locations {
if err := filepath.Walk(loc, func(path string, fileInfo os.FileInfo, _ error) error {
if !(strings.Contains(path, ".git")) {
if fileInfo.Mode().IsRegular() {
fmt.Printf("The file %s is a file %t \n", path, fileInfo.Mode().IsRegular())
}
}
return nil
}); err != nil {
fmt.Printf("The path to enlightenment was fraught .........%s ,\n", err)
os.Exit(1)
}
}
}
func AggVersions(w http.ResponseWriter, r *http.Request) {
b, err := json.Marshal(ComposeJSON())
if err != nil {
fmt.Println("Error Marshalling struct j:", err)
return
}
w.Write([]byte(b))
}
func ComposeJSON() *Versions {
sshCnf := getSshConfig()
versArry := make([]Version, 2)
v := new(Versions)
file, err1 := os.Open("commands.json")
if err1 != nil {
fmt.Println("Error:", err1)
} //#2
defer file.Close()
fmt.Println("Hello json")
decoder := json.NewDecoder(file)
context := Context{}
err := decoder.Decode(&context)
if err != nil {
fmt.Println("Error:", err)
}
for _, prov := range context.Prov {
e := new(Envs)
e.AddEnv(prov.Env)
fmt.Println("envs -->", prov.Env)
for _, system := range prov.System {
client := getSshClient(sshCnf, system.DNS, system.Port)
for i, cmd := range system.Commands {
commd := getSshCommand(cmd)
if err, prov := client.RunCommand(commd); err != nil {
fmt.Fprintf(os.Stderr, "command run error: %s\n", err)
os.Exit(1)
} else {
if (strings.Contains(system.Name, "agg")) {
decode := json.NewDecoder(bytes.NewReader([]uint8(prov.(string))))
bi := BuildInfo{}
err1 := decode.Decode(&bi)
fmt.Printf("bi %s", bi)
if err1 != nil {
fmt.Println("Error: decoding build info", err1)
} else {
versArry[i] = Version{system.Name, "", nil, bi}
e.AddVersion(versArry[i])
}
} else {
str1 := strings.Split(prov.(string), ".jar")
for n, x := range str1 {
str1[n] = strings.TrimSpace(x)
}
versArry[i] = Version{system.Name, system.Channel, str1, BuildInfo{}}
e.AddVersion(versArry[i])
}
}
}
}
v.AddEnvs(*e)
}
return v
}
func getSshClient(sshCnf *ssh.ClientConfig, host string, port string) *SSHClient {
nPort, err := strconv.Atoi(port)
if err != nil {
fmt.Printf("Error: port number is invalid %s , %s", port, err)
}
client := &SSHClient{
Config: sshCnf,
Host: host,
Port: nPort,
ConnTimeout: time.Duration(time.Millisecond * 1000), //set to 500ms
}
return client
}
func getSshCommand(commd string) *SSHCommand {
cmd := &SSHCommand{
Path: commd,
Env: []string{""},
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
return cmd
}
func getSshConfig() *ssh.ClientConfig {
sshConfig := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
PublicKeyFile("/Users/adrianjackson/.ssh/tsmkey05.pem"),
},
}
return sshConfig
}
func PublicKeyFile(file string) ssh.AuthMethod {
buffer, err := ioutil.ReadFile(file)
if err != nil {
return nil
}
key, err := ssh.ParsePrivateKey(buffer)
if err != nil {
return nil
}
return ssh.PublicKeys(key)
}
func print(w io.Writer) {
fmt.Fprintln(w, "output")
}
func (client *SSHClient) RunCommand(cmd *SSHCommand) (error, interface{}) {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
cmd.Stdout = w
var (
session *ssh.Session
err error
)
fmt.Println("Session creation")
if session, err = client.newSession(); err != nil {
fmt.Println("Session creation err", err)
return err, nil
}
defer session.Close()
if err = client.prepareCommand(session, cmd); err != nil {
return err, nil
}
fmt.Println("Session run",cmd.Path)
if err = session.Run(cmd.Path); err != nil {
fmt.Println("Session run err", err)
return err, nil
}
print(cmd.Stdout)
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout
if (strings.Contains(cmd.Path, "providerLib")) {
s1 := string([]byte(out))
return err, s1
} else if (strings.Contains(cmd.Path, "build")) {
fmt.Printf("-->%s<--", string([]byte(out)))
s1 := string([]byte(out))
return err, s1
}
defer w.Close()
defer r.Close()
return err, nil
}
func (client *SSHClient) newSession() (*ssh.Session, error) {
connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Host, client.Port), client.Config)
if err != nil {
return nil, fmt.Errorf("Failed to dial: %s", err)
}
session, err := connection.NewSession()
if err != nil {
return nil, fmt.Errorf("Failed to create session: %s", err)
}
modes := ssh.TerminalModes{
// ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
if err := session.RequestPty("xterm", 80, 80, modes); err != nil {
session.Close()
return nil, fmt.Errorf("request for pseudo terminal failed: %s", err)
}
return session, nil
}
func (client *SSHClient) prepareCommand(session *ssh.Session, cmd *SSHCommand) error {
for _, env := range cmd.Env {
variable := strings.Split(env, "=")
if len(variable) != 2 {
continue
}
if err := session.Setenv(variable[0], variable[1]); err != nil {
return err
}
}
if cmd.Stdin != nil {
stdin, err := session.StdinPipe()
if err != nil {
return fmt.Errorf("Unable to setup stdin for session: %v", err)
}
go io.Copy(stdin, cmd.Stdin)
}
if cmd.Stdout != nil {
stdout, err := session.StdoutPipe()
if err != nil {
return fmt.Errorf("Unable to setup stdout for session: %v", err)
}
go io.Copy(cmd.Stdout, stdout)
}
if cmd.Stderr != nil {
stderr, err := session.StderrPipe()
if err != nil {
return fmt.Errorf("Unable to setup stderr for session: %v", err)
}
go io.Copy(cmd.Stderr, stderr)
}
return nil
}