-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatblobfs.go
96 lines (81 loc) · 2.72 KB
/
flatblobfs.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
// Implementation of FUSE's file system on top of Azure blob storage.
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
"github.com/ppanyukov/azure-sdk-for-go/storage"
"github.com/ppanyukov/azurefs-fuse/blobfs"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] MOUNTPOINT\n", os.Args[0])
fmt.Fprintf(os.Stderr, "The flags are:\n")
flag.PrintDefaults()
}
func main() {
// TODO(ppanyukov): too much args parsing, is there a better saner way?
// TODO(ppanyukov): better and more secure way of passing the account/key than ags/env vars?
// zap sensitive vars early so nobody can grab them via /proc/pid/environ
// This may actually not work, because according to docs:
// setenv_c and unsetenv_c are provided by the runtime but are no-ops
// if cgo isn't loaded.
// At least it makes them inaccessible in go.
var (
envAccountName string = os.Getenv("AZURE_STORAGE_ACCOUNT_NAME")
envAccountKey string = os.Getenv("AZURE_STORAGE_ACCOUNT_KEY")
envAccountContainer string = os.Getenv("AZURE_STORAGE_ACCOUNT_CONTAINER")
)
os.Clearenv()
var (
isTrace bool
accountName string
accountKey string
accountContainer string
mountPoint string
)
// Use custom usage printer.
flag.Usage = usage
flag.StringVar(&accountName, "accountName", "", "REQUIRED. Azure storage account name. Or use AZURE_STORAGE_ACCOUNT_NAME env var.")
flag.StringVar(&accountKey, "accountKey", "", "REQUIRED. Azure storage account key. Or use AZURE_STORAGE_ACCOUNT_KEY env var.")
flag.StringVar(&accountContainer, "accountContainer", "", "REQUIRED. Azure storage account container name. Or use AZURE_STORAGE_ACCOUNT_CONTAINER env var.")
flag.BoolVar(&isTrace, "trace", false, "OPTIONAL. Specify true to trace calls.")
flag.Parse()
if accountName == "" {
accountName = envAccountName
}
if accountKey == "" {
accountKey = envAccountKey
}
if accountContainer == "" {
accountContainer = envAccountContainer
}
if len(flag.Args()) > 0 {
mountPoint = flag.Arg(0)
}
if accountName == "" || accountKey == "" || mountPoint == "" {
flag.Usage()
os.Exit(1)
}
// good to go
fmt.Printf("OK. Will mount storage account '%s' at '%s'", accountName, mountPoint)
storageClient, err := storage.NewBasicClient(accountName, accountKey)
if err != nil {
log.Fatal("ERROR", err)
}
var fs pathfs.FileSystem
flatBlobFs := blobfs.NewFlatBlobFs(accountContainer, storageClient)
if isTrace {
fs = blobfs.NewTraceFs(flatBlobFs)
} else {
fs = flatBlobFs
}
nfs := pathfs.NewPathNodeFs(fs, nil)
server, _, err := nodefs.MountRoot(mountPoint, nfs.Root(), nil)
if err != nil {
log.Fatalf("Mount fail: %v\n", err)
}
server.Serve()
}