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

mendersoftware / inventory / 1565407895

29 Nov 2024 07:58AM UTC coverage: 91.502%. Remained the same
1565407895

push

gitlab-ci

web-flow
Merge pull request #464 from alfrunes/4.4.x

chore(deps): Upgrade docker image to golang:1.23.3-alpine3.20

3144 of 3436 relevant lines covered (91.5%)

151.63 hits per line

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

70.56
/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
        devSetup := args.GlobalBool("dev")
1✔
149

1✔
150
        l := log.New(log.Ctx{})
1✔
151

1✔
152
        if devSetup {
1✔
153
                l.Infof("setting up development configuration")
×
154
                config.Config.Set(SettingMiddleware, EnvDev)
×
155
        }
×
156

157
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
1✔
158
        if err != nil {
1✔
159
                return cli.NewExitError(
×
160
                        fmt.Sprintf("failed to connect to db: %v", err),
×
161
                        3)
×
162
        }
×
163

164
        if args.Bool("automigrate") {
2✔
165
                db = db.WithAutomigrate()
1✔
166
        }
1✔
167

168
        ctx := context.Background()
1✔
169
        err = db.Migrate(ctx, mongo.DbVersion)
1✔
170
        if err != nil {
1✔
171
                return cli.NewExitError(
×
172
                        fmt.Sprintf("failed to run migrations: %v", err),
×
173
                        3)
×
174
        }
×
175

176
        l.Print("Inventory Service starting up")
1✔
177

1✔
178
        err = RunServer(config.Config)
1✔
179
        if err != nil {
1✔
180
                return cli.NewExitError(err.Error(), 4)
×
181
        }
×
182

183
        return nil
×
184
}
185

186
func cmdMigrate(args *cli.Context) error {
2✔
187
        tenantId := args.String("tenant")
2✔
188

2✔
189
        l := log.New(log.Ctx{})
2✔
190

2✔
191
        l.Print("Inventory Service starting up")
2✔
192

2✔
193
        if tenantId != "" {
3✔
194
                l.Printf("migrating tenant %v", tenantId)
1✔
195
        } else {
2✔
196
                l.Printf("migrating all the tenants")
1✔
197
        }
1✔
198

199
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
2✔
200

2✔
201
        if err != nil {
2✔
202
                return cli.NewExitError(
×
203
                        fmt.Sprintf("failed to connect to db: %v", err),
×
204
                        3)
×
205
        }
×
206

207
        // we want to apply migrations
208
        db = db.WithAutomigrate()
2✔
209

2✔
210
        ctx := context.Background()
2✔
211

2✔
212
        if tenantId != "" {
3✔
213
                err = db.MigrateTenant(ctx, mongo.DbVersion, tenantId)
1✔
214
        } else {
2✔
215
                err = db.Migrate(ctx, mongo.DbVersion)
1✔
216
        }
1✔
217
        if err != nil {
2✔
218
                return cli.NewExitError(
×
219
                        fmt.Sprintf("failed to run migrations: %v", err),
×
220
                        3)
×
221
        }
×
222

223
        return nil
2✔
224
}
225

226
func cmdMaintenence(args *cli.Context) error {
×
227
        tenantIDs := args.StringSlice("tenant")
×
228
        version := args.String("version")
×
229

×
230
        l := log.New(log.Ctx{})
×
231

×
232
        if len(tenantIDs) > 0 {
×
233
                l.Infof("performing maintenence for tenants: %v", tenantIDs)
×
234
        } else {
×
235
                l.Info("performing maintenance for all the tenants")
×
236
        }
×
237
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
×
238

×
239
        if err != nil {
×
240
                return cli.NewExitError(
×
241
                        fmt.Sprintf("failed to connect to db: %v", err),
×
242
                        3)
×
243
        }
×
244

245
        // we want to apply migrations
246
        db = db.WithAutomigrate()
×
247

×
248
        ctx := context.Background()
×
249

×
250
        err = db.Maintenance(ctx, version, tenantIDs...)
×
251
        if err != nil {
×
252
                return cli.NewExitError(
×
253
                        fmt.Sprintf("failed to run migrations: %v", err),
×
254
                        3)
×
255
        }
×
256

257
        return nil
×
258
}
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