-
Notifications
You must be signed in to change notification settings - Fork 43
/
app.go
272 lines (232 loc) · 6.73 KB
/
app.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
package app
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/FleekHQ/space-daemon/core"
"github.com/FleekHQ/space-daemon/grpc"
"github.com/FleekHQ/space-daemon/core/space/fuse"
"github.com/FleekHQ/space-daemon/core/vault"
"github.com/FleekHQ/space-daemon/core/fsds"
"github.com/FleekHQ/space-daemon/core/spacefs"
textile "github.com/FleekHQ/space-daemon/core/textile"
"github.com/FleekHQ/space-daemon/core/textile/hub"
"github.com/FleekHQ/space-daemon/core/env"
"github.com/FleekHQ/space-daemon/core/space"
node "github.com/FleekHQ/space-daemon/core/ipfs/node"
"github.com/FleekHQ/space-daemon/core/keychain"
"github.com/FleekHQ/space-daemon/core/sync"
"github.com/FleekHQ/space-daemon/log"
"golang.org/x/sync/errgroup"
"github.com/FleekHQ/space-daemon/config"
"github.com/FleekHQ/space-daemon/core/store"
w "github.com/FleekHQ/space-daemon/core/watcher"
"github.com/golang-collections/collections/stack"
)
// Shutdown logic follows this example https://gist.github.com/akhenakh/38dbfea70dc36964e23acc19777f3869
type App struct {
eg *errgroup.Group
components *stack.Stack
cfg config.Config
env env.SpaceEnv
isShuttingDown bool
}
type componentMap struct {
name string
component core.Component
}
func New(cfg config.Config, env env.SpaceEnv) *App {
return &App{
components: stack.New(),
cfg: cfg,
env: env,
isShuttingDown: false,
}
}
// Start is the Entry point for the app.
// All module components are initialized and managed here.
// When a top level module that need to be shutdown on exit is initialized. It should be
// added to the apps list of tracked components using the `Run()` function, but if the component has a blocking
// start/run function it should be tracked with the `RunAsync()` function and call the blocking function in the
// input function block.
func (a *App) Start(ctx context.Context) error {
a.eg, ctx = errgroup.WithContext(ctx)
// setup to detect interruption
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(interrupt)
// init appStore
appStore := store.New(
store.WithPath(a.cfg.GetString(config.SpaceStorePath, "")),
)
if err := appStore.Open(); err != nil {
return err
}
a.Run("Store", appStore)
// Init keychain
kc := keychain.New(keychain.WithPath(a.cfg.GetString(config.SpaceStorePath, "")), keychain.WithStore(appStore))
// Init Vault
v := vault.New(a.cfg.GetString(config.SpaceVaultAPIURL, ""), a.cfg.GetString(config.SpaceVaultSaltSecret, ""))
watcher, err := w.New()
if err != nil {
return err
}
a.Run("FolderWatcher", watcher)
// setup local ipfs node if Ipfsnode is set
if a.cfg.GetBool(config.Ipfsnode, true) {
// setup local ipfs node
node := node.NewIpsNode(a.cfg)
err = a.RunAsync("IpfsNode", node, func() error {
return node.Start(ctx)
})
if err != nil {
log.Error("Error starting embedded IPFS node", err)
return err
}
} else {
log.Info("Skipping embedded IPFS node")
}
// setup local buckets
buckd := textile.NewBuckd(a.cfg)
err = a.RunAsync("BucketDaemon", buckd, func() error {
return buckd.Start(ctx)
})
if err != nil {
return err
}
hubAuth := hub.New(appStore, kc, a.cfg)
// setup textile client
uc := textile.CreateUserClient(a.cfg.GetString(config.TextileHubTarget, ""))
textileClient := textile.NewClient(appStore, kc, hubAuth, uc, nil)
err = a.RunAsync("TextileClient", textileClient, func() error {
return textileClient.Start(ctx, a.cfg)
})
if err != nil {
return err
}
// watcher is started inside bucket sync
bucketSync := sync.New(watcher, textileClient, appStore, nil)
// setup the Space Service
sv, svErr := space.NewService(
appStore,
textileClient,
bucketSync,
a.cfg,
kc,
v,
hubAuth,
space.WithEnv(a.env),
)
if svErr != nil {
return svErr
}
// setup FUSE FS Handler
sfs, err := spacefs.New(fsds.NewSpaceFSDataSource(
sv,
fsds.WithFilesDataSources(sv),
fsds.WithSharedWithMeDataSources(sv),
))
if err != nil {
log.Error("Failed to create space FUSE data source", err)
return err
}
fuseController := fuse.NewController(ctx, a.cfg, appStore, sfs)
if fuseController.ShouldMount() {
log.Info("Mounting FUSE Drive")
if err := fuseController.Mount(); err != nil {
log.Error("Mounting FUSE drive failed", err)
} else {
log.Info("Mounting FUSE Drive successful")
}
}
a.Run("FuseController", fuseController)
// setup gRPC Server
srv := grpc.New(
sv,
fuseController,
grpc.WithPort(a.cfg.GetInt(config.SpaceServerPort, 0)),
grpc.WithProxyPort(a.cfg.GetInt(config.SpaceProxyServerPort, 0)),
grpc.WithRestProxyPort(a.cfg.GetInt(config.SpaceRestProxyServerPort, 0)),
)
textileClient.AttachMailboxNotifier(srv)
err = a.RunAsync("BucketSync", bucketSync, func() error {
bucketSync.RegisterNotifier(srv)
return bucketSync.Start(ctx)
})
if err != nil {
return err
}
// start the gRPC server
err = a.RunAsync("gRPCServer", srv, func() error {
return srv.Start(ctx)
})
if err != nil {
return err
}
log.Info("Daemon ready")
// wait for interruption or done signal
select {
case <-interrupt:
log.Debug("Got interrupt signal")
// TODO: Track multiple interrupts in a goroutine to force exit for app.
break
case <-ctx.Done():
log.Debug("Got context done signal")
break
}
return a.Shutdown()
}
// Run registers this component to be cleaned up on Shutdown
func (a *App) Run(name string, component core.Component) {
log.Debug("Starting Component", "name:"+name)
a.components.Push(&componentMap{
name: name,
component: component,
})
}
// RunAsync performs the same function as Run() but also accepts an function to be run
// async to initialize the component.
func (a *App) RunAsync(name string, component core.AsyncComponent, fn func() error) error {
log.Debug("Starting Async Component", "name:"+name)
if a.eg == nil {
log.Warn("App.RunAsync() should be called after App.Start()")
return nil
}
errc := make(chan error)
a.eg.Go(func() error {
err := fn()
if err != nil {
errc <- err
}
return err
})
select {
case err := <-errc:
return err
case <-component.WaitForReady():
a.components.Push(&componentMap{
name: name,
component: component,
})
}
return nil
}
// Shutdown would perform a graceful shutdown of all components added through the
// Run() or RunAsync() functions
func (a *App) Shutdown() error {
log.Info("Daemon shutdown started")
for a.components.Len() > 0 {
m, ok := a.components.Pop().(*componentMap)
if ok {
log.Debug("Shutting down Component", fmt.Sprintf("name:%s", m.name))
if err := m.component.Shutdown(); err != nil {
log.Error(fmt.Sprintf("Error shutting down %s", m.name), err)
}
}
}
err := a.eg.Wait()
log.Info("Shutdown complete")
return err
}