github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/tools.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc 5 6 import ( 7 "os" 8 "path/filepath" 9 10 "github.com/juju/errors" 11 "github.com/juju/utils/symlink" 12 13 "github.com/juju/juju/juju/names" 14 ) 15 16 // EnsureSymlinks creates a symbolic link to jujuc within dir for each 17 // hook command. If the commands already exist, this operation does nothing. 18 // If dir is a symbolic link, it will be dereferenced first. 19 func EnsureSymlinks(dir string) (err error) { 20 logger.Infof("ensure jujuc symlinks in %s", dir) 21 defer func() { 22 if err != nil { 23 err = errors.Annotatef(err, "cannot initialize hook commands in %q", dir) 24 } 25 }() 26 isSymlink, err := symlink.IsSymlink(dir) 27 if err != nil { 28 return err 29 } 30 if isSymlink { 31 link, err := symlink.Read(dir) 32 if err != nil { 33 return err 34 } 35 if !filepath.IsAbs(link) { 36 logger.Infof("%s is relative", link) 37 link = filepath.Join(filepath.Dir(dir), link) 38 } 39 dir = link 40 logger.Infof("was a symlink, now looking at %s", dir) 41 } 42 43 jujudPath := filepath.Join(dir, names.Jujud) 44 logger.Debugf("jujud path %s", jujudPath) 45 for _, name := range CommandNames() { 46 // The link operation fails when the target already exists, 47 // so this is a no-op when the command names already 48 // exist. 49 err := symlink.New(jujudPath, filepath.Join(dir, name)) 50 if err != nil && !os.IsExist(err) { 51 return err 52 } 53 } 54 return nil 55 }