-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat]curveadm: add install tool command #332
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c5293e0
[feat]curveadm: add install tool command
0fatal da8a808
Copy the binary and conf of toolv2 from container to host
0fatal d4a243a
Remove unused code and reuse functions
0fatal 6e02106
Support specify install path of toolv2
0fatal e756a9c
Support specify conf path of toolv2
0fatal f1f540a
Create directory of the install path of toolv2 if not exists
0fatal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "~/.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: topology.ROLE_MDS, Host: options.host})[:1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any one service container is ok on the host. ROLE_MDS is not necessary. |
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
0fatal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The defaule value of confPath is
/etc/curve/curve.yaml