launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/provider/joyent/environ.go (about)

     1  // Copyright 2013 Joyent Inc.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package joyent
     5  
     6  import (
     7  	"sync"
     8  
     9  	"launchpad.net/juju-core/constraints"
    10  	"launchpad.net/juju-core/environs"
    11  	"launchpad.net/juju-core/environs/config"
    12  	"launchpad.net/juju-core/environs/storage"
    13  	"launchpad.net/juju-core/provider/common"
    14  	"launchpad.net/juju-core/state"
    15  	"launchpad.net/juju-core/state/api"
    16  )
    17  
    18  // This file contains the core of the joyent Environ implementation. You will
    19  // probably not need to change this file very much to begin with; and if you
    20  // never need to add any more fields, you may never need to touch it.
    21  //
    22  // The rest of the implementation is split into environ_instance.go (which
    23  // must be implemented ) and environ_firewall.go (which can be safely
    24  // ignored until you've got an environment bootstrapping successfully).
    25  
    26  type environ struct {
    27  	name string
    28  	// All mutating operations should lock the mutex. Non-mutating operations
    29  	// should read all fields (other than name, which is immutable) from a
    30  	// shallow copy taken with getSnapshot().
    31  	// This advice is predicated on the goroutine-safety of the values of the
    32  	// affected fields.
    33  	lock    sync.Mutex
    34  	ecfg    *environConfig
    35  	storage storage.Storage
    36  }
    37  
    38  var _ environs.Environ = (*environ)(nil)
    39  
    40  func (env *environ) Name() string {
    41  	return env.name
    42  }
    43  
    44  func (*environ) Provider() environs.EnvironProvider {
    45  	return providerInstance
    46  }
    47  
    48  func (env *environ) SetConfig(cfg *config.Config) error {
    49  	env.lock.Lock()
    50  	defer env.lock.Unlock()
    51  	ecfg, err := validateConfig(cfg, env.ecfg)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	storage, err := newStorage(ecfg)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	env.ecfg = ecfg
    60  	env.storage = storage
    61  	return nil
    62  }
    63  
    64  func (env *environ) getSnapshot() *environ {
    65  	env.lock.Lock()
    66  	clone := *env
    67  	env.lock.Unlock()
    68  	clone.lock = sync.Mutex{}
    69  	return &clone
    70  }
    71  
    72  func (env *environ) Config() *config.Config {
    73  	return env.getSnapshot().ecfg.Config
    74  }
    75  
    76  func (env *environ) Storage() storage.Storage {
    77  	return env.getSnapshot().storage
    78  }
    79  
    80  func (env *environ) PublicStorage() storage.StorageReader {
    81  	return environs.EmptyStorage
    82  }
    83  
    84  func (env *environ) Bootstrap(ctx environs.BootstrapContext, cons constraints.Value) error {
    85  	return common.Bootstrap(ctx, env, cons)
    86  }
    87  
    88  func (env *environ) StateInfo() (*state.Info, *api.Info, error) {
    89  	return common.StateInfo(env)
    90  }
    91  
    92  func (env *environ) Destroy() error {
    93  	return common.Destroy(env)
    94  }