-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshare.go
136 lines (126 loc) · 2.76 KB
/
share.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
package main
import (
"encoding/json"
"fmt"
"github.com/dreyk/s3share/pkg/share"
"github.com/dreyk/s3share/pkg/util"
"log/syslog"
"os"
"strings"
"syscall"
)
var slog *syslog.Writer
func main() {
args := os.Args
if len(args) < 2 {
log("unknown", ResultStatus{
Status: util.Failure,
Message: fmt.Sprintf("Wrong args number: %d", len(args)-1),
})
os.Exit(-1)
}
var err error
slog, err = syslog.New(syslog.LOG_WARNING|syslog.LOG_DAEMON, "kuberlab-share")
if err != nil {
panic(err)
}
switch args[1] {
case "mount":
if len(args) < 4 {
log("mount", ResultStatus{
Status: util.Failure,
Message: fmt.Sprintf("Wrong args number: %d", len(args)-1),
})
os.Exit(-1)
}
mount(args[2], args[3])
case "init":
log("init", ResultStatus{
Status: util.Success,
Capabilities: map[string]interface{}{"attach": false},
})
case "unmount":
if len(args) < 3 {
log("unmount", ResultStatus{
Status: util.Failure,
Message: fmt.Sprintf("Wrong args number: %d", len(args)-1),
})
os.Exit(-1)
}
unmount(args[2])
default:
log(args[1], ResultStatus{
Status: util.NotSupported,
})
}
}
type ResultStatus struct {
Status string `json:"status"`
Message string `json:"message"`
Capabilities map[string]interface{} `json:"capabilities"`
}
func mount(path string, conf string) {
slog.Info(fmt.Sprintf("Mount request '%s'", path))
s := getShare("mount", conf)
err := s.Mount(path)
if err != nil {
log("mount", ResultStatus{
Status: util.Failure,
Message: err.Error(),
})
os.Exit(1)
}
log("mount", ResultStatus{
Status: util.Success,
})
}
func unmount(path string) {
slog.Info(fmt.Sprintf("Unmount request '%s'", path))
err := syscall.Unmount(path, 0)
if err != nil {
log("unmount", ResultStatus{
Status: util.Failure,
Message: err.Error(),
})
os.Exit(1)
}
log("unmount", ResultStatus{
Status: util.Success,
})
}
func getShare(command string, conf string) share.Share {
c, err := getConf(conf)
if err != nil {
log(command, ResultStatus{
Status: util.Failure,
Message: err.Error(),
})
os.Exit(1)
}
s, err := share.NewShare(slog, c)
if err != nil {
log(command, ResultStatus{
Status: util.Failure,
Message: err.Error(),
})
os.Exit(1)
}
return s
}
func log(command string, res ResultStatus) {
slog.Info(fmt.Sprintf("Command '%s' result '%s' message: %s", command, res.Status, res.Message))
log0(res)
}
func log0(v interface{}) {
enc := json.NewEncoder(os.Stdout)
enc.Encode(v)
}
func getConf(s string) (map[string]interface{}, error) {
dec := json.NewDecoder(strings.NewReader(s))
var c map[string]interface{}
err := dec.Decode(&c)
if err != nil {
return nil, fmt.Errorf("Decode share param failed: %v", err)
}
return c, nil
}