package cmd

import (
	"os/exec"

	log "github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

// stopCmd represents the stop command
var stopCmd = &cobra.Command{
	Use:   "stop",
	Short: "Stop the TiDB cluster",
	Long:  `Stop the services of TiDB cluster which includes PD, TiDB, TiKV, Monitor(Prometheus/Pushgateway) and Dashboard UI..`,
	Run:   stop,
}

func init() {
	rootCmd.AddCommand(stopCmd)
}

func stop(cmd *cobra.Command, args []string) {
	preTask()

	go progressBar(stopTaskCount)

	command := exec.Command("sh", "-c", "ansible-playbook stop.yml")
	err := command.Run()
	if err != nil {
		printFailedLog()
		log.Fatalln("failed to stop the TiDB cluster.")
	}

	postTask()
}
