-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
145 lines (121 loc) · 3.67 KB
/
service.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
//
// Author:: Salim Afiune <afiune@chef.io>
// Copyright:: Copyright 2017, Chef Software Inc.
//
package config
import (
"errors"
"fmt"
"net/url"
"path"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/chef/automate/components/config-mgmt-service/backend"
"github.com/chef/automate/components/config-mgmt-service/backend/elastic"
"github.com/chef/automate/components/config-mgmt-service/backend/mock"
"github.com/chef/automate/lib/tls/certs"
"github.com/chef/automate/lib/version"
)
var SERVICE_NAME = "config-mgmt-service"
// Service config definition
type Service struct {
Name string `json:"name" mapstructure:"name"`
Version string `json:"version" mapstructure:"version"`
Host string `json:"host" mapstructure:"host"`
Port int `json:"port" mapstructure:"port"`
LogLevel string `json:"log_level" mapstructure:"log_level"`
client backend.Client
TLSConfig certs.TLSConfig `mapstructure:"tls"`
serviceCerts *certs.ServiceCerts
}
// Default returns a blank Service instance with default parameters
func Default() *Service {
return &Service{
Version: version.Version,
Name: SERVICE_NAME,
Host: "localhost",
Port: 1234,
LogLevel: "info",
}
}
// New returns a Service instance with default parameters and backend client
func New(b backend.Client) *Service {
service := Default()
service.client = b
return service
}
// ConfigFromViper returns a Service instance from the current viper config
func ConfigFromViper() (*Service, error) {
cfg := Default()
// Unmarshall the viper config into the server Config
if err := viper.Unmarshal(cfg); err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("Failed to marshall viper config to server config")
return cfg, err
}
// Validates that the configuration has a valid host/port
_, err := url.ParseRequestURI(path.Join("http://", cfg.ListenAddress()))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error(fmt.Sprintf("Listen adddress '%s' is not valid. Please check the 'host' and 'port' configuration", cfg.ListenAddress()))
return cfg, err
}
// Setup the Elasticsearch backend
var backend backend.Client
switch viper.GetString("backend") {
case "elasticsearch":
backend = elastic.New(viper.GetString("elasticsearch-url"))
case "mock":
backend = mock.New()
default:
err := errors.New("Unavailable backend machanism")
log.Error(err)
return cfg, err
}
cfg.SetBackend(backend)
// Fix any relative paths that might be in the config file before we
// read in their values.
cfg.TLSConfig.FixupRelativeTLSPaths(viper.ConfigFileUsed())
serviceCerts, err := cfg.TLSConfig.ReadCerts()
if err != nil {
log.WithFields(log.Fields{
"err": err.Error(),
}).Error("Failed to loading x509 key pair and/or root CA certificate")
return cfg, err
}
cfg.serviceCerts = serviceCerts
// Log level
cfg.SetLogLevel()
return cfg, nil
}
func (s *Service) SetBackend(b backend.Client) {
s.client = b
}
// Returns the configured backend
func (s *Service) GetBackend() backend.Client {
return s.client
}
func (s *Service) GetServiceCerts() *certs.ServiceCerts {
return s.serviceCerts
}
// ListenAddress is the address where gRPC server will bind and listen
func (c *Service) ListenAddress() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}
// SetLogLevel sets the log level for the service
func (c *Service) SetLogLevel() {
if c.LogLevel == "" {
return
}
log.WithFields(log.Fields{
"level": c.LogLevel,
}).Info("Setting log level")
level, err := log.ParseLevel(c.LogLevel)
if err != nil {
log.WithField("level", c.LogLevel).WithError(err).Error("Using default level 'info'")
return
}
log.SetLevel(level)
}