From 24dbca0ad72f38de7cd42bec34b3afe300b3250e Mon Sep 17 00:00:00 2001 From: 0fatal <2816813070@qq.com> Date: Mon, 6 Nov 2023 13:47:40 +0000 Subject: [PATCH] Remove unused code and reuse functions --- cli/command/install/tool.go | 9 ++---- internal/errno/errno.go | 1 - internal/playbook/factory.go | 3 +- internal/task/step/file.go | 16 ---------- internal/task/step/shell.go | 36 ---------------------- internal/task/task/install/install_tool.go | 24 ++++----------- 6 files changed, 9 insertions(+), 80 deletions(-) diff --git a/cli/command/install/tool.go b/cli/command/install/tool.go index d36df72d1..5366602db 100644 --- a/cli/command/install/tool.go +++ b/cli/command/install/tool.go @@ -43,18 +43,13 @@ func genInstallToolPlaybook(curveadm *cli.CurveAdm, dcs []*topology.DeployConfig, options installOptions, ) (*playbook.Playbook, error) { - dcs = curveadm.FilterDeployConfig(dcs, topology.FilterOption{ - Id: "*", - Role: "*", - Host: options.host, - }) - + configs := curveadm.FilterDeployConfigByRole(dcs, topology.ROLE_MDS)[:1] steps := INSTALL_TOOL_PLAYBOOK_STEPS pb := playbook.NewPlaybook(curveadm) for _, step := range steps { pb.AddStep(&playbook.PlaybookStep{ Type: step, - Configs: dcs, + Configs: configs, Options: map[string]interface{}{ comm.KEY_CLIENT_HOST: options.host, }, diff --git a/internal/errno/errno.go b/internal/errno/errno.go index 2d32b455f..fd40bbc00 100644 --- a/internal/errno/errno.go +++ b/internal/errno/errno.go @@ -553,7 +553,6 @@ var ( ERR_INSTALL_OR_REMOVE_DEBIAN_PACKAGE_FAILED = EC(620024, "install or remove debian package failed (dpkg)") ERR_INSTALL_OR_REMOVE_RPM_PACKAGE_FAILED = EC(620025, "install or remove rpm package failed (rpm)") ERR_SECURE_COPY_FILE_TO_REMOTE_FAILED = EC(620026, "secure copy file to remote failed (scp)") - ERR_CHMOD_FILE_FAILED = EC(620027, "chmod file failed (chmod)") ERR_RUN_SCRIPT_FAILED = EC(620998, "run script failed (bash script.sh)") ERR_RUN_A_BASH_COMMAND_FAILED = EC(620999, "run a bash command failed (bash -c)") diff --git a/internal/playbook/factory.go b/internal/playbook/factory.go index 8b6ff7b35..69842500c 100644 --- a/internal/playbook/factory.go +++ b/internal/playbook/factory.go @@ -171,8 +171,7 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) { switch step.Type { case CHECK_SSH_CONNECT, GET_HOST_DATE, - PULL_IMAGE, - INSTALL_TOOL: + PULL_IMAGE: host := config.GetDC(i).GetHost() if once[host] { continue diff --git a/internal/task/step/file.go b/internal/task/step/file.go index 18714e525..cfd4fc575 100644 --- a/internal/task/step/file.go +++ b/internal/task/step/file.go @@ -53,13 +53,6 @@ type ( module.ExecOptions } - ExtractFile struct { - HostDestPath string - ContainerId string - ContainerSrcPath string - module.ExecOptions - } - InstallFile struct { Content *string HostDestPath string @@ -159,15 +152,6 @@ func (s *ReadFile) Execute(ctx *context.Context) error { return nil } -func (s *ExtractFile) Execute(ctx *context.Context) error { - dockerCli := ctx.Module().DockerCli().CopyFromContainer(s.ContainerId, s.ContainerSrcPath, s.HostDestPath) - _, err := dockerCli.Execute(s.ExecOptions) - if err != nil { - return errno.ERR_COPY_FROM_CONTAINER_FAILED.FD("(%s cp CONTAINER:SRC_PATH DEST_PATH)", s.ExecWithEngine).E(err) - } - return nil -} - func (s *InstallFile) Execute(ctx *context.Context) error { localPath := utils.RandFilename(TEMP_DIR) defer os.Remove(localPath) diff --git a/internal/task/step/shell.go b/internal/task/step/shell.go index 34ae3a6f1..e002e1cac 100644 --- a/internal/task/step/shell.go +++ b/internal/task/step/shell.go @@ -79,22 +79,6 @@ type ( module.ExecOptions } - MoveFile struct { - Source string - Dest string - NoClobber bool // do not overwrite an existing file - Out *string - module.ExecOptions - } - - Chmod struct { - Mode string - File string - Recursive bool - Out *string - module.ExecOptions - } - Stat struct { Files []string Format string @@ -340,26 +324,6 @@ func (s *CopyFile) Execute(ctx *context.Context) error { return PostHandle(nil, s.Out, out, err, errno.ERR_COPY_FILES_AND_DIRECTORIES_FAILED) } -func (s *MoveFile) Execute(ctx *context.Context) error { - cmd := ctx.Module().Shell().Rename(s.Source, s.Dest) - if s.NoClobber { - cmd.AddOption("--no-clobber") - } - - out, err := cmd.Execute(s.ExecOptions) - return PostHandle(nil, s.Out, out, err, errno.ERR_RENAME_FILE_OR_DIRECTORY_FAILED) -} - -func (s *Chmod) Execute(ctx *context.Context) error { - cmd := ctx.Module().Shell().Chmod(s.Mode, s.File) - if s.Recursive { - cmd.AddOption("--recursive") - } - - out, err := cmd.Execute(s.ExecOptions) - return PostHandle(nil, s.Out, out, err, errno.ERR_CHMOD_FILE_FAILED) -} - func (s *Stat) Execute(ctx *context.Context) error { cmd := ctx.Module().Shell().Stat(s.Files...) if len(s.Format) > 0 { diff --git a/internal/task/task/install/install_tool.go b/internal/task/task/install/install_tool.go index 0087887a0..5ddfd5434 100644 --- a/internal/task/task/install/install_tool.go +++ b/internal/task/task/install/install_tool.go @@ -26,34 +26,22 @@ func NewInstallToolTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*tas subname := fmt.Sprintf("host=%s", host) t := task.NewTask("Install tool v2", subname, hc.GetSSHConfig()) - var confContent string - - t.AddStep(&step.ExtractFile{ + t.AddStep(&step.CopyFromContainer{ ContainerSrcPath: layout.ToolsV2BinaryPath, ContainerId: containerId, - HostDestPath: "/usr/bin/curve", + HostDestPath: "/usr/local/bin/curve", ExecOptions: curveadm.ExecOptions(), }) - t.AddStep(&step.Chmod{ - Mode: "+x", - File: "/usr/bin/curve", + t.AddStep(&step.CreateDirectory{ + Paths: []string{"~/.curve"}, ExecOptions: curveadm.ExecOptions(), }) - t.AddStep(&step.ReadFile{ + t.AddStep(&step.CopyFromContainer{ ContainerSrcPath: layout.ToolsV2ConfSystemPath, ContainerId: containerId, - Content: &confContent, + HostDestPath: "~/.curve/curve.yaml", ExecOptions: curveadm.ExecOptions(), }) - t.AddStep(&step.CreateDirectory{ - Paths: []string{"~/.curve"}, - ExecOptions: curveadm.ExecOptions(), - }) - t.AddStep(&step.InstallFile{ - Content: &confContent, - HostDestPath: "~/.curve/curve.yaml", - ExecOptions: curveadm.ExecOptions(), - }) return t, nil }