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

mendersoftware / mender-server / 1622978334

13 Jan 2025 03:51PM UTC coverage: 72.802% (-3.8%) from 76.608%
1622978334

Pull #300

gitlab-ci

alfrunes
fix: Deployment device count should not exceed max devices

Added a condition to skip deployments when the device count reaches max
devices.

Changelog: Title
Ticket: MEN-7847
Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #300: fix: Deployment device count should not exceed max devices

4251 of 6164 branches covered (68.96%)

Branch coverage included in aggregate %.

0 of 18 new or added lines in 1 file covered. (0.0%)

2544 existing lines in 83 files now uncovered.

42741 of 58384 relevant lines covered (73.21%)

21.49 hits per line

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

71.64
/backend/services/useradm/main.go
1
// Copyright 2022 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
        "encoding/json"
19
        "fmt"
20
        "os"
21
        "strings"
22

23
        "github.com/urfave/cli"
24

25
        "github.com/mendersoftware/mender-server/pkg/config"
26
        "github.com/mendersoftware/mender-server/pkg/log"
27
        "github.com/mendersoftware/mender-server/pkg/version"
28

29
        . "github.com/mendersoftware/mender-server/services/useradm/config"
30
        "github.com/mendersoftware/mender-server/services/useradm/model"
31
        "github.com/mendersoftware/mender-server/services/useradm/store/mongo"
32
)
33

34
var appVersion = version.Get()
35

36
func main() {
2✔
37
        if err := doMain(os.Args); err != nil {
2✔
38
                os.Exit(1)
×
39
        }
×
40
}
41

42
func doMain(args []string) error {
2✔
43
        var debug bool
2✔
44
        var configPath string
2✔
45

2✔
46
        app := cli.NewApp()
2✔
47
        app.Usage = "user administration service"
2✔
48
        app.Flags = []cli.Flag{
2✔
49
                cli.StringFlag{
2✔
50
                        Name: "config",
2✔
51
                        Usage: "Configuration `FILE`." +
2✔
52
                                " Supports JSON, TOML, YAML and HCL formatted configs.",
2✔
53
                        Destination: &configPath,
2✔
54
                },
2✔
55
                cli.BoolFlag{
2✔
56
                        Name:        "debug",
2✔
57
                        Usage:       "Enable debug logging",
2✔
58
                        Destination: &debug,
2✔
59
                },
2✔
60
        }
2✔
61
        app.Commands = []cli.Command{
2✔
62
                {
2✔
63
                        Name:  "server",
2✔
64
                        Usage: "Run the service as a server",
2✔
65
                        Flags: []cli.Flag{
2✔
66
                                cli.BoolFlag{
2✔
67
                                        Name:  "automigrate",
2✔
68
                                        Usage: "Run database migrations before starting.",
2✔
69
                                },
2✔
70
                        },
2✔
71

2✔
72
                        Action: runServer,
2✔
73
                },
2✔
74
                {
2✔
75
                        Name:  "create-user",
2✔
76
                        Usage: "Create user",
2✔
77
                        Flags: []cli.Flag{
2✔
78
                                cli.StringFlag{
2✔
79
                                        Name:  "username",
2✔
80
                                        Usage: "Name of created user, must be an email address",
2✔
81
                                },
2✔
82
                                cli.StringFlag{
2✔
83
                                        Name:  "password",
2✔
84
                                        Usage: "User's password, leave empty to have it read from stdin",
2✔
85
                                },
2✔
86
                                cli.StringFlag{
2✔
87
                                        Name:  "user-id",
2✔
88
                                        Usage: "User ID, if already generated/available (optional).",
2✔
89
                                },
2✔
90
                                cli.StringFlag{
2✔
91
                                        Name:  "tenant-id",
2✔
92
                                        Usage: "Tenant ID, if running a multitenant setup (optional).",
2✔
93
                                },
2✔
94
                        },
2✔
95
                        Action: runCreateUser,
2✔
96
                },
2✔
97
                {
2✔
98
                        Name:  "set-password",
2✔
99
                        Usage: "Set password",
2✔
100
                        Flags: []cli.Flag{
2✔
101
                                cli.StringFlag{
2✔
102
                                        Name:  "username",
2✔
103
                                        Usage: "Name of created user, must be an email address",
2✔
104
                                },
2✔
105
                                cli.StringFlag{
2✔
106
                                        Name:  "password",
2✔
107
                                        Usage: "User's password, leave empty to have it read from stdin",
2✔
108
                                },
2✔
109
                                cli.StringFlag{
2✔
110
                                        Name:  "tenant-id",
2✔
111
                                        Usage: "Tenant ID, if running a multitenant setup (optional).",
2✔
112
                                },
2✔
113
                        },
2✔
114
                        Action: runSetPassword,
2✔
115
                },
2✔
116
                {
2✔
117
                        Name:  "migrate",
2✔
118
                        Usage: "Run migrations",
2✔
119
                        Flags: []cli.Flag{
2✔
120
                                cli.StringFlag{
2✔
121
                                        Name:  "tenant",
2✔
122
                                        Usage: "Takes ID of specific tenant to migrate.",
2✔
123
                                },
2✔
124
                        },
2✔
125

2✔
126
                        Action: runMigrate,
2✔
127
                },
2✔
128
                {
2✔
129
                        Name:  "version",
2✔
130
                        Usage: "Show version information",
2✔
131
                        Flags: []cli.Flag{
2✔
132
                                cli.StringFlag{
2✔
133
                                        Name:  "output",
2✔
134
                                        Usage: "Output format <json|text>",
2✔
135
                                        Value: "text",
2✔
136
                                },
2✔
137
                        },
2✔
138
                        Action: func(args *cli.Context) error {
2✔
139
                                switch strings.ToLower(args.String("output")) {
×
140
                                case "text":
×
141
                                        fmt.Print(appVersion)
×
142
                                case "json":
×
143
                                        _ = json.NewEncoder(os.Stdout).Encode(appVersion)
×
144
                                default:
×
145
                                        return fmt.Errorf("Unknown output format %q", args.String("output"))
×
146
                                }
147
                                return nil
×
148
                        },
149
                },
150
        }
151

152
        app.Version = appVersion.Version
2✔
153
        app.Action = runServer
2✔
154
        app.Before = func(args *cli.Context) error {
4✔
155
                log.Setup(debug)
2✔
156

2✔
157
                err := config.FromConfigFile(configPath, ConfigDefaults)
2✔
158
                if err != nil {
2✔
159
                        return cli.NewExitError(
×
160
                                fmt.Sprintf("error loading configuration: %s", err),
×
161
                                1)
×
162
                }
×
163

164
                // Enable setting conig values by environment variables
165
                config.Config.SetEnvPrefix("USERADM")
2✔
166
                config.Config.AutomaticEnv()
2✔
167

2✔
168
                if config.Config.IsSet(SettingPlanDefinitions) {
4✔
169
                        log.NewEmpty().Info(
2✔
170
                                "loading plan definitions from configuration",
2✔
171
                        )
2✔
172
                        err := model.LoadPlans(
2✔
173
                                config.Config.GetString(SettingPlanDefinitions),
2✔
174
                        )
2✔
175
                        if err != nil {
2✔
176
                                return fmt.Errorf(
×
177
                                        "failed to load Plans from config: %w",
×
178
                                        err,
×
179
                                )
×
180
                        }
×
181
                }
182

183
                return nil
2✔
184
        }
185
        err := app.Run(args)
2✔
186
        if err != nil {
2✔
187
                log.NewEmpty().Fatal(err)
×
188
        }
×
189
        return err
2✔
190
}
191

UNCOV
192
func runServer(args *cli.Context) error {
×
UNCOV
193
        l := log.New(log.Ctx{})
×
UNCOV
194

×
UNCOV
195
        l.Print("User Administration Service starting up")
×
UNCOV
196

×
UNCOV
197
        ctx := context.Background()
×
UNCOV
198

×
UNCOV
199
        db, err := mongo.NewDataStoreMongo(dataStoreMongoConfigFromAppConfig(config.Config))
×
UNCOV
200
        if err != nil {
×
201
                return cli.NewExitError(
×
202
                        fmt.Sprintf("failed to connect to db: %v", err),
×
203
                        2)
×
204
        }
×
205

UNCOV
206
        if args.Bool("automigrate") {
×
UNCOV
207
                db = db.WithAutomigrate()
×
UNCOV
208
        }
×
209

UNCOV
210
        if config.Config.Get(SettingTenantAdmAddr) != "" {
×
211
                db = db.WithMultitenant()
×
212
        }
×
UNCOV
213
        err = db.Migrate(ctx, mongo.DbVersion)
×
UNCOV
214
        if err != nil {
×
215
                return cli.NewExitError(
×
216
                        fmt.Sprintf("failed to run migrations: %v", err),
×
217
                        3)
×
218
        }
×
219

UNCOV
220
        err = RunServer(config.Config)
×
UNCOV
221
        if err != nil {
×
222
                return cli.NewExitError(err.Error(), 4)
×
223
        }
×
UNCOV
224
        return nil
×
225
}
226

227
func runCreateUser(args *cli.Context) error {
2✔
228
        email := model.Email(strings.ToLower(args.String("username")))
2✔
229
        err := commandCreateUser(
2✔
230
                config.Config,
2✔
231
                email,
2✔
232
                args.String("password"),
2✔
233
                args.String("user-id"),
2✔
234
                args.String("tenant-id"),
2✔
235
        )
2✔
236
        if err != nil {
2✔
237
                return cli.NewExitError(err.Error(), 5)
×
238
        }
×
239
        return nil
2✔
240
}
241

242
func runMigrate(args *cli.Context) error {
2✔
243
        err := commandMigrate(config.Config, args.String("tenant"))
2✔
244
        if err != nil {
2✔
245
                return cli.NewExitError(err.Error(), 6)
×
246
        }
×
247
        return nil
2✔
248
}
249

250
func runSetPassword(args *cli.Context) error {
1✔
251
        email := model.Email(strings.ToLower(args.String("username")))
1✔
252
        err := commandSetPassword(
1✔
253
                config.Config, email,
1✔
254
                args.String("password"), args.String("tenant-id"),
1✔
255
        )
1✔
256
        if err != nil {
1✔
257
                return cli.NewExitError(err.Error(), 7)
×
258
        }
×
259
        return nil
1✔
260
}
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