github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/juju/paths/paths.go (about)

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