forked from lucaslorentz/caddy-supervisor
-
Notifications
You must be signed in to change notification settings - Fork 7
/
caddyfile.go
165 lines (131 loc) · 3.75 KB
/
caddyfile.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
package supervisor
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
)
func init() {
httpcaddyfile.RegisterGlobalOption("supervisor", parseSupervisor)
}
// parseSupervisor configures the "exec" global option from Caddyfile.
// Syntax:
// supervisor {
// php-fpm -d /etc/php-fpm/config.ini {
// dir /home/user
// redirect_stdout stdout
// redirect_stderr stderr
// restart_policy always
// env MAX_CHILDREN 5
// }
// }
func parseSupervisor(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
app := new(App)
// consume the option name
if !d.Next() {
return nil, d.ArgErr()
}
// handle the block, can have more than one command defined
for d.NextBlock(0) {
def := Definition{}
def.Command = append([]string{d.Val()}, d.RemainingArgs()...)
if len(def.Command) == 0 {
return nil, d.ArgErr()
}
// handle any options
for d.NextBlock(1) {
switch d.Val() {
case "dir":
if !d.Args(&def.Dir) {
return nil, d.ArgErr()
}
case "redirect_stdout":
target, err := parseOutpoutRedirect(d)
if err != nil {
return nil, err
}
def.RedirectStdout = target
case "redirect_stderr":
target, err := parseOutpoutRedirect(d)
if err != nil {
return nil, err
}
def.RedirectStderr = target
case "restart_policy":
var p string
if !d.Args(&p) {
return nil, d.ArgErr()
}
if p != string(RestartAlways) && p != string(RestartNever) && p != string(RestartOnFailure) {
return nil, d.Errf("'restart_policy' should be either '%s', '%s', or '%s': '%s' given", RestartAlways, RestartNever, RestartOnFailure, p)
}
def.RestartPolicy = RestartPolicy(p)
case "termination_grace_period":
if !d.Args(&def.TerminationGracePeriod) {
return nil, d.ArgErr()
}
if _, err := time.ParseDuration(def.TerminationGracePeriod); err != nil {
return nil, d.Errf("cannot parse 'termination_grace_period' into time.Duration, '%s' given", def.TerminationGracePeriod)
}
case "replicas":
var replicas string
if !d.Args(&replicas) {
return nil, d.ArgErr()
}
r, err := strconv.Atoi(replicas)
if err != nil {
return nil, d.Errf("'replicas' should be a positive integer, '%s' given", replicas)
}
if r < 0 {
return nil, d.Errf("'replicas' should be a positive integer, '%s' given", replicas)
}
def.Replicas = &r
case "env":
var envKey, envValue string
if !d.Args(&envKey, &envValue) {
return nil, d.ArgErr()
}
remaining := d.RemainingArgs()
if len(remaining) != 0 {
envValue = fmt.Sprintf("%s %s", envValue, strings.Join(remaining, " "))
}
if def.Env == nil {
def.Env = map[string]string{}
}
def.Env[envKey] = envValue
case "user":
var user string
if !d.Args(&user) {
return nil, d.ArgErr()
}
def.User = user
}
}
app.Supervise = append(app.Supervise, def)
}
// tell Caddyfile adapter that this is the JSON for an app
return httpcaddyfile.App{
Name: "supervisor",
Value: caddyconfig.JSON(app, nil),
}, nil
}
func parseOutpoutRedirect(d *caddyfile.Dispenser) (*OutputTarget, error) {
if !d.NextArg() {
return nil, d.ArgErr()
}
switch d.Val() {
case OutputTypeNull, OutputTypeStdout, OutputTypeStderr:
return &OutputTarget{Type: d.Val()}, nil
case OutputTypeFile:
var file string
if !d.Args(&file) {
return nil, d.ArgErr()
}
return &OutputTarget{Type: OutputTypeFile, File: file}, nil
default:
return nil, d.Errf("target should be either '%s', '%s', '%s', or '%s': '%s' given", OutputTypeNull, OutputTypeStdout, OutputTypeStderr, OutputTypeFile, d.Val())
}
}