From b88e58bfc20767b193042f57fddcea3e3bbb8e48 Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 14 Aug 2020 10:34:28 +0800 Subject: [PATCH] ctl: support using task-file when need task-name (#854) (#892) --- dm/ctl/common/util.go | 20 ++++++++++++++++++++ dm/ctl/master/pause_task.go | 4 ++-- dm/ctl/master/resume_task.go | 4 ++-- dm/ctl/master/stop_task.go | 4 ++-- tests/dmctl_basic/check_list/pause_task.sh | 2 +- tests/dmctl_basic/check_list/resume_task.sh | 2 +- tests/dmctl_basic/check_list/stop_task.sh | 2 +- tests/dmctl_basic/run.sh | 4 ++++ 8 files changed, 33 insertions(+), 9 deletions(-) diff --git a/dm/ctl/common/util.go b/dm/ctl/common/util.go index 2bd67d1f89..755f8bb422 100644 --- a/dm/ctl/common/util.go +++ b/dm/ctl/common/util.go @@ -20,6 +20,7 @@ import ( "strings" "time" + "github.com/pingcap/dm/dm/config" "github.com/pingcap/dm/dm/pb" parserpkg "github.com/pingcap/dm/pkg/parser" "github.com/pingcap/dm/pkg/terror" @@ -210,3 +211,22 @@ func IsDDL(sql string) (bool, error) { return false, nil } } + +// GetTaskNameFromArgOrFile tries to retrieve name from the file if arg is yaml-filename-like, otherwise returns arg directly +func GetTaskNameFromArgOrFile(arg string) string { + if !(strings.HasSuffix(arg, ".yaml") || strings.HasSuffix(arg, ".yml")) { + return arg + } + var ( + content []byte + err error + ) + if content, err = GetFileContent(arg); err != nil { + return arg + } + cfg := config.NewTaskConfig() + if err := cfg.Decode(string(content)); err != nil { + return arg + } + return cfg.Name +} diff --git a/dm/ctl/master/pause_task.go b/dm/ctl/master/pause_task.go index 9d9dc3326c..e12daec73f 100644 --- a/dm/ctl/master/pause_task.go +++ b/dm/ctl/master/pause_task.go @@ -25,7 +25,7 @@ import ( // NewPauseTaskCmd creates a PauseTask command func NewPauseTaskCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "pause-task [-w worker ...] ", + Use: "pause-task [-w worker ...] ", Short: "pause a specified running task", Run: pauseTaskFunc, } @@ -39,7 +39,7 @@ func pauseTaskFunc(cmd *cobra.Command, _ []string) { cmd.Usage() return } - name := cmd.Flags().Arg(0) + name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0)) workers, err := common.GetWorkerArgs(cmd) if err != nil { diff --git a/dm/ctl/master/resume_task.go b/dm/ctl/master/resume_task.go index 5deda3ce4b..c47fe455e3 100644 --- a/dm/ctl/master/resume_task.go +++ b/dm/ctl/master/resume_task.go @@ -25,7 +25,7 @@ import ( // NewResumeTaskCmd creates a ResumeTask command func NewResumeTaskCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "resume-task [-w worker ...] ", + Use: "resume-task [-w worker ...] ", Short: "resume a specified paused task", Run: resumeTaskFunc, } @@ -39,7 +39,7 @@ func resumeTaskFunc(cmd *cobra.Command, _ []string) { cmd.Usage() return } - name := cmd.Flags().Arg(0) + name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0)) workers, err := common.GetWorkerArgs(cmd) if err != nil { diff --git a/dm/ctl/master/stop_task.go b/dm/ctl/master/stop_task.go index 35df34aee9..bf0987cf92 100644 --- a/dm/ctl/master/stop_task.go +++ b/dm/ctl/master/stop_task.go @@ -25,7 +25,7 @@ import ( // NewStopTaskCmd creates a StopTask command func NewStopTaskCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "stop-task [-w worker ...] ", + Use: "stop-task [-w worker ...] ", Short: "stop a specified task", Run: stopTaskFunc, } @@ -39,7 +39,7 @@ func stopTaskFunc(cmd *cobra.Command, _ []string) { cmd.Usage() return } - name := cmd.Flags().Arg(0) + name := common.GetTaskNameFromArgOrFile(cmd.Flags().Arg(0)) workers, err := common.GetWorkerArgs(cmd) if err != nil { diff --git a/tests/dmctl_basic/check_list/pause_task.sh b/tests/dmctl_basic/check_list/pause_task.sh index 144c2283d2..49936f6532 100644 --- a/tests/dmctl_basic/check_list/pause_task.sh +++ b/tests/dmctl_basic/check_list/pause_task.sh @@ -3,7 +3,7 @@ function pause_task_wrong_arg() { run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ "pause-task" \ - "pause-task \[-w worker ...\] \[flags\]" 1 + "pause-task \[-w worker ...\] \[flags\]" 1 } function pause_task_success() { diff --git a/tests/dmctl_basic/check_list/resume_task.sh b/tests/dmctl_basic/check_list/resume_task.sh index c9b242ba2d..6cf6ee60bc 100644 --- a/tests/dmctl_basic/check_list/resume_task.sh +++ b/tests/dmctl_basic/check_list/resume_task.sh @@ -3,7 +3,7 @@ function resume_task_wrong_arg() { run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ "resume-task" \ - "resume-task \[-w worker ...\] \[flags\]" 1 + "resume-task \[-w worker ...\] \[flags\]" 1 } function resume_task_success() { diff --git a/tests/dmctl_basic/check_list/stop_task.sh b/tests/dmctl_basic/check_list/stop_task.sh index 193216997d..2fd9f5659a 100644 --- a/tests/dmctl_basic/check_list/stop_task.sh +++ b/tests/dmctl_basic/check_list/stop_task.sh @@ -3,5 +3,5 @@ function stop_task_wrong_arg() { run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \ "stop-task" \ - "stop-task \[-w worker ...\] \[flags\]" 1 + "stop-task \[-w worker ...\] \[flags\]" 1 } diff --git a/tests/dmctl_basic/run.sh b/tests/dmctl_basic/run.sh index 4afcd0f912..2bd50759ca 100755 --- a/tests/dmctl_basic/run.sh +++ b/tests/dmctl_basic/run.sh @@ -131,8 +131,12 @@ function run() { resume_task_success $TASK_NAME check_sync_diff $WORK_DIR $cur/conf/diff_config.toml 20 + # test use task file instead of task name + pause_task_success "$cur/conf/dm-task.yaml" + resume_task_success "$cur/conf/dm-task.yaml" update_relay_success $dm_worker1_conf 127.0.0.1:$WORKER1_PORT update_relay_success $dm_worker2_conf 127.0.0.1:$WORKER2_PORT + # check worker config backup file is correct [ -f $WORK_DIR/worker1/dm-worker-config.bak ] && cmp $WORK_DIR/worker1/dm-worker-config.bak $cur/conf/dm-worker1.toml [ -f $WORK_DIR/worker2/dm-worker-config.bak ] && cmp $WORK_DIR/worker2/dm-worker-config.bak $cur/conf/dm-worker2.toml