github.com/Axway/agent-sdk@v1.1.101/pkg/cmd/service/service.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  
     8  	corecmd "github.com/Axway/agent-sdk/pkg/cmd"
     9  	"github.com/Axway/agent-sdk/pkg/cmd/service/daemon"
    10  )
    11  
    12  var (
    13  	// Name -
    14  	Name string
    15  
    16  	dependencies = []string{"network-online.target"}
    17  
    18  	globalAgentService *AgentService
    19  
    20  	execCommand = exec.Command
    21  )
    22  
    23  // AgentService -
    24  type AgentService struct {
    25  	service     daemon.Daemon
    26  	Name        string
    27  	Description string
    28  	Path        string
    29  	PathArg     string
    30  	EnvFile     string
    31  	User        string
    32  	Group       string
    33  }
    34  
    35  func newAgentService() (*AgentService, error) {
    36  	service, err := daemon.New(Name, corecmd.BuildAgentDescription, dependencies...)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	return &AgentService{
    42  		service:     service,
    43  		Name:        Name,
    44  		Description: corecmd.BuildAgentDescription,
    45  	}, nil
    46  }
    47  
    48  // HandleServiceFlag - handles the action needed based on the service flag value
    49  func (a *AgentService) HandleServiceFlag(command string) error {
    50  	var err error
    51  	var status string
    52  	var serviceName string
    53  	// complete the appropriate action for the service
    54  	switch strings.ToLower(command) {
    55  	case "install":
    56  		fmt.Println("installing the agent service")
    57  		fmt.Printf("service will look for config file at %s\n", a.Path)
    58  		fmt.Printf("name of service to be installed: %s\n", a.service.GetServiceName())
    59  
    60  		a.service.SetEnvFile(a.EnvFile)
    61  		a.service.SetUser(a.User)
    62  		a.service.SetGroup(a.Group)
    63  		_, err = a.service.Install(a.PathArg, a.Path)
    64  	case "update":
    65  		fmt.Println("updating the agent service")
    66  
    67  		a.service.SetEnvFile(a.EnvFile)
    68  		a.service.SetUser(a.User)
    69  		a.service.SetGroup(a.Group)
    70  		_, err = a.service.Update(a.PathArg, a.Path)
    71  	case "remove":
    72  		fmt.Println("removing the agent service")
    73  		_, err = a.service.Remove()
    74  	case "start":
    75  		fmt.Println("starting the agent service")
    76  		_, err = a.service.Start()
    77  	case "stop":
    78  		fmt.Println("stopping the agent service")
    79  		_, err = a.service.Stop()
    80  	case "status":
    81  		fmt.Println("getting the agent service status")
    82  		status, err = a.service.Status()
    83  	case "enable":
    84  		fmt.Println("setting the agent to start on reboot")
    85  		_, err = a.service.Enable()
    86  	case "logs":
    87  		var logs string
    88  		fmt.Println("getting the service logs")
    89  		logs, err = a.service.Logs()
    90  		fmt.Println(logs)
    91  	case "name":
    92  		fmt.Println("getting the service name")
    93  		serviceName = a.service.GetServiceName()
    94  	default:
    95  		err = fmt.Errorf("unknown value of '%s' given", command)
    96  	}
    97  
    98  	// error hit
    99  	if err != nil {
   100  		fmt.Printf("service %s command failed: %s\n", strings.ToLower(command), err.Error())
   101  	} else {
   102  		fmt.Printf("service %s command succeeded\n", strings.ToLower(command))
   103  		if status != "" {
   104  			fmt.Println(status)
   105  		}
   106  		if serviceName != "" {
   107  			fmt.Println(serviceName)
   108  		}
   109  	}
   110  	return err
   111  }