Skip to content

Commit

Permalink
Support specify install path of toolv2
Browse files Browse the repository at this point in the history
Signed-off-by: 0fatal <[email protected]>
  • Loading branch information
0fatal committed Dec 8, 2023
1 parent a03a67a commit fbcf0b2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
11 changes: 9 additions & 2 deletions cli/command/install/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,6 +19,7 @@ var (

type installOptions struct {
host string
path string
}

func NewInstallToolCommand(curveadm *cli.CurveAdm) *cobra.Command {
Expand All @@ -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
}
Expand All @@ -43,15 +46,19 @@ 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 {
pb.AddStep(&playbook.PlaybookStep{
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,
},
})
}
Expand Down
4 changes: 4 additions & 0 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ const (

// website
KEY_WEBSITE_STATUS = "WEBSITE_STATUS"

// install
KEY_INSTALL_HOST = "INSTALL_HOST"
KEY_INSTALL_PATH = "INSTALL_PATH"
)

// others
Expand Down
28 changes: 26 additions & 2 deletions internal/task/task/install/install_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions internal/tui/common/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
)

Expand Down Expand Up @@ -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()
}

0 comments on commit fbcf0b2

Please sign in to comment.