github.com/pingcap/tiup@v1.15.1/components/dm/command/audit.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package command
    15  
    16  import (
    17  	"github.com/pingcap/errors"
    18  	"github.com/pingcap/tiup/pkg/cluster/audit"
    19  	cspec "github.com/pingcap/tiup/pkg/cluster/spec"
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  var retainDays int
    24  
    25  func newAuditCmd() *cobra.Command {
    26  	cmd := &cobra.Command{
    27  		Use:   "audit [audit-id]",
    28  		Short: "Show audit log of cluster operation",
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			switch len(args) {
    31  			case 0:
    32  				return audit.ShowAuditList(cspec.AuditDir())
    33  			case 1:
    34  				return audit.ShowAuditLog(cspec.AuditDir(), args[0])
    35  			default:
    36  				return cmd.Help()
    37  			}
    38  		},
    39  	}
    40  	cmd.AddCommand(newAuditCleanupCmd())
    41  	return cmd
    42  }
    43  
    44  func newAuditCleanupCmd() *cobra.Command {
    45  	cmd := &cobra.Command{
    46  		Use:   "cleanup",
    47  		Short: "cleanup dm audit logs",
    48  		RunE: func(cmd *cobra.Command, args []string) error {
    49  			if retainDays < 0 {
    50  				return errors.Errorf("retain-days cannot be less than 0")
    51  			}
    52  
    53  			err := audit.DeleteAuditLog(cspec.AuditDir(), retainDays, skipConfirm, gOpt.DisplayMode)
    54  			if err != nil {
    55  				return err
    56  			}
    57  			return nil
    58  		},
    59  	}
    60  
    61  	cmd.Flags().IntVar(&retainDays, "retain-days", 60, "Number of days to keep audit logs for deletion")
    62  	return cmd
    63  }