From 8a96cd6b13413d37136712503304a446758b2c10 Mon Sep 17 00:00:00 2001 From: sjf Date: Thu, 5 Oct 2023 21:30:32 +0800 Subject: [PATCH] [feat]curveadm: add clean command for bs format Signed-off-by: sjf --- cli/command/format.go | 47 +++++++++----- internal/playbook/factory.go | 3 + internal/task/task/bs/format_clean.go | 93 +++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 15 deletions(-) create mode 100644 internal/task/task/bs/format_clean.go diff --git a/cli/command/format.go b/cli/command/format.go index 8a0b949f6..e66bc24f6 100644 --- a/cli/command/format.go +++ b/cli/command/format.go @@ -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 ( @@ -63,6 +65,10 @@ var ( FORMAT_STOP_PLAYBOOK_STEPS = []int{ playbook.STOP_FORMAT, } + + FORMAT_CLEAN_PLAYBOOK_STEPS = []int{ + playbook.CLEAN_FORMAT, + } ) type formatOptions struct { @@ -70,25 +76,31 @@ type formatOptions struct { 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 @@ -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 } @@ -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 { @@ -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 { diff --git a/internal/playbook/factory.go b/internal/playbook/factory.go index 68ce8993d..0749e719a 100644 --- a/internal/playbook/factory.go +++ b/internal/playbook/factory.go @@ -90,6 +90,7 @@ const ( CREATE_VOLUME MAP_IMAGE UNMAP_IMAGE + CLEAN_FORMAT // monitor PULL_MONITOR_IMAGE @@ -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: diff --git a/internal/task/task/bs/format_clean.go b/internal/task/task/bs/format_clean.go new file mode 100644 index 000000000..87b194d3d --- /dev/null +++ b/internal/task/task/bs/format_clean.go @@ -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 +} +