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