forked from GoogleCloudPlatform/gcsfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
396 lines (334 loc) · 9 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2015 Google Inc. 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.
// A fuse file system for Google Cloud Storage buckets.
//
// Usage:
//
// gcsfuse [flags] bucket mount_point
//
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"runtime/pprof"
"syscall"
"time"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"github.com/codegangsta/cli"
"github.com/googlecloudplatform/gcsfuse/internal/canned"
"github.com/jacobsa/daemonize"
"github.com/jacobsa/fuse"
"github.com/jacobsa/gcloud/gcs"
"github.com/jacobsa/syncutil"
"github.com/kardianos/osext"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
func registerSIGINTHandler(mountPoint string) {
// Register for SIGINT.
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
// Start a goroutine that will unmount when the signal is received.
go func() {
for {
<-signalChan
log.Println("Received SIGINT, attempting to unmount...")
err := fuse.Unmount(mountPoint)
if err != nil {
log.Printf("Failed to unmount in response to SIGINT: %v", err)
} else {
log.Printf("Successfully unmounted in response to SIGINT.")
return
}
}
}()
}
func handleCPUProfileSignals() {
profileOnce := func(duration time.Duration, path string) (err error) {
// Set up the file.
var f *os.File
f, err = os.Create(path)
if err != nil {
err = fmt.Errorf("Create: %v", err)
return
}
defer func() {
closeErr := f.Close()
if err == nil {
err = closeErr
}
}()
// Profile.
pprof.StartCPUProfile(f)
time.Sleep(duration)
pprof.StopCPUProfile()
return
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
for range c {
const path = "/tmp/cpu.pprof"
const duration = 10 * time.Second
log.Printf("Writing %v CPU profile to %s...", duration, path)
err := profileOnce(duration, path)
if err == nil {
log.Printf("Done writing CPU profile to %s.", path)
} else {
log.Printf("Error writing CPU profile: %v", err)
}
}
}
func handleMemoryProfileSignals() {
profileOnce := func(path string) (err error) {
// Trigger a garbage collection to get up to date information (cf.
// https://goo.gl/aXVQfL).
runtime.GC()
// Open the file.
var f *os.File
f, err = os.Create(path)
if err != nil {
err = fmt.Errorf("Create: %v", err)
return
}
defer func() {
closeErr := f.Close()
if err == nil {
err = closeErr
}
}()
// Dump to the file.
err = pprof.Lookup("heap").WriteTo(f, 0)
if err != nil {
err = fmt.Errorf("WriteTo: %v", err)
return
}
return
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR2)
for range c {
const path = "/tmp/mem.pprof"
err := profileOnce(path)
if err == nil {
log.Printf("Wrote memory profile to %s.", path)
} else {
log.Printf("Error writing memory profile: %v", err)
}
}
}
// Create token source from the JSON file at the supplide path.
func newTokenSourceFromPath(
path string,
scope string) (ts oauth2.TokenSource, err error) {
// Read the file.
contents, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("ReadFile(%q): %v", path, err)
return
}
// Create a config struct based on its contents.
jwtConfig, err := google.JWTConfigFromJSON(contents, scope)
if err != nil {
err = fmt.Errorf("JWTConfigFromJSON: %v", err)
return
}
// Create the token source.
ts = jwtConfig.TokenSource(context.Background())
return
}
func getConn(flags *flagStorage) (c gcs.Conn, err error) {
// Create the oauth2 token source.
const scope = gcs.Scope_FullControl
var tokenSrc oauth2.TokenSource
if flags.KeyFile != "" {
tokenSrc, err = newTokenSourceFromPath(flags.KeyFile, scope)
if err != nil {
err = fmt.Errorf("newTokenSourceFromPath: %v", err)
return
}
} else {
tokenSrc, err = google.DefaultTokenSource(context.Background(), scope)
if err != nil {
err = fmt.Errorf("DefaultTokenSource: %v", err)
return
}
}
// Create the connection.
const userAgent = "gcsfuse/0.0"
cfg := &gcs.ConnConfig{
TokenSource: tokenSrc,
UserAgent: userAgent,
}
if flags.DebugHTTP {
cfg.HTTPDebugLogger = log.New(os.Stdout, "http: ", 0)
}
if flags.DebugGCS {
cfg.GCSDebugLogger = log.New(os.Stdout, "gcs: ", log.Flags())
}
return gcs.NewConn(cfg)
}
////////////////////////////////////////////////////////////////////////
// main logic
////////////////////////////////////////////////////////////////////////
// Mount the file system according to arguments in the supplied context.
func mountWithArgs(
bucketName string,
mountPoint string,
flags *flagStorage,
mountStatus *log.Logger) (mfs *fuse.MountedFileSystem, err error) {
// Enable invariant checking if requested.
if flags.DebugInvariants {
syncutil.EnableInvariantChecking()
}
// Grab the connection.
//
// Special case: if we're mounting the fake bucket, we don't need an actual
// connection.
var conn gcs.Conn
if bucketName != canned.FakeBucketName {
mountStatus.Println("Opening GCS connection...")
conn, err = getConn(flags)
if err != nil {
err = fmt.Errorf("getConn: %v", err)
return
}
}
// Mount the file system.
mfs, err = mountWithConn(
context.Background(),
bucketName,
mountPoint,
flags,
conn,
mountStatus)
if err != nil {
err = fmt.Errorf("mountWithConn: %v", err)
return
}
return
}
func runCLIApp(c *cli.Context) (err error) {
flags := populateFlags(c)
// Extract arguments.
if len(c.Args()) != 2 {
err = fmt.Errorf(
"%s takes exactly two arguments. Run `%s --help` for more info.",
path.Base(os.Args[0]),
path.Base(os.Args[0]))
return
}
bucketName := c.Args()[0]
mountPoint := c.Args()[1]
// Canonicalize the mount point, making it absolute. This is important when
// daemonizing below, since the daemon will change its working directory
// before running this code again.
mountPoint, err = filepath.Abs(mountPoint)
if err != nil {
err = fmt.Errorf("canonicalizing mount point: %v", err)
return
}
fmt.Fprintf(os.Stdout, "Using mount point: %s\n", mountPoint)
// If we haven't been asked to run in foreground mode, we should run a daemon
// with the foreground flag set and wait for it to mount.
if !flags.Foreground {
// Find the executable.
var path string
path, err = osext.Executable()
if err != nil {
err = fmt.Errorf("osext.Executable: %v", err)
return
}
// Set up arguments. Be sure to use foreground mode, and to send along the
// potentially-modified mount point.
args := append([]string{"--foreground"}, os.Args[1:]...)
args[len(args)-1] = mountPoint
// Pass along PATH so that the daemon can find fusermount on Linux.
env := []string{
fmt.Sprintf("PATH=%s", os.Getenv("PATH")),
}
// Pass along GOOGLE_APPLICATION_CREDENTIALS, since we document in
// mounting.md that it can be used for specifying a key file.
if p, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS"); ok {
env = append(env, fmt.Sprintf("GOOGLE_APPLICATION_CREDENTIALS=%s", p))
}
// Run.
err = daemonize.Run(path, args, env, os.Stdout)
if err != nil {
err = fmt.Errorf("daemonize.Run: %v", err)
return
}
return
}
// Mount, writing information about our progress to the writer that package
// daemonize gives us and telling it about the outcome.
var mfs *fuse.MountedFileSystem
{
mountStatus := log.New(daemonize.StatusWriter, "", 0)
mfs, err = mountWithArgs(bucketName, mountPoint, flags, mountStatus)
if err == nil {
mountStatus.Println("File system has been successfully mounted.")
daemonize.SignalOutcome(nil)
} else {
err = fmt.Errorf("mountWithArgs: %v", err)
daemonize.SignalOutcome(err)
return
}
}
// Let the user unmount with Ctrl-C (SIGINT).
registerSIGINTHandler(mfs.Dir())
// Wait for the file system to be unmounted.
err = mfs.Join(context.Background())
if err != nil {
err = fmt.Errorf("MountedFileSystem.Join: %v", err)
return
}
return
}
func run() (err error) {
// Set up the app.
app := newApp()
var appErr error
app.Action = func(c *cli.Context) {
appErr = runCLIApp(c)
}
// Run it.
err = app.Run(os.Args)
if err != nil {
return
}
err = appErr
return
}
func main() {
// Make logging output better.
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
// Set up profiling handlers.
go handleCPUProfileSignals()
go handleMemoryProfileSignals()
// Run.
err := run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}