/
core.go
178 lines (147 loc) · 3.97 KB
/
core.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
//go:generate go run semver/semver.go -f semver.tpl -o core_generated.go
package core
import (
"fmt"
"os"
"os/signal"
"runtime"
"strconv"
"sync"
"syscall"
"time"
"github.com/rexray/rexray/libstorage/api/context"
apitypes "github.com/rexray/rexray/libstorage/api/types"
)
var (
// DockerLegacyMode is true if Docker legacy mode is enabled.
DockerLegacyMode bool
// Arch is the uname OS-Arch string.
Arch = fmt.Sprintf(
"%s-%s",
goosToUname[runtime.GOOS],
goarchToUname[runtime.GOARCH])
// SemVer is the semantic version.
SemVer string
// CommitSha7 is the short version of the commit hash from which
// this program was built.
CommitSha7 string
// CommitSha32 is the long version of the commit hash from which
// this program was built.
CommitSha32 string
// CommitTime is the commit timestamp of the commit from which
// this program was built.
CommitTime time.Time
// BuildType is the build type of this binary.
BuildType = "client+agent+controller"
// Debug is whether or not the REXRAY_DEBUG environment variable is set
// to a truthy value.
Debug, _ = strconv.ParseBool(os.Getenv("REXRAY_DEBUG"))
)
func init() {
if v, ok := os.LookupEnv("DOCKER_LEGACY"); ok {
DockerLegacyMode, _ = strconv.ParseBool(v)
} else {
DockerLegacyMode = true
}
}
// SignalHandlerFunc is a function that can be registered with
// `core.RegisterSignalHandler` to receive a callback when the process receives
// a signal.
type SignalHandlerFunc func(ctx apitypes.Context, s os.Signal)
var (
sigHandlers []SignalHandlerFunc
sigHandlersRWL = &sync.RWMutex{}
)
// RegisterSignalHandler registers a SignalHandlerFunc.
func RegisterSignalHandler(f SignalHandlerFunc) {
sigHandlersRWL.Lock()
defer sigHandlersRWL.Unlock()
sigHandlers = append(sigHandlers, f)
}
type signalContextKeyType int
const signalContextKey signalContextKeyType = 0
func (k signalContextKeyType) String() string {
return "signal"
}
// TrapSignals tells the process to trap incoming process signals.
func TrapSignals(ctx apitypes.Context) {
context.RegisterCustomKey(signalContextKey, context.CustomLoggerKey)
sigc := make(chan os.Signal, 1)
signal.Notify(
sigc,
syscall.SIGKILL,
syscall.SIGTERM,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT)
go func() {
for s := range sigc {
ctx := ctx.WithValue(signalContextKey, s.String())
isExitSignal, isGraceful := IsExitSignal(s)
if isExitSignal {
if isGraceful {
ctx.Info("received signal; shutting down")
} else {
ctx.Error("received signal; aborting")
os.Exit(1)
}
}
func() {
sigHandlersRWL.RLock()
defer sigHandlersRWL.RUnlock()
// execute the signal handlers in reverse order. the first
// one registered should be executed last as it was registered
// the earliest
for i := len(sigHandlers) - 1; i >= 0; i-- {
sigHandlers[i](ctx, s)
}
}()
if isExitSignal {
os.Exit(0)
}
}
}()
}
// IsExitSignal returns a flag indicating whether a signal is SIGKILL, SIGHUP,
// SIGINT, SIGTERM, or SIGQUIT. The second return value is whether it is a
// graceful exit. This flag is true for SIGTERM, SIGHUP, SIGINT, and SIGQUIT.
func IsExitSignal(s os.Signal) (bool, bool) {
switch s {
case syscall.SIGKILL:
return true, false
case syscall.SIGTERM,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT:
return true, true
default:
return false, false
}
}
var goosToUname = map[string]string{
"android": "Android",
"darwin": "Darwin",
"dragonfly": "DragonFly",
"freebsd": "kFreeBSD",
"linux": "Linux",
"nacl": "NaCl",
"netbsd": "NetBSD",
"openbsd": "OpenBSD",
"plan9": "Plan9",
"solaris": "Solaris",
"windows": "Windows",
}
var goarchToUname = map[string]string{
"386": "i386",
"amd64": "x86_64",
"amd64p32": "x86_64_P32",
"arm": "ARMv7",
"arm64": "ARMv8",
"mips": "MIPS32",
"mips64": "MIPS64",
"mips64le": "MIPS64LE",
"mipsle": "MIPS32LE",
"ppc64": "PPC64",
"ppc64le": "PPC64LE",
"s390x": "s390x",
}