-
Notifications
You must be signed in to change notification settings - Fork 116
/
main.go
259 lines (235 loc) · 6.83 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"fmt"
"net/url"
"os"
"runtime"
"strconv"
"go.pedge.io/dlog"
"github.com/codegangsta/cli"
"github.com/docker/docker/pkg/reexec"
"github.com/libopenstorage/openstorage/api"
"github.com/libopenstorage/openstorage/api/server"
"github.com/libopenstorage/openstorage/api/flexvolume"
osdcli "github.com/libopenstorage/openstorage/cli"
"github.com/libopenstorage/openstorage/cluster"
"github.com/libopenstorage/openstorage/config"
"github.com/libopenstorage/openstorage/graph/drivers"
"github.com/libopenstorage/openstorage/volume"
"github.com/libopenstorage/openstorage/volume/drivers"
"github.com/portworx/kvdb"
"github.com/portworx/kvdb/consul"
etcd "github.com/portworx/kvdb/etcd/v2"
"github.com/portworx/kvdb/mem"
)
var (
datastores = []string{mem.Name, etcd.Name, consul.Name}
)
func main() {
if reexec.Init() {
return
}
app := cli.NewApp()
app.Name = "osd"
app.Usage = "Open Storage CLI"
app.Version = config.Version
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "json,j",
Usage: "output in json",
},
cli.BoolFlag{
Name: osdcli.DaemonAlias,
Usage: "Start OSD in daemon mode",
},
cli.StringSliceFlag{
Name: "driver",
Usage: "driver name and options: name=btrfs,home=/var/openstorage/btrfs",
Value: new(cli.StringSlice),
},
cli.StringFlag{
Name: "kvdb,k",
Usage: "uri to kvdb e.g. kv-mem://localhost, etcd-kv://localhost:4001, consul-kv://localhost:8500",
Value: "kv-mem://localhost",
},
cli.StringFlag{
Name: "file,f",
Usage: "file to read the OSD configuration from.",
Value: "",
},
}
app.Action = wrapAction(start)
app.Commands = []cli.Command{
{
Name: "driver",
Aliases: []string{"d"},
Usage: "Manage drivers",
Subcommands: osdcli.DriverCommands(),
},
{
Name: "cluster",
Aliases: []string{"c"},
Usage: "Manage cluster",
Subcommands: osdcli.ClusterCommands(),
},
{
Name: "version",
Aliases: []string{"v"},
Usage: "Display version",
Action: wrapAction(showVersion),
},
}
// Register all volume drivers with the CLI.
for _, v := range volumedrivers.AllDrivers {
// TODO(pedge): was an and, but we have no drivers that have two types
switch v.DriverType {
case api.DriverType_DRIVER_TYPE_BLOCK:
bCmds := osdcli.BlockVolumeCommands(v.Name)
cmds := append(bCmds)
c := cli.Command{
Name: v.Name,
Usage: fmt.Sprintf("Manage %s storage", v.Name),
Subcommands: cmds,
}
app.Commands = append(app.Commands, c)
case api.DriverType_DRIVER_TYPE_FILE:
fCmds := osdcli.FileVolumeCommands(v.Name)
cmds := append(fCmds)
c := cli.Command{
Name: v.Name,
Usage: fmt.Sprintf("Manage %s volumes", v.Name),
Subcommands: cmds,
}
app.Commands = append(app.Commands, c)
}
}
// Register all graph drivers with the CLI.
for _, v := range graphdrivers.AllDrivers {
// TODO(pedge): was an and, but we have no drivers that have two types
switch v.DriverType {
case api.DriverType_DRIVER_TYPE_GRAPH:
cmds := osdcli.GraphDriverCommands(v.Name)
c := cli.Command{
Name: v.Name,
Usage: fmt.Sprintf("Manage %s graph storage", v.Name),
Subcommands: cmds,
}
app.Commands = append(app.Commands, c)
}
}
app.Run(os.Args)
}
func start(c *cli.Context) error {
if !osdcli.DaemonMode(c) {
cli.ShowAppHelp(c)
return nil
}
// We are in daemon mode.
file := c.String("file")
if file == "" {
return fmt.Errorf("OSD configuration file not specified. Visit openstorage.org for an example.")
}
cfg, err := config.Parse(file)
if err != nil {
return err
}
kvdbURL := c.String("kvdb")
u, err := url.Parse(kvdbURL)
scheme := u.Scheme
u.Scheme = "http"
kv, err := kvdb.New(scheme, "openstorage", []string{u.String()}, nil, dlog.Panicf)
if err != nil {
return fmt.Errorf("Failed to initialize KVDB: %v (%v)\nSupported datastores: %v", scheme, err, datastores)
}
if err := kvdb.SetInstance(kv); err != nil {
return fmt.Errorf("Failed to initialize KVDB: %v", err)
}
// Start the cluster state machine, if enabled.
clusterInit := false
if cfg.Osd.ClusterConfig.NodeId != "" && cfg.Osd.ClusterConfig.ClusterId != "" {
dlog.Infof("OSD enabling cluster mode.")
if err := cluster.Init(cfg.Osd.ClusterConfig); err != nil {
return fmt.Errorf("Unable to init cluster server: %v", err)
}
if err := server.StartClusterAPI(cluster.APIBase, 0); err != nil {
return fmt.Errorf("Unable to start cluster API server: %v", err)
}
clusterInit = true
}
isDefaultSet := false
// Start the volume drivers.
for d, v := range cfg.Osd.Drivers {
dlog.Infof("Starting volume driver: %v", d)
if err := volumedrivers.Register(d, v); err != nil {
return fmt.Errorf("Unable to start volume driver: %v, %v", d, err)
}
var mgmtPort, pluginPort uint64
if port, ok := v[config.MgmtPortKey]; ok {
mgmtPort, err = strconv.ParseUint(port, 10, 16)
if err != nil {
return fmt.Errorf("Invalid OSD Config File. Invalid Mgmt Port number for Driver : %s", d)
}
} else {
mgmtPort = 0
}
if port, ok := v[config.PluginPortKey]; ok {
pluginPort, err = strconv.ParseUint(port, 10, 16)
if err != nil {
return fmt.Errorf("Invalid OSD Config File. Invalid Plugin Port number for Driver : %s", d)
}
} else {
pluginPort = 0
}
if err := server.StartPluginAPI(
d,
volume.DriverAPIBase,
volume.PluginAPIBase,
uint16(mgmtPort),
uint16(pluginPort),
); err != nil {
return fmt.Errorf("Unable to start volume plugin: %v", err)
}
if d != "" && cfg.Osd.ClusterConfig.DefaultDriver == d {
isDefaultSet = true
}
}
if cfg.Osd.ClusterConfig.DefaultDriver != "" && !isDefaultSet {
return fmt.Errorf("Invalid OSD config file: Default Driver specified but driver not initialized")
}
if err := flexvolume.StartFlexVolumeAPI(config.FlexVolumePort, cfg.Osd.ClusterConfig.DefaultDriver); err != nil {
return fmt.Errorf("Unable to start flexvolume API: %v", err)
}
// Start the graph drivers.
for d := range cfg.Osd.GraphDrivers {
dlog.Infof("Starting graph driver: %v", d)
if err := server.StartGraphAPI(d, volume.PluginAPIBase); err != nil {
return fmt.Errorf("Unable to start graph plugin: %v", err)
}
}
if clusterInit {
cm, err := cluster.Inst()
if err != nil {
return fmt.Errorf("Unable to find cluster instance: %v", err)
}
if err := cm.Start(0, false); err != nil {
return fmt.Errorf("Unable to start cluster manager: %v", err)
}
}
// Daemon does not exit.
select {}
}
func showVersion(c *cli.Context) error {
fmt.Println("OSD Version:", config.Version)
fmt.Println("Go Version:", runtime.Version())
fmt.Println("OS:", runtime.GOOS)
fmt.Println("Arch:", runtime.GOARCH)
return nil
}
func wrapAction(f func(*cli.Context) error) func(*cli.Context) {
return func(c *cli.Context) {
if err := f(c); err != nil {
dlog.Warnln(err.Error())
os.Exit(1)
}
}
}