• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / inventory / 1645624557

13 Sep 2024 11:01AM UTC coverage: 74.471% (-16.7%) from 91.217%
1645624557

push

gitlab-ci

web-flow
Merge pull request #463 from mzedel/chore/deprecate

Chore/deprecate

3095 of 4156 relevant lines covered (74.47%)

121.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.2
/main.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package main
15

16
import (
17
        "context"
18
        "fmt"
19
        "os"
20

21
        "github.com/urfave/cli"
22

23
        "github.com/mendersoftware/go-lib-micro/log"
24

25
        "github.com/mendersoftware/inventory/config"
26
        "github.com/mendersoftware/inventory/store/mongo"
27
)
28

29
func main() {
×
30
        doMain(os.Args)
×
31
}
×
32

33
const maintenanceDescription = `Run migrations in maintenance mode.
34
   WARNING: All external endpoints modifying state in the database must be
35
            temporarily disabled while maintenance is in progress:
36
       - PUT    /api/management/v1/inventory/devices/{id}/group
37
       - DELETE /api/management/v1/inventory/devices/{id}
38
       - DELETE /api/management/v1/inventory/devices/{id}/group/{name}
39
       - PATCH  /api/devices/v1/inventory/devices/attributes`
40

41
func doMain(args []string) {
3✔
42
        var configPath string
3✔
43
        var debug bool
3✔
44

3✔
45
        app := cli.NewApp()
3✔
46
        app.Usage = "Device Authentication Service"
3✔
47

3✔
48
        app.Flags = []cli.Flag{
3✔
49
                cli.StringFlag{
3✔
50
                        Name: "config",
3✔
51
                        Usage: "Configuration `FILE`." +
3✔
52
                                " Supports JSON, TOML, YAML and HCL formatted configs.",
3✔
53
                        Destination: &configPath,
3✔
54
                },
3✔
55
                cli.BoolFlag{
3✔
56
                        Name:  "dev",
3✔
57
                        Usage: "Use development setup",
3✔
58
                },
3✔
59
                cli.BoolFlag{
3✔
60
                        Name:        "debug",
3✔
61
                        Usage:       "Enable debug logging",
3✔
62
                        Destination: &debug,
3✔
63
                },
3✔
64
        }
3✔
65

3✔
66
        app.Commands = []cli.Command{
3✔
67
                {
3✔
68
                        Name:  "server",
3✔
69
                        Usage: "Run the service as a server",
3✔
70
                        Flags: []cli.Flag{
3✔
71
                                cli.BoolFlag{
3✔
72
                                        Name:  "automigrate",
3✔
73
                                        Usage: "Run database migrations before starting.",
3✔
74
                                },
3✔
75
                        },
3✔
76

3✔
77
                        Action: cmdServer,
3✔
78
                },
3✔
79
                {
3✔
80
                        Name:  "migrate",
3✔
81
                        Usage: "Run migrations",
3✔
82
                        Flags: []cli.Flag{
3✔
83
                                cli.StringFlag{
3✔
84
                                        Name:  "tenant",
3✔
85
                                        Usage: "Takes ID of specific tenant to migrate.",
3✔
86
                                },
3✔
87
                        },
3✔
88

3✔
89
                        Action: cmdMigrate,
3✔
90
                },
3✔
91
                {
3✔
92
                        Name:        "maintenance",
3✔
93
                        Description: maintenanceDescription,
3✔
94
                        Flags: []cli.Flag{
3✔
95
                                cli.StringSliceFlag{
3✔
96
                                        Name: "tenant, t",
3✔
97
                                        Usage: "Takes ID of specific " +
3✔
98
                                                "tenant(s) to migrate. " +
3✔
99
                                                "Flag can be provided " +
3✔
100
                                                "multiple times.",
3✔
101
                                },
3✔
102
                                cli.StringFlag{
3✔
103
                                        Name:  "version",
3✔
104
                                        Usage: "Target version to migrate",
3✔
105
                                        Value: mongo.DbVersion,
3✔
106
                                },
3✔
107
                        },
3✔
108

3✔
109
                        Action: cmdMaintenence,
3✔
110
                },
3✔
111
        }
3✔
112

3✔
113
        app.Action = cmdServer
3✔
114
        app.Before = func(args *cli.Context) error {
6✔
115
                log.Setup(debug)
3✔
116

3✔
117
                err := config.FromConfigFile(configPath, configDefaults)
3✔
118
                if err != nil {
3✔
119
                        return cli.NewExitError(
×
120
                                fmt.Sprintf("error loading configuration: %s", err),
×
121
                                1)
×
122
                }
×
123

124
                // Enable setting conig values by environment variables
125
                config.Config.SetEnvPrefix("INVENTORY")
3✔
126
                config.Config.AutomaticEnv()
3✔
127

3✔
128
                return nil
3✔
129
        }
130

131
        _ = app.Run(args)
3✔
132
}
133

134
func makeDataStoreConfig() mongo.DataStoreMongoConfig {
4✔
135
        return mongo.DataStoreMongoConfig{
4✔
136
                ConnectionString: config.Config.GetString(SettingDb),
4✔
137

4✔
138
                SSL:           config.Config.GetBool(SettingDbSSL),
4✔
139
                SSLSkipVerify: config.Config.GetBool(SettingDbSSLSkipVerify),
4✔
140

4✔
141
                Username: config.Config.GetString(SettingDbUsername),
4✔
142
                Password: config.Config.GetString(SettingDbPassword),
4✔
143
        }
4✔
144

4✔
145
}
4✔
146

147
func cmdServer(args *cli.Context) error {
1✔
148
        l := log.New(log.Ctx{})
1✔
149

1✔
150
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
1✔
151
        if err != nil {
1✔
152
                return cli.NewExitError(
×
153
                        fmt.Sprintf("failed to connect to db: %v", err),
×
154
                        3)
×
155
        }
×
156

157
        if args.Bool("automigrate") {
2✔
158
                db = db.WithAutomigrate()
1✔
159
        }
1✔
160

161
        ctx := context.Background()
1✔
162
        err = db.Migrate(ctx, mongo.DbVersion)
1✔
163
        if err != nil {
1✔
164
                return cli.NewExitError(
×
165
                        fmt.Sprintf("failed to run migrations: %v", err),
×
166
                        3)
×
167
        }
×
168

169
        l.Print("Inventory Service starting up")
1✔
170

1✔
171
        err = RunServer(config.Config)
1✔
172
        if err != nil {
1✔
173
                return cli.NewExitError(err.Error(), 4)
×
174
        }
×
175

176
        return nil
×
177
}
178

179
func cmdMigrate(args *cli.Context) error {
2✔
180
        tenantId := args.String("tenant")
2✔
181

2✔
182
        l := log.New(log.Ctx{})
2✔
183

2✔
184
        l.Print("Inventory Service starting up")
2✔
185

2✔
186
        if tenantId != "" {
3✔
187
                l.Printf("migrating tenant %v", tenantId)
1✔
188
        } else {
2✔
189
                l.Printf("migrating all the tenants")
1✔
190
        }
1✔
191

192
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
2✔
193

2✔
194
        if err != nil {
2✔
195
                return cli.NewExitError(
×
196
                        fmt.Sprintf("failed to connect to db: %v", err),
×
197
                        3)
×
198
        }
×
199

200
        // we want to apply migrations
201
        db = db.WithAutomigrate()
2✔
202

2✔
203
        ctx := context.Background()
2✔
204

2✔
205
        if tenantId != "" {
3✔
206
                err = db.MigrateTenant(ctx, mongo.DbVersion, tenantId)
1✔
207
        } else {
2✔
208
                err = db.Migrate(ctx, mongo.DbVersion)
1✔
209
        }
1✔
210
        if err != nil {
2✔
211
                return cli.NewExitError(
×
212
                        fmt.Sprintf("failed to run migrations: %v", err),
×
213
                        3)
×
214
        }
×
215

216
        return nil
2✔
217
}
218

219
func cmdMaintenence(args *cli.Context) error {
×
220
        tenantIDs := args.StringSlice("tenant")
×
221
        version := args.String("version")
×
222

×
223
        l := log.New(log.Ctx{})
×
224

×
225
        if len(tenantIDs) > 0 {
×
226
                l.Infof("performing maintenence for tenants: %v", tenantIDs)
×
227
        } else {
×
228
                l.Info("performing maintenance for all the tenants")
×
229
        }
×
230
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
×
231

×
232
        if err != nil {
×
233
                return cli.NewExitError(
×
234
                        fmt.Sprintf("failed to connect to db: %v", err),
×
235
                        3)
×
236
        }
×
237

238
        // we want to apply migrations
239
        db = db.WithAutomigrate()
×
240

×
241
        ctx := context.Background()
×
242

×
243
        err = db.Maintenance(ctx, version, tenantIDs...)
×
244
        if err != nil {
×
245
                return cli.NewExitError(
×
246
                        fmt.Sprintf("failed to run migrations: %v", err),
×
247
                        3)
×
248
        }
×
249

250
        return nil
×
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc