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

mendersoftware / mender-server / 1495380963

14 Oct 2024 03:35PM UTC coverage: 70.373% (-2.5%) from 72.904%
1495380963

Pull #101

gitlab-ci

mineralsfree
feat: tenant list added

Ticket: MEN-7568
Changelog: None

Signed-off-by: Mikita Pilinka <mikita.pilinka@northern.tech>
Pull Request #101: feat: tenant list added

4406 of 6391 branches covered (68.94%)

Branch coverage included in aggregate %.

88 of 183 new or added lines in 10 files covered. (48.09%)

2623 existing lines in 65 files now uncovered.

36673 of 51982 relevant lines covered (70.55%)

31.07 hits per line

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

0.0
/backend/services/deviceconfig/main.go
1
// Copyright 2021 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

15
package main
16

17
import (
18
        "context"
19
        "crypto/tls"
20
        "fmt"
21
        "net/url"
22
        "os"
23
        "strings"
24

25
        "github.com/urfave/cli"
26

27
        "github.com/mendersoftware/mender-server/pkg/config"
28
        "github.com/mendersoftware/mender-server/pkg/identity"
29
        "github.com/mendersoftware/mender-server/pkg/log"
30

31
        . "github.com/mendersoftware/mender-server/services/deviceconfig/config"
32
        "github.com/mendersoftware/mender-server/services/deviceconfig/server"
33
        "github.com/mendersoftware/mender-server/services/deviceconfig/store"
34
        "github.com/mendersoftware/mender-server/services/deviceconfig/store/mongo"
35
)
36

UNCOV
37
func main() {
×
UNCOV
38
        doMain(os.Args)
×
UNCOV
39
}
×
40

UNCOV
41
func doMain(args []string) {
×
UNCOV
42
        var configPath string
×
UNCOV
43

×
UNCOV
44
        app := &cli.App{
×
UNCOV
45
                Flags: []cli.Flag{
×
UNCOV
46
                        &cli.StringFlag{
×
UNCOV
47
                                Name: "config",
×
UNCOV
48
                                Usage: "Configuration `FILE`. " +
×
UNCOV
49
                                        "Supports JSON, TOML, YAML and HCL " +
×
UNCOV
50
                                        "formatted configs.",
×
UNCOV
51
                                Destination: &configPath,
×
UNCOV
52
                        },
×
UNCOV
53
                },
×
UNCOV
54
                Commands: []cli.Command{
×
UNCOV
55
                        {
×
UNCOV
56
                                Name:   "server",
×
UNCOV
57
                                Usage:  "Run the HTTP API server",
×
UNCOV
58
                                Action: cmdServer,
×
UNCOV
59
                                Flags: []cli.Flag{
×
UNCOV
60
                                        &cli.BoolFlag{
×
UNCOV
61
                                                Name:  "automigrate",
×
UNCOV
62
                                                Usage: "Run database migrations before starting.",
×
UNCOV
63
                                        },
×
UNCOV
64
                                },
×
UNCOV
65
                        },
×
UNCOV
66
                        {
×
UNCOV
67
                                Name:   "migrate",
×
UNCOV
68
                                Usage:  "Run the migrations",
×
UNCOV
69
                                Action: cmdMigrate,
×
UNCOV
70
                                Flags: []cli.Flag{
×
UNCOV
71
                                        &cli.StringFlag{
×
UNCOV
72
                                                Name: "tenant-id",
×
UNCOV
73
                                                Usage: "If an `ID` is provided, the migrations " +
×
UNCOV
74
                                                        "will only apply to the specified tenant.",
×
UNCOV
75
                                        },
×
UNCOV
76
                                        &cli.StringFlag{
×
UNCOV
77
                                                Name:  "db-version",
×
UNCOV
78
                                                Value: mongo.DbVersion,
×
UNCOV
79
                                                Usage: "Target `VERSION` for the migration.",
×
UNCOV
80
                                        },
×
UNCOV
81
                                },
×
UNCOV
82
                        },
×
UNCOV
83
                },
×
UNCOV
84
        }
×
UNCOV
85
        app.Usage = "Device Configure"
×
UNCOV
86
        app.Version = "1.0.0"
×
UNCOV
87
        app.Action = cmdServer
×
UNCOV
88

×
UNCOV
89
        app.Before = func(args *cli.Context) error {
×
UNCOV
90
                err := config.FromConfigFile(configPath, Defaults)
×
UNCOV
91
                if err != nil {
×
92
                        return cli.NewExitError(
×
93
                                fmt.Sprintf("error loading configuration: %s", err),
×
94
                                1)
×
95
                }
×
96

97
                // Enable setting config values by environment variables
UNCOV
98
                config.Config.SetEnvPrefix("DEVICECONFIG")
×
UNCOV
99
                config.Config.AutomaticEnv()
×
UNCOV
100
                config.Config.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
×
UNCOV
101

×
UNCOV
102
                log.Setup(config.Config.GetBool(SettingDebugLog))
×
UNCOV
103

×
UNCOV
104
                return nil
×
105
        }
106

UNCOV
107
        err := app.Run(args)
×
UNCOV
108
        if err != nil {
×
109
                log.Log.Fatal(err)
×
110
        }
×
111
}
112

UNCOV
113
func initStoreFromConfig() (store.DataStore, error) {
×
UNCOV
114
        mgoURL, err := url.Parse(config.Config.GetString(SettingMongo))
×
UNCOV
115
        if err != nil {
×
116
                return nil, err
×
117
        }
×
118

UNCOV
119
        storeConfig := mongo.MongoStoreConfig{
×
UNCOV
120
                MongoURL: mgoURL,
×
UNCOV
121
                Username: config.Config.GetString(SettingDbUsername),
×
UNCOV
122
                Password: config.Config.GetString(SettingDbPassword),
×
UNCOV
123
                DbName:   mongo.DbName,
×
UNCOV
124
        }
×
UNCOV
125

×
UNCOV
126
        if config.Config.GetBool(SettingDbSSLSkipVerify) {
×
127
                storeConfig.TLSConfig = &tls.Config{
×
128
                        InsecureSkipVerify: true,
×
129
                }
×
130
        }
×
UNCOV
131
        return mongo.NewMongoStore(context.Background(), storeConfig)
×
132
}
133

UNCOV
134
func cmdServer(args *cli.Context) error {
×
UNCOV
135
        ctx := context.Background()
×
UNCOV
136
        ds, err := initStoreFromConfig()
×
UNCOV
137
        if err != nil {
×
138
                return err
×
139
        }
×
UNCOV
140
        defer ds.Close(ctx)
×
UNCOV
141
        err = ds.Migrate(ctx, mongo.DbVersion, args.Bool("automigrate"))
×
UNCOV
142
        if err != nil {
×
143
                return err
×
144
        }
×
UNCOV
145
        return server.InitAndRun(ds)
×
146
}
147

148
func cmdMigrate(args *cli.Context) error {
×
149
        ctx := context.Background()
×
150
        version := args.String("db-version")
×
151
        tenantID := args.String("tenant-id")
×
152
        if tenantID != "" {
×
153
                ctx = identity.WithContext(ctx, &identity.Identity{
×
154
                        Tenant: tenantID,
×
155
                })
×
156
        }
×
157
        if version == "" {
×
158
                version = mongo.DbVersion
×
159
        }
×
160

161
        ds, err := initStoreFromConfig()
×
162
        if err != nil {
×
163
                return err
×
164
        }
×
165
        defer ds.Close(ctx)
×
166

×
167
        return ds.Migrate(ctx, version, true)
×
168
}
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