github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/cmd/juju/common.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "launchpad.net/juju-core/cmd" 8 "launchpad.net/juju-core/environs" 9 "launchpad.net/juju-core/environs/configstore" 10 "launchpad.net/juju-core/errors" 11 ) 12 13 // destroyPreparedEnviron destroys the environment and logs an error if it fails. 14 func destroyPreparedEnviron(env environs.Environ, store configstore.Storage, err *error, action string) { 15 if *err == nil { 16 return 17 } 18 if err := environs.Destroy(env, store); err != nil { 19 logger.Errorf("%s failed, and the environment could not be destroyed: %v", action, err) 20 } 21 } 22 23 // environFromName loads an existing environment or prepares a new one. 24 func environFromName( 25 ctx *cmd.Context, envName string, resultErr *error, action string) (environs.Environ, func(), error) { 26 27 store, err := configstore.Default() 28 if err != nil { 29 return nil, nil, err 30 } 31 var existing bool 32 if environInfo, err := store.ReadInfo(envName); !errors.IsNotFoundError(err) { 33 existing = true 34 logger.Warningf("ignoring environments.yaml: using bootstrap config in %s", environInfo.Location()) 35 } 36 environ, err := environs.PrepareFromName(envName, ctx, store) 37 if err != nil { 38 return nil, nil, err 39 } 40 cleanup := func() { 41 if !existing { 42 destroyPreparedEnviron(environ, store, resultErr, action) 43 } 44 } 45 return environ, cleanup, nil 46 }