github.com/cloudcredo/cloudrocker@v0.0.0-20160108110610-1320f8cc2dfd/utils/utils.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "log" 8 "os" 9 "os/exec" 10 ) 11 12 const launcher = ` 13 cd "$1" 14 15 if [ -d .profile.d ]; then 16 for env_file in .profile.d/*; do 17 source $env_file 18 done 19 fi 20 21 shift 22 23 eval "$@" 24 ` 25 26 func GetRootfsUrl() string { 27 url := os.Getenv("ROCKER_ROOTFS_URL") 28 if url == "" { 29 url = "https://s3.amazonaws.com/blob.cfblob.com/978883d5-2e4d-495b-8aec-fc7c7e2988ad" 30 } 31 return url 32 } 33 34 func CloudrockerHome() string { 35 cfhome := os.Getenv("CLOUDROCKER_HOME") 36 if cfhome == "" { 37 cfhome = os.Getenv("HOME") + "/cloudrocker" 38 } 39 return cfhome 40 } 41 42 func SubDirs(dir string) ([]string, error) { 43 var contents []os.FileInfo 44 var err error 45 dirs := []string{} 46 if contents, err = ioutil.ReadDir(dir); err != nil { 47 return dirs, err 48 } 49 for _, file := range contents { 50 if file.IsDir() { 51 dirs = append(dirs, file.Name()) 52 } 53 } 54 return dirs, nil 55 } 56 57 func CopyRockerBinaryToDir(destinationDir string) error { 58 if err := os.MkdirAll(destinationDir, 0755); err != nil { 59 return err 60 } 61 var rockPath string 62 var err error 63 if rockPath, err = exec.LookPath("rock"); err != nil { 64 return fmt.Errorf("Could not find rock binary, please install it in your path") 65 } 66 newRockPath := destinationDir + "/rock" 67 if err := Cp(rockPath, newRockPath); err != nil { 68 return err 69 } 70 if err := os.Chmod(newRockPath, 0755); err != nil { 71 return err 72 } 73 return nil 74 } 75 76 func AddLauncherRunScript(appDir string) error { 77 return ioutil.WriteFile(appDir+"/cloudrocker-start-1c4352a23e52040ddb1857d7675fe3cc.sh", []byte(launcher), 0644) 78 } 79 80 func Pwd() string { 81 pwd, err := os.Getwd() 82 if err != nil { 83 log.Fatalf(" %s", err) 84 } 85 return pwd 86 } 87 88 //C&P(ha!) from https://gist.github.com/elazarl/5507969 89 //src and dest swapped for sanity 90 func Cp(src, dst string) error { 91 s, err := os.Open(src) 92 if err != nil { 93 return err 94 } 95 // no need to check errors on read only file, we already got everything 96 // we need from the filesystem, so nothing can go wrong now. 97 defer s.Close() 98 d, err := os.Create(dst) 99 if err != nil { 100 return err 101 } 102 if _, err := io.Copy(d, s); err != nil { 103 d.Close() 104 return err 105 } 106 return d.Close() 107 }