forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
247 lines (207 loc) · 5.67 KB
/
config.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package config
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"runtime"
"strconv"
"strings"
"github.com/Sirupsen/logrus"
goUUID "github.com/nu7hatch/gouuid"
"github.com/pkg/errors"
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/constants"
)
func URL() string {
ret := DefaultValue("CONFIG_URL", "")
if len(ret) == 0 {
return APIURL("")
}
return ret
}
func stripSchemas(url string) string {
if len(url) == 0 {
return ""
}
if strings.HasSuffix(url, "/schemas") {
return url[0 : len(url)-len("schemas")]
}
return url
}
func APIURL(df string) string {
return stripSchemas(DefaultValue("URL", df))
}
func APIProxyListenPort() int {
ret, _ := strconv.Atoi(DefaultValue("API_PROXY_LISTEN_PORT", "9342"))
return ret
}
func Builds() string {
return DefaultValue("BUILD_DIR", path.Join(Home(), "builds"))
}
func StateDir() string {
return DefaultValue("STATE_DIR", Home())
}
func physicalHostUUIDFile() string {
defValue := fmt.Sprintf("%s/.physical_host_uuid", StateDir())
return DefaultValue("PHYSICAL_HOST_UUID_FILE", defValue)
}
func PhysicalHostUUID(forceWrite bool) (string, error) {
return GetUUIDFromFile("PHYSICAL_HOST_UUID", physicalHostUUIDFile(), forceWrite)
}
func Home() string {
if runtime.GOOS == "windows" {
return DefaultValue("HOME", "c:/ProgramData/rancher")
}
return DefaultValue("HOME", "/var/lib/cattle")
}
func getUUIDFromFile(uuidFilePath string) (string, error) {
uuid := ""
fileBuffer, err := ioutil.ReadFile(uuidFilePath)
if err != nil && !os.IsNotExist(err) {
return "", errors.Wrap(err, constants.ReadUUIDFromFileError+"failed to read uuid file")
}
uuid = string(fileBuffer)
if uuid == "" {
newUUID, err := goUUID.NewV4()
if err != nil {
return "", errors.Wrap(err, constants.ReadUUIDFromFileError+"failed to generate uuid")
}
uuid = newUUID.String()
file, err := os.Create(uuidFilePath)
if err != nil {
return "", errors.Wrap(err, constants.ReadUUIDFromFileError+"failed to create uuid file")
}
if _, err := file.WriteString(uuid); err != nil {
return "", errors.Wrap(err, constants.ReadUUIDFromFileError+"failed to write uuid to file")
}
}
return uuid, nil
}
func GetUUIDFromFile(envName string, uuidFilePath string, forceWrite bool) (string, error) {
uuid := DefaultValue(envName, "")
if uuid != "" {
if forceWrite {
_, err := os.Open(uuidFilePath)
if err == nil {
os.Remove(uuidFilePath)
} else if !os.IsNotExist(err) {
return "", errors.Wrap(err, constants.GetUUIDFromFileError+"failed to open uuid file")
}
file, err := os.Create(uuidFilePath)
if err != nil {
return "", errors.Wrap(err, constants.GetUUIDFromFileError+"failed to create uuid file")
}
if _, err := file.WriteString(uuid); err != nil {
return "", errors.Wrap(err, constants.GetUUIDFromFileError+"failed to write uuid to file")
}
}
return uuid, nil
}
return getUUIDFromFile(uuidFilePath)
}
func DoPing() bool {
return DefaultValue("PING_ENABLED", "true") == "true"
}
func SetSecretKey(value string) {
constants.ConfigOverride["SECRET_KEY"] = value
}
func SecretKey() string {
return DefaultValue("SECRET_KEY", "adminpass")
}
func SetAccessKey(value string) {
constants.ConfigOverride["ACCESS_KEY"] = value
}
func AccessKey() string {
return DefaultValue("ACCESS_KEY", "admin")
}
func SetAPIURL(value string) {
constants.ConfigOverride["URL"] = value
}
func agentIP() string {
return DefaultValue("AGENT_IP", "")
}
func Sh() string {
return DefaultValue("CONFIG_SCRIPT", fmt.Sprintf("%s/config.sh", Home()))
}
func PhysicalHost() (model.PingResource, error) {
uuid, err := PhysicalHostUUID(false)
if err != nil {
return model.PingResource{}, errors.Wrap(err, constants.PhysicalHostError+"failed to get physical host uuid")
}
hostname, err := Hostname()
if err != nil {
return model.PingResource{}, errors.Wrap(err, constants.PhysicalHostError+"failed to get hostname")
}
return model.PingResource{
UUID: uuid,
Type: "physicalHost",
Kind: "physicalHost",
Name: hostname,
}, nil
}
func UpdatePyagent() bool {
return DefaultValue("CONFIG_UPDATE_PYAGENT", "true") == "true"
}
func HostAPIIP() string {
return DefaultValue("HOST_API_IP", "0.0.0.0")
}
func HostAPIPort() string {
return DefaultValue("HOST_API_PORT", "9345")
}
func JwtPublicKeyFile() string {
path := path.Join(Home(), "etc", "cattle", "api.crt")
return DefaultValue("CONSOLE_HOST_API_PUBLIC_KEY", path)
}
func HostProxy() string {
return DefaultValue("HOST_API_PROXY", "")
}
func Labels() map[string]string {
val := DefaultValue("HOST_LABELS", "")
ret := map[string]string{}
if val != "" {
m, err := url.ParseQuery(val)
if err != nil {
logrus.Error(err)
}
for k, v := range m {
ret[k] = v[0]
}
}
return ret
}
func DockerEnable() bool {
return DefaultValue("DOCKER_ENABLED", "true") == "true"
}
func DockerHostIP() string {
return DefaultValue("DOCKER_HOST_IP", agentIP())
}
func SetDockerUUID() (string, error) {
return GetUUIDFromFile("DOCKER_UUID", dockerUUIDFile(), true)
}
func DockerUUID() (string, error) {
return GetUUIDFromFile("DOCKER_UUID", dockerUUIDFile(), false)
}
func dockerUUIDFile() string {
defValue := fmt.Sprintf("%v/.docker_uuid", StateDir())
return DefaultValue("DOCKER_UUID_FILE", defValue)
}
func CadvisorIP() string {
return DefaultValue("CADVISOR_IP", "127.0.0.1")
}
func CadvisorPort() string {
return DefaultValue("CADVISOR_PORT", "9344")
}
func DefaultValue(name string, df string) string {
if value, ok := constants.ConfigOverride[name]; ok {
return value
}
if result := os.Getenv(fmt.Sprintf("CATTLE_%s", name)); result != "" {
return result
}
return df
}
func Stamp() string {
return DefaultValue("STAMP_FILE", path.Join(Home(), ".pyagent-stamp"))
}