This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
/
core.go
281 lines (247 loc) · 7.42 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
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
package server
import (
"context"
"fmt"
"os"
"runtime/pprof"
"strings"
"sync"
"time"
"github.com/Benchkram/errz"
"github.com/puppetlabs/wash/activity"
"github.com/puppetlabs/wash/analytics"
"github.com/puppetlabs/wash/api"
"github.com/puppetlabs/wash/fuse"
"github.com/puppetlabs/wash/plugin"
"github.com/puppetlabs/wash/plugin/aws"
"github.com/puppetlabs/wash/plugin/docker"
"github.com/puppetlabs/wash/plugin/gcp"
"github.com/puppetlabs/wash/plugin/kubernetes"
log "github.com/sirupsen/logrus"
)
// InternalPlugins lists the plugins enabled by default in Wash.
var InternalPlugins = map[string]plugin.Root{
"aws": &aws.Root{},
"docker": &docker.Root{},
"gcp": &gcp.Root{},
"kubernetes": &kubernetes.Root{},
}
// Opts exposes additional configuration for server operation.
type Opts struct {
CPUProfilePath string
LogFile string
// LogLevel can be "warn", "info", "debug", or "trace".
LogLevel string
PluginConfig map[string]map[string]interface{}
}
// SetupLogging configures log level and output file according to configured options.
// If an output file was configured, returns a handle for you to close later.
func (o Opts) SetupLogging() (*os.File, error) {
level, err := log.ParseLevel(o.LogLevel)
if err != nil {
return nil, fmt.Errorf("%v is not a valid level; use warn, info, debug, trace", o.LogLevel)
}
log.SetLevel(level)
if o.LogFile != "" {
logFH, err := os.Create(o.LogFile)
if err != nil {
return nil, err
}
log.SetOutput(logFH)
return logFH, nil
}
return nil, nil
}
type controlChannels struct {
stopCh chan<- context.Context
stoppedCh <-chan struct{}
}
// Server encapsulates a running wash server with both Socket and FUSE servers.
type Server struct {
mountpoint string
socket string
opts Opts
logFH *os.File
api controlChannels
fuse controlChannels
plugins map[string]plugin.Root
analyticsClient analytics.Client
forVerifyInstall bool
}
// New creates a new Server. Accepts a list of plugins to load.
func New(mountpoint string, socket string, plugins map[string]plugin.Root, opts Opts) *Server {
return &Server{
mountpoint: mountpoint,
socket: socket,
plugins: plugins,
opts: opts,
}
}
// ForVerifyInstall creates a new Server that's meant to be used
// for verifying a given Wash installation.
func ForVerifyInstall(mountpoint string, socket string) *Server {
return &Server{
mountpoint: mountpoint,
socket: socket,
forVerifyInstall: true,
opts: Opts{
LogLevel: "warn",
},
}
}
// Start starts the server. It returns once the server is ready. The Boolean
// value is true if all plugins were successfully loaded
func (s *Server) Start() (bool, error) {
var err error
if s.logFH, err = s.opts.SetupLogging(); err != nil {
return false, err
}
registry := plugin.NewRegistry()
successfullyLoadedPlugins := true
if !s.forVerifyInstall {
successfullyLoadedPlugins = s.loadPlugins(registry)
if len(registry.Plugins()) == 0 {
return successfullyLoadedPlugins, fmt.Errorf("no plugins loaded. If you're planning on using Wash just for its external plugins, then go to https://puppetlabs.github.io/wash/docs/external-plugins")
}
plugin.InitCache()
analyticsConfig, err := analytics.GetConfig()
if err != nil {
return successfullyLoadedPlugins, err
}
s.analyticsClient = analytics.NewClient(analyticsConfig)
}
apiServerStopCh, apiServerStoppedCh, err := api.StartAPI(
registry,
s.mountpoint,
s.socket,
s.analyticsClient,
)
if err != nil {
return successfullyLoadedPlugins, err
}
s.api = controlChannels{stopCh: apiServerStopCh, stoppedCh: apiServerStoppedCh}
fuseServerStopCh, fuseServerStoppedCh, err := fuse.ServeFuseFS(
registry,
s.mountpoint,
s.analyticsClient,
)
if err != nil {
s.stopAPIServer()
return successfullyLoadedPlugins, err
}
s.fuse = controlChannels{stopCh: fuseServerStopCh, stoppedCh: fuseServerStoppedCh}
if !s.forVerifyInstall {
if s.opts.CPUProfilePath != "" {
f, err := os.Create(s.opts.CPUProfilePath)
if err != nil {
log.Fatal(err)
}
errz.Fatal(pprof.StartCPUProfile(f))
}
// Submit the initial start-up ping to GA. It's OK to do this synchronously
// because this is the first hit so the analytics client will not send it
// over the network.
if err := s.analyticsClient.Screenview("wash", analytics.Params{}); err != nil {
log.Infof("Failed to submit the initial start-up ping: %v", err)
}
}
return successfullyLoadedPlugins, nil
}
func (s *Server) stopAPIServer() {
// Shutdown the API server; wait for the shutdown to finish
apiShutdownDeadline := time.Now().Add(3 * time.Second)
apiShutdownCtx, cancelFunc := context.WithDeadline(context.Background(), apiShutdownDeadline)
defer cancelFunc()
s.api.stopCh <- apiShutdownCtx
close(s.api.stopCh)
<-s.api.stoppedCh
}
func (s *Server) stopFUSEServer() {
// Shutdown the FUSE server; wait for the shutdown to finish
close(s.fuse.stopCh)
<-s.fuse.stoppedCh
}
func (s *Server) shutdown() {
if s.forVerifyInstall {
return
}
if s.opts.CPUProfilePath != "" {
pprof.StopCPUProfile()
}
// Close any open journals on shutdown to ensure remaining entries are flushed to disk.
activity.CloseAll()
// Flush any outstanding analytics hits. We do this asynchronously
// so that the server process isn't blocked on its cleanup (in case
// the network is slow).
doneCh := make(chan struct{})
ticker := time.NewTicker(analytics.FlushDuration)
defer ticker.Stop()
go func() {
s.analyticsClient.Flush()
close(doneCh)
}()
select {
case <-doneCh:
// Pass-thru
case <-ticker.C:
// Pass-thru
}
if s.logFH != nil {
s.logFH.Close()
}
}
// Wait blocks until the server exits due to an error or a signal is delivered.
// Only one of Wait or Stop should be called.
func (s *Server) Wait(sigCh chan os.Signal) {
select {
case <-sigCh:
s.stopAPIServer()
s.stopFUSEServer()
case <-s.fuse.stoppedCh:
// This code-path is possible if the FUSE server prematurely shuts down, which
// can happen if the user unmounts the mountpoint while the server's running.
s.stopAPIServer()
case <-s.api.stoppedCh:
// This code-path is possible if the API server prematurely shuts down
s.stopFUSEServer()
}
s.shutdown()
}
// Stop the server and any related activity. Only one of Wait or Stop should be called.
func (s *Server) Stop() {
s.stopAPIServer()
s.stopFUSEServer()
s.shutdown()
}
func (s *Server) loadPlugins(registry *plugin.Registry) bool {
log.Debug("Loading plugins")
var wg sync.WaitGroup
var mux sync.Mutex
var failedPlugins []string
for name, root := range s.plugins {
log.Infof("Loading %v", name)
wg.Add(1)
go func(name string, root plugin.Root) {
if err := registry.RegisterPlugin(root, s.opts.PluginConfig[name]); err != nil {
// %+v is a convention used by some errors to print additional context such as a stack trace
log.Warnf("%v failed to load: %+v", name, err)
if _, ok := InternalPlugins[name]; ok {
mux.Lock()
failedPlugins = append(failedPlugins, name)
mux.Unlock()
}
}
wg.Done()
}(name, root)
}
wg.Wait()
if len(failedPlugins) > 0 {
log.Warnf(
"You can use 'docs <plugin>' (e.g. 'docs %v') to view set-up instructions for %v. However, you'll need to exit then restart the shell in order to properly reload them.\n",
failedPlugins[0],
strings.Join(failedPlugins, ", "),
)
}
log.Debug("Finished loading plugins")
return len(failedPlugins) <= 0
}