forked from mendersoftware/inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
259 lines (211 loc) · 5.86 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
// Copyright 2020 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"
"os"
"github.com/mendersoftware/go-lib-micro/log"
"github.com/urfave/cli"
"github.com/mendersoftware/inventory/config"
"github.com/mendersoftware/inventory/store/mongo"
)
func main() {
doMain(os.Args)
}
const maintenanceDescription = `Run migrations in maintenance mode.
WARNING: All external endpoints modifying state in the database must be
temporarily disabled while maintenance is in progress:
- PUT /api/management/v1/inventory/devices/{id}/group
- DELETE /api/management/v1/inventory/devices/{id}
- DELETE /api/management/v1/inventory/devices/{id}/group/{name}
- PATCH /api/devices/v1/inventory/devices/attributes`
func doMain(args []string) {
var configPath string
var debug bool
app := cli.NewApp()
app.Usage = "Device Authentication Service"
app.Version = CreateVersionString()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "Configuration `FILE`. Supports JSON, TOML, YAML and HCL formatted configs.",
Destination: &configPath,
},
cli.BoolFlag{
Name: "dev",
Usage: "Use development setup",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug logging",
Destination: &debug,
},
}
app.Commands = []cli.Command{
{
Name: "server",
Usage: "Run the service as a server",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "automigrate",
Usage: "Run database migrations before starting.",
},
},
Action: cmdServer,
},
{
Name: "migrate",
Usage: "Run migrations",
Flags: []cli.Flag{
cli.StringFlag{
Name: "tenant",
Usage: "Takes ID of specific tenant to migrate.",
},
},
Action: cmdMigrate,
},
{
Name: "maintenance",
Description: maintenanceDescription,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "tenant, t",
Usage: "Takes ID of specific " +
"tenant(s) to migrate. " +
"Flag can be provided " +
"multiple times.",
},
cli.StringFlag{
Name: "version",
Usage: "Target version to migrate",
Value: mongo.DbVersion,
},
},
Action: cmdMaintenence,
},
}
app.Action = cmdServer
app.Before = func(args *cli.Context) error {
log.Setup(debug)
err := config.FromConfigFile(configPath, configDefaults)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error loading configuration: %s", err),
1)
}
// Enable setting conig values by environment variables
config.Config.SetEnvPrefix("INVENTORY")
config.Config.AutomaticEnv()
return nil
}
app.Run(args)
}
func makeDataStoreConfig() mongo.DataStoreMongoConfig {
return mongo.DataStoreMongoConfig{
ConnectionString: config.Config.GetString(SettingDb),
SSL: config.Config.GetBool(SettingDbSSL),
SSLSkipVerify: config.Config.GetBool(SettingDbSSLSkipVerify),
Username: config.Config.GetString(SettingDbUsername),
Password: config.Config.GetString(SettingDbPassword),
}
}
func cmdServer(args *cli.Context) error {
devSetup := args.GlobalBool("dev")
l := log.New(log.Ctx{})
if devSetup {
l.Infof("setting up development configuration")
config.Config.Set(SettingMiddleware, EnvDev)
}
db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to connect to db: %v", err),
3)
}
if args.Bool("automigrate") {
db = db.WithAutomigrate()
}
ctx := context.Background()
err = db.Migrate(ctx, mongo.DbVersion)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to run migrations: %v", err),
3)
}
l.Printf("Inventory Service, version %s starting up",
CreateVersionString())
err = RunServer(config.Config)
if err != nil {
return cli.NewExitError(err.Error(), 4)
}
return nil
}
func cmdMigrate(args *cli.Context) error {
tenantId := args.String("tenant")
l := log.New(log.Ctx{})
l.Printf("Inventory Service, version %s starting up",
CreateVersionString())
if tenantId != "" {
l.Printf("migrating tenant %v", tenantId)
} else {
l.Printf("migrating all the tenants")
}
db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to connect to db: %v", err),
3)
}
// we want to apply migrations
db = db.WithAutomigrate()
ctx := context.Background()
if tenantId != "" {
err = db.MigrateTenant(ctx, mongo.DbVersion, tenantId)
} else {
err = db.Migrate(ctx, mongo.DbVersion)
}
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to run migrations: %v", err),
3)
}
return nil
}
func cmdMaintenence(args *cli.Context) error {
tenantIDs := args.StringSlice("tenant")
version := args.String("version")
l := log.New(log.Ctx{})
if len(tenantIDs) > 0 {
l.Infof("performing maintenence for tenants: %v", tenantIDs)
} else {
l.Info("performing maintenance for all the tenants")
}
db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to connect to db: %v", err),
3)
}
// we want to apply migrations
db = db.WithAutomigrate()
ctx := context.Background()
err = db.Maintenance(ctx, version, tenantIDs...)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to run migrations: %v", err),
3)
}
return nil
}