Skip to content

Commit

Permalink
Feature(install tool): add install tool command
Browse files Browse the repository at this point in the history
Signed-off-by: 0fatal <[email protected]>
  • Loading branch information
0fatal committed Dec 20, 2023
1 parent a703f1e commit a52be8a
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cli/command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/opencurve/curveadm/cli/command/cluster"
"github.com/opencurve/curveadm/cli/command/config"
"github.com/opencurve/curveadm/cli/command/hosts"
"github.com/opencurve/curveadm/cli/command/install"
"github.com/opencurve/curveadm/cli/command/monitor"
"github.com/opencurve/curveadm/cli/command/pfs"
"github.com/opencurve/curveadm/cli/command/playground"
Expand Down Expand Up @@ -66,6 +67,7 @@ func addSubCommands(cmd *cobra.Command, curveadm *cli.CurveAdm) {
target.NewTargetCommand(curveadm), // curveadm target ...
pfs.NewPFSCommand(curveadm), // curveadm pfs ...
monitor.NewMonitorCommand(curveadm), // curveadm monitor ...
install.NewInstallCommand(curveadm), // curveadm install ...

NewAuditCommand(curveadm), // curveadm audit
NewCleanCommand(curveadm), // curveadm clean
Expand Down
21 changes: 21 additions & 0 deletions cli/command/install/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package install

import (
"github.com/opencurve/curveadm/cli/cli"
cliutil "github.com/opencurve/curveadm/internal/utils"
"github.com/spf13/cobra"
)

func NewInstallCommand(curveadm *cli.CurveAdm) *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Short: "Manage install",
Args: cliutil.NoArgs,
RunE: cliutil.ShowHelp(curveadm.Err()),
}

cmd.AddCommand(
NewInstallToolCommand(curveadm),
)
return cmd
}
89 changes: 89 additions & 0 deletions cli/command/install/tool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package install

import (
"github.com/fatih/color"
"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"
)

var (
INSTALL_TOOL_PLAYBOOK_STEPS = []int{
playbook.INSTALL_TOOL,
}
)

type installOptions struct {
host string
path string
confPath string
}

func NewInstallToolCommand(curveadm *cli.CurveAdm) *cobra.Command {
var options installOptions

cmd := &cobra.Command{
Use: "tool [OPTIONS]",
Short: "Install tool v2 on the specified host",
Args: cliutil.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runInstallTool(curveadm, options)
},
DisableFlagsInUseLine: true,
}

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")
flags.StringVar(&options.confPath, "confPath", "/etc/curve/curve.yaml", "Specify target config path of tool v2")

return cmd
}

func genInstallToolPlaybook(curveadm *cli.CurveAdm,
dcs []*topology.DeployConfig,
options installOptions,
) (*playbook.Playbook, error) {
configs := curveadm.FilterDeployConfig(dcs, topology.FilterOption{Id: "*", Role: "*", 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_INSTALL_PATH: options.path,
comm.KEY_INSTALL_CONF_PATH: options.confPath,
},
})
}
return pb, nil
}

func runInstallTool(curveadm *cli.CurveAdm, options installOptions) error {
dcs, err := curveadm.ParseTopology()
if err != nil {
return err
}

pb, err := genInstallToolPlaybook(curveadm, dcs, options)
if err != nil {
return err
}

err = pb.Run()
if err != nil {
return err
}

curveadm.WriteOutln(color.GreenString("Install %s to %s success."),
"curve tool v2", options.host)
return nil
}
4 changes: 4 additions & 0 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ const (
KEY_SERVICE_HOSTS = "SERVICE_HOSTS"
KEY_MONITOR_STATUS = "MONITOR_STATUS"
CLEANED_MONITOR_CONF = "-"

// install
KEY_INSTALL_PATH = "INSTALL_PATH"
KEY_INSTALL_CONF_PATH = "INSTALL_CONF_PATH"
)

// others
Expand Down
4 changes: 4 additions & 0 deletions internal/playbook/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/opencurve/curveadm/internal/task/task/checker"
comm "github.com/opencurve/curveadm/internal/task/task/common"
"github.com/opencurve/curveadm/internal/task/task/fs"
"github.com/opencurve/curveadm/internal/task/task/install"
"github.com/opencurve/curveadm/internal/task/task/monitor"
pg "github.com/opencurve/curveadm/internal/task/task/playground"
"github.com/opencurve/curveadm/internal/tasks"
Expand Down Expand Up @@ -84,6 +85,7 @@ const (
INSTALL_CLIENT
UNINSTALL_CLIENT
ATTACH_LEADER_OR_RANDOM_CONTAINER
INSTALL_TOOL

// bs
FORMAT_CHUNKFILE_POOL
Expand Down Expand Up @@ -250,6 +252,8 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) {
t, err = comm.NewInstallClientTask(curveadm, config.GetCC(i))
case UNINSTALL_CLIENT:
t, err = comm.NewUninstallClientTask(curveadm, nil)
case INSTALL_TOOL:
t, err = install.NewInstallToolTask(curveadm, config.GetDC(i))
// bs
case FORMAT_CHUNKFILE_POOL:
t, err = bs.NewFormatChunkfilePoolTask(curveadm, config.GetFC(i))
Expand Down
79 changes: 79 additions & 0 deletions internal/task/task/install/install_tool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package install

import (
"fmt"
"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"
"path/filepath"
)

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()
path := curveadm.MemStorage().Get(comm.KEY_INSTALL_PATH).(string)
confPath := curveadm.MemStorage().Get(comm.KEY_INSTALL_CONF_PATH).(string)
hc, err := curveadm.GetHost(dc.GetHost())
if err != nil {
return nil, err
}

serviceId := curveadm.GetServiceId(dc.GetId())
containerId, err := curveadm.GetContainerId(serviceId)
if err != nil {
return nil, err
}

if err = checkPathExist(path, hc.GetSSHConfig(), curveadm); err != nil {
return nil, err
}
if err = checkPathExist(confPath, hc.GetSSHConfig(), curveadm); err != nil {
return nil, err
}

subname := fmt.Sprintf("host=%s", dc.GetHost())
t := task.NewTask("Install tool v2", subname, hc.GetSSHConfig())

t.AddStep(&step.CreateDirectory{
Paths: []string{filepath.Dir(path)},
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step.CopyFromContainer{
ContainerSrcPath: layout.ToolsV2BinaryPath,
ContainerId: containerId,
HostDestPath: path,
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step.CreateDirectory{
Paths: []string{filepath.Dir(confPath)},
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step.CopyFromContainer{
ContainerSrcPath: layout.ToolsV2ConfSystemPath,
ContainerId: containerId,
HostDestPath: confPath,
ExecOptions: curveadm.ExecOptions(),
})

return t, nil
}
11 changes: 10 additions & 1 deletion 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 @@ -210,7 +213,7 @@ func PromptErrorCode(code int, description, clue, logpath string) string {
if len(clue) > 0 {
prompt.data["clue"] = prettyClue(clue)
}
prompt.data["website"] = fmt.Sprintf("https://github.com/opencurve/curveadm/wiki/errno%d#%06d", code / 100000, code)
prompt.data["website"] = fmt.Sprintf("https://github.com/opencurve/curveadm/wiki/errno%d#%06d", code/100000, code)
if len(logpath) > 0 {
prompt.data["logpath"] = logpath
}
Expand All @@ -230,3 +233,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 a52be8a

Please sign in to comment.