forked from coreos/torus
/
main.go
158 lines (138 loc) · 4.18 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
package main
import (
"fmt"
"os"
"github.com/coreos/pkg/capnslog"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/coreos/torus"
"github.com/coreos/torus/distributor"
"github.com/coreos/torus/internal/http"
// Register all the drivers.
_ "github.com/coreos/torus/metadata/etcd"
_ "github.com/coreos/torus/storage"
)
var (
etcdAddress string
localBlockSizeStr string
localBlockSize uint64
readCacheSizeStr string
readCacheSize uint64
readLevel string
writeLevel string
logpkg string
httpAddr string
cfg torus.Config
)
var rootCommand = &cobra.Command{
Use: "torusblk",
Short: "torus block volume tool",
Long: "Control block volumes on the torus distributed storage system",
PersistentPreRun: configureServer,
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
os.Exit(1)
},
}
var versionCommand = &cobra.Command{
Use: "version",
Short: "print version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("torusblk\nVersion: %s\n", torus.Version)
os.Exit(0)
},
}
func init() {
rootCommand.AddCommand(aoeCommand)
rootCommand.AddCommand(nbdCommand)
rootCommand.AddCommand(volumeCommand)
rootCommand.AddCommand(versionCommand)
// Flexvolume commands
rootCommand.AddCommand(initCommand)
rootCommand.AddCommand(attachCommand)
rootCommand.AddCommand(detachCommand)
rootCommand.AddCommand(mountCommand)
rootCommand.AddCommand(unmountCommand)
rootCommand.AddCommand(flexprepvolCommand)
rootCommand.PersistentFlags().StringVarP(&etcdAddress, "etcd", "C", "127.0.0.1:2379", "hostname:port to the etcd instance storing the metadata")
rootCommand.PersistentFlags().StringVarP(&localBlockSizeStr, "write-cache-size", "", "128MiB", "Maximum amount of memory to use for the local write cache")
rootCommand.PersistentFlags().StringVarP(&readCacheSizeStr, "read-cache-size", "", "50MiB", "Amount of memory to use for read cache")
rootCommand.PersistentFlags().StringVarP(&logpkg, "logpkg", "", "", "Specific package logging")
rootCommand.PersistentFlags().StringVarP(&readLevel, "read-level", "", "block", "Read replication level")
rootCommand.PersistentFlags().StringVarP(&writeLevel, "write-level", "", "all", "Write replication level")
rootCommand.PersistentFlags().StringVarP(&httpAddr, "http", "", "", "HTTP endpoint for debug and stats")
}
func configureServer(cmd *cobra.Command, args []string) {
capnslog.SetGlobalLogLevel(capnslog.NOTICE)
if logpkg != "" {
rl := capnslog.MustRepoLogger("github.com/coreos/torus")
llc, err := rl.ParseLogLevelConfig(logpkg)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing logpkg: %s\n", err)
os.Exit(1)
}
rl.SetLogLevel(llc)
}
var err error
readCacheSize, err = humanize.ParseBytes(readCacheSizeStr)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing read-cache-size: %s\n", err)
os.Exit(1)
}
localBlockSize, err = humanize.ParseBytes(localBlockSizeStr)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing write-cache-size: %s\n", err)
os.Exit(1)
}
var rl torus.ReadLevel
switch readLevel {
case "spread":
rl = torus.ReadSpread
case "seq":
rl = torus.ReadSequential
case "block":
rl = torus.ReadBlock
default:
fmt.Fprintf(os.Stderr, "invalid readlevel; use one of 'spread', 'seq', or 'block'")
os.Exit(1)
}
wl, err := torus.ParseWriteLevel(writeLevel)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
cfg = torus.Config{
StorageSize: localBlockSize,
MetadataAddress: etcdAddress,
ReadCacheSize: readCacheSize,
WriteLevel: wl,
ReadLevel: rl,
}
}
func createServer() *torus.Server {
srv, err := torus.NewServer(cfg, "etcd", "temp")
if err != nil {
fmt.Printf("Couldn't start: %s\n", err)
os.Exit(1)
}
err = distributor.OpenReplication(srv)
if err != nil {
fmt.Printf("Couldn't start: %s", err)
os.Exit(1)
}
if httpAddr != "" {
go http.ServeHTTP(httpAddr, srv)
}
return srv
}
func main() {
capnslog.SetGlobalLogLevel(capnslog.WARNING)
if err := rootCommand.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func die(why string, args ...interface{}) {
fmt.Fprintf(os.Stderr, why+"\n", args...)
os.Exit(1)
}