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