/
mount.go
131 lines (104 loc) · 2.33 KB
/
mount.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
package command
import (
"flag"
"fmt"
"os"
"strings"
"syscall"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/funkygao/gafka/cmd/gk/command/kfs"
"github.com/funkygao/gafka/ctx"
"github.com/funkygao/gocli"
"github.com/funkygao/golib/signal"
log "github.com/funkygao/log4go"
)
type Mount struct {
Ui cli.Ui
Cmd string
zone string
cluster string
logLevel string
mountPoint string
}
func (this *Mount) Run(args []string) (exitCode int) {
cmdFlags := flag.NewFlagSet("mount", flag.ContinueOnError)
cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
cmdFlags.StringVar(&this.zone, "z", ctx.ZkDefaultZone(), "")
cmdFlags.StringVar(&this.cluster, "c", "", "")
cmdFlags.StringVar(&this.logLevel, "l", "info", "")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
if validateArgs(this, this.Ui).
on("-z", "-c").
invalid(args) {
return 2
}
this.mountPoint = args[len(args)-1]
if !strings.HasPrefix(this.mountPoint, "/") {
this.Ui.Error("mount point must start with /")
return 1
}
setupLogging("stdout", this.logLevel, "")
c, err := fuse.Mount(
this.mountPoint,
fuse.FSName("kfs"),
fuse.Subtype("kfs"),
fuse.VolumeName("Kafka FS"),
fuse.ReadOnly(),
fuse.AllowOther(),
)
if err != nil {
log.Critical(err)
}
signal.RegisterHandler(func(sig os.Signal) {
var err error
for i := 0; i < 5; i++ {
err = fuse.Unmount(this.mountPoint)
if err == nil {
break
}
log.Warn(err)
time.Sleep(time.Second * 5)
}
if err == nil {
log.Info("Kafka FS unmounted")
} else {
log.Error("Kafka FS unable to umount")
}
c.Close()
os.Exit(0)
}, syscall.SIGINT, syscall.SIGTERM)
srv := fs.New(c, &fs.Config{})
fs := kfs.New(this.zone, this.cluster)
if err := srv.Serve(fs); err != nil {
log.Error(err)
}
<-c.Ready
if err := c.MountError; err != nil {
log.Error(err)
}
return
}
func (*Mount) Synopsis() string {
return "A FUSE module to mount a Kafka cluster in the filesystem"
}
func (this *Mount) Help() string {
help := fmt.Sprintf(`
Usage: %s mount [options] mount_point
%s
Pre-requisites:
yum install -y fuse
modprobe fuse
lsmod | grep fuse
Options:
-z zone
Default '%s'.
-c cluster
-l log level
Default 'info'.
`, this.Cmd, this.Synopsis(), ctx.ZkDefaultZone())
return strings.TrimSpace(help)
}