github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/paths/paths.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Copyright 2014 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package paths
     6  
     7  import (
     8  	"os"
     9  	"runtime"
    10  
    11  	"github.com/juju/juju/core/os/ostype"
    12  )
    13  
    14  type OS int // strongly typed runtime.GOOS value to help with refactoring
    15  
    16  const (
    17  	OSWindows  OS = 1
    18  	OSUnixLike OS = 2
    19  )
    20  
    21  type osVarType int
    22  
    23  const (
    24  	tmpDir osVarType = iota
    25  	logDir
    26  	dataDir
    27  	storageDir
    28  	confDir
    29  	jujuExec
    30  	certDir
    31  	metricsSpoolDir
    32  	uniterStateDir
    33  	jujuDumpLogs
    34  	jujuIntrospect
    35  	instanceCloudInitDir
    36  	cloudInitCfgDir
    37  	curtinInstallConfig
    38  	transientDataDir
    39  	controlSocket
    40  )
    41  
    42  const (
    43  	// NixDataDir is location for agent binaries on *nix operating systems.
    44  	NixDataDir = "/var/lib/juju"
    45  
    46  	// NixTransientDataDir is location for storing transient data on *nix
    47  	// operating systems.
    48  	NixTransientDataDir = "/var/run/juju"
    49  
    50  	// NixLogDir is location for Juju logs on *nix operating systems.
    51  	NixLogDir = "/var/log"
    52  )
    53  
    54  var nixVals = map[osVarType]string{
    55  	tmpDir:               "/tmp",
    56  	logDir:               NixLogDir,
    57  	dataDir:              NixDataDir,
    58  	transientDataDir:     NixTransientDataDir,
    59  	storageDir:           "/var/lib/juju/storage",
    60  	confDir:              "/etc/juju",
    61  	jujuExec:             "/usr/bin/juju-exec",
    62  	jujuDumpLogs:         "/usr/bin/juju-dumplogs",
    63  	jujuIntrospect:       "/usr/bin/juju-introspect",
    64  	certDir:              "/etc/juju/certs.d",
    65  	metricsSpoolDir:      "/var/lib/juju/metricspool",
    66  	uniterStateDir:       "/var/lib/juju/uniter/state",
    67  	instanceCloudInitDir: "/var/lib/cloud/instance",
    68  	cloudInitCfgDir:      "/etc/cloud/cloud.cfg.d",
    69  	curtinInstallConfig:  "/root/curtin-install-cfg.yaml",
    70  	controlSocket:        "/var/lib/juju/control.socket",
    71  }
    72  
    73  var winVals = map[osVarType]string{
    74  	tmpDir:           "C:/Juju/tmp",
    75  	logDir:           "C:/Juju/log",
    76  	dataDir:          "C:/Juju/lib/juju",
    77  	transientDataDir: "C:/Juju/lib/juju-transient",
    78  	storageDir:       "C:/Juju/lib/juju/storage",
    79  	confDir:          "C:/Juju/etc",
    80  	jujuExec:         "C:/Juju/bin/juju-exec.exe",
    81  	jujuDumpLogs:     "C:/Juju/bin/juju-dumplogs.exe",
    82  	jujuIntrospect:   "C:/Juju/bin/juju-introspect.exe",
    83  	certDir:          "C:/Juju/certs",
    84  	metricsSpoolDir:  "C:/Juju/lib/juju/metricspool",
    85  	uniterStateDir:   "C:/Juju/lib/juju/uniter/state",
    86  }
    87  
    88  // Chown is a variable here so it can be mocked out in tests to a no-op.
    89  // Agents run as root, but users don't.
    90  var Chown = os.Chown
    91  
    92  // CurrentOS returns the OS value for the currently-running system.
    93  func CurrentOS() OS {
    94  	switch runtime.GOOS {
    95  	case "windows":
    96  		return OSWindows
    97  	default:
    98  		return OSUnixLike
    99  	}
   100  }
   101  
   102  // OSType converts the given os name to an OS value.
   103  func OSType(osName string) OS {
   104  	switch ostype.OSTypeForName(osName) {
   105  	case ostype.Windows:
   106  		return OSWindows
   107  	default:
   108  		return OSUnixLike
   109  	}
   110  }
   111  
   112  // osVal will lookup the value of the key valname
   113  // in the appropriate map, based on the OS value.
   114  func osVal(os OS, valname osVarType) string {
   115  	switch os {
   116  	case OSWindows:
   117  		return winVals[valname]
   118  	default:
   119  		return nixVals[valname]
   120  	}
   121  }
   122  
   123  // LogDir returns filesystem path the directory where juju may
   124  // save log files.
   125  func LogDir(os OS) string {
   126  	return osVal(os, logDir)
   127  }
   128  
   129  // DataDir returns a filesystem path to the folder used by juju to
   130  // store tools, charms, locks, etc
   131  func DataDir(os OS) string {
   132  	return osVal(os, dataDir)
   133  }
   134  
   135  // TransientDataDir returns a filesystem path to the folder used by juju to
   136  // store transient data that will not survive a reboot.
   137  func TransientDataDir(os OS) string {
   138  	return osVal(os, transientDataDir)
   139  }
   140  
   141  // MetricsSpoolDir returns a filesystem path to the folder used by juju
   142  // to store metrics.
   143  func MetricsSpoolDir(os OS) string {
   144  	return osVal(os, metricsSpoolDir)
   145  }
   146  
   147  // CertDir returns a filesystem path to the folder used by juju to
   148  // store certificates that are added by default to the Juju client
   149  // api certificate pool.
   150  func CertDir(os OS) string {
   151  	return osVal(os, certDir)
   152  }
   153  
   154  // StorageDir returns a filesystem path to the folder used by juju to
   155  // mount machine-level storage.
   156  func StorageDir(os OS) string {
   157  	return osVal(os, storageDir)
   158  }
   159  
   160  // ConfDir returns the path to the directory where Juju may store
   161  // configuration files.
   162  func ConfDir(os OS) string {
   163  	return osVal(os, confDir)
   164  }
   165  
   166  // JujuExec returns the absolute path to the juju-exec binary for
   167  // a particular series.
   168  func JujuExec(os OS) string {
   169  	return osVal(os, jujuExec)
   170  }
   171  
   172  // JujuDumpLogs returns the absolute path to the juju-dumplogs binary
   173  // for a particular series.
   174  func JujuDumpLogs(os OS) string {
   175  	return osVal(os, jujuDumpLogs)
   176  }
   177  
   178  // JujuIntrospect returns the absolute path to the juju-introspect
   179  // binary for a particular series.
   180  func JujuIntrospect(os OS) string {
   181  	return osVal(os, jujuIntrospect)
   182  }
   183  
   184  // MachineCloudInitDir returns the absolute path to the instance
   185  // cloudinit directory for a particular series.
   186  func MachineCloudInitDir(os OS) string {
   187  	return osVal(os, instanceCloudInitDir)
   188  }
   189  
   190  // CurtinInstallConfig returns the absolute path the configuration file
   191  // written by Curtin during machine provisioning.
   192  func CurtinInstallConfig(os OS) string {
   193  	return osVal(os, curtinInstallConfig)
   194  }
   195  
   196  // CloudInitCfgDir returns the absolute path to the instance
   197  // cloud config directory for a particular series.
   198  func CloudInitCfgDir(os OS) string {
   199  	return osVal(os, cloudInitCfgDir)
   200  }
   201  
   202  // ControlSocket returns the absolute path to the Juju control socket.
   203  func ControlSocket(os OS) string {
   204  	return osVal(os, controlSocket)
   205  }