From 8ae017036576c4231a07fc2d88407bc8820640a4 Mon Sep 17 00:00:00 2001 From: 0fatal <2816813070@qq.com> Date: Fri, 8 Dec 2023 13:10:48 +0000 Subject: [PATCH] Support specify install path of toolv2 Signed-off-by: 0fatal <2816813070@qq.com> --- cli/command/install/tool.go | 11 +++++++-- internal/common/common.go | 4 ++++ internal/task/task/install/install_tool.go | 28 ++++++++++++++++++++-- internal/tui/common/prompt.go | 9 +++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/cli/command/install/tool.go b/cli/command/install/tool.go index 5366602db..8c2f94415 100644 --- a/cli/command/install/tool.go +++ b/cli/command/install/tool.go @@ -5,6 +5,7 @@ import ( "github.com/opencurve/curveadm/cli/cli" comm "github.com/opencurve/curveadm/internal/common" "github.com/opencurve/curveadm/internal/configure/topology" + "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/playbook" cliutil "github.com/opencurve/curveadm/internal/utils" "github.com/spf13/cobra" @@ -18,6 +19,7 @@ var ( type installOptions struct { host string + path string } func NewInstallToolCommand(curveadm *cli.CurveAdm) *cobra.Command { @@ -35,6 +37,7 @@ func NewInstallToolCommand(curveadm *cli.CurveAdm) *cobra.Command { flags := cmd.Flags() flags.StringVar(&options.host, "host", "localhost", "Specify target host") + flags.StringVar(&options.path, "path", "/usr/local/bin/curve", "Specify target install path of tool v2") return cmd } @@ -43,7 +46,10 @@ func genInstallToolPlaybook(curveadm *cli.CurveAdm, dcs []*topology.DeployConfig, options installOptions, ) (*playbook.Playbook, error) { - configs := curveadm.FilterDeployConfigByRole(dcs, topology.ROLE_MDS)[:1] + configs := curveadm.FilterDeployConfig(dcs, topology.FilterOption{Id: "*", Role: topology.ROLE_MDS, Host: options.host})[:1] + if len(configs) == 0 { + return nil, errno.ERR_NO_SERVICES_MATCHED + } steps := INSTALL_TOOL_PLAYBOOK_STEPS pb := playbook.NewPlaybook(curveadm) for _, step := range steps { @@ -51,7 +57,8 @@ func genInstallToolPlaybook(curveadm *cli.CurveAdm, Type: step, Configs: configs, Options: map[string]interface{}{ - comm.KEY_CLIENT_HOST: options.host, + comm.KEY_INSTALL_HOST: options.host, + comm.KEY_INSTALL_PATH: options.path, }, }) } diff --git a/internal/common/common.go b/internal/common/common.go index df2b68c5b..2b718f20c 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -134,6 +134,10 @@ const ( // website KEY_WEBSITE_STATUS = "WEBSITE_STATUS" + + // install + KEY_INSTALL_HOST = "INSTALL_HOST" + KEY_INSTALL_PATH = "INSTALL_PATH" ) // others diff --git a/internal/task/task/install/install_tool.go b/internal/task/task/install/install_tool.go index 5ddfd5434..20be81242 100644 --- a/internal/task/task/install/install_tool.go +++ b/internal/task/task/install/install_tool.go @@ -5,13 +5,33 @@ import ( "github.com/opencurve/curveadm/cli/cli" comm "github.com/opencurve/curveadm/internal/common" "github.com/opencurve/curveadm/internal/configure/topology" + "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/task/step" "github.com/opencurve/curveadm/internal/task/task" + tui "github.com/opencurve/curveadm/internal/tui/common" + "github.com/opencurve/curveadm/pkg/module" ) +func checkPathExist(path string, sshConfig *module.SSHConfig, curveadm *cli.CurveAdm) error { + sshClient, err := module.NewSSHClient(*sshConfig) + if err != nil { + return errno.ERR_SSH_CONNECT_FAILED.E(err) + } + + module := module.NewModule(sshClient) + cmd := module.Shell().Stat(path) + if _, err := cmd.Execute(curveadm.ExecOptions()); err == nil { + if pass := tui.ConfirmYes(tui.PromptPathExist(path)); !pass { + return errno.ERR_CANCEL_OPERATION + } + } + return nil +} + func NewInstallToolTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) { layout := dc.GetProjectLayout() - host := curveadm.MemStorage().Get(comm.KEY_CLIENT_HOST).(string) + host := curveadm.MemStorage().Get(comm.KEY_INSTALL_HOST).(string) + path := curveadm.MemStorage().Get(comm.KEY_INSTALL_PATH).(string) hc, err := curveadm.GetHost(host) if err != nil { return nil, err @@ -23,13 +43,17 @@ func NewInstallToolTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*tas return nil, err } + if err = checkPathExist(path, hc.GetSSHConfig(), curveadm); err != nil { + return nil, err + } + subname := fmt.Sprintf("host=%s", host) t := task.NewTask("Install tool v2", subname, hc.GetSSHConfig()) t.AddStep(&step.CopyFromContainer{ ContainerSrcPath: layout.ToolsV2BinaryPath, ContainerId: containerId, - HostDestPath: "/usr/local/bin/curve", + HostDestPath: path, ExecOptions: curveadm.ExecOptions(), }) t.AddStep(&step.CreateDirectory{ diff --git a/internal/tui/common/prompt.go b/internal/tui/common/prompt.go index 52e8b1588..794c15e02 100644 --- a/internal/tui/common/prompt.go +++ b/internal/tui/common/prompt.go @@ -71,6 +71,9 @@ to watch the formatting progress. ` PROMPT_CANCEL_OPERATION = `[x] {{.operation}} canceled` + PROMPT_PATH_EXIST = `{{.path}} already exists. +` + DEFAULT_CONFIRM_PROMPT = "Do you want to continue?" ) @@ -236,3 +239,9 @@ func PromptAutoUpgrade(version string) string { prompt.data["version"] = version return prompt.Build() } + +func PromptPathExist(path string) string { + prompt := NewPrompt(color.YellowString(PROMPT_PATH_EXIST) + DEFAULT_CONFIRM_PROMPT) + prompt.data["path"] = path + return prompt.Build() +}