Skip to content

Commit

Permalink
[feat]curveadm: add clean command for bs format
Browse files Browse the repository at this point in the history
Signed-off-by: sjf <s1973853034@163.com>
  • Loading branch information
Songjf-ttk committed Oct 5, 2023
1 parent bec09a1 commit 8a96cd6
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 15 deletions.
47 changes: 32 additions & 15 deletions cli/command/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ const (
$ curveadm format -f /path/to/format.yaml # Format chunkfile pool with specified configure file
$ curveadm format --status -f /path/to/format.yaml # Display formatting status
$ curveadm format --stop -f /path/to/format.yaml # Stop formatting progress
$ curveadm format --debug -f /path/to/format.yaml # Format chunkfile with debug mode`
$ curveadm format --debug -f /path/to/format.yaml # Format chunkfile with debug mode
$ curveadm format --clean -f /path/to/format.yaml # clean the container left by debug mode`
)

const (
FORMAT_CHUNKFILE_POOL = playbook.FORMAT_CHUNKFILE_POOL
GET_FORMAT_STATUS = playbook.GET_FORMAT_STATUS
STOP_FORMAT = playbook.STOP_FORMAT
CLEAN_FORMAT = playbook.CLEAN_FORMAT
)

var (
Expand All @@ -63,32 +65,42 @@ var (
FORMAT_STOP_PLAYBOOK_STEPS = []int{
playbook.STOP_FORMAT,
}

FORMAT_CLEAN_PLAYBOOK_STEPS = []int{
playbook.CLEAN_FORMAT,
}
)

type formatOptions struct {
filename string
showStatus bool
stopFormat bool
debug bool
cleanFormat bool
}

func checkFormatOptions(options formatOptions) error {
showStatus := options.showStatus
stopFormat := options.stopFormat
debug := options.debug
if showStatus && stopFormat {
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
}
if showStatus && debug {
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
opts := []bool{
options.showStatus,
options.stopFormat,
options.debug,
options.cleanFormat,
}
if stopFormat && debug {
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE

trueCount := 0
for _, opt := range opts {
if opt {
trueCount++
if trueCount > 1 {
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
}
}
}

return nil
return nil
}


func NewFormatCommand(curveadm *cli.CurveAdm) *cobra.Command {
var options formatOptions

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

return cmd
}
Expand All @@ -122,9 +135,10 @@ func genFormatPlaybook(curveadm *cli.CurveAdm,
return nil, errno.ERR_NO_DISK_FOR_FORMATTING
}

showStatus := options.showStatus
stopFormat := options.stopFormat
debug := options.debug
showStatus := options.showStatus
stopFormat := options.stopFormat
debug := options.debug
cleanFormat := options.cleanFormat

steps := FORMAT_PLAYBOOK_STEPS
if showStatus {
Expand All @@ -133,6 +147,9 @@ func genFormatPlaybook(curveadm *cli.CurveAdm,
if stopFormat {
steps = FORMAT_STOP_PLAYBOOK_STEPS
}
if cleanFormat {
steps = FORMAT_CLEAN_PLAYBOOK_STEPS
}

pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
Expand Down
3 changes: 3 additions & 0 deletions internal/playbook/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const (
CREATE_VOLUME
MAP_IMAGE
UNMAP_IMAGE
CLEAN_FORMAT

// monitor
PULL_MONITOR_IMAGE
Expand Down Expand Up @@ -265,6 +266,8 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) {
t, err = bs.NewGetFormatStatusTask(curveadm, config.GetFC(i))
case STOP_FORMAT:
t, err = bs.NewStopFormatTask(curveadm, config.GetFC(i))
case CLEAN_FORMAT:
t, err = bs.NewCleanFormatTask(curveadm, config.GetFC(i))
case BALANCE_LEADER:
t, err = bs.NewBalanceTask(curveadm, config.GetDC(i))
case START_NEBD_SERVICE:
Expand Down
93 changes: 93 additions & 0 deletions internal/task/task/bs/format_clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2022 NetEase Inc.
*
* 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.
*/

/*
* Project: CurveAdm
* Created Date: 2022-11-16
* Author: guiming liang (demoliang)
*/

package bs

import (
"fmt"

"github.com/opencurve/curveadm/cli/cli"
"github.com/opencurve/curveadm/internal/configure"
"github.com/opencurve/curveadm/internal/task/step"
"github.com/opencurve/curveadm/internal/task/context"
"github.com/opencurve/curveadm/internal/task/task"
)

type step2FormatClean struct {
containerId *string
fc *configure.FormatConfig
curveadm *cli.CurveAdm
}

func (s *step2FormatClean) Execute(ctx *context.Context) error {
if len(*s.containerId) == 0 {
return nil
}

var success bool
steps := []task.Step{}
steps = append(steps, &step.StopContainer{
ContainerId: *s.containerId,
Time: 1,
ExecOptions: s.curveadm.ExecOptions(),
})
steps = append(steps, &step.RemoveContainer{
Success: &success,
ContainerId: *s.containerId,
ExecOptions: s.curveadm.ExecOptions(),
})

return nil
}

func NewCleanFormatTask(curveadm *cli.CurveAdm, fc *configure.FormatConfig) (*task.Task, error) {
hc, err := curveadm.GetHost(fc.GetHost())
if err != nil {
return nil, err
}

// new task
device := fc.GetDevice()
mountPoint := fc.GetMountPoint()
containerName := device2ContainerName(device)
subname := fmt.Sprintf("host=%s device=%s mountPoint=%s containerName=%s",
fc.GetHost(), device, mountPoint, containerName)
t := task.NewTask("Clean Precheck Environment", subname, hc.GetSSHConfig())

// add step to task
var out string
t.AddStep(&step.ListContainers{
ShowAll: true,
Format: `"{{.ID}}"`,
Filter: fmt.Sprintf("name=%s", containerName),
Out: &out,
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step2FormatClean{
containerId: &out,
fc: fc,
curveadm: curveadm,
})

return t, nil
}

0 comments on commit 8a96cd6

Please sign in to comment.