-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.go
302 lines (278 loc) · 7.35 KB
/
run.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2019 github.com/ucirello
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"cirello.io/runner/procfile"
"cirello.io/runner/runner"
cli "github.com/urfave/cli"
)
// DefaultProcfile is the file that runner will open by default if no custom
// is given.
const DefaultProcfile = "Procfile"
func run() {
app := cli.NewApp()
app.Name = "runner"
app.Usage = "simple Procfile runner"
app.HideVersion = true
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "convert",
Usage: "takes a declared Procfile and prints as JSON to standard output",
},
cli.IntFlag{
Name: "port",
Value: 5000,
Usage: "base IP port used to set $`PORT` for each process type. Should be multiple of 1000.",
},
cli.StringFlag{
Name: "service-discovery",
Value: "localhost:64000",
Usage: "service discovery address",
},
cli.StringFlag{
Name: "formation",
Value: "",
Usage: "formation allows to start more than one instance of a process type, format: `procTypeA=# procTypeB=# ... procTypeN=#`",
},
cli.StringFlag{
Name: "env",
Value: ".env",
Usage: "environment `file` to be loaded for all processes.",
},
cli.StringFlag{
Name: "skip",
Value: "",
Usage: "does not run some of the process types, format: `procTypeA procTypeB procTypeN`",
},
cli.StringFlag{
Name: "only",
Value: "",
Usage: "only runs some of the process types, format: `procTypeA procTypeB procTypeN`",
},
cli.StringFlag{
Name: "optional",
Value: "",
Usage: "forcefully runs some of the process types, format: `procTypeA procTypeB procTypeN`",
},
}
app.Commands = []cli.Command{logs()}
app.Action = mainRunner
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func mainRunner(c *cli.Context) error {
origStdout := os.Stdout
basePort := c.Int("port")
convertToJSON := c.Bool("convert")
envFN := c.String("env")
skipProcs := c.String("skip")
onlyProcs := c.String("only")
optionalProcs := c.String("optional")
discoveryAddr := c.String("service-discovery")
var (
filterPatternMu sync.RWMutex
filterPattern string
)
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
text := scanner.Text()
filterPatternMu.Lock()
filterPattern = text
filterPatternMu.Unlock()
if text != "" {
log.Println("filtering with:", scanner.Text())
}
}
if err := scanner.Err(); err != nil {
log.Println("reading standard output:", err)
}
}()
go func() {
r, w, _ := os.Pipe()
os.Stdout = w
scanner := bufio.NewScanner(r)
scanner.Buffer(make([]byte, 2097152), 262144)
for scanner.Scan() {
filterPatternMu.RLock()
p := filterPattern
filterPatternMu.RUnlock()
text := scanner.Text()
if p == "" {
fmt.Fprintln(origStdout, text)
continue
}
words := strings.Fields(p)
for _, w := range words {
if strings.Contains(text, w) {
fmt.Fprintln(origStdout, text)
break
}
}
}
if err := scanner.Err(); err != nil {
log.Println("reading standard output:", err)
}
}()
fn := DefaultProcfile
if argFn := c.Args().First(); argFn != "" {
fn = argFn
}
fd, err := os.Open(fn)
if err != nil {
return err
}
if basePort < 1 || basePort > 65535 {
return errors.New("invalid IP port")
}
var s runner.Runner
switch filepath.Ext(fn) {
case ".json":
if err := json.NewDecoder(fd).Decode(&s); err != nil {
return fmt.Errorf("cannot parse spec file (json): %v", err)
}
default:
s, err = procfile.Parse(fd)
if err != nil {
return fmt.Errorf("cannot parse spec file (procfile): %v", err)
}
}
if convertToJSON {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(&s); err != nil {
return fmt.Errorf("cannot encode procfile into JSON: %v", err)
}
return nil
}
s.WorkDir = os.ExpandEnv(s.WorkDir)
if s.WorkDir == "" {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot load current workdir: %v", err)
}
s.WorkDir = wd
}
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
<-sigint
log.Println("shutting down")
cancel()
}()
s.BasePort = basePort
if fd, err := os.Open(envFN); err == nil {
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
line := strings.Split(strings.TrimSpace(scanner.Text()), "=")
if len(line) != 2 {
continue
}
s.BaseEnvironment = append(s.BaseEnvironment, scanner.Text())
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading environment file (%v): %v", envFN, err)
}
}
if skipProcs != "" {
s.Processes = filterSkippedProcs(skipProcs, s.Processes)
} else if onlyProcs != "" {
s.Processes = filterOnlyProcs(onlyProcs, s.Processes)
}
if optionalProcs != "" {
s.Processes = keepOptionalProcs(optionalProcs, s.Processes)
} else {
s.Processes = filterOptionalProcs(s.Processes)
}
s.ServiceDiscoveryAddr = discoveryAddr
if err := s.Start(ctx); err != nil {
return fmt.Errorf("cannot serve: %v", err)
}
return nil
}
func keepOptionalProcs(optionals string, processes []*runner.ProcessType) []*runner.ProcessType {
optionalProcs, newProcs := strings.Split(optionals, " "), []*runner.ProcessType{}
procTypes:
for _, procType := range processes {
if !procType.Optional {
newProcs = append(newProcs, procType)
continue
}
for _, optional := range optionalProcs {
if procType.Name == optional {
fmt.Println("enabling", optional)
newProcs = append(newProcs, procType)
continue procTypes
}
}
}
return newProcs
}
func filterOptionalProcs(processes []*runner.ProcessType) []*runner.ProcessType {
groups := make(map[string]struct{})
newProcs := []*runner.ProcessType{}
for _, procType := range processes {
if procType.Optional && procType.Group == "" {
continue
} else if procType.Optional && procType.Group != "" {
if _, ok := groups[procType.Group]; ok {
continue
}
groups[procType.Group] = struct{}{}
}
newProcs = append(newProcs, procType)
}
return newProcs
}
func filterSkippedProcs(skip string, processes []*runner.ProcessType) []*runner.ProcessType {
skipProcs, newProcs := strings.Split(skip, " "), []*runner.ProcessType{}
procTypes:
for _, procType := range processes {
for _, skip := range skipProcs {
if procType.Name == skip {
fmt.Println("skipping", skip)
continue procTypes
}
}
newProcs = append(newProcs, procType)
}
return newProcs
}
func filterOnlyProcs(only string, processes []*runner.ProcessType) []*runner.ProcessType {
onlyProcs, newProcs := strings.Split(only, " "), []*runner.ProcessType{}
procTypes:
for _, procType := range processes {
for _, only := range onlyProcs {
if procType.Name == only {
newProcs = append(newProcs, procType)
continue procTypes
}
}
}
return newProcs
}