/
daemon.go
153 lines (129 loc) · 4.02 KB
/
daemon.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
// MITHRAS: Javascript configuration management tool for AWS.
// Copyright (C) 2016, Colin Steele
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package daemon
import (
log "github.com/Sirupsen/logrus"
"os"
"syscall"
"github.com/codegangsta/cli"
"github.com/robertkrimen/otto"
"github.com/sevlyar/go-daemon"
"github.com/cvillecsteele/mithras/modules/core"
"github.com/cvillecsteele/mithras/modules/script"
)
var (
Version = "1.0.0"
Context = daemon.Context{
PidFileName: "pid",
PidFilePerm: 0644,
LogFileName: "log",
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
}
)
// Graceful shutdown
func Term(c *cli.Context) {
cntxt := &Context
Context.PidFileName = c.Parent().String("pidfile")
Context.LogFileName = c.Parent().String("logfile")
Context.WorkDir = c.Parent().String("workdir")
d, err := cntxt.Search()
if err != nil {
log.Fatalln("Unable send signal to the daemon:", err)
}
if err = d.Signal(syscall.SIGTERM); err != nil {
log.Println("Error sending signal; daemon may not be running.")
return
}
}
// BYE NOW
func Quit(c *cli.Context) {
cntxt := &Context
Context.PidFileName = c.Parent().String("pidfile")
Context.LogFileName = c.Parent().String("logfile")
Context.WorkDir = c.Parent().String("workdir")
d, err := cntxt.Search()
if err != nil {
log.Fatalln("Unable send signal to the daemon:", err)
}
if err = d.Signal(syscall.SIGQUIT); err != nil {
return
}
}
// Start the daemon
func Run(c *cli.Context, versions []core.ModuleVersion, version string) {
cntxt := &Context
Context.PidFileName = c.Parent().String("pidfile")
Context.LogFileName = c.Parent().String("logfile")
Context.WorkDir = c.Parent().String("workdir")
d, err := cntxt.Reborn()
if err != nil {
log.Fatalln(err)
}
if d != nil {
return
}
defer cntxt.Release()
rt := script.RunCli(c, versions, version)
daemon.SetSigHandler(makeTermHandler(rt), syscall.SIGQUIT)
daemon.SetSigHandler(makeTermHandler(rt), syscall.SIGTERM)
daemon.SetSigHandler(makeReloadHandler(rt), syscall.SIGHUP)
err = daemon.ServeSignals()
if err != nil {
log.Println("Error:", err)
}
}
// Shutdown handlers
func makeTermHandler(rt *otto.Otto) func(os.Signal) error {
return func(sig os.Signal) error {
// By convention we require scripts have a set entry point
result, err := rt.Call("stop", nil, sig)
if err != nil {
if ottoErr, ok := err.(*otto.Error); ok {
log.Fatalf("JS error calling 'stop' in script: %s", ottoErr.String())
}
log.Fatalf("Error calling 'stop' in script: %s", err)
}
// If the js function did not return a bool error out because
// the script is invalid
_, err = result.ToBoolean()
if err != nil {
log.Fatalf("Error converting 'stop' return value to boolean: %s", err)
}
return daemon.ErrStop
}
}
// Relaod
func makeReloadHandler(rt *otto.Otto) func(os.Signal) error {
return func(sig os.Signal) error {
// By convention we require scripts have a set entry point
result, err := rt.Call("reload", nil, sig)
if err != nil {
if ottoErr, ok := err.(*otto.Error); ok {
log.Fatalf("JS error calling 'reload' in script: %s", ottoErr.String())
}
log.Fatalf("Error calling 'reload' in script: %s", err)
}
// If the js function did not return a bool error out because
// the script is invalid
_, err = result.ToBoolean()
if err != nil {
log.Fatalf("Error converting 'reload' return value to boolean: %s", err)
}
return nil
}
}