-
Notifications
You must be signed in to change notification settings - Fork 116
/
main.go
463 lines (422 loc) · 12 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//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"
"io/ioutil"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"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/pkg/auth"
"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",
},
cli.StringFlag{
Name: "nodeid",
Usage: "Name of this node",
Value: "1",
},
cli.StringFlag{
Name: "clusterid",
Usage: "Cluster id",
Value: "openstorage.cluster",
},
}
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
}
var (
cfg *config.Config
)
// We are in daemon mode.
file := c.String("file")
if len(file) != 0 {
// Read from file
var err error
cfg, err = config.Parse(file)
if err != nil {
return err
}
} else {
cfg = &config.Config{}
}
// Check if values are set
if len(cfg.Osd.ClusterConfig.ClusterId) == 0 {
cfg.Osd.ClusterConfig.ClusterId = c.String("clusterid")
}
if len(cfg.Osd.ClusterConfig.NodeId) == 0 {
cfg.Osd.ClusterConfig.NodeId = c.String("nodeid")
}
// Get driver information
driverInfoList := c.StringSlice("driver")
if len(driverInfoList) != 0 {
if cfg.Osd.Drivers == nil {
cfg.Osd.Drivers = make(map[string]map[string]string)
}
params := make(map[string]string)
var name string
// many driver infos provided as a []string
for _, driverInfo := range driverInfoList {
// driverInfo of the format name=xxx,opt1=val1,opt2=val2
for _, pair := range strings.Split(driverInfo, ",") {
kv := strings.Split(pair, "=")
if len(kv) != 2 {
return fmt.Errorf("driver option has a an invalid pair %s", kv)
}
k := kv[0]
v := kv[1]
if len(k) == 0 || len(v) == 0 {
return fmt.Errorf("driver option '%s' is invalid", pair)
}
if k == "name" {
// Driver name
name = v
} else {
// Options for driver
params[k] = v
}
}
if len(name) == 0 {
return fmt.Errorf("driver option is missing driver name")
}
cfg.Osd.Drivers[name] = params
}
}
if len(cfg.Osd.Drivers) == 0 {
return fmt.Errorf("Must supply driver information")
}
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
}
sdksocket := fmt.Sprintf("/var/lib/osd/driver/%s-sdk.sock", d)
if err := server.StartPluginAPI(
d, sdksocket,
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 := os.Getenv("CSI_ENDPOINT")
if len(csisock) == 0 {
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
os.Remove(sdksocket)
sdkServer, err := sdk.New(&sdk.ServerConfig{
Net: "tcp",
Address: ":" + c.String("sdkport"),
RestPort: c.String("sdkrestport"),
Socket: sdksocket,
DriverName: d,
Cluster: cm,
Auth: setupAuth(),
Tls: setupSdkTls(),
})
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)
}
}
}
func setupAuth() *auth.JwtAuthConfig {
var err error
rsaFile := os.Getenv("OPENSTORAGE_AUTH_RSA_PUBKEY")
ecdsFile := os.Getenv("OPENSTORAGE_AUTH_ECDS_PUBKEY")
sharedsecret := os.Getenv("OPENSTORAGE_AUTH_SHAREDSECRET")
if len(rsaFile) == 0 &&
len(ecdsFile) == 0 &&
len(sharedsecret) == 0 {
return nil
}
authConfig := &auth.JwtAuthConfig{
SharedSecret: []byte(sharedsecret),
}
// Read RSA file
if len(rsaFile) != 0 {
authConfig.RsaPublicPem, err = ioutil.ReadFile(rsaFile)
if err != nil {
logrus.Errorf("Failed to read %s", rsaFile)
}
}
// Read Ecds file
if len(ecdsFile) != 0 {
authConfig.ECDSPublicPem, err = ioutil.ReadFile(ecdsFile)
if err != nil {
logrus.Errorf("Failed to read %s", ecdsFile)
}
}
return authConfig
}
func setupSdkTls() *sdk.TLSConfig {
certFile := os.Getenv("OPENSTORAGE_CERTFILE")
keyFile := os.Getenv("OPENSTORAGE_KEYFILE")
if len(certFile) != 0 && len(keyFile) != 0 {
logrus.Infof("TLS %s and %s", certFile, keyFile)
return &sdk.TLSConfig{
CertFile: certFile,
KeyFile: keyFile,
}
}
return nil
}