-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
211 lines (193 loc) · 4.82 KB
/
main.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
/*
*
* Copyright © 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* 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 main
import (
"context"
"flag"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"text/template"
log "github.com/sirupsen/logrus"
"github.com/dell/csi-metadata-retriever/provider"
"github.com/dell/csi-metadata-retriever/retriever"
"github.com/dell/csi-metadata-retriever/utils"
"github.com/dell/gocsi"
csictx "github.com/dell/gocsi/context"
)
// main is ignored when this package is built as a go plug-in.
func main() {
Run(
context.Background(),
"MetadataRetriever",
"A description of the SP",
"",
provider.New())
}
const netUnix = "unix"
// Run launches a CSI storage plug-in.
func Run(
ctx context.Context,
appName, appDescription, appUsage string,
sp retriever.PluginProvider,
) {
// Check for the debug value.
if v, ok := csictx.LookupEnv(ctx, gocsi.EnvVarDebug); ok {
/* #nosec G104 */
if ok, _ := strconv.ParseBool(v); ok {
err := csictx.Setenv(ctx, gocsi.EnvVarLogLevel, "debug")
if err != nil {
log.Warnf("failed to set EnvVarLogLevel")
}
err = csictx.Setenv(ctx, gocsi.EnvVarReqLogging, "true")
if err != nil {
log.Warnf("failed to set EnvVarReqLogging")
}
err = csictx.Setenv(ctx, gocsi.EnvVarRepLogging, "true")
if err != nil {
log.Warnf("failed to set EnvVarRepLogging")
}
}
}
// Adjust the log level.
lvl := log.InfoLevel
if v, ok := csictx.LookupEnv(ctx, gocsi.EnvVarLogLevel); ok {
var err error
if lvl, err = log.ParseLevel(v); err != nil {
lvl = log.InfoLevel
}
}
log.SetLevel(lvl)
printUsage := func() {
// app is the information passed to the printUsage function
app := struct {
Name string
Description string
Usage string
BinPath string
}{
appName,
appDescription,
appUsage,
os.Args[0],
}
t, err := template.New("t").Parse(usage)
if err != nil {
log.WithError(err).Fatalln("failed to parse usage template")
}
if err := t.Execute(os.Stderr, app); err != nil {
log.WithError(err).Fatalln("failed emitting usage")
}
return
}
// Check for a help flag.
fs := flag.NewFlagSet("csp", flag.ExitOnError)
fs.Usage = printUsage
var help bool
fs.BoolVar(&help, "?", false, "")
err := fs.Parse(os.Args)
if err == flag.ErrHelp || help {
printUsage()
os.Exit(1)
}
// If no endpoint is set then print the usage.
if os.Getenv(utils.EnvVarEndpoint) == "" {
printUsage()
os.Exit(1)
}
l, err := utils.GetCSIEndpointListener()
if err != nil {
log.WithError(err).Fatalln("failed to listen")
}
// Define a lambda that can be used in the exit handler
// to remove a potential UNIX sock file.
var rmSockFileOnce sync.Once
rmSockFile := func() {
rmSockFileOnce.Do(func() {
if l == nil || l.Addr() == nil {
return
}
/* #nosec G104 */
if l.Addr().Network() == netUnix {
sockFile := l.Addr().String()
err := os.RemoveAll(sockFile)
if err != nil {
log.Warnf("failed to remove sock file: %s", err)
}
log.WithField("path", sockFile).Info("removed sock file")
}
})
}
trapSignals(func() {
sp.GracefulStop(ctx)
rmSockFile()
log.Info("server stopped gracefully")
}, func() {
sp.Stop(ctx)
rmSockFile()
log.Info("server aborted")
})
if err := sp.Serve(ctx, l); err != nil {
rmSockFile()
log.WithError(err).Fatal("grpc failed")
}
}
func trapSignals(onExit, onAbort func()) {
sigc := make(chan os.Signal, 1)
sigs := []os.Signal{
syscall.SIGTERM,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
}
signal.Notify(sigc, sigs...)
go func() {
for s := range sigc {
ok, graceful := isExitSignal(s)
if !ok {
continue
}
if !graceful {
log.WithField("signal", s).Error("received signal; aborting")
if onAbort != nil {
onAbort()
}
os.Exit(1)
}
log.WithField("signal", s).Info("received signal; shutting down")
if onExit != nil {
onExit()
}
os.Exit(0)
}
}()
}
// isExitSignal returns a flag indicating whether a signal 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.SIGTERM,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT:
return true, true
default:
return false, false
}
}