-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (182 loc) · 6.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) 2021 Dell Inc., or its subsidiaries. All Rights Reserved.
//
// 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
package main
import (
"fmt"
"net/url"
"os"
"path"
"github.com/dell/csm-deployment/utils/constants"
"github.com/dell/csm-deployment/ytt"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/spf13/viper"
"github.com/dell/csm-deployment/db"
"github.com/dell/csm-deployment/docs" // docs is generated by Swag CLI, you have to import it.
"github.com/dell/csm-deployment/handler"
"github.com/dell/csm-deployment/k8s"
"github.com/dell/csm-deployment/kapp"
"github.com/dell/csm-deployment/model"
"github.com/dell/csm-deployment/router"
"github.com/dell/csm-deployment/store"
"github.com/dell/csm-deployment/utils"
"github.com/fsnotify/fsnotify"
echoSwagger "github.com/swaggo/echo-swagger" // echo-swagger middleware
)
// @title CSM Deployment API
// @version 1.0
// @description CSM Deployment API
// @title CSM Deployment API
// @BasePath /api/v1
// @produce application/json
// @consumes application/json
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @securityDefinitions.basic BasicAuth
// @in header
// @name Authorization
const (
defaultConfigFile = "/etc/config/config.yaml"
)
func main() {
scheme := utils.GetEnv("SCHEME", "https")
hostName := utils.GetEnv("HOST", "127.0.0.1")
port := utils.GetEnv("PORT", "8080")
apiServerIP := utils.GetEnv("API_SERVER_IP", "127.0.0.1")
apiServerPort := utils.GetEnv("API_SERVER_PORT", "31313")
certDir := "/app/service-certificates"
certFileName := "tls.crt"
keyFileName := "tls.key"
dbUserName := utils.GetEnv(constants.EnvDBUserName, "root")
dbPassword := utils.GetEnv(constants.EnvDBPassword, "")
hostNameWithPort := fmt.Sprintf("%s:%s", hostName, port)
rt := router.New()
utils.JWTSecret = []byte(utils.GetEnv("JWT_KEY", ""))
if len(utils.JWTSecret) == 0 {
rt.Logger.Fatal("jwt key is empty")
}
utils.CipherKey = []byte(utils.GetEnv("CIPHER_KEY", ""))
if len(utils.CipherKey) != utils.RequiredCipherKeyLength {
rt.Logger.Fatalf("cipher key must be %d bytes in length", utils.RequiredCipherKeyLength)
}
// enable viper to get properties from environment variables or default configuration file
viper.AutomaticEnv()
viper.SetConfigFile(defaultConfigFile)
err := viper.ReadInConfig()
// if unable to read configuration file, proceed in case we use environment variables
if err != nil {
rt.Logger.Errorf("unable to read config file: %v", err)
}
updateLoggingSettings := func(logger echo.Logger) {
logLevel := viper.GetString("LOG_LEVEL")
switch logLevel {
case "ERROR":
rt.Logger.SetLevel(log.ERROR)
case "WARN":
rt.Logger.SetLevel(log.WARN)
case "DEBUG":
rt.Logger.SetLevel(log.DEBUG)
default:
rt.Logger.SetLevel(log.INFO)
}
}
updateLoggingSettings(rt.Logger)
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
rt.Logger.Info("configuration file changed")
updateLoggingSettings(rt.Logger)
})
// Update docs
swaggerIPwithPort := fmt.Sprintf("%s:%s", apiServerIP, apiServerPort)
docs.SwaggerInfo.Schemes = append(docs.SwaggerInfo.Schemes, scheme)
docs.SwaggerInfo.Host = swaggerIPwithPort
swaggerURL := url.URL{
Scheme: scheme,
Host: swaggerIPwithPort,
Path: "swagger/doc.json",
}
rt.GET("/swagger/*", echoSwagger.EchoWrapHandler(echoSwagger.URL(swaggerURL.String())))
api := rt.Group("/api/v1")
d, err := db.New(dbUserName, dbPassword)
if err != nil {
rt.Logger.Fatal("Error in initializing db", err.Error())
}
err = db.AutoMigrate(d)
if err != nil {
rt.Logger.Fatal("Error in migrating the db", err.Error())
}
storageArrayTypesStore := store.NewStorageArrayTypeStore(d)
db.PopulateInventory(d, storageArrayTypesStore)
adminUsername := os.Getenv("ADMIN_USERNAME")
if adminUsername == "" || adminUsername == "<admin-username>" {
rt.Logger.Fatal("ADMIN_USERNAME was not provided")
}
adminPassword := os.Getenv("ADMIN_PASSWORD")
if adminPassword == "" || adminPassword == "<admin-password>" {
rt.Logger.Fatal("ADMIN_PASSWORD was not provided")
}
// Add a single default user
adminUser := &model.User{
Username: adminUsername,
Password: adminPassword,
Admin: true,
}
hashPassword, err := adminUser.HashPassword(adminUser.Password)
if err != nil {
rt.Logger.Fatalf("failed to hash password", err)
}
adminUser.Password = hashPassword
us := store.NewUserStore(d)
existingUser, err := us.GetByUsername(adminUsername)
if err != nil {
rt.Logger.Fatalf("failed to query users", err)
}
if existingUser != nil {
rt.Logger.Info("user already exists in the database")
} else {
rt.Logger.Info("adding new user")
if err := us.Create(adminUser); err != nil {
rt.Logger.Fatalf("failed to create default Admin User: %v", err)
}
}
h := handler.New(us)
h.Register(api)
applicationStateChanges := store.NewApplicationStateChangeStore(d)
clusters := store.NewClusterStore(d)
hc := handler.NewClusterHandler(clusters, k8s.Client{})
hc.Register(api)
tasks := store.NewTaskStore(d)
applications := store.NewApplicationStore(d)
arrays := store.NewStorageArrayStore(d)
modules := store.NewModuleTypeStore(d)
dts := store.NewDriverTypeStore(d)
dt := handler.NewDriverTypeHandler(dts)
dt.Register(api)
cf := store.NewConfigFileStore(d)
cfh := handler.NewConfigFileHandler(cf)
cfh.Register(api)
as := handler.NewApplicationHandler(applications, tasks, clusters, applicationStateChanges, arrays, modules, dts, cf, kapp.NewClient(""), ytt.NewClient())
as.Register(api)
th := handler.NewTaskHandler(tasks, applications, applicationStateChanges, clusters, kapp.NewClient(""))
th.Register(api)
storageArrays := store.NewStorageArrayStore(d)
sah := handler.NewStorageArrayHandler(storageArrays)
sah.Register(api)
mt := handler.NewModuleTypeHandler(modules)
mt.Register(api)
sat := handler.NewStorageArrayTypeHandler(storageArrayTypesStore)
sat.Register(api)
if scheme == "http" {
rt.Logger.Fatal(rt.Start(hostNameWithPort))
} else if scheme == "https" {
rt.Logger.Fatal(rt.StartTLS(hostNameWithPort, path.Join(certDir, certFileName), path.Join(certDir, keyFileName)))
} else {
rt.Logger.Fatal("unknown scheme specified")
}
}