github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/uniter/paths.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package uniter
     6  
     7  import (
     8  	"fmt"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/names"
    12  	"github.com/juju/utils/os"
    13  
    14  	"github.com/juju/juju/agent/tools"
    15  )
    16  
    17  // Paths represents the set of filesystem paths a uniter worker has reason to
    18  // care about.
    19  type Paths struct {
    20  
    21  	// ToolsDir is the directory containing the jujud executable running this
    22  	// process; and also containing jujuc tool symlinks to that executable. It's
    23  	// the only path in this struct that is not typically pointing inside the
    24  	// directory reserved for the exclusive use of this worker (typically
    25  	// /var/lib/juju/agents/$UNIT_TAG/ )
    26  	ToolsDir string
    27  
    28  	// Runtime represents the set of paths that are relevant at runtime.
    29  	Runtime RuntimePaths
    30  
    31  	// State represents the set of paths that hold persistent local state for
    32  	// the uniter.
    33  	State StatePaths
    34  }
    35  
    36  // GetToolsDir exists to satisfy the context.Paths interface.
    37  func (paths Paths) GetToolsDir() string {
    38  	return paths.ToolsDir
    39  }
    40  
    41  // GetCharmDir exists to satisfy the context.Paths interface.
    42  func (paths Paths) GetCharmDir() string {
    43  	return paths.State.CharmDir
    44  }
    45  
    46  // GetJujucSocket exists to satisfy the context.Paths interface.
    47  func (paths Paths) GetJujucSocket() string {
    48  	return paths.Runtime.JujucServerSocket
    49  }
    50  
    51  // GetMetricsSpoolDir exists to satisfy the runner.Paths interface.
    52  func (paths Paths) GetMetricsSpoolDir() string {
    53  	return paths.State.MetricsSpoolDir
    54  }
    55  
    56  // RuntimePaths represents the set of paths that are relevant at runtime.
    57  type RuntimePaths struct {
    58  
    59  	// JujuRunSocket listens for juju-run invocations, and is always
    60  	// active.
    61  	JujuRunSocket string
    62  
    63  	// JujucServerSocket listens for jujuc invocations, and is only
    64  	// active when supporting a jujuc execution context.
    65  	JujucServerSocket string
    66  }
    67  
    68  // StatePaths represents the set of paths that hold persistent local state for
    69  // the uniter.
    70  type StatePaths struct {
    71  
    72  	// CharmDir is the directory to which the charm the uniter runs is deployed.
    73  	CharmDir string
    74  
    75  	// OperationsFile holds information about what the uniter is doing
    76  	// and/or has done.
    77  	OperationsFile string
    78  
    79  	// RelationsDir holds relation-specific information about what the
    80  	// uniter is doing and/or has done.
    81  	RelationsDir string
    82  
    83  	// BundlesDir holds downloaded charms.
    84  	BundlesDir string
    85  
    86  	// DeployerDir holds metadata about charms that are installing or have
    87  	// been installed.
    88  	DeployerDir string
    89  
    90  	// StorageDir holds storage-specific information about what the
    91  	// uniter is doing and/or has done.
    92  	StorageDir string
    93  
    94  	// MetricsSpoolDir acts as temporary storage for metrics being sent from
    95  	// the uniter to state.
    96  	MetricsSpoolDir string
    97  }
    98  
    99  // NewPaths returns the set of filesystem paths that the supplied unit should
   100  // use, given the supplied root juju data directory path.
   101  func NewPaths(dataDir string, unitTag names.UnitTag) Paths {
   102  
   103  	join := filepath.Join
   104  	baseDir := join(dataDir, "agents", unitTag.String())
   105  	stateDir := join(baseDir, "state")
   106  
   107  	socket := func(name string, abstract bool) string {
   108  		if os.HostOS() == os.Windows {
   109  			return fmt.Sprintf(`\\.\pipe\%s-%s`, unitTag, name)
   110  		}
   111  		path := join(baseDir, name+".socket")
   112  		if abstract {
   113  			path = "@" + path
   114  		}
   115  		return path
   116  	}
   117  
   118  	toolsDir := tools.ToolsDir(dataDir, unitTag.String())
   119  	return Paths{
   120  		ToolsDir: filepath.FromSlash(toolsDir),
   121  		Runtime: RuntimePaths{
   122  			JujuRunSocket:     socket("run", false),
   123  			JujucServerSocket: socket("agent", true),
   124  		},
   125  		State: StatePaths{
   126  			CharmDir:        join(baseDir, "charm"),
   127  			OperationsFile:  join(stateDir, "uniter"),
   128  			RelationsDir:    join(stateDir, "relations"),
   129  			BundlesDir:      join(stateDir, "bundles"),
   130  			DeployerDir:     join(stateDir, "deployer"),
   131  			StorageDir:      join(stateDir, "storage"),
   132  			MetricsSpoolDir: join(stateDir, "spool", "metrics"),
   133  		},
   134  	}
   135  }