forked from rexray/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
138 lines (116 loc) · 3.53 KB
/
client.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
package client
import (
gofig "github.com/akutz/gofig/types"
log "github.com/sirupsen/logrus"
gocontext "golang.org/x/net/context"
"github.com/rexray/rexray/libstorage/api/context"
"github.com/rexray/rexray/libstorage/api/registry"
"github.com/rexray/rexray/libstorage/api/types"
"github.com/rexray/rexray/libstorage/api/utils"
apicnfg "github.com/rexray/rexray/libstorage/api/utils/config"
// load the config
_ "github.com/rexray/rexray/libstorage/imports/config"
// load the libStorage storage executors
_ "github.com/rexray/rexray/libstorage/imports/executors"
// load the libStorage storage driver
_ "github.com/rexray/rexray/libstorage/drivers/storage/libstorage"
)
type client struct {
config gofig.Config
sd types.StorageDriver
od types.OSDriver
id types.IntegrationDriver
ctx types.Context
api types.APIClient
xli types.StorageExecutorCLI
}
// New returns a new libStorage client.
func New(goCtx gocontext.Context, config gofig.Config) (types.Client, error) {
if goCtx == nil {
goCtx = context.Background()
}
ctx := context.New(goCtx)
if _, ok := context.PathConfig(ctx); !ok {
pathConfig := utils.NewPathConfig()
ctx = ctx.WithValue(context.PathConfigKey, pathConfig)
registry.ProcessRegisteredConfigs(ctx)
}
if config == nil {
var err error
if config, err = apicnfg.NewConfig(ctx); err != nil {
return nil, err
}
}
config = config.Scope(types.ConfigClient)
types.BackCompat(config)
var (
c *client
err error
)
c = &client{ctx: ctx, config: config}
c.ctx = c.ctx.WithValue(context.ClientKey, c)
logFields := log.Fields{}
logConfig, err := utils.ParseLoggingConfig(
config, logFields, types.ConfigClient)
if err != nil {
return nil, err
}
// always update the server context's log level
context.SetLogLevel(c.ctx, logConfig.Level)
c.ctx.WithFields(logFields).Info("configured logging")
if v := config.GetString(types.ConfigService); v != "" {
c.ctx = c.ctx.WithValue(context.ServiceKey, v)
c.ctx.WithField("serviceName", v).Info("set client service name")
}
storDriverName := config.GetString(types.ConfigStorageDriver)
if storDriverName == "" {
c.ctx.Warn("no storage driver found")
} else {
if c.sd, err = registry.NewStorageDriver(storDriverName); err != nil {
return nil, err
}
if err = c.sd.Init(c.ctx, config); err != nil {
return nil, err
}
if papi, ok := c.sd.(types.ProvidesAPIClient); ok {
c.api = papi.API()
}
if pxli, pxliOk := c.sd.(types.ProvidesStorageExecutorCLI); pxliOk {
c.xli = pxli.XCLI()
}
c.ctx.Info("storage driver initialized")
}
// if the API or XLI are nil, then the storage driver is not the libStorage
// storage driver, and we should jump avoid any more initialization
if c.api == nil || c.xli == nil {
c.ctx.Info("created libStorage client")
return c, nil
}
osDriverName := config.GetString(types.ConfigOSDriver)
if osDriverName == "" {
c.ctx.Warn("no os driver found")
} else {
if c.od, err = registry.NewOSDriver(osDriverName); err != nil {
return nil, err
}
if err = c.od.Init(c.ctx, config); err != nil {
return nil, err
}
c.ctx.Info("os driver initialized")
}
intDriverName := config.GetString(types.ConfigIntegrationDriver)
if intDriverName == "" {
c.ctx.Warn("no integration driver found")
} else {
if c.id, err = registry.NewIntegrationDriver(
intDriverName); err != nil {
return nil, err
}
if err := c.id.Init(c.ctx, config); err != nil {
return nil, err
}
c.ctx.Info("integration driver initialized")
}
c.ctx.Info("created libStorage client")
return c, nil
}