blob: 7d1112269240bae0ea14a49f318ffa22d039136e [file] [log] [blame]
Matteo Scandolo8df63df2019-09-12 10:34:32 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package config
19
20import (
Matteo Scandolo38305492019-10-11 11:36:00 -070021 "fmt"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070022 log "github.com/sirupsen/logrus"
Matteo Scandolo38305492019-10-11 11:36:00 -070023 "gopkg.in/yaml.v2"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070024 "io/ioutil"
25 "net"
26 "os"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070027 "time"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070028)
29
30var (
Matteo Scandolo38305492019-10-11 11:36:00 -070031 Version string
32 BuildTime string
33 CommitHash string
34 GitStatus string
Matteo Scandolo8df63df2019-09-12 10:34:32 -070035)
36
37var GlobalOptions struct {
Matteo Scandolo38305492019-10-11 11:36:00 -070038 Config string `short:"c" long:"config" env:"BBSIMCTL_CONFIG" value-name:"FILE" default:"" description:"Location of client config file"`
39 Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070040 //Protoset string `long:"protoset" value-name:"FILENAME" description:"Load protobuf definitions from protoset instead of reflection api"`
Matteo Scandolo38305492019-10-11 11:36:00 -070041 Debug bool `short:"d" long:"debug" description:"Enable debug mode"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070042}
43
44type GrpcConfigSpec struct {
45 Timeout time.Duration `yaml:"timeout"`
46}
47
48type GlobalConfigSpec struct {
Matteo Scandolo38305492019-10-11 11:36:00 -070049 Server string `yaml:"server"`
50 Grpc GrpcConfigSpec
Matteo Scandolo8df63df2019-09-12 10:34:32 -070051}
52
53var GlobalConfig = GlobalConfigSpec{
54 Server: "localhost:50070",
55 Grpc: GrpcConfigSpec{
56 Timeout: time.Second * 10,
57 },
58}
59
60func ProcessGlobalOptions() {
61 if len(GlobalOptions.Config) == 0 {
62 home, err := os.UserHomeDir()
63 if err != nil {
64 log.Printf("Unable to discover the users home directory: %s\n", err)
65 }
66 GlobalOptions.Config = fmt.Sprintf("%s/.bbsim/config", home)
67 }
68
69 info, err := os.Stat(GlobalOptions.Config)
70 if err == nil && !info.IsDir() {
71 configFile, err := ioutil.ReadFile(GlobalOptions.Config)
72 if err != nil {
73 log.Printf("configFile.Get err #%v ", err)
74 }
75 err = yaml.Unmarshal(configFile, &GlobalConfig)
76 if err != nil {
77 log.Fatalf("Unmarshal: %v", err)
78 }
79 }
80
81 // Override from environment
82 // in particualr, for passing env vars via `go test`
83 env_server, present := os.LookupEnv("BBSIMCTL_SERVER")
84 if present {
85 GlobalConfig.Server = env_server
86 }
87
88 // Override from command line
89 if GlobalOptions.Server != "" {
90 GlobalConfig.Server = GlobalOptions.Server
91 }
92
Matteo Scandolo8df63df2019-09-12 10:34:32 -070093 // Generate error messages for required settings
94 if GlobalConfig.Server == "" {
95 log.Fatal("Server is not set. Please update config file or use the -s option")
96 }
97
98 //Try to resolve hostname if provided for the server
99 if host, port, err := net.SplitHostPort(GlobalConfig.Server); err == nil {
100 if addrs, err := net.LookupHost(host); err == nil {
101 GlobalConfig.Server = net.JoinHostPort(addrs[0], port)
102 }
103 }
Matteo Scandolo38305492019-10-11 11:36:00 -0700104}