github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/vmware/environ.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package vmware
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/environs"
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/provider/common"
    14  )
    15  
    16  // Note: This provider/environment does *not* implement storage.
    17  
    18  type environ struct {
    19  	common.SupportsUnitPlacementPolicy
    20  
    21  	name   string
    22  	uuid   string
    23  	ecfg   *environConfig
    24  	client *client
    25  
    26  	lock     sync.Mutex
    27  	archLock sync.Mutex
    28  
    29  	supportedArchitectures []string
    30  }
    31  
    32  var _ environs.Environ = (*environ)(nil)
    33  
    34  func newEnviron(ecfg *environConfig) (*environ, error) {
    35  	uuid, ok := ecfg.UUID()
    36  	if !ok {
    37  		return nil, errors.New("UUID not set")
    38  	}
    39  
    40  	client, err := newClient(ecfg)
    41  	if err != nil {
    42  		return nil, errors.Annotatef(err, "Failed to create new client")
    43  	}
    44  
    45  	env := &environ{
    46  		name:   ecfg.Name(),
    47  		uuid:   uuid,
    48  		ecfg:   ecfg,
    49  		client: client,
    50  	}
    51  	return env, nil
    52  }
    53  
    54  // Name returns the name of the environment.
    55  func (env *environ) Name() string {
    56  	return env.name
    57  }
    58  
    59  // Provider returns the environment provider that created this env.
    60  func (*environ) Provider() environs.EnvironProvider {
    61  	return providerInstance
    62  }
    63  
    64  // SetConfig updates the env's configuration.
    65  func (env *environ) SetConfig(cfg *config.Config) error {
    66  	env.lock.Lock()
    67  	defer env.lock.Unlock()
    68  
    69  	if env.ecfg == nil {
    70  		return errors.New("cannot set config on uninitialized env")
    71  	}
    72  
    73  	if err := env.ecfg.update(cfg); err != nil {
    74  		return errors.Annotate(err, "invalid config change")
    75  	}
    76  	return nil
    77  }
    78  
    79  // getSnapshot returns a copy of the environment. This is useful for
    80  // ensuring the env you are using does not get changed by other code
    81  // while you are using it.
    82  func (env *environ) getSnapshot() *environ {
    83  	env.lock.Lock()
    84  	clone := *env
    85  	env.lock.Unlock()
    86  
    87  	clone.lock = sync.Mutex{}
    88  	return &clone
    89  }
    90  
    91  // Config returns the configuration data with which the env was created.
    92  func (env *environ) Config() *config.Config {
    93  	return env.getSnapshot().ecfg.Config
    94  }
    95  
    96  var bootstrap = common.Bootstrap
    97  
    98  // Bootstrap creates a new instance, chosing the series and arch out of
    99  // available tools. The series and arch are returned along with a func
   100  // that must be called to finalize the bootstrap process by transferring
   101  // the tools and installing the initial juju state server.
   102  func (env *environ) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (arch, series string, _ environs.BootstrapFinalizer, _ error) {
   103  	return bootstrap(ctx, env, params)
   104  }
   105  
   106  var destroyEnv = common.Destroy
   107  
   108  // Destroy shuts down all known machines and destroys the rest of the
   109  // known environment.
   110  func (env *environ) Destroy() error {
   111  	ports, err := env.Ports()
   112  	if err != nil {
   113  		return errors.Trace(err)
   114  	}
   115  
   116  	if len(ports) > 0 {
   117  		if err := env.ClosePorts(ports); err != nil {
   118  			return errors.Trace(err)
   119  		}
   120  	}
   121  
   122  	return destroyEnv(env)
   123  }