github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/provider/vsphere/environ.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build !gccgo
     5  
     6  package vsphere
     7  
     8  import (
     9  	"sync"
    10  
    11  	"github.com/juju/errors"
    12  
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/config"
    15  	"github.com/juju/juju/instance"
    16  	"github.com/juju/juju/provider/common"
    17  )
    18  
    19  // TODO(ericsnow) All imports from github.com/juju/govmomi should change
    20  // back to github.com/vmware/govmomi once our min Go is 1.3 or higher.
    21  
    22  // Note: This provider/environment does *not* implement storage.
    23  
    24  type environ struct {
    25  	name   string
    26  	cloud  environs.CloudSpec
    27  	client *client
    28  
    29  	// namespace is used to create the machine and device hostnames.
    30  	namespace instance.Namespace
    31  
    32  	lock sync.Mutex // lock protects access the following fields.
    33  	ecfg *environConfig
    34  
    35  	archLock               sync.Mutex
    36  	supportedArchitectures []string
    37  }
    38  
    39  func newEnviron(cloud environs.CloudSpec, cfg *config.Config) (*environ, error) {
    40  	ecfg, err := newValidConfig(cfg, configDefaults)
    41  	if err != nil {
    42  		return nil, errors.Annotate(err, "invalid config")
    43  	}
    44  
    45  	client, err := newClient(cloud)
    46  	if err != nil {
    47  		return nil, errors.Annotatef(err, "failed to create new client")
    48  	}
    49  
    50  	namespace, err := instance.NewNamespace(cfg.UUID())
    51  	if err != nil {
    52  		return nil, errors.Trace(err)
    53  	}
    54  
    55  	env := &environ{
    56  		name:      ecfg.Name(),
    57  		cloud:     cloud,
    58  		ecfg:      ecfg,
    59  		client:    client,
    60  		namespace: namespace,
    61  	}
    62  	return env, nil
    63  }
    64  
    65  // Name returns the name of the environment.
    66  func (env *environ) Name() string {
    67  	return env.name
    68  }
    69  
    70  // Provider returns the environment provider that created this env.
    71  func (*environ) Provider() environs.EnvironProvider {
    72  	return providerInstance
    73  }
    74  
    75  // SetConfig updates the env's configuration.
    76  func (env *environ) SetConfig(cfg *config.Config) error {
    77  	env.lock.Lock()
    78  	defer env.lock.Unlock()
    79  
    80  	if env.ecfg == nil {
    81  		return errors.New("cannot set config on uninitialized env")
    82  	}
    83  
    84  	if err := env.ecfg.update(cfg); err != nil {
    85  		return errors.Annotate(err, "invalid config change")
    86  	}
    87  	return nil
    88  }
    89  
    90  // Config returns the configuration data with which the env was created.
    91  func (env *environ) Config() *config.Config {
    92  	env.lock.Lock()
    93  	cfg := env.ecfg.Config
    94  	env.lock.Unlock()
    95  	return cfg
    96  }
    97  
    98  // PrepareForBootstrap implements environs.Environ.
    99  func (env *environ) PrepareForBootstrap(ctx environs.BootstrapContext) error {
   100  	return nil
   101  }
   102  
   103  // Create implements environs.Environ.
   104  func (env *environ) Create(environs.CreateParams) error {
   105  	return nil
   106  }
   107  
   108  //this variable is exported, because it has to be rewritten in external unit tests
   109  var Bootstrap = common.Bootstrap
   110  
   111  // Bootstrap creates a new instance, chosing the series and arch out of
   112  // available tools. The series and arch are returned along with a func
   113  // that must be called to finalize the bootstrap process by transferring
   114  // the tools and installing the initial juju controller.
   115  func (env *environ) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (*environs.BootstrapResult, error) {
   116  	return Bootstrap(ctx, env, params)
   117  }
   118  
   119  // BootstrapMessage is part of the Environ interface.
   120  func (env *environ) BootstrapMessage() string {
   121  	return ""
   122  }
   123  
   124  //this variable is exported, because it has to be rewritten in external unit tests
   125  var DestroyEnv = common.Destroy
   126  
   127  // Destroy shuts down all known machines and destroys the rest of the
   128  // known environment.
   129  func (env *environ) Destroy() error {
   130  	return DestroyEnv(env)
   131  }
   132  
   133  // DestroyController implements the Environ interface.
   134  func (env *environ) DestroyController(controllerUUID string) error {
   135  	return env.Destroy()
   136  }