/
lookupenv.go
186 lines (156 loc) · 4.83 KB
/
lookupenv.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
package etc
import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"time"
)
//ParseEnv is exported
func (conf *Configuration) parseEnv() error {
pidFile := os.Getenv("CLOUDTASK_PIDFILE")
if pidFile != "" {
conf.PidFile = pidFile
}
retryStartup := os.Getenv("CLOUDTASK_RETRYSTARTUP")
if retryStartup != "" {
value, err := strconv.ParseBool(retryStartup)
if err != nil {
return fmt.Errorf("CLOUDTASK_RETRYSTARTUP invalid, %s", err.Error())
}
conf.RetryStartup = value
}
useServerConfig := os.Getenv("CLOUDTASK_USESERVERCONFIG")
if useServerConfig != "" {
value, err := strconv.ParseBool(useServerConfig)
if err != nil {
return fmt.Errorf("CLOUDTASK_USESERVERCONFIG invalid, %s", err.Error())
}
conf.UseServerConfig = value
}
centerHost := os.Getenv("CLOUDTASK_CENTERHOST")
if centerHost != "" {
conf.CenterHost = centerHost
}
websiteHost := os.Getenv("CLOUDTASK_WEBSITEHOST")
if websiteHost != "" {
conf.WebsiteHost = websiteHost
}
var err error
//parse cluster env
if err = parseClusterEnv(conf); err != nil {
return err
}
//parse API env
if err = parseAPIEnv(conf); err != nil {
return err
}
//parse cache env
if err = parseCacheEnv(conf); err != nil {
return err
}
//parse logger env
return parseLoggerEnv(conf)
}
func parseClusterEnv(conf *Configuration) error {
if clusterHosts := os.Getenv("CLOUDTASK_CLUSTER_HOSTS"); clusterHosts != "" {
conf.Cluster.Hosts = clusterHosts
}
if clusterName := os.Getenv("CLOUDTASK_CLUSTER_NAME"); clusterName != "" {
if ret := filepath.HasPrefix(clusterName, "/"); !ret {
clusterName = "/" + clusterName
}
conf.Cluster.Root = clusterName
}
if clusterDevice := os.Getenv("CLOUDTASK_CLUSTER_DEVICE"); clusterDevice != "" {
conf.Cluster.Device = clusterDevice
}
if clusterRuntime := os.Getenv("CLOUDTASK_CLUSTER_RUNTIME"); clusterRuntime != "" {
conf.Cluster.Runtime = clusterRuntime
}
if clusterPulse := os.Getenv("CLOUDTASK_CLUSTER_PULSE"); clusterPulse != "" {
if _, err := time.ParseDuration(clusterPulse); err != nil {
return fmt.Errorf("CLOUDTASK_CLUSTER_PULSE invalid, %s", err.Error())
}
conf.Cluster.Pulse = clusterPulse
}
if clusterThreshold := os.Getenv("CLOUDTASK_CLUSTER_THRESHOLD"); clusterThreshold != "" {
value, err := strconv.Atoi(clusterThreshold)
if err != nil {
return fmt.Errorf("CLOUDTASK_CLUSTER_THRESHOLD invalid, %s", err.Error())
}
conf.Cluster.Threshold = value
}
return nil
}
func parseAPIEnv(conf *Configuration) error {
if apiHost := os.Getenv("CLOUDTASK_API_HOST"); apiHost != "" {
hostIP, hostPort, err := net.SplitHostPort(apiHost)
if err != nil {
return fmt.Errorf("CLOUDTASK_API_HOST invalid, %s", err.Error())
}
if hostIP != "" {
if _, err := net.LookupHost(hostIP); err != nil {
return fmt.Errorf("CLOUDTASK_API_HOST invalid, %s", err.Error())
}
}
conf.API.Hosts = []string{net.JoinHostPort(hostIP, hostPort)}
}
if enableCors := os.Getenv("CLOUDTASK_API_ENABLECORS"); enableCors != "" {
value, err := strconv.ParseBool(enableCors)
if err != nil {
return fmt.Errorf("CLOUDTASK_API_ENABLECORS invalid, %s", err.Error())
}
conf.API.EnableCors = value
}
return nil
}
func parseCacheEnv(conf *Configuration) error {
if maxJobs := os.Getenv("CLOUDTASK_CACHE_MAXJOBS"); maxJobs != "" {
value, err := strconv.Atoi(maxJobs)
if err != nil {
return fmt.Errorf("CLOUDTASK_CACHE_MAXJOBS invalid, %s", err.Error())
}
conf.Cache.MaxJobs = value
}
if saveDirectory := os.Getenv("CLOUDTASK_CACHE_DIRECTORY"); saveDirectory != "" {
conf.Cache.SaveDirectory = saveDirectory
}
if autoClean := os.Getenv("CLOUDTASK_CACHE_AUTOCLEAN"); autoClean != "" {
value, err := strconv.ParseBool(autoClean)
if err != nil {
return fmt.Errorf("CLOUDTASK_CACHE_AUTOCLEAN invalid, %s", err.Error())
}
conf.Cache.AutoClean = value
}
if cleanInterval := os.Getenv("CLOUDTASK_CACHE_CLEANINTERVAL"); cleanInterval != "" {
if _, err := time.ParseDuration(cleanInterval); err != nil {
return fmt.Errorf("CLOUDTASK_CACHE_CLEANINTERVAL invalid, %s", err.Error())
}
conf.Cache.CleanInterval = cleanInterval
}
if pullRecovery := os.Getenv("CLOUDTASK_CACHE_PULLRECOVERY"); pullRecovery != "" {
if _, err := time.ParseDuration(pullRecovery); err != nil {
return fmt.Errorf("CLOUDTASK_CACHE_PULLRECOVERY invalid, %s", err.Error())
}
conf.Cache.PullRecovery = pullRecovery
}
return nil
}
func parseLoggerEnv(conf *Configuration) error {
if logFile := os.Getenv("CLOUDTASK_LOG_FILE"); logFile != "" {
conf.Logger.LogFile = logFile
}
if logLevel := os.Getenv("CLOUDTASK_LOG_LEVEL"); logLevel != "" {
conf.Logger.LogLevel = logLevel
}
if logSize := os.Getenv("CLOUDTASK_LOG_SIZE"); logSize != "" {
value, err := strconv.ParseInt(logSize, 10, 64)
if err != nil {
return fmt.Errorf("CLOUDTASK_LOG_SIZE invalid, %s", err.Error())
}
conf.Logger.LogSize = value
}
return nil
}