Skip to content

Commit

Permalink
[feat]Curveadm: add debug mode
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 7d3bd4b commit bec09a1
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
2 changes: 2 additions & 0 deletions cli/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
"time"

"github.com/kpango/glg"
comm "github.com/opencurve/curveadm/internal/common"
configure "github.com/opencurve/curveadm/internal/configure/curveadm"
"github.com/opencurve/curveadm/internal/configure/hosts"
Expand Down Expand Up @@ -308,6 +309,7 @@ func (curveadm *CurveAdm) ClusterName() string { return curveadm.c
func (curveadm *CurveAdm) ClusterTopologyData() string { return curveadm.clusterTopologyData }
func (curveadm *CurveAdm) ClusterPoolData() string { return curveadm.clusterPoolData }
func (curveadm *CurveAdm) Monitor() storage.Monitor { return curveadm.monitor }
func (curveadm *CurveAdm) SetDebugLevel() { glg.Get().SetLevel(glg.DEBG) }

func (curveadm *CurveAdm) GetHost(host string) (*hosts.HostConfig, error) {
if len(curveadm.Hosts()) == 0 {
Expand Down
23 changes: 16 additions & 7 deletions cli/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type deployOptions struct {
insecure bool
poolset string
poolsetDiskType string
debug bool
}

func checkDeployOptions(options deployOptions) error {
Expand Down Expand Up @@ -145,6 +146,7 @@ func NewDeployCommand(curveadm *cli.CurveAdm) *cobra.Command {
flags.BoolVarP(&options.insecure, "insecure", "k", false, "Deploy without precheck")
flags.StringVar(&options.poolset, "poolset", "default", "poolset name")
flags.StringVar(&options.poolsetDiskType, "poolset-disktype", "ssd", "Specify the disk type of physical pool")
flags.BoolVar(&options.debug, "debug", false, "Debug deploy progress")
return cmd
}

Expand Down Expand Up @@ -299,36 +301,43 @@ func displayDeployTitle(curveadm *cli.CurveAdm, dcs []*topology.DeployConfig) {
* 6) balance leader rapidly
*/
func runDeploy(curveadm *cli.CurveAdm, options deployOptions) error {
// 1) parse cluster topology

// 1) check debug mode
debug := options.debug
if debug {
curveadm.SetDebugLevel()
}

// 2) parse cluster topology
dcs, err := curveadm.ParseTopology()
if err != nil {
return err
}

// 2) skip service role
// 3) skip service role
dcs = skipServiceRole(dcs, options)

// 3) precheck before deploy
// 4) precheck before deploy
err = precheckBeforeDeploy(curveadm, dcs, options)
if err != nil {
return err
}

// 4) generate deploy playbook
// 5) generate deploy playbook
pb, err := genDeployPlaybook(curveadm, dcs, options)
if err != nil {
return err
}

// 5) display title
// 6) display title
displayDeployTitle(curveadm, dcs)

// 6) run playground
// 7) run playground
if err = pb.Run(); err != nil {
return err
}

// 7) print success prompt
// 8) print success prompt
curveadm.WriteOutln("")
curveadm.WriteOutln(color.GreenString("Cluster '%s' successfully deployed ^_^."), curveadm.ClusterName())
return nil
Expand Down
60 changes: 50 additions & 10 deletions cli/command/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ const (
FORMAT_EXAMPLE = `Examples:
$ 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 --stop -f /path/to/format.yaml # Stop formatting progress
$ curveadm format --debug -f /path/to/format.yaml # Format chunkfile with debug mode`
)

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

var (
Expand All @@ -59,9 +66,27 @@ var (
)

type formatOptions struct {
filename string
showStatus bool
stopFormat bool
filename string
showStatus bool
stopFormat bool
debug 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
}
if stopFormat && debug {
return errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
}

return nil
}

func NewFormatCommand(curveadm *cli.CurveAdm) *cobra.Command {
Expand All @@ -72,6 +97,9 @@ func NewFormatCommand(curveadm *cli.CurveAdm) *cobra.Command {
Short: "Format chunkfile pool",
Args: cliutil.NoArgs,
Example: FORMAT_EXAMPLE,
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkFormatOptions(options)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runFormat(curveadm, options)
},
Expand All @@ -82,6 +110,7 @@ func NewFormatCommand(curveadm *cli.CurveAdm) *cobra.Command {
flags.StringVarP(&options.filename, "formatting", "f", "format.yaml", "Specify the configure file for formatting chunkfile pool")
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")

return cmd
}
Expand All @@ -93,25 +122,32 @@ func genFormatPlaybook(curveadm *cli.CurveAdm,
return nil, errno.ERR_NO_DISK_FOR_FORMATTING
}

if options.showStatus && options.stopFormat {
return nil, errno.ERR_UNSUPPORT_CONFIGURE_VALUE_TYPE
}
showStatus := options.showStatus
stopFormat := options.stopFormat
debug := options.debug

steps := FORMAT_PLAYBOOK_STEPS
if options.showStatus {
if showStatus {
steps = FORMAT_STATUS_PLAYBOOK_STEPS
}
if options.stopFormat {
if stopFormat {
steps = FORMAT_STOP_PLAYBOOK_STEPS
}

pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
// options
options := map[string]interface{}{}
if step == FORMAT_CHUNKFILE_POOL {
options[comm.DEBUG_MODE] = debug
}
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: fcs,
ExecOptions: playbook.ExecOptions{
SilentSubBar: options.showStatus,
SilentSubBar: showStatus,
},
Options: options,
})
}
return pb, nil
Expand All @@ -133,6 +169,10 @@ func runFormat(curveadm *cli.CurveAdm, options formatOptions) error {
var err error
var fcs []*configure.FormatConfig
diskRecords := curveadm.DiskRecords()
debug := options.debug
if debug {
curveadm.SetDebugLevel()
}

// 1) parse format config from yaml file or database
if len(diskRecords) == 0 {
Expand Down
1 change: 1 addition & 0 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
POOL_TYPE_PHYSICAL = "physicalpool"
POOLSET = "poolset"
POOLSET_DISK_TYPE = "poolset-disktype"
DEBUG_MODE = "DEBUG_MODE"

// disk
DISK_DEFAULT_NULL_SIZE = "-"
Expand Down
4 changes: 3 additions & 1 deletion internal/task/task/bs/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/opencurve/curveadm/cli/cli"
comm "github.com/opencurve/curveadm/internal/common"
"github.com/opencurve/curveadm/internal/configure"
"github.com/opencurve/curveadm/internal/configure/disks"
os "github.com/opencurve/curveadm/internal/configure/os"
Expand Down Expand Up @@ -247,6 +248,7 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf
formatScriptPath := fmt.Sprintf("%s/format.sh", layout.ToolsBinDir)
formatCommand := fmt.Sprintf("%s %s %d %d %s %s", formatScriptPath, layout.FormatBinaryPath,
usagePercent, DEFAULT_CHUNKFILE_SIZE, layout.ChunkfilePoolDir, layout.ChunkfilePoolMetaPath)
debug := curveadm.MemStorage().Get(comm.DEBUG_MODE).(bool)

// 1: skip if formating container exist
t.AddStep(&step.ListContainers{
Expand Down Expand Up @@ -319,7 +321,7 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf
Command: formatCommand,
Entrypoint: "/bin/bash",
Name: containerName,
Remove: true,
Remove: !debug,
Volumes: []step.Volume{{HostPath: mountPoint, ContainerPath: chunkfilePoolRootDir}},
Out: &containerId,
ExecOptions: curveadm.ExecOptions(),
Expand Down
2 changes: 1 addition & 1 deletion internal/task/task/bs/format_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *step2FormatStatus) Execute(ctx *context.Context) error {
}
if usage == 0 {
status = "Mounting"
} else if len(*s.containerStatus) > 1 {
} else if len(*s.containerStatus) > 1 && !strings.Contains(*s.containerStatus, "Exited") {
status = "Formatting"
} else if usage < s.config.GetFormatPercent() {
status = "Pulling image"
Expand Down
2 changes: 1 addition & 1 deletion internal/task/task/checker/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (s *step2StopContainer) Execute(ctx *context.Context) error {
ExecOptions: s.curveadm.ExecOptions(),
})
steps = append(steps, &step.RemoveContainer{
Success: &success, // FIXME(P1): rmeove iff container exist
Success: &success, // FIXME(P1): remove if container exist
ContainerId: *s.containerId,
ExecOptions: s.curveadm.ExecOptions(),
})
Expand Down

0 comments on commit bec09a1

Please sign in to comment.