-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
283 lines (252 loc) · 6.74 KB
/
main.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
// Copyright 2023 Northern.tech AS
//
// 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 (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/mendersoftware/go-lib-micro/config"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/mendersoftware/workflows/app/server"
"github.com/mendersoftware/workflows/app/worker"
"github.com/mendersoftware/workflows/client/nats"
dconfig "github.com/mendersoftware/workflows/config"
"github.com/mendersoftware/workflows/model"
store "github.com/mendersoftware/workflows/store/mongo"
)
func main() {
doMain(os.Args)
}
func doMain(args []string) {
var configPath string
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "Configuration `FILE`." +
" Supports JSON, TOML, YAML and HCL formatted configs.",
Destination: &configPath,
},
},
Commands: []cli.Command{
{
Name: "server",
Usage: "Run the HTTP API server",
Action: cmdServer,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "automigrate",
Usage: "Run database migrations before starting.",
},
},
},
{
Name: "worker",
Usage: "Run the worker process",
Action: cmdWorker,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "automigrate",
Usage: "Run database migrations before starting.",
},
&cli.StringFlag{
Name: "workflows",
Usage: "Comma-separated list of workflows executed by this worker",
},
&cli.StringFlag{
Name: "excluded-workflows",
Usage: "Comma-separated list of workflows NOT executed by this worker",
},
},
},
{
Name: "migrate",
Usage: "Run the migrations",
Action: cmdMigrate,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "skip-nats",
Usage: "Skip migrating the NATS Jetstream configuration",
EnvVar: "WORKFLOWS_MIGRATION_SKIP_NATS",
},
cli.BoolFlag{
Name: "skip-database",
Usage: "Skip migrating the database",
EnvVar: "WORKFLOWS_MIGRATION_SKIP_DATABASE",
},
},
},
{
Name: "list-jobs",
Usage: "List jobs",
Action: cmdListJobs,
Flags: []cli.Flag{
cli.Int64Flag{
Name: "page",
Usage: "page number to show",
},
cli.Int64Flag{
Name: "perPage",
Usage: "number of results per page",
},
},
},
},
}
app.Usage = "Workflows"
app.Action = cmdServer
app.Before = func(args *cli.Context) error {
err := config.FromConfigFile(configPath, dconfig.Defaults)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error loading configuration: %s", err),
1)
}
// Enable setting config values by environment variables
config.Config.SetEnvPrefix("WORKFLOWS")
config.Config.AutomaticEnv()
config.Config.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
return nil
}
err := app.Run(args)
if err != nil {
log.Fatal(err)
}
}
func getNatsClient() (nats.Client, error) {
natsURI := config.Config.GetString(dconfig.SettingNatsURI)
streamName := config.Config.GetString(dconfig.SettingNatsStreamName)
nats, err := nats.NewClientWithDefaults(natsURI, streamName)
if err != nil {
return nil, errors.Wrap(err, "failed to connect to nats")
}
return nats, err
}
func initJetstream(nc nats.Client, producer, upsert bool) (err error) {
durableName := config.Config.GetString(dconfig.SettingNatsSubscriberDurable)
if producer {
err = nc.JetStreamCreateStream(nc.StreamName())
if err != nil {
return err
}
} else {
var cfg nats.ConsumerConfig
cfg, err = dconfig.GetNatsConsumerConfig(config.Config)
if err != nil {
return err
}
err = nc.CreateConsumer(durableName, upsert, cfg)
}
return err
}
func cmdServer(args *cli.Context) error {
dataStore, err := store.SetupDataStore(args.Bool("automigrate"))
if err != nil {
return err
}
defer dataStore.Close()
nats, err := getNatsClient()
if err != nil {
return err
}
defer nats.Close()
if err = initJetstream(nats, true, args.Bool("automigrate")); err != nil {
return errors.WithMessage(err, "failed to apply Jetstream migrations")
}
return server.InitAndRun(config.Config, dataStore, nats)
}
func cmdWorker(args *cli.Context) error {
dataStore, err := store.SetupDataStore(args.Bool("automigrate"))
if err != nil {
return err
}
defer dataStore.Close()
nats, err := getNatsClient()
if err != nil {
return err
}
defer nats.Close()
if err = initJetstream(nats, false, args.Bool("automigrate")); err != nil {
return errors.WithMessage(err, "failed to apply Jetstream consumer migrations")
}
var included, excluded []string
includedWorkflows := args.String("workflows")
if includedWorkflows != "" {
included = strings.Split(includedWorkflows, ",")
}
excludedWorkflows := args.String("excluded-workflows")
if excludedWorkflows != "" {
excluded = strings.Split(excludedWorkflows, ",")
}
workflows := worker.Workflows{
Included: included,
Excluded: excluded,
}
return worker.InitAndRun(config.Config, workflows, dataStore, nats)
}
func cmdMigrate(args *cli.Context) error {
var err error
if !args.Bool("skip-database") {
_, err = store.SetupDataStore(true)
if err != nil {
return err
}
}
if !args.Bool("skip-nats") {
var nc nats.Client
nc, err = getNatsClient()
if err == nil {
if err = initJetstream(nc, true, true); err == nil {
err = initJetstream(nc, false, true)
}
}
}
return err
}
func cmdListJobs(args *cli.Context) error {
dataStore, err := store.SetupDataStore(false)
if err != nil {
return err
}
defer dataStore.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var page int64
var perPage int64
page = args.Int64("page")
perPage = args.Int64("perPage")
if page < 1 {
page = 1
}
if perPage < 1 {
perPage = 75
}
jobs, count, _ := dataStore.GetAllJobs(ctx, page, perPage)
fmt.Printf("all jobs: %d; page: %d/%d perPage:%d\n%29s %24s %10s %s\n",
count, page, count/perPage, perPage, "insert time", "id", "status", "workflow")
for _, j := range jobs {
format := "Mon, 2 Jan 2006 15:04:05 MST"
fmt.Printf(
"%29s %24s %10s %s\n",
j.InsertTime.Format(format),
j.ID, model.StatusToString(j.Status),
j.WorkflowName,
)
}
fmt.Printf("all jobs: %d; page: %d/%d\n", count, page, count/perPage)
return nil
}