forked from muesli/beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beehive.go
163 lines (137 loc) · 3.45 KB
/
beehive.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
/*
* Copyright (C) 2014-2017 Christian Muehlhaeuser
* 2014 Michael Wendland
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Christian Muehlhaeuser <[email protected]>
* Michael Wendland <[email protected]>
* Johannes Fürmann <[email protected]>
*/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/mattn/go-colorable"
"github.com/muesli/beehive/api"
"github.com/muesli/beehive/app"
_ "github.com/muesli/beehive/filters"
_ "github.com/muesli/beehive/filters/template"
"github.com/muesli/beehive/bees"
)
var (
configFile string
versionFlag bool
)
// Config contains an entire configuration set for Beehive
type Config struct {
Bees []bees.BeeConfig
Actions []bees.Action
Chains []bees.Chain
}
// Loads chains from config
func loadConfig() Config {
config := Config{}
j, err := ioutil.ReadFile(configFile)
if err == nil {
err = json.Unmarshal(j, &config)
if err != nil {
log.Fatal("Error parsing config file: ", err)
}
}
return config
}
// Saves chains to config
func saveConfig(c Config) {
j, err := json.MarshalIndent(c, "", " ")
if err == nil {
err = ioutil.WriteFile(configFile, j, 0644)
}
if err != nil {
log.Fatal(err)
}
}
func main() {
app.AddFlags([]app.CliFlag{
{
V: &configFile,
Name: "config",
Value: "./beehive.conf",
Desc: "Config-file to use",
},
{
V: &versionFlag,
Name: "version",
Value: false,
Desc: "Beehive version",
},
})
// Parse command-line args for all registered bees
app.Run()
if versionFlag {
fmt.Printf("Beehive %s (%s)\n", Version, CommitSHA)
os.Exit(0)
}
api.Run()
log.Println()
log.Println("Beehive is buzzing...")
config := loadConfig()
// Load actions from config
bees.SetActions(config.Actions)
// Load chains from config
bees.SetChains(config.Chains)
// Initialize bees
bees.StartBees(config.Bees)
// Wait for signals
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGKILL)
for s := range ch {
log.Println("Got signal:", s)
abort := false
switch s {
case syscall.SIGHUP:
config = loadConfig()
bees.StopBees()
bees.SetActions(config.Actions)
bees.SetChains(config.Chains)
bees.StartBees(config.Bees)
case syscall.SIGTERM:
fallthrough
case syscall.SIGKILL:
fallthrough
case os.Interrupt:
abort = true
break
}
if abort {
break
}
}
// Save actions & chains to config
log.Println("Storing config...")
config.Bees = bees.BeeConfigs()
config.Chains = bees.GetChains()
config.Actions = bees.GetActions()
saveConfig(config)
}
func init() {
log.SetFormatter(&log.TextFormatter{ForceColors: true})
log.SetOutput(colorable.NewColorableStdout())
}