github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/caasoperator/paths.go (about)

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