-
Notifications
You must be signed in to change notification settings - Fork 6
/
cronner.go
139 lines (109 loc) · 2.68 KB
/
cronner.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
// Copyright 2015 PagerDuty, Inc, et al.
// Copyright 2016-2017 Tim Heckman
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.
// Package main is the main thing, man.
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/PagerDuty/godspeed"
"github.com/codeskyblue/go-uuid"
"github.com/tideland/golib/logger"
)
// Version is the program's version string
const Version = "1.0.0"
type cmdHandler struct {
gs *godspeed.Godspeed
opts *binArgs
cmd *exec.Cmd
uuid string
hostname string
parentEventTags []string
parentMetricTags []string
}
var cronnerEventEnvVars = []string{
"CRONNER_PARENT_UUID",
"CRONNER_PARENT_EVENT_GROUP",
}
var cronnerMetricEnvVars = []string{
"CRONNER_PARENT_GROUP",
"CRONNER_PARENT_NAMESPACE",
"CRONNER_PARENT_LABEL",
}
func parseEnv(vars []string) []string {
if len(vars) == 0 {
return nil
}
tags := make([]string, len(vars))
var count int
for i, key := range vars {
if val := os.Getenv(key); len(val) > 0 {
tags[i] = fmt.Sprintf("%s:%s", strings.ToLower(key), val)
count++
}
}
if count == 0 {
return nil
}
return tags[0:count]
}
func parseEnvForParent() (eventTags, metricTags []string) {
// get CRONNER_PARENT_UUID to see if we're a parent process
if os.Getenv(cronnerEventEnvVars[0]) == "" {
return
}
eventTags = parseEnv(cronnerEventEnvVars)
metricTags = parseEnv(cronnerMetricEnvVars)
return
}
func main() {
logger.SetLogger(logger.NewStandardLogger(os.Stderr))
// get and parse the command line options
opts := &binArgs{}
output, err := opts.parse(nil)
// make sure parsing didn't bomb
if err != nil {
logger.Errorf("error: %v\n", err)
os.Exit(1)
}
// if parsing had output, print it and exit 0
if len(output) > 0 {
fmt.Print(output)
os.Exit(0)
}
// build a Godspeed client
var gs *godspeed.Godspeed
if opts.StatsdHost == "" {
gs, err = godspeed.NewDefault()
} else {
gs, err = godspeed.New(opts.StatsdHost, godspeed.DefaultPort, false)
}
// make sure nothing went wrong with Godspeed
if err != nil {
logger.Errorf("error: %v\n", err)
os.Exit(1)
}
gs.SetNamespace(opts.Namespace)
// get the hostname and validate nothing happened
hostname, err := os.Hostname()
if err != nil {
logger.Errorf("error: %v\n", err)
os.Exit(1)
}
handler := &cmdHandler{
opts: opts,
hostname: hostname,
gs: gs,
uuid: uuid.New(),
cmd: exec.Command(opts.Cmd, opts.CmdArgs...),
}
handler.parentEventTags, handler.parentMetricTags = parseEnvForParent()
ret, _, _, err := handleCommand(handler)
if err != nil {
logger.Errorf(err.Error())
}
os.Exit(ret)
}