Skip to content

Commit 6e53f2d

Browse files
committed
[feat]curveadm: add clean command for bs format
Signed-off-by: sjf <s1973853034@163.com>
1 parent bec09a1 commit 6e53f2d

File tree

3 files changed

+135
-15
lines changed

3 files changed

+135
-15
lines changed

cli/command/format.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ const (
4242
$ curveadm format -f /path/to/format.yaml # Format chunkfile pool with specified configure file
4343
$ curveadm format --status -f /path/to/format.yaml # Display formatting status
4444
$ curveadm format --stop -f /path/to/format.yaml # Stop formatting progress
45-
$ curveadm format --debug -f /path/to/format.yaml # Format chunkfile with debug mode`
45+
$ curveadm format --debug -f /path/to/format.yaml # Format chunkfile with debug mode
46+
$ curveadm format --clean -f /path/to/format.yaml # clean the container left by debug mode`
4647
)
4748

4849
const (
4950
FORMAT_CHUNKFILE_POOL = playbook.FORMAT_CHUNKFILE_POOL
5051
GET_FORMAT_STATUS = playbook.GET_FORMAT_STATUS
5152
STOP_FORMAT = playbook.STOP_FORMAT
53+
CLEAN_FORMAT = playbook.CLEAN_FORMAT
5254
)
5355

5456
var (
@@ -63,32 +65,42 @@ var (
6365
FORMAT_STOP_PLAYBOOK_STEPS = []int{
6466
playbook.STOP_FORMAT,
6567
}
68+
69+
FORMAT_CLEAN_PLAYBOOK_STEPS = []int{
70+
playbook.CLEAN_FORMAT,
71+
}
6672
)
6773

6874
type formatOptions struct {
6975
filename string
7076
showStatus bool
7177
stopFormat bool
7278
debug bool
79+
cleanFormat bool
7380
}
7481

7582
func checkFormatOptions(options formatOptions) error {
76-
showStatus := options.showStatus
77-
stopFormat := options.stopFormat
78-
debug := options.debug
79-
if showStatus && stopFormat {
80-
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
81-
}
82-
if showStatus && debug {
83-
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
83+
opts := []bool{
84+
options.showStatus,
85+
options.stopFormat,
86+
options.debug,
87+
options.cleanFormat,
8488
}
85-
if stopFormat && debug {
86-
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
89+
90+
trueCount := 0
91+
for _, opt := range opts {
92+
if opt {
93+
trueCount++
94+
if trueCount > 1 {
95+
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
96+
}
97+
}
8798
}
8899

89-
return nil
100+
return nil
90101
}
91102

103+
92104
func NewFormatCommand(curveadm *cli.CurveAdm) *cobra.Command {
93105
var options formatOptions
94106

@@ -111,6 +123,7 @@ func NewFormatCommand(curveadm *cli.CurveAdm) *cobra.Command {
111123
flags.BoolVar(&options.showStatus, "status", false, "Show formatting status")
112124
flags.BoolVar(&options.stopFormat, "stop", false, "Stop formatting progress")
113125
flags.BoolVar(&options.debug, "debug", false, "Debug formatting progress")
126+
flags.BoolVar(&options.cleanFormat, "clean", false, "Clean the Container")
114127

115128
return cmd
116129
}
@@ -122,9 +135,10 @@ func genFormatPlaybook(curveadm *cli.CurveAdm,
122135
return nil, errno.ERR_NO_DISK_FOR_FORMATTING
123136
}
124137

125-
showStatus := options.showStatus
126-
stopFormat := options.stopFormat
127-
debug := options.debug
138+
showStatus := options.showStatus
139+
stopFormat := options.stopFormat
140+
debug := options.debug
141+
cleanFormat := options.cleanFormat
128142

129143
steps := FORMAT_PLAYBOOK_STEPS
130144
if showStatus {
@@ -133,6 +147,9 @@ func genFormatPlaybook(curveadm *cli.CurveAdm,
133147
if stopFormat {
134148
steps = FORMAT_STOP_PLAYBOOK_STEPS
135149
}
150+
if cleanFormat {
151+
steps = FORMAT_CLEAN_PLAYBOOK_STEPS
152+
}
136153

137154
pb := playbook.NewPlaybook(curveadm)
138155
for _, step := range steps {

internal/playbook/factory.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const (
9090
CREATE_VOLUME
9191
MAP_IMAGE
9292
UNMAP_IMAGE
93+
CLEAN_FORMAT
9394

9495
// monitor
9596
PULL_MONITOR_IMAGE
@@ -265,6 +266,8 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) {
265266
t, err = bs.NewGetFormatStatusTask(curveadm, config.GetFC(i))
266267
case STOP_FORMAT:
267268
t, err = bs.NewStopFormatTask(curveadm, config.GetFC(i))
269+
case CLEAN_FORMAT:
270+
t, err = bs.NewCleanFormatTask(curveadm, config.GetFC(i))
268271
case BALANCE_LEADER:
269272
t, err = bs.NewBalanceTask(curveadm, config.GetDC(i))
270273
case START_NEBD_SERVICE:

internal/task/task/bs/format_clean.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2022 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: CurveAdm
19+
* Created Date: 2023-10-05
20+
* Author: junfan song (Sonjf-ttk)
21+
*/
22+
23+
package bs
24+
25+
import (
26+
"fmt"
27+
28+
"github.com/opencurve/curveadm/cli/cli"
29+
"github.com/opencurve/curveadm/internal/configure"
30+
"github.com/opencurve/curveadm/internal/task/step"
31+
"github.com/opencurve/curveadm/internal/task/context"
32+
"github.com/opencurve/curveadm/internal/task/task"
33+
)
34+
35+
type step2FormatClean struct {
36+
containerId *string
37+
fc *configure.FormatConfig
38+
curveadm *cli.CurveAdm
39+
}
40+
41+
func (s *step2FormatClean) Execute(ctx *context.Context) error {
42+
if len(*s.containerId) == 0 {
43+
return nil
44+
}
45+
46+
var success bool
47+
steps := []task.Step{}
48+
steps = append(steps, &step.StopContainer{
49+
ContainerId: *s.containerId,
50+
Time: 1,
51+
ExecOptions: s.curveadm.ExecOptions(),
52+
})
53+
steps = append(steps, &step.RemoveContainer{
54+
Success: &success,
55+
ContainerId: *s.containerId,
56+
ExecOptions: s.curveadm.ExecOptions(),
57+
})
58+
59+
for _, step := range steps {
60+
err := step.Execute(ctx)
61+
if err != nil {
62+
return err
63+
}
64+
}
65+
66+
return nil
67+
}
68+
69+
func NewCleanFormatTask(curveadm *cli.CurveAdm, fc *configure.FormatConfig) (*task.Task, error) {
70+
hc, err := curveadm.GetHost(fc.GetHost())
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
// new task
76+
device := fc.GetDevice()
77+
mountPoint := fc.GetMountPoint()
78+
containerName := device2ContainerName(device)
79+
subname := fmt.Sprintf("host=%s device=%s mountPoint=%s containerName=%s",
80+
fc.GetHost(), device, mountPoint, containerName)
81+
t := task.NewTask("Clean Format Container", subname, hc.GetSSHConfig())
82+
83+
// add step to task
84+
var out string
85+
t.AddStep(&step.ListContainers{
86+
ShowAll: true,
87+
Format: `"{{.ID}}"`,
88+
Filter: fmt.Sprintf("name=%s", containerName),
89+
Out: &out,
90+
ExecOptions: curveadm.ExecOptions(),
91+
})
92+
t.AddStep(&step2FormatClean{
93+
containerId: &out,
94+
fc: fc,
95+
curveadm: curveadm,
96+
})
97+
98+
return t, nil
99+
}
100+

0 commit comments

Comments
 (0)