-
Notifications
You must be signed in to change notification settings - Fork 116
/
main.go
336 lines (309 loc) · 9.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//go:generate swagger generate spec -m -o ../../api/swagger/swagger.json
// Package classification OSD API.
//
// OpenStorage is a clustered implementation of the Open Storage specification and relies on the OCI runtime.
// It allows you to run stateful services in containers in a multi-host clustered environment.
// This document represents the API documentaton of Openstorage, for the GO client please visit:
// https://github.com/libopenstorage/openstorage
//
// Schemes: http, https
// Host: localhost
// BasePath: /v1
// Version: 2.0.0
// License: APACHE2 https://opensource.org/licenses/Apache-2.0
// Contact: https://github.com/libopenstorage/openstorage
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// swagger:meta
package main
import (
"fmt"
"net/url"
"os"
"runtime"
"strconv"
"github.com/sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/docker/docker/pkg/reexec"
"github.com/libopenstorage/openstorage/api"
"github.com/libopenstorage/openstorage/api/flexvolume"
"github.com/libopenstorage/openstorage/api/server"
"github.com/libopenstorage/openstorage/api/server/sdk"
osdcli "github.com/libopenstorage/openstorage/cli"
"github.com/libopenstorage/openstorage/cluster"
clustermanager "github.com/libopenstorage/openstorage/cluster/manager"
"github.com/libopenstorage/openstorage/config"
"github.com/libopenstorage/openstorage/csi"
"github.com/libopenstorage/openstorage/graph/drivers"
"github.com/libopenstorage/openstorage/objectstore"
"github.com/libopenstorage/openstorage/schedpolicy"
"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: "",
},
cli.StringFlag{
Name: "sdkport",
Usage: "gRPC port for SDK. Example: 9100",
Value: "9100",
},
cli.StringFlag{
Name: "sdkrestport",
Usage: "gRPC REST Gateway port for SDK. Example: 9110",
Value: "9110",
},
}
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, logrus.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 != "" {
logrus.Infof("OSD enabling cluster mode.")
if err := clustermanager.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 {
logrus.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
}
// Start CSI Server for this driver
csisock := fmt.Sprintf("/var/lib/osd/driver/%s-csi.sock", d)
os.Remove(csisock)
cm, err := clustermanager.Inst()
if err != nil {
return fmt.Errorf("Unable to find cluster instance: %v", err)
}
csiServer, err := csi.NewOsdCsiServer(&csi.OsdCsiServerConfig{
Net: "unix",
Address: csisock,
DriverName: d,
Cluster: cm,
})
if err != nil {
return fmt.Errorf("Failed to start CSI server for driver %s: %v", d, err)
}
csiServer.Start()
// Start SDK Server for this driver
sdkServer, err := sdk.New(&sdk.ServerConfig{
Net: "tcp",
Address: ":" + c.String("sdkport"),
RestPort: c.String("sdkrestport"),
DriverName: d,
Cluster: cm,
})
if err != nil {
return fmt.Errorf("Failed to start SDK server for driver %s: %v", d, err)
}
sdkServer.Start()
}
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 {
logrus.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 := clustermanager.Inst()
if err != nil {
return fmt.Errorf("Unable to find cluster instance: %v", err)
}
if err := cm.StartWithConfiguration(
0,
false,
"9002",
&cluster.ClusterServerConfiguration{
ConfigSchedManager: schedpolicy.NewFakeScheduler(),
ConfigObjectStoreManager: objectstore.NewfakeObjectstore(),
},
); 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 {
logrus.Warnln(err.Error())
os.Exit(1)
}
}
}