github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/service/agentinfo.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/utils/shell"
    11  
    12  	"github.com/juju/juju/agent/tools"
    13  )
    14  
    15  // AgentKind identifies the kind of agent.
    16  type AgentKind string
    17  
    18  // These are the agent kinds in juju.
    19  const (
    20  	AgentKindMachine AgentKind = "machine"
    21  	AgentKindUnit    AgentKind = "unit"
    22  )
    23  
    24  var idOptions = map[AgentKind]string{
    25  	AgentKindMachine: "--machine-id",
    26  	AgentKindUnit:    "--unit-name",
    27  }
    28  
    29  // AgentInfo holds commonly used information about a juju agent.
    30  type AgentInfo struct {
    31  	name string
    32  
    33  	// ID is the string that identifies the agent uniquely within
    34  	// a juju environment.
    35  	ID string
    36  
    37  	// Kind is the kind of agent.
    38  	Kind AgentKind
    39  
    40  	// DataDir is the path to the agent's data dir.
    41  	DataDir string
    42  
    43  	// LogDir is the path to the agent's log dir.
    44  	LogDir string
    45  }
    46  
    47  // NewAgentInfo composes a new AgentInfo for the given essentials.
    48  func NewAgentInfo(kind AgentKind, id, dataDir, logDir string) AgentInfo {
    49  	name := fmt.Sprintf("%s-%s", kind, strings.Replace(id, "/", "-", -1))
    50  
    51  	info := AgentInfo{
    52  		Kind:    kind,
    53  		ID:      id,
    54  		DataDir: dataDir,
    55  		LogDir:  logDir,
    56  
    57  		name: name,
    58  	}
    59  	return info
    60  }
    61  
    62  // NewMachineAgentInfo returns a new AgentInfo for a machine agent.
    63  func NewMachineAgentInfo(id, dataDir, logDir string) AgentInfo {
    64  	return NewAgentInfo(AgentKindMachine, id, dataDir, logDir)
    65  }
    66  
    67  // NewUnitAgentInfo returns a new AgentInfo for a unit agent.
    68  func NewUnitAgentInfo(id, dataDir, logDir string) AgentInfo {
    69  	return NewAgentInfo(AgentKindUnit, id, dataDir, logDir)
    70  }
    71  
    72  // ToolsDir returns the path to the agent's tools dir.
    73  func (ai AgentInfo) ToolsDir(renderer shell.Renderer) string {
    74  	return renderer.FromSlash(tools.ToolsDir(ai.DataDir, ai.name))
    75  }
    76  
    77  func (ai AgentInfo) jujud(renderer shell.Renderer) string {
    78  	exeName := "jujud" + renderer.ExeSuffix()
    79  	return renderer.Join(ai.ToolsDir(renderer), exeName)
    80  }
    81  
    82  func (ai AgentInfo) cmd(renderer shell.Renderer) string {
    83  	// The agent always starts with debug turned on. The logger worker
    84  	// will update this to the system logging environment as soon as
    85  	// it starts.
    86  	return strings.Join([]string{
    87  		renderer.Quote(ai.jujud(renderer)),
    88  		string(ai.Kind),
    89  		"--data-dir", renderer.Quote(renderer.FromSlash(ai.DataDir)),
    90  		idOptions[ai.Kind], ai.ID,
    91  		"--debug",
    92  	}, " ")
    93  }
    94  
    95  // execArgs returns an unquoted array of service arguments in case we need
    96  // them later. One notable place where this is needed, is the windows service
    97  // package, where CreateService correctly does quoting of executable path and
    98  // individual arguments
    99  func (ai AgentInfo) execArgs(renderer shell.Renderer) []string {
   100  	return []string{
   101  		string(ai.Kind),
   102  		"--data-dir", renderer.FromSlash(ai.DataDir),
   103  		idOptions[ai.Kind], ai.ID,
   104  		"--debug",
   105  	}
   106  }
   107  
   108  func (ai AgentInfo) logFile(renderer shell.Renderer) string {
   109  	return renderer.Join(renderer.FromSlash(ai.LogDir), ai.name+".log")
   110  }