-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimplePuppetProvisioner.go
98 lines (81 loc) · 2.84 KB
/
SimplePuppetProvisioner.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
package main
import (
"context"
"flag"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/mbaynton/SimplePuppetProvisioner/interfaces"
"github.com/mbaynton/SimplePuppetProvisioner/lib"
"github.com/mbaynton/SimplePuppetProvisioner/lib/certsign"
"github.com/mbaynton/SimplePuppetProvisioner/lib/sppexec"
"github.com/mbaynton/go-genericexec"
)
func main() {
configFile := flag.String("config", "", "Path to the spp configuration file.")
logStdout := flag.Bool("log-stdout", false, "Log to stdout.")
flag.Parse()
var searchDirs []string
if *configFile != "" {
searchDirs = []string{}
} else {
*configFile = "spp.conf"
searchDirs = []string{".", "/etc/spp"}
}
appConfig := lib.LoadTheConfig(*configFile, searchDirs)
notifier := lib.NewNotifications(&appConfig)
csrWatcher, err := fsnotify.NewWatcher()
if err != nil {
appConfig.Log.Println("Unable to start certificate signing request watcher. Cannot proceed.")
os.Exit(1)
}
watcher := interfaces.FsnotifyWatcher{
Add: csrWatcher.Add,
Close: csrWatcher.Close,
Remove: csrWatcher.Remove,
Events: csrWatcher.Events,
Errors: csrWatcher.Errors,
}
certSigner, err := certsign.NewCertSigner(*appConfig.PuppetConfig, appConfig.Log, &watcher, notifier.Notify)
if err != nil {
appConfig.Log.Println("Unable to start certificate signing manager. Cannot proceed.")
os.Exit(1)
}
execConfigMap := makeExecTaskConfigsMap(&appConfig)
if appConfig.GithubWebhooks.EnableStandardR10kListener {
appConfig.GithubWebhooks.Listeners = append(appConfig.GithubWebhooks.Listeners, lib.StandardR10kListenerConfig(appConfig.GithubWebhooks))
}
lib.SetWebhookExecTaskConfigMap(appConfig.GithubWebhooks, execConfigMap)
execManager := sppexec.NewSppExecManager(execConfigMap, appConfig.PuppetConfig, appConfig.Log, notifier.Notify)
server := lib.NewHttpServer(appConfig, notifier, certSigner, execManager)
if *logStdout == false {
appConfig.MoveLoggingToFile()
}
stop := make(chan os.Signal, 1)
if runtime.GOOS == "windows" {
signal.Notify(stop, os.Interrupt)
} else {
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
}
go server.Start()
// Wait for a SIGTERM.
<-stop
appConfig.Log.Println("Stopping...")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
server.Shutdown(ctx)
appConfig.Log.Println("HTTP server shutdown.")
certSigner.Shutdown()
appConfig.Log.Println("Certificate signing manager shutdown.")
appConfig.Log.Println("Process will now exit.")
}
func makeExecTaskConfigsMap(config *lib.AppConfig) map[string]genericexec.GenericExecConfig {
execTaskDefns := config.GenericExecTasks
execTaskConfigsByName := make(map[string]genericexec.GenericExecConfig, len(execTaskDefns))
for _, configuredTask := range execTaskDefns {
execTaskConfigsByName[configuredTask.Name] = *configuredTask
}
return execTaskConfigsByName
}