blob: 9d1b9499bd88794964121393d0dcaca859af75e6 [file] [log] [blame]
Scott Bakerf04b6392020-03-20 08:29:46 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Implements global configuration for nem-ondemand-proxy
18package proxy
19
20import (
21 "fmt"
22 flags "github.com/jessevdk/go-flags"
23 "gopkg.in/yaml.v2"
24 "io/ioutil"
25 "log"
26 "os"
27 "path"
28 "path/filepath"
29 "strings"
30 "time"
31)
32
33type OutputType uint8
34
35type GrpcConfigSpec struct {
36 Timeout time.Duration `yaml:"timeout"`
37}
38
39type TlsConfigSpec struct {
40 UseTls bool `yaml:"useTls"`
41 CACert string `yaml:"caCert"`
42 Cert string `yaml:"cert"`
43 Key string `yaml:"key"`
44 Verify string `yaml:"verify"`
45}
46
47type GlobalConfigSpec struct {
48 Server string `yaml:"server"`
49 Kafka string `yaml:"kafka"`
50 Local string `yaml:"local"`
51 Tls TlsConfigSpec `yaml:"tls"`
52 Grpc GrpcConfigSpec `yaml:"grpc"`
53}
54
55var (
56 CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n")
57
58 GlobalConfig = GlobalConfigSpec{
59 Server: "voltha-rw-core.voltha:50057",
60 Kafka: "voltha-kafka.voltha:9092",
61 Local: "0.0.0.0:50052",
62 Tls: TlsConfigSpec{
63 UseTls: false,
64 },
65 Grpc: GrpcConfigSpec{
66 Timeout: time.Minute * 5,
67 },
68 }
69
70 GlobalCommandOptions = make(map[string]map[string]string)
71
72 GlobalOptions struct {
73 Config string `short:"c" long:"config" env:"PROXYCONFIG" value-name:"FILE" default:"" description:"Location of proxy config file"`
74 Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of VOLTHA"`
75 Kafka string `short:"k" long:"kafka" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of Kafka"`
76 Local string `short:"l" long:"local" default:"" value-name:"SERVER:PORT" description:"IP/Host and port to listen on"`
77
78 // The following are not necessarily implemented yet.
79 Debug bool `short:"d" long:"debug" description:"Enable debug mode"`
80 Timeout string `short:"t" long:"timeout" description:"API call timeout duration" value-name:"DURATION" default:""`
81 UseTLS bool `long:"tls" description:"Use TLS"`
82 CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"`
83 Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"`
84 Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"`
85 Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"`
86 }
87
88 Debug = log.New(os.Stdout, "DEBUG: ", 0)
89 Info = log.New(os.Stdout, "INFO: ", 0)
90 Warn = log.New(os.Stderr, "WARN: ", 0)
91 Error = log.New(os.Stderr, "ERROR: ", 0)
92)
93
94func ParseCommandLine() {
95 parser := flags.NewNamedParser(path.Base(os.Args[0]),
96 flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
97 _, err := parser.AddGroup("Global Options", "", &GlobalOptions)
98 if err != nil {
99 panic(err)
100 }
101
102 _, err = parser.ParseArgs(os.Args[1:])
103 if err != nil {
104 _, ok := err.(*flags.Error)
105 if ok {
106 real := err.(*flags.Error)
107 if real.Type == flags.ErrHelp {
108 os.Stdout.WriteString(err.Error() + "\n")
109 os.Exit(0)
110 }
111 }
112
113 fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err.Error())
114
115 os.Exit(1)
116 }
117}
118
119func ProcessGlobalOptions() {
120 if len(GlobalOptions.Config) == 0 {
121 home, err := os.UserHomeDir()
122 if err != nil {
123 Warn.Printf("Unable to discover the user's home directory: %s", err)
124 home = "~"
125 }
126 GlobalOptions.Config = filepath.Join(home, ".nem", "config")
127 }
128
129 if info, err := os.Stat(GlobalOptions.Config); err == nil && !info.IsDir() {
130 configFile, err := ioutil.ReadFile(GlobalOptions.Config)
131 if err != nil {
132 Error.Fatalf("Unable to read the configuration file '%s': %s",
133 GlobalOptions.Config, err.Error())
134 }
135 if err = yaml.Unmarshal(configFile, &GlobalConfig); err != nil {
136 Error.Fatalf("Unable to parse the configuration file '%s': %s",
137 GlobalOptions.Config, err.Error())
138 }
139 }
140
141 // Override from command line
142 if GlobalOptions.Server != "" {
143 GlobalConfig.Server = GlobalOptions.Server
144 }
145 if GlobalOptions.Kafka != "" {
146 GlobalConfig.Kafka = GlobalOptions.Kafka
147 }
148 if GlobalOptions.Local != "" {
149 GlobalConfig.Local = GlobalOptions.Local
150 }
151
152 if GlobalOptions.Timeout != "" {
153 timeout, err := time.ParseDuration(GlobalOptions.Timeout)
154 if err != nil {
155 Error.Fatalf("Unable to parse specified timeout duration '%s': %s",
156 GlobalOptions.Timeout, err.Error())
157 }
158 GlobalConfig.Grpc.Timeout = timeout
159 }
160}
161
162func ShowGlobalOptions() {
163 log.Printf("Configuration:")
164 log.Printf(" Voltha gRPC Server: %v", GlobalConfig.Server)
165 log.Printf(" Kafka: %v", GlobalConfig.Kafka)
166 log.Printf(" Listen Address: %v", GlobalConfig.Local)
167}