-
Notifications
You must be signed in to change notification settings - Fork 327
/
util.go
383 lines (318 loc) · 9.72 KB
/
util.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package util
import (
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strconv"
"time"
"github.com/akutz/gofig"
"github.com/akutz/gotil"
apiversion "github.com/emccode/libstorage/api"
"github.com/emccode/libstorage/api/context"
apiserver "github.com/emccode/libstorage/api/server"
apitypes "github.com/emccode/libstorage/api/types"
"github.com/emccode/rexray/core/version"
)
const (
logDirPathSuffix = "/var/log/rexray"
etcDirPathSuffix = "/etc/rexray"
binDirPathSuffix = "/usr/bin"
runDirPathSuffix = "/var/run/rexray"
libDirPathSuffix = "/var/lib/rexray"
// UnitFilePath is the path to the SystemD service's unit file.
UnitFilePath = "/etc/systemd/system/rexray.service"
// InitFilePath is the path to the SystemV Service's init script.
InitFilePath = "/etc/init.d/rexray"
// EnvFileName is the name of the environment file used by the SystemD
// service.
EnvFileName = "rexray.env"
)
var (
thisExeDir string
thisExeName string
thisExeAbsPath string
prefix string
binDirPath string
binFilePath string
logDirPath string
libDirPath string
runDirPath string
etcDirPath string
pidFilePath string
)
func init() {
prefix = os.Getenv("REXRAY_HOME")
thisExeDir, thisExeName, thisExeAbsPath = gotil.GetThisPathParts()
}
// GetPrefix gets the root path to the REX-Ray data.
func GetPrefix() string {
return prefix
}
// Prefix sets the root path to the REX-Ray data.
func Prefix(p string) {
if p == "" || p == "/" {
return
}
binDirPath = ""
binFilePath = ""
logDirPath = ""
libDirPath = ""
runDirPath = ""
etcDirPath = ""
pidFilePath = ""
prefix = p
}
// IsPrefixed returns a flag indicating whether or not a prefix value is set.
func IsPrefixed() bool {
return !(prefix == "" || prefix == "/")
}
// Install executes the system install command.
func Install(args ...string) {
exec.Command("install", args...).Run()
}
// InstallChownRoot executes the system install command and chowns the target
// to the root user and group.
func InstallChownRoot(args ...string) {
a := []string{"-o", "0", "-g", "0"}
for _, i := range args {
a = append(a, i)
}
exec.Command("install", a...).Run()
}
// InstallDirChownRoot executes the system install command with a -d flag and
// chowns the target to the root user and group.
func InstallDirChownRoot(dirPath string) {
InstallChownRoot("-d", dirPath)
}
// EtcDirPath returns the path to the REX-Ray etc directory.
func EtcDirPath() string {
if etcDirPath == "" {
etcDirPath = fmt.Sprintf("%s%s", prefix, etcDirPathSuffix)
os.MkdirAll(etcDirPath, 0755)
}
return etcDirPath
}
// RunDirPath returns the path to the REX-Ray run directory.
func RunDirPath() string {
if runDirPath == "" {
runDirPath = fmt.Sprintf("%s%s", prefix, runDirPathSuffix)
os.MkdirAll(runDirPath, 0755)
}
return runDirPath
}
// LogDirPath returns the path to the REX-Ray log directory.
func LogDirPath() string {
if logDirPath == "" {
logDirPath = fmt.Sprintf("%s%s", prefix, logDirPathSuffix)
os.MkdirAll(logDirPath, 0755)
}
return logDirPath
}
// LibDirPath returns the path to the REX-Ray bin directory.
func LibDirPath() string {
if libDirPath == "" {
libDirPath = fmt.Sprintf("%s%s", prefix, libDirPathSuffix)
os.MkdirAll(libDirPath, 0755)
}
return libDirPath
}
// LibFilePath returns the path to a file inside the REX-Ray lib directory
// with the provided file name.
func LibFilePath(fileName string) string {
return fmt.Sprintf("%s/%s", LibDirPath(), fileName)
}
// RunFilePath returns the path to a file inside the REX-Ray run directory
// with the provided file name.
func RunFilePath(fileName string) string {
return fmt.Sprintf("%s/%s", RunDirPath(), fileName)
}
// BinDirPath returns the path to the REX-Ray bin directory.
func BinDirPath() string {
if binDirPath == "" {
binDirPath = fmt.Sprintf("%s%s", prefix, binDirPathSuffix)
os.MkdirAll(binDirPath, 0755)
}
return binDirPath
}
// PidFilePath returns the path to the REX-Ray PID file.
func PidFilePath() string {
if pidFilePath == "" {
pidFilePath = fmt.Sprintf("%s/rexray.pid", RunDirPath())
}
return pidFilePath
}
// BinFilePath returns the path to the REX-Ray executable.
func BinFilePath() string {
if binFilePath == "" {
binFilePath = fmt.Sprintf("%s/rexray", BinDirPath())
}
return binFilePath
}
// EtcFilePath returns the path to a file inside the REX-Ray etc directory
// with the provided file name.
func EtcFilePath(fileName string) string {
return fmt.Sprintf("%s/%s", EtcDirPath(), fileName)
}
// LogFilePath returns the path to a file inside the REX-Ray log directory
// with the provided file name.
func LogFilePath(fileName string) string {
return fmt.Sprintf("%s/%s", LogDirPath(), fileName)
}
// LogFile returns a writer to a file inside the REX-Ray log directory
// with the provided file name.
func LogFile(fileName string) (io.Writer, error) {
return os.OpenFile(
LogFilePath(fileName), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
}
// StdOutAndLogFile returns a mutltiplexed writer for the current process's
// stdout descriptor and a REX-Ray log file with the provided name.
func StdOutAndLogFile(fileName string) (io.Writer, error) {
lf, lfErr := LogFile(fileName)
if lfErr != nil {
return nil, lfErr
}
return io.MultiWriter(os.Stdout, lf), nil
}
// WritePidFile writes the current process ID to the REX-Ray PID file.
func WritePidFile(pid int) error {
if pid < 0 {
pid = os.Getpid()
}
return gotil.WriteStringToFile(fmt.Sprintf("%d", pid), PidFilePath())
}
// ReadPidFile reads the REX-Ray PID from the PID file.
func ReadPidFile() (int, error) {
pidStr, pidStrErr := gotil.ReadFileToString(PidFilePath())
if pidStrErr != nil {
return -1, pidStrErr
}
pid, atoiErr := strconv.Atoi(pidStr)
if atoiErr != nil {
return -1, atoiErr
}
return pid, nil
}
// PrintVersion prints the current version information to the provided writer.
func PrintVersion(out io.Writer) {
fmt.Fprintln(out, "REX-Ray")
fmt.Fprintln(out, "-------")
fmt.Fprintf(out, "Binary: %s\n", thisExeAbsPath)
fmt.Fprintf(out, "SemVer: %s\n", version.SemVer)
fmt.Fprintf(out, "OsArch: %s\n", version.Arch)
fmt.Fprintf(out, "Branch: %s\n", version.Branch)
fmt.Fprintf(out, "Commit: %s\n", version.ShaLong)
fmt.Fprintf(out, "Formed: %s\n\n", version.EpochToRfc1123())
fmt.Fprintln(out, "libStorage")
fmt.Fprintln(out, "----------")
fmt.Fprintf(out, "SemVer: %s\n", apiversion.Version.SemVer)
fmt.Fprintf(out, "OsArch: %s\n", apiversion.Version.Arch)
fmt.Fprintf(out, "Branch: %s\n", apiversion.Version.Branch)
fmt.Fprintf(out, "Commit: %s\n", apiversion.Version.ShaLong)
timestamp := apiversion.Version.BuildTimestamp.Format(time.RFC1123)
fmt.Fprintf(out, "Formed: %s\n", timestamp)
}
// WaitUntilLibStorageStopped blocks until libStorage is stopped.
func WaitUntilLibStorageStopped(ctx apitypes.Context, errs <-chan error) {
ctx.Debug("waiting until libStorage is stopped")
// if there is no err channel then do not wait until libStorage is stopped
// as the absence of the err channel means libStorage was not started in
// embedded mode
if errs == nil {
ctx.Debug("done waiting on err chan; err chan is nil")
return
}
// in a goroutine, range over the apiserver.Close channel until it's closed
for range apiserver.Close() {
}
ctx.Debug("done sending close signals to libStorage")
// block until the err channel is closed
for range errs {
}
ctx.Debug("done waiting on err chan")
}
var localHostRX = regexp.MustCompile(
`(?i)^(localhost|(?:127\.0\.0\.1))(?::(\d+))?$`)
// IsLocalServerActive returns a flag indicating whether or not a local
// libStorage is already running.
func IsLocalServerActive(
ctx apitypes.Context, config gofig.Config) (host string, running bool) {
host = config.GetString(apitypes.ConfigHost)
if host == "" {
return "", false
}
proto, addr, err := gotil.ParseAddress(host)
if err != nil {
return "", false
}
switch proto {
case "unix":
ctx.WithField("sock", addr).Debug("is local unix server active")
return host, gotil.FileExists(addr)
case "tcp":
m := localHostRX.FindStringSubmatch(addr)
if len(m) < 3 {
return "", false
}
port, err := strconv.Atoi(m[2])
if err != nil {
return "", false
}
ctx.WithField("port", port).Debug("is local tcp server active")
return host, !gotil.IsTCPPortAvailable(port)
}
return "", false
}
// ActivateLibStorage activates a libStorage server if conditions are met and
// returns a possibly mutated context.
func ActivateLibStorage(
ctx apitypes.Context,
config gofig.Config) (apitypes.Context, gofig.Config, <-chan error, error) {
config = config.Scope("rexray")
if !config.IsSet(apitypes.ConfigIgVolOpsMountPath) {
config.Set(apitypes.ConfigIgVolOpsMountPath, LibFilePath("volumes"))
}
var (
host string
err error
isRunning bool
errs <-chan error
server apitypes.Server
)
if host = config.GetString(apitypes.ConfigHost); host != "" {
if !config.GetBool(apitypes.ConfigEmbedded) {
ctx.WithField(
"host", host,
).Debug("not starting embeddded server; embedded mode disabled")
return ctx, config, nil, nil
}
}
if host, isRunning = IsLocalServerActive(ctx, config); isRunning {
ctx = ctx.WithValue(context.HostKey, host)
ctx.WithField("host", host).Debug(
"not starting embeddded server; already running")
return ctx, config, nil, nil
}
// if no host was specified then see if a set of default services need to
// be initialized
if host == "" {
if err = initDefaultLibStorageServices(ctx, config); err != nil {
return ctx, config, nil, err
}
}
ctx.Debug("starting embedded libStorage server")
apiserver.CloseOnAbort()
if server, errs, err = apiserver.Serve(ctx, config); err != nil {
return ctx, config, nil, err
}
go func() {
if err := <-errs; err != nil {
ctx.Error(err)
}
}()
if host == "" {
config.Set(apitypes.ConfigHost, server.Addrs()[0])
}
return ctx, config, errs, nil
}