This repository has been archived by the owner on Sep 28, 2018. It is now read-only.
/
main.go
146 lines (125 loc) · 2.96 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
// etcdfs implements a read-only fuse filesystem for accessing etcd.
//
// It accesses an etcd cluster whose address is taken from the ETCD_ENDPOINTS
// environment variable, defaulting to http://localhost:4001/. It supports
// slicing the etcd keyspace, meaning that you can mount an arbitrary
// subdirectory of etcd.
//
// Usage:
// etcdfs [flags…] [<subdir>] <mountpoint>
//
// Flags:
// -allow_other
// Allow other users to access this filesystem
// -allow_root
// Allow root to access this filesystem
// -debug
// Enable debugging
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"path"
"strings"
"syscall"
"github.com/coreos/etcd/client"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
var (
debug = flag.Bool("debug", false, "Enable debugging")
allowOther = flag.Bool("allow_other", false, "Allow other users to access this filesystem")
allowRoot = flag.Bool("allow_root", false, "Allow root to access this filesystem")
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\t%s [flags…] [<subdir>] <mountpoint>\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
flag.Usage = usage
flag.Parse()
if *debug {
fuse.Debug = func(v interface{}) { log.Println("[fuse]", v) }
}
var subdir, mountpoint string
switch flag.NArg() {
case 1:
subdir = "/"
mountpoint = flag.Arg(0)
case 2:
subdir = path.Join("/", flag.Arg(0))
mountpoint = flag.Arg(1)
default:
usage()
os.Exit(1)
}
var endpoints []string
if ep := os.Getenv("ETCD_ENDPOINTS"); ep != "" {
endpoints = strings.Split(ep, ",")
} else {
endpoints = []string{"localhost:4001"}
}
log.Printf("Using endpoints %v", endpoints)
cfg := client.Config{
Endpoints: endpoints,
}
etcd, err := client.New(cfg)
if err != nil {
return err
}
var mountOpts []fuse.MountOption
if *allowOther {
mountOpts = append(mountOpts, fuse.AllowOther())
}
if *allowRoot {
mountOpts = append(mountOpts, fuse.AllowRoot())
}
mountOpts = append(mountOpts, fuse.DefaultPermissions())
mountOpts = append(mountOpts, fuse.FSName("etcd:"+subdir))
mountOpts = append(mountOpts, fuse.ReadOnly())
mountOpts = append(mountOpts, fuse.Subtype("etcdFS"))
log.Printf("Mounting etcd:%s to %s", subdir, mountpoint)
c, err := fuse.Mount(
mountpoint,
mountOpts...,
)
if err != nil {
return err
}
defer c.Close()
srv := fs.New(c, nil)
filesys := &etcdFS{
etcd: client.NewKeysAPI(etcd),
base: subdir,
}
errch := make(chan error)
log.Printf("Start serving")
go func() {
errch <- srv.Serve(filesys)
}()
<-c.Ready
if c.MountError != nil {
return c.MountError
}
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGHUP, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
select {
case err := <-errch:
return err
case s := <-sigs:
log.Printf("Caught signal: %v", s)
err := c.Close()
log.Printf("Error: %v", err)
return err
}
}