forked from daos-stack/daos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
433 lines (364 loc) · 13.3 KB
/
pool.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
//
// (C) Copyright 2019-2020 Intel Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
// The Government's rights to use, modify, reproduce, release, perform, display,
// or disclose this software are subject to the terms of the Apache License as
// provided in Contract No. 8F-30005.
// Any reproduction of computer software, computer software documentation, or
// portions thereof marked with this legend must also reproduce the markings.
//
package main
import (
"fmt"
"os"
"os/user"
"strconv"
"strings"
"github.com/dustin/go-humanize"
"github.com/pkg/errors"
"github.com/daos-stack/daos/src/control/client"
"github.com/daos-stack/daos/src/control/common"
)
const (
msgSizeNoNumber = "size string doesn't specify a number"
msgSizeZeroScm = "non-zero scm size is required"
maxNumSvcReps = 13
)
// PoolCmd is the struct representing the top-level pool subcommand.
type PoolCmd struct {
Create PoolCreateCmd `command:"create" alias:"c" description:"Create a DAOS pool"`
Destroy PoolDestroyCmd `command:"destroy" alias:"d" description:"Destroy a DAOS pool"`
List systemListPoolsCmd `command:"list" alias:"l" description:"List DAOS pools"`
Query PoolQueryCmd `command:"query" alias:"q" description:"Query a DAOS pool"`
GetACL PoolGetACLCmd `command:"get-acl" alias:"ga" description:"Get a DAOS pool's Access Control List"`
OverwriteACL PoolOverwriteACLCmd `command:"overwrite-acl" alias:"oa" description:"Overwrite a DAOS pool's Access Control List"`
UpdateACL PoolUpdateACLCmd `command:"update-acl" alias:"ua" description:"Update entries in a DAOS pool's Access Control List"`
DeleteACL PoolDeleteACLCmd `command:"delete-acl" alias:"da" description:"Delete an entry from a DAOS pool's Access Control List"`
SetProp PoolSetPropCmd `command:"set-prop" alias:"sp" description:"Set pool property"`
}
// PoolCreateCmd is the struct representing the command to create a DAOS pool.
type PoolCreateCmd struct {
logCmd
connectedCmd
GroupName string `short:"g" long:"group" description:"DAOS pool to be owned by given group, format name@domain"`
UserName string `short:"u" long:"user" description:"DAOS pool to be owned by given user, format name@domain"`
ACLFile string `short:"a" long:"acl-file" description:"Access Control List file path for DAOS pool"`
ScmSize string `short:"s" long:"scm-size" required:"1" description:"Size of SCM component of DAOS pool"`
NVMeSize string `short:"n" long:"nvme-size" description:"Size of NVMe component of DAOS pool"`
RankList string `short:"r" long:"ranks" description:"Storage server unique identifiers (ranks) for DAOS pool"`
NumSvcReps uint32 `short:"v" long:"nsvc" default:"1" description:"Number of pool service replicas"`
Sys string `short:"S" long:"sys" default:"daos_server" description:"DAOS system that pool is to be a part of"`
}
// Execute is run when PoolCreateCmd subcommand is activated
func (c *PoolCreateCmd) Execute(args []string) error {
msg := "SUCCEEDED: "
scmBytes, err := humanize.ParseBytes(c.ScmSize)
if err != nil {
return errors.Wrap(err, "pool SCM size")
}
var nvmeBytes uint64
if c.NVMeSize != "" {
nvmeBytes, err = humanize.ParseBytes(c.NVMeSize)
if err != nil {
return errors.Wrap(err, "pool NVMe size")
}
}
var acl *client.AccessControlList
if c.ACLFile != "" {
acl, err = readACLFile(c.ACLFile)
if err != nil {
return err
}
}
if c.NumSvcReps > maxNumSvcReps {
return errors.Errorf("max number of service replicas is %d, got %d",
maxNumSvcReps, c.NumSvcReps)
}
usr, grp, err := formatNameGroup(c.UserName, c.GroupName)
if err != nil {
return errors.WithMessage(err, "formatting user/group strings")
}
ranks, err := common.ParseInts(c.RankList)
if err != nil {
return errors.WithMessage(err, "parsing rank list")
}
req := &client.PoolCreateReq{
ScmBytes: scmBytes, NvmeBytes: nvmeBytes, RankList: ranks,
NumSvcReps: c.NumSvcReps, Sys: c.Sys, Usr: usr, Grp: grp, ACL: acl,
}
resp, err := c.conns.PoolCreate(req)
if err != nil {
msg = errors.WithMessage(err, "FAILED").Error()
} else {
msg += fmt.Sprintf("UUID: %s, Service replicas: %s",
resp.UUID, formatPoolSvcReps(resp.SvcReps))
}
c.log.Infof("Pool-create command %s\n", msg)
return err
}
// formatNameGroup converts system names to principal and if both user and group
// are unspecified, takes effective user name and that user's primary group.
func formatNameGroup(usr string, grp string) (string, string, error) {
if usr == "" && grp == "" {
eUsr, err := user.Current()
if err != nil {
return "", "", err
}
eGrp, err := user.LookupGroupId(eUsr.Gid)
if err != nil {
return "", "", err
}
usr, grp = eUsr.Username, eGrp.Name
}
if usr != "" && !strings.Contains(usr, "@") {
usr += "@"
}
if grp != "" && !strings.Contains(grp, "@") {
grp += "@"
}
return usr, grp, nil
}
// PoolDestroyCmd is the struct representing the command to destroy a DAOS pool.
type PoolDestroyCmd struct {
logCmd
connectedCmd
// TODO: implement --sys & --svc options (currently unsupported server side)
UUID string `long:"pool" required:"1" description:"UUID of DAOS pool to destroy"`
Force bool `short:"f" long:"force" description:"Force removal of DAOS pool"`
}
// Execute is run when PoolDestroyCmd subcommand is activated
func (d *PoolDestroyCmd) Execute(args []string) error {
msg := "succeeded"
req := &client.PoolDestroyReq{UUID: d.UUID, Force: d.Force}
err := d.conns.PoolDestroy(req)
if err != nil {
msg = errors.WithMessage(err, "failed").Error()
}
d.log.Infof("Pool-destroy command %s\n", msg)
return err
}
// PoolQueryCmd is the struct representing the command to destroy a DAOS pool.
type PoolQueryCmd struct {
logCmd
connectedCmd
UUID string `long:"pool" required:"1" description:"UUID of DAOS pool to query"`
}
// Execute is run when PoolQueryCmd subcommand is activated
func (c *PoolQueryCmd) Execute(args []string) error {
req := client.PoolQueryReq{
UUID: c.UUID,
}
resp, err := c.conns.PoolQuery(req)
if err != nil {
return errors.Wrap(err, "pool query failed")
}
// Maintain output compability with the `daos pool query` output.
var bld strings.Builder
fmt.Fprintf(&bld, "Pool %s, ntarget=%d, disabled=%d\n",
resp.UUID, resp.TotalTargets, resp.DisabledTargets)
bld.WriteString("Pool space info:\n")
fmt.Fprintf(&bld, "- Target(VOS) count:%d\n", resp.ActiveTargets)
if resp.Scm != nil {
bld.WriteString("- SCM:\n")
fmt.Fprintf(&bld, " Total size: %s\n", humanize.Bytes(resp.Scm.Total))
fmt.Fprintf(&bld, " Free: %s, min:%s, max:%s, mean:%s\n",
humanize.Bytes(resp.Scm.Free), humanize.Bytes(resp.Scm.Min),
humanize.Bytes(resp.Scm.Max), humanize.Bytes(resp.Scm.Mean))
}
if resp.Nvme != nil {
bld.WriteString("- NVMe:\n")
fmt.Fprintf(&bld, " Total size: %s\n", humanize.Bytes(resp.Nvme.Total))
fmt.Fprintf(&bld, " Free: %s, min:%s, max:%s, mean:%s\n",
humanize.Bytes(resp.Nvme.Free), humanize.Bytes(resp.Nvme.Min),
humanize.Bytes(resp.Nvme.Max), humanize.Bytes(resp.Nvme.Mean))
}
if resp.Rebuild != nil {
if resp.Rebuild.Status == 0 {
fmt.Fprintf(&bld, "Rebuild %s, %d objs, %d recs\n",
resp.Rebuild.State, resp.Rebuild.Objects, resp.Rebuild.Records)
} else {
fmt.Fprintf(&bld, "Rebuild failed, rc=%d, status=%d", resp.Status, resp.Rebuild.Status)
}
}
c.log.Info(bld.String())
return nil
}
// PoolSetPropCmd represents the command to set a property on a pool.
type PoolSetPropCmd struct {
logCmd
connectedCmd
UUID string `long:"pool" required:"1" description:"UUID of DAOS pool"`
Property string `short:"n" long:"name" required:"1" description:"Name of property to be set"`
Value string `short:"v" long:"value" required:"1" description:"Value of property to be set"`
}
// Execute is run when PoolSetPropCmd subcommand is activated.
func (c *PoolSetPropCmd) Execute(_ []string) error {
req := client.PoolSetPropReq{
UUID: c.UUID,
Property: c.Property,
}
req.SetString(c.Value)
if numVal, err := strconv.ParseUint(c.Value, 10, 64); err == nil {
req.SetNumber(numVal)
}
resp, err := c.conns.PoolSetProp(req)
if err != nil {
return errors.Wrap(err, "pool set-prop failed")
}
c.log.Infof("pool set-prop succeeded (%s=%q)", resp.Property, resp.Value)
return nil
}
// PoolGetACLCmd represents the command to fetch an Access Control List of a
// DAOS pool.
type PoolGetACLCmd struct {
logCmd
connectedCmd
UUID string `long:"pool" required:"1" description:"UUID of DAOS pool"`
File string `short:"o" long:"outfile" required:"0" description:"Output ACL to file"`
Force bool `short:"f" long:"force" required:"0" description:"Allow to clobber output file"`
Verbose bool `short:"v" long:"verbose" required:"0" description:"Add descriptive comments to ACL entries"`
}
// Execute is run when the PoolGetACLCmd subcommand is activated
func (d *PoolGetACLCmd) Execute(args []string) error {
req := client.PoolGetACLReq{UUID: d.UUID}
resp, err := d.conns.PoolGetACL(req)
if err != nil {
d.log.Infof("Pool-get-ACL command failed: %s\n", err.Error())
return err
}
d.log.Debugf("Pool-get-ACL command succeeded, UUID: %s\n", d.UUID)
acl := formatACL(resp.ACL, d.Verbose)
if d.File != "" {
err = d.writeACLToFile(acl)
if err != nil {
return err
}
d.log.Infof("Wrote ACL to output file: %s", d.File)
} else {
d.log.Info(acl)
}
return nil
}
func (d *PoolGetACLCmd) writeACLToFile(acl string) error {
if !d.Force {
// Keep the user from clobbering existing files
_, err := os.Stat(d.File)
if err == nil {
return errors.New(fmt.Sprintf("file already exists: %s", d.File))
}
}
f, err := os.Create(d.File)
if err != nil {
d.log.Errorf("Unable to create file: %s", d.File)
return err
}
defer f.Close()
_, err = f.WriteString(acl)
if err != nil {
d.log.Errorf("Failed to write to file: %s", d.File)
return err
}
return nil
}
// PoolOverwriteACLCmd represents the command to overwrite the Access Control
// List of a DAOS pool.
type PoolOverwriteACLCmd struct {
logCmd
connectedCmd
UUID string `long:"pool" required:"1" description:"UUID of DAOS pool"`
ACLFile string `short:"a" long:"acl-file" required:"1" description:"Path for new Access Control List file"`
}
// Execute is run when the PoolOverwriteACLCmd subcommand is activated
func (d *PoolOverwriteACLCmd) Execute(args []string) error {
acl, err := readACLFile(d.ACLFile)
if err != nil {
return err
}
req := client.PoolOverwriteACLReq{
UUID: d.UUID,
ACL: acl,
}
resp, err := d.conns.PoolOverwriteACL(req)
if err != nil {
d.log.Infof("Pool-overwrite-ACL command failed: %s\n", err.Error())
return err
}
d.log.Infof("Pool-overwrite-ACL command succeeded, UUID: %s\n", d.UUID)
d.log.Info(formatACLDefault(resp.ACL))
return nil
}
// PoolUpdateACLCmd represents the command to update the Access Control List of
// a DAOS pool.
type PoolUpdateACLCmd struct {
logCmd
connectedCmd
UUID string `long:"pool" required:"1" description:"UUID of DAOS pool"`
ACLFile string `short:"a" long:"acl-file" required:"0" description:"Path for new Access Control List file"`
Entry string `short:"e" long:"entry" required:"0" description:"Single Access Control Entry to add or update"`
}
// Execute is run when the PoolUpdateACLCmd subcommand is activated
func (d *PoolUpdateACLCmd) Execute(args []string) error {
if (d.ACLFile == "" && d.Entry == "") || (d.ACLFile != "" && d.Entry != "") {
return errors.New("either ACL file or entry parameter is required")
}
var acl *client.AccessControlList
if d.ACLFile != "" {
aclFileResult, err := readACLFile(d.ACLFile)
if err != nil {
return err
}
acl = aclFileResult
} else {
acl = &client.AccessControlList{
Entries: []string{d.Entry},
}
}
req := client.PoolUpdateACLReq{
UUID: d.UUID,
ACL: acl,
}
resp, err := d.conns.PoolUpdateACL(req)
if err != nil {
d.log.Infof("Pool-update-ACL command failed: %s\n", err.Error())
return err
}
d.log.Infof("Pool-update-ACL command succeeded, UUID: %s\n", d.UUID)
d.log.Info(formatACLDefault(resp.ACL))
return nil
}
// PoolDeleteACLCmd represents the command to delete an entry from the Access
// Control List of a DAOS pool.
type PoolDeleteACLCmd struct {
logCmd
connectedCmd
UUID string `long:"pool" required:"1" description:"UUID of DAOS pool"`
Principal string `short:"p" long:"principal" required:"1" description:"Principal whose entry should be removed"`
}
// Execute is run when the PoolDeleteACLCmd subcommand is activated
func (d *PoolDeleteACLCmd) Execute(args []string) error {
req := client.PoolDeleteACLReq{
UUID: d.UUID,
Principal: d.Principal,
}
resp, err := d.conns.PoolDeleteACL(req)
if err != nil {
d.log.Infof("Pool-delete-ACL command failed: %s\n", err.Error())
return err
}
d.log.Infof("Pool-delete-ACL command succeeded, UUID: %s\n", d.UUID)
d.log.Info(formatACLDefault(resp.ACL))
return nil
}