github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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  	"github.com/juju/names/v5"
    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  // GetBaseDir exists to satisfy the context.Paths interface.
    40  func (paths Paths) GetBaseDir() string {
    41  	return paths.State.BaseDir
    42  }
    43  
    44  // GetCharmDir exists to satisfy the context.Paths interface.
    45  func (paths Paths) GetCharmDir() string {
    46  	return paths.State.CharmDir
    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  // ComponentDir returns the filesystem path to the directory
    55  // containing all data files for a component.
    56  func (paths Paths) ComponentDir(name string) string {
    57  	return filepath.Join(paths.State.BaseDir, name)
    58  }
    59  
    60  // RuntimePaths represents the set of paths that are relevant at runtime.
    61  type RuntimePaths struct {
    62  
    63  	// JujuExecSocket listens for juju-exec invocations, and is always
    64  	// active.
    65  	JujuExecSocket string
    66  
    67  	// HookCommandServerSocket listens for hook command invocations, and is only
    68  	// active when supporting a hook execution context.
    69  	HookCommandServerSocket string
    70  }
    71  
    72  // StatePaths represents the set of paths that hold persistent local state for
    73  // the operator.
    74  type StatePaths struct {
    75  
    76  	// BaseDir is the operator's base directory.
    77  	BaseDir string
    78  
    79  	// CharmDir is the directory to which the charm the operator runs is deployed.
    80  	CharmDir string
    81  
    82  	// OperationsFile holds information about what the operator is doing
    83  	// and/or has done.
    84  	OperationsFile string
    85  
    86  	// BundlesDir holds downloaded charms.
    87  	BundlesDir string
    88  
    89  	// DeployerDir holds metadata about charms that are installing or have
    90  	// been installed.
    91  	DeployerDir string
    92  
    93  	// MetricsSpoolDir acts as temporary storage for metrics being sent from
    94  	// the operator to state.
    95  	MetricsSpoolDir string
    96  }
    97  
    98  // NewPaths returns the set of filesystem paths that the supplied unit should
    99  // use, given the supplied root juju data directory path.
   100  func NewPaths(dataDir string, applicationTag names.ApplicationTag) Paths {
   101  	join := filepath.Join
   102  	baseDir := join(dataDir, "agents", applicationTag.String())
   103  	stateDir := join(baseDir, "state")
   104  
   105  	toolsDir := tools.ToolsDir(dataDir, "")
   106  	return Paths{
   107  		ToolsDir: filepath.FromSlash(toolsDir),
   108  		State: StatePaths{
   109  			BaseDir:         baseDir,
   110  			CharmDir:        join(baseDir, "charm"),
   111  			BundlesDir:      join(stateDir, "bundles"),
   112  			DeployerDir:     join(stateDir, "deployer"),
   113  			OperationsFile:  join(stateDir, "operator"),
   114  			MetricsSpoolDir: join(stateDir, "spool", "metrics"),
   115  		},
   116  	}
   117  }