agentctl.go 2.2 KB
Newer Older
O
ob-robot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
package agent

import (
	"os/exec"

	log "github.com/sirupsen/logrus"

	"github.com/oceanbase/obagent/lib/mask"
	"github.com/oceanbase/obagent/lib/path"
)

type AgentctlCmd struct {
	agentctlPath string
}

// AgentctlResponse the formatted content written to stdout or stderr
type AgentctlResponse struct {
	Successful bool        `json:"successful"`
	Message    interface{} `json:"message"`
	Error      string      `json:"error"`
}

func NewAgentctlCmd() *AgentctlCmd {
	return &AgentctlCmd{
		agentctlPath: path.AgentctlPath(),
	}
}

func (c *AgentctlCmd) StopService(param StartStopServiceParam) error {
	args := []string{"service", "stop", param.Service, "--task-token=" + param.TaskToken.TaskToken}
	log.Infof("execute agentctl '%s' %v", c.agentctlPath, args)
	result, err := exec.Command(c.agentctlPath, args...).CombinedOutput()
	if err != nil {
		return AgentctlStopServiceFailedErr.NewError(string(result)).WithCause(err)
	}
	return nil
}

func (c *AgentctlCmd) StartService(param StartStopServiceParam) error {
	args := []string{"service", "start", param.Service, "--task-token=" + param.TaskToken.TaskToken}
	log.Infof("execute agentctl '%s' %v", c.agentctlPath, args)
	result, err := exec.Command(c.agentctlPath, args...).CombinedOutput()
	if err != nil {
		return AgentctlStartServiceFailedErr.NewError(string(result)).WithCause(err)
	}
	return nil
}

func (c *AgentctlCmd) Restart(token TaskToken) error {
	args := []string{"restart", "--task-token=" + token.TaskToken}
	log.Infof("execute agentctl '%s' %v", c.agentctlPath, args)
	result, err := exec.Command(c.agentctlPath, args...).CombinedOutput()
	if err != nil {
		return AgentctlRestartFailedErr.NewError(string(result)).WithCause(err)
	}
	return nil
}

func (c *AgentctlCmd) Reinstall(param ReinstallParam) error {
	args := []string{
		"reinstall",
		"--source=" + param.Source,
		"--checksum=" + param.Checksum,
		"--version=" + param.Version,
		"--task-token=" + param.TaskToken.TaskToken,
	}
	log.Infof("execute agentctl '%s' %v", c.agentctlPath, mask.MaskSlice(args))
	result, err := exec.Command(c.agentctlPath, args...).CombinedOutput()
	if err != nil {
		return AgentctlReinstallFailedErr.NewError(string(result)).WithCause(err)
	}
	return nil
}