Skip to content

Commit

Permalink
Support deploy and manage dm 2.0 (#587)
Browse files Browse the repository at this point in the history
This PR refactor some code and make tiup-dm support all commands like tiup-cluster(except for `check` and `import`)
- code ` components/cluster/command/*` are move into `pkg/cluster/deploy/` and change it to deploy a general cluster instead of tidb cluster, both tiup-cluster and tiup-dm use it to avoid many duplicate codes, and maybe later for some tiup-** to use it too.
- move `pkg/dm/` into `components/dm`
- many code in pkg/cluster/* are change to support work with a general cluster, I prefer to move code `tiup-cluser` specified code into component/cluster later.
- note tiup-dm does not support monitor yet in this pr.

all the existing command are supported:
```
Deploy a DM cluster for production

Usage:
  tiup-dm [flags]
  tiup-dm [command]

Available Commands:
  deploy      Deploy a DM cluster for production
  start       Start a DM cluster
  stop        Stop a DM cluster
  restart     Restart a DM cluster
  list        List all clusters
  destroy     Destroy a specified DM cluster
  audit       Show audit log of cluster operation
  exec        Run shell command on host in the dm cluster
  edit-config Edit DM cluster config
  display     Display information of a DM cluster
  reload      Reload a DM cluster's config and restart if needed
  upgrade     Upgrade a specified DM cluster
  patch       Replace the remote package with a specified package and restart the service
  scale-out   Scale out a DM cluster
  scale-in    Scale in a DM cluster
  help        Help about any command

Flags:
  -h, --help               help for tiup-dm
      --ssh-timeout int    Timeout in seconds to connect host via SSH, ignored for operations that don't need an SSH connection. (default 5)
  -v, --version            version for tiup-dm
      --wait-timeout int   Timeout in seconds to wait for an operation to complete, ignored for operations that don't fit. (default 60)
  -y, --yes                Skip all confirmations and assumes 'yes'

Use "tiup-dm help [command]" for more information about a command.
```

a cluster may look like:
```
root@control:/tiup-cluster# tdm display test
dm Cluster: test
dm Version: nightly
ID                 Role       Host          Ports      OS/Arch       Status     Data Dir                   Deploy Dir
--                 ----       ----          -----      -------       ------     --------                   ----------
172.19.0.101:8261  dm-master  172.19.0.101  8261/8291  linux/x86_64  Healthy    data                       deploy/dm-master-8261
172.19.0.101:8361  dm-master  172.19.0.101  8361/8292  linux/x86_64  Unhealthy  data                       deploy/dm-master-8361
172.19.0.102:8261  dm-master  172.19.0.102  8261/8291  linux/x86_64  Healthy    data                       deploy/dm-master-8261
172.19.0.103:8261  dm-master  172.19.0.103  8261/8291  linux/x86_64  Healthy    /home/tidb/my_master_data  deploy/dm-master-8261
172.19.0.104:8261  dm-master  172.19.0.104  8261/8291  linux/x86_64  Healthy|L  data                       deploy/dm-master-8261
172.19.0.105:8261  dm-master  172.19.0.105  8261/8291  linux/x86_64  Healthy    data                       deploy/dm-master-8261
172.19.0.101:8280  dm-portal  172.19.0.101  8280       linux/x86_64  Up         data                       deploy/dm-portal-8280
172.19.0.102:8280  dm-portal  172.19.0.102  8280       linux/x86_64  Up         data                       deploy/dm-portal-8280
172.19.0.101:8262  dm-worker  172.19.0.101  8262       linux/x86_64  Free       data                       deploy/dm-worker-8262
172.19.0.101:8263  dm-worker  172.19.0.101  8263       linux/x86_64  Free       data                       deploy/dm-worker-8263
172.19.0.102:8262  dm-worker  172.19.0.102  8262       linux/x86_64  Free       data                       deploy/dm-worker-8262
172.19.0.103:8262  dm-worker  172.19.0.103  8262       linux/x86_64  Free       data                       deploy/dm-worker-8262
172.19.0.104:8262  dm-worker  172.19.0.104  8262       linux/x86_64  Free       data                       deploy/dm-worker-8262
```
  • Loading branch information
july2993 authored Jul 29, 2020
1 parent 651808e commit 5f45fb7
Show file tree
Hide file tree
Showing 76 changed files with 3,230 additions and 4,344 deletions.
92 changes: 3 additions & 89 deletions components/cluster/command/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,8 @@
package command

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"time"

"github.com/fatih/color"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/base52"
"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/cluster/audit"
"github.com/pingcap/tiup/pkg/cluster/spec"
tiuputils "github.com/pingcap/tiup/pkg/utils"
"github.com/spf13/cobra"
)

Expand All @@ -38,87 +26,13 @@ func newAuditCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
switch len(args) {
case 0:
return showAuditList()
return audit.ShowAuditList(spec.AuditDir())
case 1:
return showAuditLog(args[0])
return audit.ShowAuditLog(spec.AuditDir(), args[0])
default:
return cmd.Help()
}
},
}
return cmd
}

func showAuditList() error {
firstLine := func(fileName string) (string, error) {
file, err := os.Open(spec.ProfilePath(spec.TiOpsAuditDir, fileName))
if err != nil {
return "", errors.Trace(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
if scanner.Scan() {
return scanner.Text(), nil
}
return "", errors.New("unknown audit log format")
}

auditDir := spec.ProfilePath(spec.TiOpsAuditDir)
// Header
clusterTable := [][]string{{"ID", "Time", "Command"}}
fileInfos, err := ioutil.ReadDir(auditDir)
if err != nil && !os.IsNotExist(err) {
return err
}
for _, fi := range fileInfos {
if fi.IsDir() {
continue
}
ts, err := base52.Decode(fi.Name())
if err != nil {
continue
}
t := time.Unix(ts, 0)
cmd, err := firstLine(fi.Name())
if err != nil {
continue
}
clusterTable = append(clusterTable, []string{
fi.Name(),
t.Format(time.RFC3339),
cmd,
})
}

sort.Slice(clusterTable[1:], func(i, j int) bool {
return clusterTable[i+1][1] > clusterTable[j+1][1]
})

cliutil.PrintTable(clusterTable, true)
return nil
}

func showAuditLog(auditID string) error {
path := spec.ProfilePath(spec.TiOpsAuditDir, auditID)
if tiuputils.IsNotExist(path) {
return errors.Errorf("cannot find the audit log '%s'", auditID)
}

ts, err := base52.Decode(auditID)
if err != nil {
return errors.Annotatef(err, "unrecognized audit id '%s'", auditID)
}

content, err := ioutil.ReadFile(path)
if err != nil {
return errors.Trace(err)
}

t := time.Unix(ts, 0)
hint := fmt.Sprintf("- OPERATION TIME: %s -", t.Format("2006-01-02T15:04:05"))
line := strings.Repeat("-", len(hint))
_, _ = os.Stdout.WriteString(color.MagentaString("%s\n%s\n%s\n", line, hint, line))
_, _ = os.Stdout.Write(content)
return nil
}
3 changes: 0 additions & 3 deletions components/cluster/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/cluster/task"
"github.com/pingcap/tiup/pkg/logger"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/meta"
tiuputils "github.com/pingcap/tiup/pkg/utils"
Expand Down Expand Up @@ -63,8 +62,6 @@ conflict checks with other clusters`,
return cmd.Help()
}

logger.EnableAuditLog()

var topo spec.Specification
if opt.existCluster { // check for existing cluster
clusterName := args[0]
Expand Down
Loading

0 comments on commit 5f45fb7

Please sign in to comment.