-
Notifications
You must be signed in to change notification settings - Fork 0
/
converge.go
70 lines (62 loc) · 1.46 KB
/
converge.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
// Copyright (c) 2017-2021 Ingram Micro Inc.
package converge
import (
"bufio"
"errors"
"io"
"os/exec"
"path"
"regexp"
"runtime"
"github.com/ingrammicro/cio/utils"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func traceOutput(ls *bufio.Reader) {
garbageOutput, _ := regexp.Compile("[\\[][^\\[|^\\]]*[\\]]\\s[A-Z]*:\\s")
output, _ := regexp.Compile("Chef Run")
for {
line, isPrefix, err := ls.ReadLine()
if isPrefix {
log.Errorf("%s", errors.New("isPrefix: true"))
}
if err != nil {
if err != io.EOF {
log.Errorf("%s", err.Error())
}
break
}
outputLine := garbageOutput.ReplaceAllString(string(line), "")
if output.MatchString(outputLine) {
log.Infof("%s", outputLine)
} else {
log.Debugf("%s", outputLine)
}
}
}
func CmbConverge(c *cli.Context) error {
firstBootJsonChef := path.Join("/etc/chef", "first-boot.json")
if runtime.GOOS == "windows" {
firstBootJsonChef = path.Join("c:\\chef", "first-boot.json")
}
if utils.FileExists(firstBootJsonChef) {
cmd := exec.Command("chef-client", "-j", firstBootJsonChef)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Errorf("%s", err.Error())
}
ls := bufio.NewReader(stdout)
err = cmd.Start()
if err != nil {
log.Errorf("%s", err.Error())
}
traceOutput(ls)
err = cmd.Wait()
if err != nil {
log.Errorf("%s", err.Error())
}
} else {
log.Fatalf("Make sure %s chef client configuration exists.", firstBootJsonChef)
}
return nil
}