forked from sapcc/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (141 loc) · 4.82 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
/*******************************************************************************
*
* Copyright 2017 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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 (
"flag"
"fmt"
"os"
"strings"
"log"
"github.com/databus23/goslo.policy"
"github.com/sapcc/hermes/pkg/api"
"github.com/sapcc/hermes/pkg/configdb"
"github.com/sapcc/hermes/pkg/identity"
"github.com/sapcc/hermes/pkg/storage"
"github.com/sapcc/hermes/pkg/util"
"github.com/spf13/viper"
)
var configPath *string
func main() {
parseCmdlineFlags()
setDefaultConfig()
readConfig(configPath)
keystoneDriver := configuredKeystoneDriver()
storageDriver := configuredStorageDriver()
dbDriver := configuredDBDriver()
readPolicy()
api.Server(keystoneDriver, storageDriver, dbDriver)
}
func parseCmdlineFlags() {
// Get config file location
configPath = flag.String("f", "hermes.conf", "specifies the location of the TOML-format configuration file")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
}
var nullEnforcer, _ = policy.NewEnforcer(make(map[string]string))
func setDefaultConfig() {
viper.SetDefault("hermes.keystone_driver", "keystone")
viper.SetDefault("hermes.storage_driver", "elasticsearch")
viper.SetDefault("hermes.configdb_driver", "mysql")
viper.SetDefault("hermes.enrich_keystone_events", "False")
viper.SetDefault("hermes.PolicyEnforcer", &nullEnforcer)
viper.SetDefault("API.ListenAddress", "0.0.0.0:8788")
viper.SetDefault("elasticsearch.url", "localhost:9200")
viper.SetDefault("mysql.dsn", "user:password@tcp(hostname:3306)/database")
// index.max_result_window defaults to 10000, as per
// https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html
viper.SetDefault("elasticsearch.max_result_window", "10000")
}
func readConfig(configPath *string) {
// Don't read config file if the default config file isn't there,
// as we will just fall back to config defaults in that case
var shouldReadConfig = true
if _, err := os.Stat(*configPath); os.IsNotExist(err) {
shouldReadConfig = *configPath != flag.Lookup("f").DefValue
}
// Now we sorted that out, read the config
util.LogDebug("Should read config: %v, config file is %s", shouldReadConfig, *configPath)
if shouldReadConfig {
viper.SetConfigFile(*configPath)
viper.SetConfigType("toml")
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
}
// Setup environment variable overrides for OpenStack authentication
var OSVars = []string{"username", "password", "auth_url", "user_domain_name", "project_name", "project_domain_name"}
for _, osVarName := range OSVars {
viper.BindEnv("keystone."+osVarName, "OS_"+strings.ToUpper(osVarName))
}
}
var keystoneIdentity = identity.Keystone{}
var mockIdentity = identity.Mock{}
func configuredKeystoneDriver() identity.Identity {
driverName := viper.GetString("hermes.keystone_driver")
switch driverName {
case "keystone":
return keystoneIdentity
case "mock":
return mockIdentity
default:
log.Printf("Couldn't match a keystone driver for configured value \"%s\"", driverName)
return nil
}
}
var elasticSearchStorage = storage.ElasticSearch{}
var mockStorage = storage.Mock{}
func configuredStorageDriver() storage.Storage {
driverName := viper.GetString("hermes.storage_driver")
switch driverName {
case "elasticsearch":
return elasticSearchStorage
case "mock":
return mockStorage
default:
log.Printf("Couldn't match a storage driver for configured value \"%s\"", driverName)
return nil
}
}
var mockconfigdb = configdb.Mock{}
func configuredDBDriver() configdb.Driver {
driverName := viper.GetString("hermes.configdb_driver")
switch driverName {
case "mysql":
return nil
case "mock":
return mockconfigdb
default:
log.Printf("Couldn't match a storage driver for configured value \"%s\"", driverName)
return nil
}
}
func readPolicy() {
//load the policy file
policyEnforcer, err := util.LoadPolicyFile(viper.GetString("hermes.PolicyFilePath"))
if err != nil {
util.LogFatal(err.Error())
}
if policyEnforcer != nil {
viper.Set("hermes.PolicyEnforcer", policyEnforcer)
}
}