github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/environ/wait.go (about) 1 // Copyright 2012-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package environ 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/loggo" 9 10 "github.com/juju/juju/environs" 11 "github.com/juju/juju/watcher" 12 ) 13 14 // TODO(fwereade) remove WaitForEnviron, use a manifold-managed Tracker to share 15 // a single environs.Environ among firewaller, instancepoller, provisioner. 16 17 var logger = loggo.GetLogger("juju.worker.environ") 18 19 // ErrWaitAborted is returned from WaitForEnviron when the wait is terminated by 20 // closing the abort chan. 21 var ErrWaitAborted = errors.New("environ wait aborted") 22 23 // WaitForEnviron waits for an valid environment to arrive from the given 24 // watcher. It terminates with ErrWaitAborted if it receives a value on abort. 25 // 26 // In practice, it shouldn't wait at all: juju *should* never deliver invalid 27 // environ configs. Regardless, it should be considered deprecated; clients 28 // should prefer to access an Environ via a shared Tracker. 29 // 30 // It never takes responsibility for the supplied watcher; the client remains 31 // responsible for detecting and handling any watcher errors that may occur, 32 // whether this func succeeds or fails. 33 func WaitForEnviron( 34 // TODO(wallyworld) - pass in credential watcher 35 w watcher.NotifyWatcher, 36 getter environs.EnvironConfigGetter, 37 newEnviron environs.NewEnvironFunc, 38 abort <-chan struct{}, 39 ) (environs.Environ, error) { 40 for { 41 select { 42 case <-abort: 43 return nil, ErrWaitAborted 44 case _, ok := <-w.Changes(): 45 if !ok { 46 return nil, errors.New("environ config watch closed") 47 } 48 // First check the model config is valid as we want to exit with 49 // an error if we have received a config but it is not valid. 50 // This distinguishes from the case where environ construction fails 51 // because no config has been received yet. 52 if _, err := getter.ModelConfig(); err != nil { 53 return nil, errors.Annotate(err, "cannot read environ config") 54 } 55 environ, err := environs.GetEnviron(getter, newEnviron) 56 if err == nil { 57 return environ, nil 58 } 59 logger.Errorf("loaded invalid environment configuration: %v", err) 60 } 61 } 62 }