github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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/utils/os"
    12  	"gopkg.in/juju/names.v2"
    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  // ComponentDir returns the filesystem path to the directory
    57  // containing all data files for a component.
    58  func (paths Paths) ComponentDir(name string) string {
    59  	return filepath.Join(paths.State.BaseDir, name)
    60  }
    61  
    62  // RuntimePaths represents the set of paths that are relevant at runtime.
    63  type RuntimePaths struct {
    64  
    65  	// JujuRunSocket listens for juju-run invocations, and is always
    66  	// active.
    67  	JujuRunSocket string
    68  
    69  	// JujucServerSocket listens for jujuc invocations, and is only
    70  	// active when supporting a jujuc execution context.
    71  	JujucServerSocket string
    72  }
    73  
    74  // StatePaths represents the set of paths that hold persistent local state for
    75  // the uniter.
    76  type StatePaths struct {
    77  
    78  	// BaseDir is the unit agent's base directory.
    79  	BaseDir string
    80  
    81  	// CharmDir is the directory to which the charm the uniter runs is deployed.
    82  	CharmDir string
    83  
    84  	// OperationsFile holds information about what the uniter is doing
    85  	// and/or has done.
    86  	OperationsFile string
    87  
    88  	// RelationsDir holds relation-specific information about what the
    89  	// uniter is doing and/or has done.
    90  	RelationsDir string
    91  
    92  	// BundlesDir holds downloaded charms.
    93  	BundlesDir string
    94  
    95  	// DeployerDir holds metadata about charms that are installing or have
    96  	// been installed.
    97  	DeployerDir string
    98  
    99  	// StorageDir holds storage-specific information about what the
   100  	// uniter is doing and/or has done.
   101  	StorageDir string
   102  
   103  	// MetricsSpoolDir acts as temporary storage for metrics being sent from
   104  	// the uniter to state.
   105  	MetricsSpoolDir string
   106  }
   107  
   108  // NewPaths returns the set of filesystem paths that the supplied unit should
   109  // use, given the supplied root juju data directory path.
   110  func NewPaths(dataDir string, unitTag names.UnitTag) Paths {
   111  	return NewWorkerPaths(dataDir, unitTag, "")
   112  }
   113  
   114  // NewWorkerPaths returns the set of filesystem paths that the supplied unit worker should
   115  // use, given the supplied root juju data directory path and worker identifier.
   116  // Distinct worker identifiers ensure that runtime paths of different worker do not interfere.
   117  func NewWorkerPaths(dataDir string, unitTag names.UnitTag, worker string) Paths {
   118  	join := filepath.Join
   119  	baseDir := join(dataDir, "agents", unitTag.String())
   120  	stateDir := join(baseDir, "state")
   121  
   122  	socket := func(name string, abstract bool) string {
   123  		if os.HostOS() == os.Windows {
   124  			base := fmt.Sprintf("%s", unitTag)
   125  			if worker != "" {
   126  				base = fmt.Sprintf("%s-%s", unitTag, worker)
   127  			}
   128  			return fmt.Sprintf(`\\.\pipe\%s-%s`, base, name)
   129  		}
   130  		path := join(baseDir, name+".socket")
   131  		if worker != "" {
   132  			path = join(baseDir, fmt.Sprintf("%s-%s.socket", worker, name))
   133  		}
   134  		if abstract {
   135  			path = "@" + path
   136  		}
   137  		return path
   138  	}
   139  
   140  	toolsDir := tools.ToolsDir(dataDir, unitTag.String())
   141  	return Paths{
   142  		ToolsDir: filepath.FromSlash(toolsDir),
   143  		Runtime: RuntimePaths{
   144  			JujuRunSocket:     socket("run", false),
   145  			JujucServerSocket: socket("agent", true),
   146  		},
   147  		State: StatePaths{
   148  			BaseDir:         baseDir,
   149  			CharmDir:        join(baseDir, "charm"),
   150  			OperationsFile:  join(stateDir, "uniter"),
   151  			RelationsDir:    join(stateDir, "relations"),
   152  			BundlesDir:      join(stateDir, "bundles"),
   153  			DeployerDir:     join(stateDir, "deployer"),
   154  			StorageDir:      join(stateDir, "storage"),
   155  			MetricsSpoolDir: join(stateDir, "spool", "metrics"),
   156  		},
   157  	}
   158  }