-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
131 lines (115 loc) · 3.31 KB
/
util.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
package main
import (
"bufio"
"fmt"
"regexp"
"strconv"
"strings"
nomad "github.com/hashicorp/nomad/api"
)
const (
maxLineSize = 32 * 1024
)
const shipperPrefix = "logger_"
var indexRegex = regexp.MustCompile(`\[(?P<index>[0-9]+)\]`)
// getLocalNodeID returns the node ID of the local Nomad Client and an error if
// it couldn't be determined or the Agent is not running in Client mode.
func getLocalNodeID(client *nomad.Client) (string, error) {
info, err := client.Agent().Self()
if err != nil {
return "", fmt.Errorf("Error querying agent info: %s", err)
}
clientStats, ok := info.Stats["client"]
if !ok {
return "", fmt.Errorf("Nomad not running in client mode")
}
nodeID, ok := clientStats["node_id"]
if !ok {
return "", fmt.Errorf("Failed to determine node ID")
}
return nodeID, nil
}
func isTerminal(alloc *nomad.Allocation) bool {
switch alloc.ClientStatus {
case nomad.AllocClientStatusComplete, nomad.AllocClientStatusFailed, nomad.AllocClientStatusLost:
return true
default:
return false
}
}
// sizeSpliter wraps a bufio.SplitFunc by limiting the max size of the split.
// This allow us not to use too much memory and avoid the bufio.ErrTooLong.
// This works by checking the output of the SplitFunc. If no token is found
// and the provided buffer length is greater than maxSize it will be returned
// as a new token. If a SplitFunc finds a token earlier, that token will be
// returned.
func sizeSpliter(maxSize int, splitFunc bufio.SplitFunc) bufio.SplitFunc {
return func(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = splitFunc(data, atEOF)
if err != nil {
return
}
if advance == 0 && token == nil && len(data) > maxSize {
advance = len(data)
token = data
}
return
}
}
func filterMeta(alloc map[string]string, meta map[string]string) {
for k, v := range alloc {
if strings.HasPrefix(k, shipperPrefix) {
meta[strings.ToLower(strings.TrimPrefix(k, shipperPrefix))] = v
}
}
}
func getMeta(alloc *nomad.Allocation, task string) map[string]string {
meta := map[string]string{
"_version": version,
}
filterMeta(alloc.Job.Meta, meta)
for _, tg := range alloc.Job.TaskGroups {
if *tg.Name == alloc.TaskGroup {
filterMeta(tg.Meta, meta)
for _, t := range tg.Tasks {
if t.Name == task {
filterMeta(t.Meta, meta)
}
}
}
}
return meta
}
func getProperties(alloc *nomad.Allocation, task, dc string) map[string]interface{} {
properties := map[string]interface{}{
"region": *alloc.Job.Region,
"dc": dc,
"namespace": alloc.Namespace,
"job": alloc.JobID,
"group": alloc.TaskGroup,
"task": task,
"allocation": alloc.ID,
}
// -- wright now to add this we should interpolate the values, because in the
// spec they could come as env values
// services := make([]string, 0)
// for _, tg := range alloc.Job.TaskGroups {
// if *tg.Name == alloc.TaskGroup {
// for _, t := range tg.Tasks {
// if t.Name == task {
// for _, s := range t.Services {
// services = append(services, s.Name)
// }
// }
// }
// }
// }
// if len(services) > 0 {
// properties["service"] = services
// }
if matchs := indexRegex.FindStringSubmatch(alloc.Name); len(matchs) == 2 {
index, _ := strconv.Atoi(matchs[1])
properties["alloc_index"] = index
}
return properties
}