-
Notifications
You must be signed in to change notification settings - Fork 52
/
interface.go
196 lines (168 loc) · 4.84 KB
/
interface.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
// Copyright 2019 HAProxy Technologies
//
// 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
//
// 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 configuration
import (
"context"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"
parser "github.com/haproxytech/config-parser/v5"
parser_options "github.com/haproxytech/config-parser/v5/options"
"github.com/haproxytech/client-native/v5/configuration/options"
)
type Configuration interface {
Parser
ACL
Backend
Bind
Cache
Capture
Defaults
DgramBind
FCGIApp
Filter
Frontend
Global
HTTPCheck
HTTPRequestRule
HTTPResponseRule
HTTPAfterResponseRule
HTTPErrorRule
HTTPErrorsSection
LogTarget
LogForward
MailerEntry
MailersSection
Nameserver
PeerEntry
PeerSection
Program
Raw
Resolver
Ring
Server
ServerTemplate
Site
StickRule
ServiceI
Table
TCPCheck
TCPRequestRule
TCPResponseRule
Transactions
TransactionHandling
Version
Userlist
User
Group
}
func New(ctx context.Context, opt ...options.ConfigurationOption) (Configuration, error) { //nolint:revive
c := &client{}
var err error
optionsWrapper := options.ConfigurationOptions{}
for _, option := range opt {
err = option.Set(&optionsWrapper)
if err != nil {
return nil, err
}
}
if optionsWrapper.TransactionDir == "" {
optionsWrapper.TransactionDir = options.DefaultTransactionDir
}
if optionsWrapper.ConfigurationFile == "" {
optionsWrapper.ConfigurationFile = options.DefaultConfigurationFile
}
if optionsWrapper.BackupsDir == "" {
optionsWrapper.BackupsDir = filepath.Dir(optionsWrapper.ConfigurationFile)
}
if optionsWrapper.Haproxy == "" {
optionsWrapper.Haproxy = options.DefaultHaproxy
}
versionString, err := c.fetchVersion(optionsWrapper.Haproxy)
if err != nil {
return nil, NewConfError(ErrCannotFindHAProxy, fmt.Sprintf("path to HAProxy binary not valid: %s, err: %s", optionsWrapper.Haproxy, err.Error()))
}
noNamedDefaultsFrom := noNamedDefaultsFrom(versionString)
c.noNamedDefaultsFrom = true
c.TransactionClient = c
c.ConfigurationOptions = optionsWrapper
c.parsers = make(map[string]parser.Parser)
c.services = make(map[string]*Service)
if err2 := c.InitTransactionParsers(); err2 != nil {
return nil, err2
}
parserOptions := []parser_options.ParserOption{}
if c.ConfigurationOptions.UseMd5Hash {
parserOptions = append(parserOptions, parser_options.UseMd5Hash)
}
// HAProxy lower than 2.4 doesn't support from keyword to inherit defaults section, so if it's lower than that don't set it
// if it isn't set then add from to all frontend/backend section that have it unset to the proper defaults section
if noNamedDefaultsFrom {
parserOptions = append(parserOptions, parser_options.NoNamedDefaultsFrom)
}
parserOptions = append(parserOptions, parser_options.Path(optionsWrapper.ConfigurationFile))
p, err := parser.New(parserOptions...)
if err != nil {
return nil, NewConfError(ErrCannotReadConfFile, fmt.Sprintf("Cannot read %s", c.ConfigurationFile))
}
c.parser = p
return c, nil
}
func (c *client) Parser() parser.Parser {
return c.parser
}
func (c *client) fetchVersion(haproxy string) (string, error) {
versionString, err := exec.Command(haproxy, "-v").Output()
if err != nil {
return "", err
}
return string(versionString), nil
}
func getVersionNumbers(version string) (int64, int64, int64, error) {
if !strings.HasPrefix(version, "HAProxy version ") {
return 0, 0, 0, fmt.Errorf("not a haproxy version string")
}
version = version[strings.Index(version, "HAProxy version ")+len("HAProxy version "):]
versionSlice := strings.SplitN(version, "-", 2)
if len(versionSlice) != 2 {
return 0, 0, 0, fmt.Errorf("not a haproxy version string")
}
versionInts := strings.SplitN(versionSlice[0], ".", 3)
if len(versionInts) != 3 {
return 0, 0, 0, fmt.Errorf("not a haproxy version string")
}
major, err := strconv.ParseInt(versionInts[0], 10, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("not a haproxy version string")
}
minor, err := strconv.ParseInt(versionInts[1], 10, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("not a haproxy version string")
}
patch, err := strconv.ParseInt(versionInts[2], 10, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("not a haproxy version string")
}
return major, minor, patch, nil
}
func noNamedDefaultsFrom(version string) bool {
major, minor, _, err := getVersionNumbers(version)
if err != nil {
return true
}
return major < 2 || (major == 2 && minor < 4)
}