Skip to content

Commit 929c35e

Browse files
committed
Improve(client/status): show client config in verbose mode.
Signed-off-by: Wine93 <[email protected]>
1 parent 0b403f8 commit 929c35e

File tree

13 files changed

+184
-17
lines changed

13 files changed

+184
-17
lines changed

cli/command/client/status.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func NewStatusCommand(curveadm *cli.CurveAdm) *cobra.Command {
5757
DisableFlagsInUseLine: true,
5858
}
5959

60+
flags := cmd.Flags()
61+
flags.BoolVarP(&options.verbose, "verbose", "v", false, "Verbose output for status")
62+
6063
return cmd
6164
}
6265

@@ -74,6 +77,9 @@ func genStatusPlaybook(curveadm *cli.CurveAdm,
7477
pb.AddStep(&playbook.PlaybookStep{
7578
Type: step,
7679
Configs: config,
80+
Options: map[string]interface{}{
81+
comm.KEY_CLIENT_STATUS_VERBOSE: options.verbose,
82+
},
7783
ExecOptions: playbook.ExecOptions{
7884
SilentSubBar: true,
7985
},

internal/build/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838
DEBUG_TOOL = "DEBUG_TOOL"
3939
DEBUG_CLUSTER = "DEBUG_CLUSTER"
4040
DEBUG_CREATE_POOL = "DEBUG_CREATE_POOL"
41-
DEBUG_CLIENT_CONFIGURE = "DEBUG_CLIENT_CONFIGURE"
41+
DEBUG_CLIENT_CONFIGURE = "DEBUG_CLIENT_CONFIGURE"
4242
)
4343

4444
type Field struct {

internal/common/common.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ const (
7777
CLEANED_CONTAINER_ID = "-"
7878

7979
// client
80-
KEY_CLIENT_HOST = "CLIENT_HOST"
81-
KEY_CLIENT_KIND = "CLIENT_KIND"
82-
KEY_ALL_CLIENT_STATUS = "ALL_CLIENT_STATUS"
83-
KEY_MAP_OPTIONS = "MAP_OPTIONS"
84-
KEY_MOUNT_OPTIONS = "MOUNT_OPTIONS"
85-
CLIENT_STATUS_LOSED = "Losed"
86-
KERNERL_MODULE_NBD = "nbd"
87-
KERNERL_MODULE_FUSE = "fuse"
80+
KEY_CLIENT_HOST = "CLIENT_HOST"
81+
KEY_CLIENT_KIND = "CLIENT_KIND"
82+
KEY_ALL_CLIENT_STATUS = "ALL_CLIENT_STATUS"
83+
KEY_CLIENT_STATUS_VERBOSE = "CLIENT_STATUS_VERBOSE"
84+
KEY_MAP_OPTIONS = "MAP_OPTIONS"
85+
KEY_MOUNT_OPTIONS = "MOUNT_OPTIONS"
86+
CLIENT_STATUS_LOSED = "Losed"
87+
KERNERL_MODULE_NBD = "nbd"
88+
KERNERL_MODULE_FUSE = "fuse"
8889

8990
// polarfs
9091
KEY_POLARFS_HOST = "POLARFS_HOST"

internal/configure/client.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,36 @@ func ParseClientCfg(data string) (*ClientConfig, error) {
144144
}
145145

146146
func ParseClientConfig(filename string) (*ClientConfig, error) {
147+
// 1. read file content
148+
data, err := utils.ReadFile(filename)
149+
if err != nil {
150+
return nil, errno.ERR_PARSE_CLIENT_CONFIGURE_FAILED.E(err)
151+
}
152+
153+
// 2. new parser
147154
parser := viper.NewWithOptions(viper.KeyDelimiter("::"))
148155
parser.SetConfigFile(filename)
149156
parser.SetConfigType("yaml")
150-
if err := parser.ReadInConfig(); err != nil {
157+
err = parser.ReadInConfig()
158+
if err != nil {
151159
return nil, errno.ERR_PARSE_CLIENT_CONFIGURE_FAILED.E(err)
152160
}
153161

154-
config := map[string]interface{}{}
155-
if err := parser.Unmarshal(&config); err != nil {
162+
// 3. parse
163+
m := map[string]interface{}{}
164+
err = parser.Unmarshal(&m)
165+
if err != nil {
156166
return nil, errno.ERR_PARSE_CLIENT_CONFIGURE_FAILED.E(err)
157167
}
158-
build.DEBUG(build.DEBUG_CLIENT_CONFIGURE, config)
159-
return NewClientConfig(config)
168+
169+
// 4. new config
170+
cfg, err := NewClientConfig(m)
171+
if err != nil {
172+
return nil, err
173+
}
174+
175+
cfg.data = data
176+
return cfg, nil
160177
}
161178

162179
func (cc *ClientConfig) getString(key string) string {
@@ -186,6 +203,7 @@ func (cc *ClientConfig) GetS3BucketName() string { return cc.getStri
186203
func (cc *ClientConfig) GetContainerPid() string { return cc.getString(KEY_CONTAINER_PID) }
187204
func (cc *ClientConfig) GetEnvironments() string { return cc.getString(KEY_ENVIRONMENT) }
188205
func (cc *ClientConfig) GetCoreLocateDir() string { return DEFAULT_CORE_LOCATE_DIR }
206+
func (cc *ClientConfig) GetData() string { return cc.data }
189207
func (cc *ClientConfig) GetServiceConfig() map[string]string { return cc.serviceConfig }
190208
func (cc *ClientConfig) GetVariables() *variable.Variables { return cc.variables }
191209
func (cc *ClientConfig) GetContainerImage() string {

internal/errno/errno.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func (e *ErrorCode) Error() string {
126126
* * 113: clients table
127127
* * 114: plauground table
128128
* * 115: audit table
129+
* * 116: any table
129130
*
130131
* 2xx: command options
131132
* 20*: hosts
@@ -223,6 +224,10 @@ var (
223224
ERR_DELETE_PLAYGROUND_FAILED = EC(114003, "execute SQL failed which delete playground")
224225
// 115: database/SQL (execute SQL statement: audit table)
225226
ERR_GET_AUDIT_LOGS_FAILE = EC(115000, "execute SQL failed which get audit logs")
227+
// 116: database/SQL (execute SQL statement: any table)
228+
ERR_INSERT_CLIENT_CONFIG_FAILED = EC(116000, "execute SQL failed which insert client config")
229+
ERR_SELECT_CLIENT_CONFIG_FAILED = EC(116001, "execute SQL failed which select client config")
230+
ERR_DELETE_CLIENT_CONFIG_FAILED = EC(116002, "execute SQL failed which delete client config")
226231

227232
// 200: command options (hosts)
228233

internal/storage/sql.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,34 @@ var (
297297
SelectAuditLogById = `SELECT * FROM audit WHERE id = ?`
298298
)
299299

300+
// any: we can store anything
301+
type Any struct {
302+
Id string
303+
Data string
304+
}
305+
306+
var (
307+
// table: any
308+
CreateAnyTable = `
309+
CREATE TABLE IF NOT EXISTS any (
310+
id TEXT PRIMARY KEY,
311+
data TEXT NOT NULL
312+
)
313+
`
314+
315+
// insert item
316+
InsertAnyItem = `INSERT INTO any(id, data) VALUES(?, ?)`
317+
318+
// set item
319+
SetAnyItem = `UPDATE any SET data = ? WHERE id = ?`
320+
321+
// select item by id
322+
SelectAnyItem = `SELECT * FROM any WHERE id = ?`
323+
324+
// delete item
325+
DeleteAnyItem = `DELETE from any WHERE id = ?`
326+
)
327+
300328
var (
301329
// check pool column
302330
CheckPoolColumn = `

internal/storage/storage.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (s *Storage) init() error {
7575
CreateClientsTable,
7676
CreatePlaygroundTable,
7777
CreateAuditTable,
78+
CreateAnyTable,
7879
}
7980

8081
for _, sql := range sqls {
@@ -403,3 +404,43 @@ func (s *Storage) GetAuditLogs() ([]AuditLog, error) {
403404
func (s *Storage) GetAuditLog(id int64) ([]AuditLog, error) {
404405
return s.getAuditLogs(SelectAuditLogById, id)
405406
}
407+
408+
// any item prefix
409+
const (
410+
PREFIX_CLIENT_CONFIG = 0x01
411+
)
412+
413+
func (s *Storage) realId(prefix int, id string) string {
414+
return fmt.Sprintf("%d:%s", prefix, id)
415+
}
416+
417+
func (s *Storage) InsertClientConfig(id, data string) error {
418+
id = s.realId(PREFIX_CLIENT_CONFIG, id)
419+
return s.write(InsertAnyItem, id, data)
420+
}
421+
422+
func (s *Storage) GetClientConfig(id string) ([]Any, error) {
423+
id = s.realId(PREFIX_CLIENT_CONFIG, id)
424+
result, err := s.db.Query(SelectAnyItem, id)
425+
if err != nil {
426+
return nil, err
427+
}
428+
defer result.Close()
429+
430+
items := []Any{}
431+
var item Any
432+
for result.Next() {
433+
err = result.Scan(&item.Id, &item.Data)
434+
if err != nil {
435+
return nil, err
436+
}
437+
items = append(items, item)
438+
}
439+
440+
return items, nil
441+
}
442+
443+
func (s *Storage) DeleteClientConfig(id string) error {
444+
id = s.realId(PREFIX_CLIENT_CONFIG, id)
445+
return s.write(DeleteAnyItem, id)
446+
}

internal/task/task/bs/start_nebd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ func (s *step2InsertClient) Execute(ctx *context.Context) error {
137137
if err != nil {
138138
return errno.ERR_INSERT_CLIENT_FAILED.E(err)
139139
}
140+
141+
err = curveadm.Storage().InsertClientConfig(volumeId, config.GetData())
142+
if err != nil {
143+
return errno.ERR_INSERT_CLIENT_CONFIG_FAILED.E(err)
144+
}
145+
140146
return nil
141147
}
142148

internal/task/task/bs/unmap.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func (s *step2DeleteClient) Execute(ctx *context.Context) error {
121121
if err != nil {
122122
return errno.ERR_DELETE_CLIENT_FAILED.E(err)
123123
}
124+
125+
err = s.curveadm.Storage().DeleteClientConfig(s.volumeId)
126+
if err != nil {
127+
return errno.ERR_DELETE_CLIENT_CONFIG_FAILED.E(err)
128+
}
129+
124130
return nil
125131
}
126132

internal/task/task/common/client_status.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/opencurve/curveadm/cli/cli"
2929
comm "github.com/opencurve/curveadm/internal/common"
30+
"github.com/opencurve/curveadm/internal/errno"
3031
"github.com/opencurve/curveadm/internal/storage"
3132
"github.com/opencurve/curveadm/internal/task/context"
3233
"github.com/opencurve/curveadm/internal/task/step"
@@ -35,11 +36,16 @@ import (
3536
"github.com/opencurve/curveadm/internal/utils"
3637
)
3738

39+
const (
40+
MISSING_CLIENT_CONFIG = "-"
41+
)
42+
3843
type (
3944
step2FormatClientStatus struct {
4045
client storage.Client
4146
containerId string
4247
status *string
48+
cfgPath *string
4349
memStorage *utils.SafeMap
4450
}
4551

@@ -50,9 +56,32 @@ type (
5056
ContainerId string
5157
Status string
5258
AuxInfo string
59+
CfgPath string
5360
}
5461
)
5562

63+
func dumpCfg(curveadm *cli.CurveAdm, id string, cfgPath *string) step.LambdaType {
64+
return func(ctx *context.Context) error {
65+
*cfgPath = MISSING_CLIENT_CONFIG
66+
cfgs, err := curveadm.Storage().GetClientConfig(id)
67+
if err != nil {
68+
return errno.ERR_SELECT_CLIENT_CONFIG_FAILED.E(err)
69+
} else if len(cfgs) == 0 {
70+
return nil
71+
}
72+
73+
data := cfgs[0].Data
74+
path := utils.RandFilename("/tmp")
75+
err = utils.WriteFile(path, data, 0644)
76+
if err != nil {
77+
return errno.ERR_WRITE_FILE_FAILED.E(err)
78+
}
79+
80+
*cfgPath = path
81+
return nil
82+
}
83+
}
84+
5685
// TODO(P0): init client status
5786
func setClientStatus(memStorage *utils.SafeMap, id string, status ClientStatus) {
5887
memStorage.TX(func(kv *utils.SafeMap) error {
@@ -82,6 +111,7 @@ func (s *step2FormatClientStatus) Execute(ctx *context.Context) error {
82111
ContainerId: s.containerId,
83112
Status: status,
84113
AuxInfo: client.AuxInfo,
114+
CfgPath: *s.cfgPath,
85115
})
86116
return nil
87117
}
@@ -99,18 +129,22 @@ func NewGetClientStatusTask(curveadm *cli.CurveAdm, v interface{}) (*task.Task,
99129
t := task.NewTask("Get Client Status", subname, hc.GetSSHConfig())
100130

101131
// add step
102-
var status string
132+
var status, cfgPath string
103133
t.AddStep(&step.ListContainers{
104134
ShowAll: true,
105135
Format: `"{{.Status}}"`,
106136
Filter: fmt.Sprintf("id=%s", containerId),
107137
Out: &status,
108138
ExecOptions: curveadm.ExecOptions(),
109139
})
140+
t.AddStep(&step.Lambda{
141+
Lambda: dumpCfg(curveadm, client.Id, &cfgPath),
142+
})
110143
t.AddStep(&step2FormatClientStatus{
111144
client: client,
112145
containerId: containerId,
113146
status: &status,
147+
cfgPath: &cfgPath,
114148
memStorage: curveadm.MemStorage(),
115149
})
116150

0 commit comments

Comments
 (0)