github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/juju/paths/paths.go (about)

     1  package paths
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/version"
    10  )
    11  
    12  type osVarType int
    13  
    14  const (
    15  	tmpDir osVarType = iota
    16  	logDir
    17  	dataDir
    18  	storageDir
    19  	confDir
    20  	jujuRun
    21  	certDir
    22  )
    23  
    24  var nixVals = map[osVarType]string{
    25  	tmpDir:     "/tmp",
    26  	logDir:     "/var/log",
    27  	dataDir:    "/var/lib/juju",
    28  	storageDir: "/var/lib/juju/storage",
    29  	confDir:    "/etc/juju",
    30  	jujuRun:    "/usr/bin/juju-run",
    31  	certDir:    "/etc/juju/certs.d",
    32  }
    33  
    34  var winVals = map[osVarType]string{
    35  	tmpDir:     "C:/Juju/tmp",
    36  	logDir:     "C:/Juju/log",
    37  	dataDir:    "C:/Juju/lib/juju",
    38  	storageDir: "C:/Juju/lib/juju/storage",
    39  	confDir:    "C:/Juju/etc",
    40  	jujuRun:    "C:/Juju/bin/juju-run.exe",
    41  	certDir:    "C:/Juju/certs",
    42  }
    43  
    44  // osVal will lookup the value of the key valname
    45  // in the apropriate map, based on the series. This will
    46  // help reduce boilerplate code
    47  func osVal(series string, valname osVarType) (string, error) {
    48  	os, err := version.GetOSFromSeries(series)
    49  	if err != nil {
    50  		return "", err
    51  	}
    52  	switch os {
    53  	case version.Windows:
    54  		return winVals[valname], nil
    55  	default:
    56  		return nixVals[valname], nil
    57  	}
    58  }
    59  
    60  // TempDir returns the path on disk to the corect tmp directory
    61  // for the series. This value will be the same on virtually
    62  // all linux systems, but will differ on windows
    63  func TempDir(series string) (string, error) {
    64  	return osVal(series, tmpDir)
    65  }
    66  
    67  // LogDir returns filesystem path the directory where juju may
    68  // save log files.
    69  func LogDir(series string) (string, error) {
    70  	return osVal(series, logDir)
    71  }
    72  
    73  // DataDir returns a filesystem path to the folder used by juju to
    74  // store tools, charms, locks, etc
    75  func DataDir(series string) (string, error) {
    76  	return osVal(series, dataDir)
    77  }
    78  
    79  // CertDir returns a filesystem path to the folder used by juju to
    80  // store certificates that are added by default to the Juju client
    81  // api certificate pool.
    82  func CertDir(series string) (string, error) {
    83  	return osVal(series, certDir)
    84  }
    85  
    86  // StorageDir returns a filesystem path to the folder used by juju to
    87  // mount machine-level storage.
    88  func StorageDir(series string) (string, error) {
    89  	return osVal(series, storageDir)
    90  }
    91  
    92  // ConfDir returns the path to the directory where Juju may store
    93  // configuration files.
    94  func ConfDir(series string) (string, error) {
    95  	return osVal(series, confDir)
    96  }
    97  
    98  // JujuRun returns the absolute path to the juju-run binary for
    99  // a particular series
   100  func JujuRun(series string) (string, error) {
   101  	return osVal(series, jujuRun)
   102  }
   103  
   104  func MustSucceed(s string, e error) string {
   105  	if e != nil {
   106  		panic(e)
   107  	}
   108  	return s
   109  }
   110  
   111  var osStat = os.Stat
   112  var execLookPath = exec.LookPath
   113  
   114  // mongorestorePath will look for mongorestore binary on the system
   115  // and return it if mongorestore actually exists.
   116  // it will look first for the juju provided one and if not found make a
   117  // try at a system one.
   118  func MongorestorePath() (string, error) {
   119  	// TODO (perrito666) this seems to be a package decission we should not
   120  	// rely on it and we should be aware of /usr/lib/juju if its something
   121  	// of ours.
   122  	const mongoRestoreFullPath string = "/usr/lib/juju/bin/mongorestore"
   123  
   124  	if _, err := osStat(mongoRestoreFullPath); err == nil {
   125  		return mongoRestoreFullPath, nil
   126  	}
   127  
   128  	path, err := execLookPath("mongorestore")
   129  	if err != nil {
   130  		return "", errors.Trace(err)
   131  	}
   132  	return path, nil
   133  }