From 49f3774bc3db811b0962a76eb0720498be1b0802 Mon Sep 17 00:00:00 2001 From: srstack Date: Mon, 29 Nov 2021 17:42:03 +0800 Subject: [PATCH 01/10] add cluster clean flag audit-log --- components/cluster/command/clean.go | 1 + pkg/cluster/operation/operation.go | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/components/cluster/command/clean.go b/components/cluster/command/clean.go index 955a42157c..616bfa54c6 100644 --- a/components/cluster/command/clean.go +++ b/components/cluster/command/clean.go @@ -62,6 +62,7 @@ You can retain some nodes and roles data when cleanup the cluster, eg: cmd.Flags().StringArrayVar(&cleanOpt.RetainDataRoles, "ignore-role", nil, "Specify the roles whose data will be retained") cmd.Flags().BoolVar(&cleanOpt.CleanupData, "data", false, "Cleanup data") cmd.Flags().BoolVar(&cleanOpt.CleanupLog, "log", false, "Cleanup log") + cmd.Flags().BoolVar(&cleanOpt.CleanupAuditLog, "audit-log", false, "Cleanup TiUP audit log") cmd.Flags().BoolVar(&cleanALl, "all", false, "Cleanup both log and data") return cmd diff --git a/pkg/cluster/operation/operation.go b/pkg/cluster/operation/operation.go index 1532ba8539..f44813e73f 100644 --- a/pkg/cluster/operation/operation.go +++ b/pkg/cluster/operation/operation.go @@ -47,8 +47,9 @@ type Options struct { SSHProxyTimeout uint64 // timeout in seconds when connecting the proxy host // What type of things should we cleanup in clean command - CleanupData bool // should we cleanup data - CleanupLog bool // should we clenaup log + CleanupData bool // should we cleanup data + CleanupLog bool // should we clenaup log + CleanupAuditLog bool //should we clenaup tiup auit log // Some data will be retained when destroying instances RetainDataRoles []string From 931389147ab45884573bb9cdf22ff9b17939db41 Mon Sep 17 00:00:00 2001 From: srstack Date: Tue, 30 Nov 2021 15:43:30 +0800 Subject: [PATCH 02/10] add --audit-log flag --- components/cluster/command/clean.go | 2 +- pkg/cluster/operation/operation.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/cluster/command/clean.go b/components/cluster/command/clean.go index 616bfa54c6..39c5bb3976 100644 --- a/components/cluster/command/clean.go +++ b/components/cluster/command/clean.go @@ -62,7 +62,7 @@ You can retain some nodes and roles data when cleanup the cluster, eg: cmd.Flags().StringArrayVar(&cleanOpt.RetainDataRoles, "ignore-role", nil, "Specify the roles whose data will be retained") cmd.Flags().BoolVar(&cleanOpt.CleanupData, "data", false, "Cleanup data") cmd.Flags().BoolVar(&cleanOpt.CleanupLog, "log", false, "Cleanup log") - cmd.Flags().BoolVar(&cleanOpt.CleanupAuditLog, "audit-log", false, "Cleanup TiUP audit log") + cmd.Flags().BoolVar(&cleanOpt.CleanupAuditLog, "audit-log", false, "Cleanup TiDB-server audit log") cmd.Flags().BoolVar(&cleanALl, "all", false, "Cleanup both log and data") return cmd diff --git a/pkg/cluster/operation/operation.go b/pkg/cluster/operation/operation.go index f44813e73f..7d8209f56b 100644 --- a/pkg/cluster/operation/operation.go +++ b/pkg/cluster/operation/operation.go @@ -49,7 +49,7 @@ type Options struct { // What type of things should we cleanup in clean command CleanupData bool // should we cleanup data CleanupLog bool // should we clenaup log - CleanupAuditLog bool //should we clenaup tiup auit log + CleanupAuditLog bool // should we clenaup tiup auit log // Some data will be retained when destroying instances RetainDataRoles []string From 161e8c3d4a97e4dd498e1c97039ff45dbca99456 Mon Sep 17 00:00:00 2001 From: srstack Date: Tue, 30 Nov 2021 17:30:23 +0800 Subject: [PATCH 03/10] add --audit-log flag --- components/cluster/command/clean.go | 2 +- pkg/cluster/manager/cleanup.go | 16 ++++++++++++---- pkg/cluster/operation/operation.go | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/components/cluster/command/clean.go b/components/cluster/command/clean.go index 39c5bb3976..e31f4f469a 100644 --- a/components/cluster/command/clean.go +++ b/components/cluster/command/clean.go @@ -63,7 +63,7 @@ You can retain some nodes and roles data when cleanup the cluster, eg: cmd.Flags().BoolVar(&cleanOpt.CleanupData, "data", false, "Cleanup data") cmd.Flags().BoolVar(&cleanOpt.CleanupLog, "log", false, "Cleanup log") cmd.Flags().BoolVar(&cleanOpt.CleanupAuditLog, "audit-log", false, "Cleanup TiDB-server audit log") - cmd.Flags().BoolVar(&cleanALl, "all", false, "Cleanup both log and data") + cmd.Flags().BoolVar(&cleanALl, "all", false, "Cleanup both log and data (not include audit logs)") return cmd } diff --git a/pkg/cluster/manager/cleanup.go b/pkg/cluster/manager/cleanup.go index 336b91beb0..2eeb832af4 100644 --- a/pkg/cluster/manager/cleanup.go +++ b/pkg/cluster/manager/cleanup.go @@ -100,17 +100,24 @@ func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt oper if !skipConfirm { target := "" switch { - case cleanOpt.CleanupData && cleanOpt.CleanupLog: - target = "data and log" + case cleanOpt.CleanupData: target = "data" case cleanOpt.CleanupLog: - target = "log" + target += "log" + case cleanOpt.CleanupAuditLog: + + target += "audit log" } // build file list string delFileList := "" for host, fileList := range delFileMap { + // target host has no files to delete + if len(fileList) == 0 { + continue + } + delFileList += fmt.Sprintf("\n%s:", color.CyanString(host)) for _, dfp := range fileList.Slice() { delFileList += fmt.Sprintf("\n %s", dfp) @@ -128,9 +135,10 @@ func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt oper delFileList); err != nil { return err } - log.Infof("Cleanup cluster...") } + log.Infof("Cleanup cluster...") + b, err := m.sshTaskBuilder(name, topo, base.User, gOpt) if err != nil { return err diff --git a/pkg/cluster/operation/operation.go b/pkg/cluster/operation/operation.go index 7d8209f56b..16555455eb 100644 --- a/pkg/cluster/operation/operation.go +++ b/pkg/cluster/operation/operation.go @@ -49,7 +49,7 @@ type Options struct { // What type of things should we cleanup in clean command CleanupData bool // should we cleanup data CleanupLog bool // should we clenaup log - CleanupAuditLog bool // should we clenaup tiup auit log + CleanupAuditLog bool // should we clenaup tidb server auit log // Some data will be retained when destroying instances RetainDataRoles []string From 872636289bfd6b3836854542dc0d470477da1877 Mon Sep 17 00:00:00 2001 From: srstack Date: Tue, 30 Nov 2021 19:16:17 +0800 Subject: [PATCH 04/10] two confirm --- pkg/cluster/manager/cleanup.go | 76 ++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/pkg/cluster/manager/cleanup.go b/pkg/cluster/manager/cleanup.go index cd3774f25d..0da3a22ab4 100644 --- a/pkg/cluster/manager/cleanup.go +++ b/pkg/cluster/manager/cleanup.go @@ -61,41 +61,7 @@ func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt oper cleanOpt.CleanupData, cleanOpt.CleanupLog, false, cleanOpt.RetainDataRoles, cleanOpt.RetainDataNodes) if !skipConfirm { - target := "" - switch { - - case cleanOpt.CleanupData: - target = "data" - case cleanOpt.CleanupLog: - target += "log" - case cleanOpt.CleanupAuditLog: - - target += "audit log" - } - - // build file list string - delFileList := "" - for host, fileList := range delFileMap { - // target host has no files to delete - if len(fileList) == 0 { - continue - } - - delFileList += fmt.Sprintf("\n%s:", color.CyanString(host)) - for _, dfp := range fileList.Slice() { - delFileList += fmt.Sprintf("\n %s", dfp) - } - } - - if err := tui.PromptForConfirmOrAbortError( - "This operation will stop %s %s cluster %s and clean its' %s.\nNodes will be ignored: %s\nRoles will be ignored: %s\nFiles to be deleted are: %s\nDo you want to continue? [y/N]:", - m.sysName, - color.HiYellowString(base.Version), - color.HiYellowString(name), - target, - cleanOpt.RetainDataNodes, - cleanOpt.RetainDataRoles, - delFileList); err != nil { + if err := cleanupConfirm(name, m.sysName, base.Version, cleanOpt, delFileMap); err != nil { return err } } @@ -127,6 +93,46 @@ func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt oper return nil } +// checkConfirm +func cleanupConfirm(clusterName, sysName, version string, cleanOpt operator.Options, delFileMap map[string]set.StringSet) error { + target := "" + switch { + case cleanOpt.CleanupData: + target += " data" + case cleanOpt.CleanupLog: + target += " log" + case cleanOpt.CleanupAuditLog: + + target += " audit log" + } + + log.Warnf("This clean operation will %s %s %s cluster %s", + color.HiYellowString("stop"), sysName, version, color.HiYellowString(clusterName)) + if err := tui.PromptForConfirmOrAbortError("Do you want to continue? [y/N]:"); err != nil { + return err + } + + // build file list string + delFileList := "" + for host, fileList := range delFileMap { + // target host has no files to delete + if len(fileList) == 0 { + continue + } + + delFileList += fmt.Sprintf("\n%s:", color.CyanString(host)) + for _, dfp := range fileList.Slice() { + delFileList += fmt.Sprintf("\n %s", dfp) + } + } + + log.Warnf("Clean the clutser %s's%s.\nNodes will be ignored: %s\nRoles will be ignored: %s\nFiles to be deleted are: %s", + color.HiYellowString(clusterName), target, cleanOpt.RetainDataNodes, + cleanOpt.RetainDataRoles, + delFileList) + return tui.PromptForConfirmOrAbortError("Do you want to continue? [y/N]:") +} + // cleanupFiles record the file that needs to be cleaned up type cleanupFiles struct { cleanupData bool // whether to clean up the data From 8fb28a9b748628411fb41c2645fd8e87b7898086 Mon Sep 17 00:00:00 2001 From: srstack Date: Tue, 30 Nov 2021 19:59:55 +0800 Subject: [PATCH 05/10] add clean audit log --- components/cluster/command/clean.go | 5 +++-- pkg/cluster/manager/cleanup.go | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/components/cluster/command/clean.go b/components/cluster/command/clean.go index e31f4f469a..78d6cc7bb8 100644 --- a/components/cluster/command/clean.go +++ b/components/cluster/command/clean.go @@ -33,6 +33,7 @@ You can retain some nodes and roles data when cleanup the cluster, eg: $ tiup cluster clean --all $ tiup cluster clean --log $ tiup cluster clean --data + $ tiup cluster clean --audit-log $ tiup cluster clean --all --ignore-role prometheus $ tiup cluster clean --all --ignore-node 172.16.13.11:9000 $ tiup cluster clean --all --ignore-node 172.16.13.12`, @@ -50,7 +51,7 @@ You can retain some nodes and roles data when cleanup the cluster, eg: cleanOpt.CleanupLog = true } - if !(cleanOpt.CleanupData || cleanOpt.CleanupLog) { + if !(cleanOpt.CleanupData || cleanOpt.CleanupLog || cleanOpt.CleanupAuditLog) { return cmd.Help() } @@ -63,7 +64,7 @@ You can retain some nodes and roles data when cleanup the cluster, eg: cmd.Flags().BoolVar(&cleanOpt.CleanupData, "data", false, "Cleanup data") cmd.Flags().BoolVar(&cleanOpt.CleanupLog, "log", false, "Cleanup log") cmd.Flags().BoolVar(&cleanOpt.CleanupAuditLog, "audit-log", false, "Cleanup TiDB-server audit log") - cmd.Flags().BoolVar(&cleanALl, "all", false, "Cleanup both log and data (not include audit logs)") + cmd.Flags().BoolVar(&cleanALl, "all", false, "Cleanup both log and data (not include audit log)") return cmd } diff --git a/pkg/cluster/manager/cleanup.go b/pkg/cluster/manager/cleanup.go index 0da3a22ab4..f326dbec86 100644 --- a/pkg/cluster/manager/cleanup.go +++ b/pkg/cluster/manager/cleanup.go @@ -58,7 +58,7 @@ func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt oper // calculate file paths to be deleted before the prompt delFileMap := getCleanupFiles(topo, - cleanOpt.CleanupData, cleanOpt.CleanupLog, false, cleanOpt.RetainDataRoles, cleanOpt.RetainDataNodes) + cleanOpt.CleanupData, cleanOpt.CleanupLog, false, cleanOpt.CleanupAuditLog, cleanOpt.RetainDataRoles, cleanOpt.RetainDataNodes) if !skipConfirm { if err := cleanupConfirm(name, m.sysName, base.Version, cleanOpt, delFileMap); err != nil { @@ -138,6 +138,7 @@ type cleanupFiles struct { cleanupData bool // whether to clean up the data cleanupLog bool // whether to clean up the log cleanupTLS bool // whether to clean up the tls files + cleanupAuditLog bool // whether to clean up the tidb server audit log retainDataRoles []string // roles that don't clean up retainDataNodes []string // roles that don't clean up delFileMap map[string]set.StringSet @@ -145,11 +146,12 @@ type cleanupFiles struct { // getCleanupFiles get the files that need to be deleted func getCleanupFiles(topo spec.Topology, - cleanupData, cleanupLog, cleanupTLS bool, retainDataRoles, retainDataNodes []string) map[string]set.StringSet { + cleanupData, cleanupLog, cleanupTLS, cleanupAuditLog bool, retainDataRoles, retainDataNodes []string) map[string]set.StringSet { c := &cleanupFiles{ cleanupData: cleanupData, cleanupLog: cleanupLog, cleanupTLS: cleanupTLS, + cleanupAuditLog: cleanupAuditLog, retainDataRoles: retainDataRoles, retainDataNodes: retainDataNodes, delFileMap: make(map[string]set.StringSet), @@ -201,7 +203,19 @@ func (c *cleanupFiles) instanceCleanupFiles(topo spec.Topology) { if c.cleanupLog && len(ins.LogDir()) > 0 { for _, logDir := range strings.Split(ins.LogDir(), ",") { - logPaths.Insert(path.Join(logDir, "*.log")) + // need to judge the audit log of tidb server + if ins.ComponentName() == spec.ComponentTiDB { + logPaths.Insert(path.Join(logDir, "tidb?[!audit]*.log")) + logPaths.Insert(path.Join(logDir, "tidb.log")) // maybe no need deleted + } else { + logPaths.Insert(path.Join(logDir, "*.log")) + } + } + } + + if c.cleanupAuditLog && ins.ComponentName() == spec.ComponentTiDB { + for _, logDir := range strings.Split(ins.LogDir(), ",") { + logPaths.Insert(path.Join(logDir, "tidb-audit*.log")) } } From 6110000d4063aea327857ed14e73662f892f4492 Mon Sep 17 00:00:00 2001 From: srstack Date: Wed, 1 Dec 2021 11:16:51 +0800 Subject: [PATCH 06/10] fix target is Inaccurate --- pkg/cluster/manager/cleanup.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/cluster/manager/cleanup.go b/pkg/cluster/manager/cleanup.go index f326dbec86..0d4cbc909d 100644 --- a/pkg/cluster/manager/cleanup.go +++ b/pkg/cluster/manager/cleanup.go @@ -99,11 +99,13 @@ func cleanupConfirm(clusterName, sysName, version string, cleanOpt operator.Opti switch { case cleanOpt.CleanupData: target += " data" + fallthrough case cleanOpt.CleanupLog: target += " log" + fallthrough case cleanOpt.CleanupAuditLog: - target += " audit log" + // fallthrough } log.Warnf("This clean operation will %s %s %s cluster %s", From b20b0043db65ff92e5b867d5fcd0477c371080d4 Mon Sep 17 00:00:00 2001 From: srstack Date: Wed, 1 Dec 2021 12:44:09 +0800 Subject: [PATCH 07/10] fix target is Inaccurate --- pkg/cluster/manager/cleanup.go | 35 +++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/pkg/cluster/manager/cleanup.go b/pkg/cluster/manager/cleanup.go index 0d4cbc909d..502ecd35e1 100644 --- a/pkg/cluster/manager/cleanup.go +++ b/pkg/cluster/manager/cleanup.go @@ -95,21 +95,8 @@ func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt oper // checkConfirm func cleanupConfirm(clusterName, sysName, version string, cleanOpt operator.Options, delFileMap map[string]set.StringSet) error { - target := "" - switch { - case cleanOpt.CleanupData: - target += " data" - fallthrough - case cleanOpt.CleanupLog: - target += " log" - fallthrough - case cleanOpt.CleanupAuditLog: - target += " audit log" - // fallthrough - } - log.Warnf("This clean operation will %s %s %s cluster %s", - color.HiYellowString("stop"), sysName, version, color.HiYellowString(clusterName)) + color.HiYellowString("stop["), sysName, version, color.HiYellowString(clusterName)) if err := tui.PromptForConfirmOrAbortError("Do you want to continue? [y/N]:"); err != nil { return err } @@ -129,12 +116,30 @@ func cleanupConfirm(clusterName, sysName, version string, cleanOpt operator.Opti } log.Warnf("Clean the clutser %s's%s.\nNodes will be ignored: %s\nRoles will be ignored: %s\nFiles to be deleted are: %s", - color.HiYellowString(clusterName), target, cleanOpt.RetainDataNodes, + color.HiYellowString(clusterName), cleanTarget(cleanOpt), cleanOpt.RetainDataNodes, cleanOpt.RetainDataRoles, delFileList) return tui.PromptForConfirmOrAbortError("Do you want to continue? [y/N]:") } +func cleanTarget(cleanOpt operator.Options) string { + target := "" + + if cleanOpt.CleanupData { + target += " data" + } + + if cleanOpt.CleanupLog { + target += (" log") + } + + if cleanOpt.CleanupAuditLog { + target += ("audit-log") + } + + return color.HiYellowString(target) +} + // cleanupFiles record the file that needs to be cleaned up type cleanupFiles struct { cleanupData bool // whether to clean up the data From 8c5dc550518fa2d963dfd13abbedb4bad27946cf Mon Sep 17 00:00:00 2001 From: srstack Date: Wed, 1 Dec 2021 12:54:12 +0800 Subject: [PATCH 08/10] perfect tips --- pkg/cluster/manager/cleanup.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cluster/manager/cleanup.go b/pkg/cluster/manager/cleanup.go index 502ecd35e1..dc8754460d 100644 --- a/pkg/cluster/manager/cleanup.go +++ b/pkg/cluster/manager/cleanup.go @@ -89,14 +89,14 @@ func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt oper return perrs.Trace(err) } - log.Infof("Cleanup cluster `%s` successfully", name) + log.Infof("Cleanup%s in cluster `%s` successfully", cleanTarget(cleanOpt), name) return nil } // checkConfirm func cleanupConfirm(clusterName, sysName, version string, cleanOpt operator.Options, delFileMap map[string]set.StringSet) error { log.Warnf("This clean operation will %s %s %s cluster %s", - color.HiYellowString("stop["), sysName, version, color.HiYellowString(clusterName)) + color.HiYellowString("stop"), sysName, version, color.HiYellowString(clusterName)) if err := tui.PromptForConfirmOrAbortError("Do you want to continue? [y/N]:"); err != nil { return err } @@ -134,7 +134,7 @@ func cleanTarget(cleanOpt operator.Options) string { } if cleanOpt.CleanupAuditLog { - target += ("audit-log") + target += (" audit-log") } return color.HiYellowString(target) From 900c8fc99432fdca426dc5998359110936666559 Mon Sep 17 00:00:00 2001 From: srstack Date: Wed, 1 Dec 2021 12:59:13 +0800 Subject: [PATCH 09/10] perfect tips --- components/cluster/command/clean.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/cluster/command/clean.go b/components/cluster/command/clean.go index 78d6cc7bb8..e455f64a72 100644 --- a/components/cluster/command/clean.go +++ b/components/cluster/command/clean.go @@ -33,7 +33,7 @@ You can retain some nodes and roles data when cleanup the cluster, eg: $ tiup cluster clean --all $ tiup cluster clean --log $ tiup cluster clean --data - $ tiup cluster clean --audit-log + $ tiup cluster clean --audit-log $ tiup cluster clean --all --ignore-role prometheus $ tiup cluster clean --all --ignore-node 172.16.13.11:9000 $ tiup cluster clean --all --ignore-node 172.16.13.12`, From 099397ae46301c9ec6bbe13c5ef0afb299b0acdf Mon Sep 17 00:00:00 2001 From: srstack Date: Wed, 1 Dec 2021 14:07:35 +0800 Subject: [PATCH 10/10] fix tips --- pkg/cluster/manager/cleanup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cluster/manager/cleanup.go b/pkg/cluster/manager/cleanup.go index dc8754460d..eda862ece9 100644 --- a/pkg/cluster/manager/cleanup.go +++ b/pkg/cluster/manager/cleanup.go @@ -95,7 +95,7 @@ func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt oper // checkConfirm func cleanupConfirm(clusterName, sysName, version string, cleanOpt operator.Options, delFileMap map[string]set.StringSet) error { - log.Warnf("This clean operation will %s %s %s cluster %s", + log.Warnf("The clean operation will %s %s %s cluster `%s`", color.HiYellowString("stop"), sysName, version, color.HiYellowString(clusterName)) if err := tui.PromptForConfirmOrAbortError("Do you want to continue? [y/N]:"); err != nil { return err