-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfd.go
More file actions
96 lines (83 loc) · 2.79 KB
/
confd.go
File metadata and controls
96 lines (83 loc) · 2.79 KB
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
// Package confd provides basic Confd support.
//
// Right now, this library is highly specific to the needs of the present
// builder. Because the confd library is not all public, we don't use it directly.
// Instead, we invoke the CLI.
package confd
import (
"fmt"
"os"
"os/exec"
"strconv"
"time"
"github.com/Masterminds/cookoo"
"github.com/Masterminds/cookoo/log"
"github.com/Masterminds/cookoo/safely"
)
// defaultEtcd is the default Etcd host.
const defaultEtcd = "127.0.0.1:4001"
// RunOnce runs the equivalent of `confd --onetime`.
//
// This may run the process repeatedly until either we time out (~20 minutes) or
// the templates are successfully built.
//
// Importantly, this blocks until the run is complete.
//
// Params:
// - node (string): The etcd node to use. (Only etcd is currently supported)
//
// Returns:
// - The []bytes from stdout and stderr when running the program.
//
func RunOnce(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
node := p.Get("node", defaultEtcd).(string)
dargs := []string{"-onetime", "-node", node, "-log-level", "error"}
log.Info(c, "Building confd templates. This may take a moment.")
limit := 1200
timeout := time.Second * 3
var lasterr error
start := time.Now()
for i := 0; i < limit; i++ {
out, err := exec.Command("confd", dargs...).CombinedOutput()
if err == nil {
log.Infof(c, "Templates generated for %s on run %d", node, i)
return out, nil
}
log.Debugf(c, "Recoverable error: %s", err)
log.Debugf(c, "Output: %q", out)
lasterr = err
time.Sleep(timeout)
log.Infof(c, "Re-trying template build. (Elapsed time: %d)", time.Now().Sub(start)/time.Second)
}
return nil, fmt.Errorf("Could not build confd templates before timeout. Last error: %s", lasterr)
}
// Run starts confd and runs it in the background.
//
// If the command fails immediately on startup, an error is immediately
// returned. But from that point, a goroutine watches the command and
// reports if the command dies.
//
// Params:
// - node (string): The etcd node to use. (Only etcd is currently supported)
// - interval (int, default:5): The rebuilding interval.
//
// Returns
// bool true if this succeeded.
func Run(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
node := p.Get("node", defaultEtcd).(string)
interval := strconv.Itoa(p.Get("interval", 5).(int))
cmd := exec.Command("confd", "-log-level", "error", "-node", node, "-interval", interval)
if err := cmd.Start(); err != nil {
return false, err
}
log.Infof(c, "Watching confd.")
safely.Go(func() {
if err := cmd.Wait(); err != nil {
// If confd exits, builder will stop functioning as intended. So
// we stop builder and let the environment restart.
log.Errf(c, "Stopping builder. confd exited with error: %s", err)
os.Exit(37)
}
})
return true, nil
}