/
main.go
234 lines (201 loc) · 5.63 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"os/exec"
"sync"
"time"
"github.com/BurntSushi/toml"
"github.com/groob/plist"
"github.com/juju/deputy"
)
var (
// Version string
Version = "unreleased"
)
// Config autopkgd config
type Config struct {
AutopkgCmdPath string `toml:"autopkg_path,omitempty"`
MakecatalogsCmdPath string `toml:"makecatalogs_path,omitempty"`
RecipesFile string `toml:"recipes_file"`
MunkiRepoPath string `toml:"munki_repo"`
ReportsPath string `toml:"reports_path"`
MaxProcesses int `toml:"max_processes"`
ExecTimeout time.Duration `toml:"autopkg_exec_timeout"`
CheckInterval time.Duration `toml:"autopkg_check_interval"`
// Slack config
Slack slack `toml:"slack"`
}
type processor struct {
DataRows []map[string]interface{} `plist:"data_rows"`
Header []string `plist:"header"`
SummaryText string `plist:"summary_text"`
}
type autopkgReport struct {
Failures []interface{} `plist:"failures"`
SummaryResults map[string]processor `plist:"summary_results"`
}
func runAutopkg(recipe, reportsPath, cmdPath string, check bool, execTimeout time.Duration) autopkgReport {
autopkgCmd := exec.Command(cmdPath, "run", "--report-plist="+reportsPath+"/"+recipe)
if check {
autopkgCmd.Args = append(autopkgCmd.Args, "--check")
}
autopkgCmd.Args = append(autopkgCmd.Args, recipe)
d := deputy.Deputy{
Errors: deputy.FromStderr,
StdoutLog: func(b []byte) { log.Print(string(b)) },
Timeout: time.Second * execTimeout,
}
if err := d.Run(autopkgCmd); err != nil {
log.Println(err)
return autopkgReport{}
}
report, err := readReportPlist(reportsPath + "/" + recipe)
if err != nil {
log.Println(err)
return autopkgReport{}
}
return report
}
func readReportPlist(path string) (autopkgReport, error) {
r := autopkgReport{}
f, err := os.Open(path)
if err != nil {
return r, err
}
defer f.Close()
return r, plist.NewDecoder(f).Decode(&r)
}
func makeCatalogs(makeCatalogsPath, repoPath string, execTimeout time.Duration) {
makecatalogsCmd := exec.Command(
makeCatalogsPath,
repoPath,
)
d := deputy.Deputy{
Errors: deputy.FromStderr,
StdoutLog: func(b []byte) { log.Println(string(b)) },
Timeout: time.Second * execTimeout,
}
if err := d.Run(makecatalogsCmd); err != nil {
log.Println(err)
return
}
}
func process(done chan<- bool, concurrency int, slackReport, check bool, recipeFile, autopkgCmdPath, makecatalogsPath, repoPath, reportsPath string, execTimeout time.Duration, slackConfig slack) {
var catalogsModified bool
sem := make(chan int, concurrency)
// make a channel of autopkgReports and create workers
// close the reports channel when done
reports := make(chan autopkgReport)
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Wait()
close(reports)
}()
// create a channel of recipes for each worker to run
// once the file is read, close the channel
recipes := make(chan string)
go func() {
defer wg.Done()
file, err := os.Open(recipeFile)
if err != nil {
log.Println(err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
recipe := scanner.Text()
// ignore empty lines, comments and MakeCatalogs.munki
if len(recipe) == 0 || recipe == "MakeCatalogs.munki" || []byte(recipe)[0] == []byte("#")[0] {
continue
}
recipes <- recipe
}
close(recipes)
}()
// Send reports to slack if flag is enabled
if slackReport {
go notifySlack(reports, slackConfig)
}
go func() {
for report := range reports {
if _, ok := report.SummaryResults["munki_importer_summary_result"]; ok {
catalogsModified = true
}
}
}()
for recipe := range recipes {
wg.Add(1)
sem <- 1
go func(recipe string) {
reports <- runAutopkg(recipe, reportsPath, autopkgCmdPath, check, execTimeout)
wg.Done()
<-sem
}(recipe)
}
if catalogsModified {
makeCatalogs(makecatalogsPath, repoPath, execTimeout)
}
done <- true
}
func main() {
var (
conf Config
fConfig = flag.String("config", "", "configuration file to load")
fSlack = flag.Bool("slack", false, "Send reports to slack?")
fCheck = flag.Bool("check", false, "autopkg check option")
fVersion = flag.Bool("version", false, "display the version")
)
flag.Parse()
if *fVersion {
fmt.Printf("autopkgd - version %s\n", Version)
os.Exit(0)
}
if _, err := toml.DecodeFile(*fConfig, &conf); err != nil {
log.Fatal(err)
}
if conf.AutopkgCmdPath == "" {
conf.AutopkgCmdPath = "/usr/local/bin/autopkg"
}
if conf.MakecatalogsCmdPath == "" {
conf.MakecatalogsCmdPath = "/usr/local/munki/makecatalogs"
}
if conf.MaxProcesses == 0 {
conf.MaxProcesses = 1
}
if conf.ExecTimeout == 0 {
conf.ExecTimeout = 600
}
if conf.CheckInterval == 0 {
conf.CheckInterval = 1
}
// is report path configured?
if conf.ReportsPath == "" {
fmt.Println("you must specify a directory for reports to be saved in your config")
os.Exit(1)
}
// does report path exist?
fileInfo, err := os.Stat(conf.ReportsPath)
if os.IsNotExist(err) {
fmt.Printf("No such file or directory: %s\n", conf.ReportsPath)
os.Exit(1)
}
if !fileInfo.IsDir() {
fmt.Printf("%v must be a directory\n", conf.ReportsPath)
os.Exit(1)
}
// loop through all the recipes at an interval
// done blocks untill process finishes
done := make(chan bool)
ticker := time.NewTicker(time.Second * conf.CheckInterval).C
for {
go process(done, conf.MaxProcesses, *fSlack, *fCheck, conf.RecipesFile, conf.AutopkgCmdPath, conf.MakecatalogsCmdPath, conf.ReportsPath, conf.ReportsPath, conf.ExecTimeout, conf.Slack)
<-done
<-ticker
}
}