This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
/
main.go
201 lines (160 loc) · 4.39 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
// Copyright 2017 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/deviceadm/config"
"github.com/mendersoftware/deviceadm/store/mongo"
)
func main() {
doMain(os.Args)
}
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,
},
}
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 config values by environment variables
config.Config.SetEnvPrefix("DEVICEADM")
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 newDataStoreMongo() (*mongo.DataStoreMongo, error) {
return mongo.NewDataStoreMongo(makeDataStoreConfig())
}
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)
}
l.Printf("Device Admission Service, version %s starting up",
CreateVersionString())
db, err := newDataStoreMongo()
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to connect to db: %v", err),
3)
}
if args.Bool("automigrate") {
db = db.WithAutomigrate().(*mongo.DataStoreMongo)
}
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("Device Admission 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("Device Admission Service, version %s starting up",
CreateVersionString())
if tenantId != "" {
l.Printf("migrating tenant %v", tenantId)
} else {
l.Printf("migrating default tenant")
}
db, err := newDataStoreMongo()
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to connect to db: %v", err),
3)
}
// we want to apply migrations
db = db.WithAutomigrate().(*mongo.DataStoreMongo)
ctx := context.Background()
err = db.MigrateTenant(ctx, mongo.DbVersion, tenantId)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to run migrations: %v", err),
3)
}
return nil
}