forked from adyatlov/bun-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.go
73 lines (69 loc) · 1.62 KB
/
bundle.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
package bun
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"regexp"
)
// Host represents a host in a DC/OS cluster.
type Host struct {
IP string
directory
}
// Bundle describes DC/OS diagnostics bundle.
type Bundle struct {
Hosts map[string]Host // IP to Host map
Masters map[string]Host
Agents map[string]Host
PublicAgents map[string]Host
directory
}
// NewBundle creates new Bundle
func NewBundle(path string) (Bundle, error) {
b := Bundle{
Hosts: make(map[string]Host),
Masters: make(map[string]Host),
Agents: make(map[string]Host),
PublicAgents: make(map[string]Host),
}
var err error
b.Path, err = filepath.Abs(path)
if err != nil {
log.Printf("bun.NewBundle: cannot determine absolute path: %v", err)
return b, err
}
infos, err := ioutil.ReadDir(b.Path)
if err != nil {
return b, err
}
const restr = `^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))_(agent_public|agent|master)$`
re := regexp.MustCompile(restr)
for _, info := range infos {
if !info.IsDir() {
continue
}
groups := re.FindStringSubmatch(info.Name())
if groups == nil {
continue
}
host := Host{}
host.IP = groups[1]
host.Path = filepath.Join(b.Path, info.Name())
switch groups[5] {
case "master":
host.Type = DTMaster
b.Masters[host.IP] = host
case "agent":
host.Type = DTAgent
b.Agents[host.IP] = host
case "agent_public":
host.Type = DTPublicAgent
b.PublicAgents[host.IP] = host
default:
panic(fmt.Sprintf("Unknown directory type: %v", groups[5]))
}
b.Hosts[host.IP] = host
}
return b, nil
}