github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/environs/interface.go (about)

     1  // Copyright 2011, 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package environs
     5  
     6  import (
     7  	"io"
     8  	"os"
     9  
    10  	"launchpad.net/juju-core/constraints"
    11  	"launchpad.net/juju-core/environs/config"
    12  	"launchpad.net/juju-core/environs/storage"
    13  	"launchpad.net/juju-core/instance"
    14  	"launchpad.net/juju-core/state"
    15  	"launchpad.net/juju-core/state/api"
    16  )
    17  
    18  // A EnvironProvider represents a computing and storage provider.
    19  type EnvironProvider interface {
    20  	// Prepare prepares an environment for use. Any additional
    21  	// configuration attributes in the returned environment should
    22  	// be saved to be used later. If the environment is already
    23  	// prepared, this call is equivalent to Open.
    24  	Prepare(ctx BootstrapContext, cfg *config.Config) (Environ, error)
    25  
    26  	// Open opens the environment and returns it.
    27  	// The configuration must have come from a previously
    28  	// prepared environment.
    29  	Open(cfg *config.Config) (Environ, error)
    30  
    31  	// Validate ensures that config is a valid configuration for this
    32  	// provider, applying changes to it if necessary, and returns the
    33  	// validated configuration.
    34  	// If old is not nil, it holds the previous environment configuration
    35  	// for consideration when validating changes.
    36  	Validate(cfg, old *config.Config) (valid *config.Config, err error)
    37  
    38  	// Boilerplate returns a default configuration for the environment in yaml format.
    39  	// The text should be a key followed by some number of attributes:
    40  	//    `environName:
    41  	//        type: environTypeName
    42  	//        attr1: val1
    43  	//    `
    44  	// The text is used as a template (see the template package) with one extra template
    45  	// function available, rand, which expands to a random hexadecimal string when invoked.
    46  	BoilerplateConfig() string
    47  
    48  	// SecretAttrs filters the supplied configuration returning only values
    49  	// which are considered sensitive. All of the values of these secret
    50  	// attributes need to be strings.
    51  	SecretAttrs(cfg *config.Config) (map[string]string, error)
    52  
    53  	// PublicAddress returns this machine's public host name.
    54  	PublicAddress() (string, error)
    55  
    56  	// PrivateAddress returns this machine's private host name.
    57  	PrivateAddress() (string, error)
    58  }
    59  
    60  // EnvironStorage implements storage access for an environment
    61  type EnvironStorage interface {
    62  	// Storage returns storage specific to the environment.
    63  	Storage() storage.Storage
    64  }
    65  
    66  // ConfigGetter implements access to an environments configuration.
    67  type ConfigGetter interface {
    68  	// Config returns the configuration data with which the Environ was created.
    69  	// Note that this is not necessarily current; the canonical location
    70  	// for the configuration data is stored in the state.
    71  	Config() *config.Config
    72  }
    73  
    74  // An Environ represents a juju environment as specified
    75  // in the environments.yaml file.
    76  //
    77  // Due to the limitations of some providers (for example ec2), the
    78  // results of the Environ methods may not be fully sequentially
    79  // consistent. In particular, while a provider may retry when it
    80  // gets an error for an operation, it will not retry when
    81  // an operation succeeds, even if that success is not
    82  // consistent with a previous operation.
    83  //
    84  // Even though Juju takes care not to share an Environ between concurrent
    85  // workers, it does allow concurrent method calls into the provider
    86  // implementation.  The typical provider implementation needs locking to
    87  // avoid undefined behaviour when the configuration changes.
    88  type Environ interface {
    89  	// Name returns the Environ's name.
    90  	Name() string
    91  
    92  	// Bootstrap initializes the state for the environment, possibly
    93  	// starting one or more instances.  If the configuration's
    94  	// AdminSecret is non-empty, the administrator password on the
    95  	// newly bootstrapped state will be set to a hash of it (see
    96  	// utils.PasswordHash), When first connecting to the
    97  	// environment via the juju package, the password hash will be
    98  	// automatically replaced by the real password.
    99  	//
   100  	// The supplied constraints are used to choose the initial instance
   101  	// specification, and will be stored in the new environment's state.
   102  	//
   103  	// Bootstrap is responsible for selecting the appropriate tools,
   104  	// and setting the agent-version configuration attribute prior to
   105  	// bootstrapping the environment.
   106  	Bootstrap(ctx BootstrapContext, cons constraints.Value) error
   107  
   108  	// StateInfo returns information on the state initialized
   109  	// by Bootstrap.
   110  	StateInfo() (*state.Info, *api.Info, error)
   111  
   112  	// InstanceBroker defines methods for starting and stopping
   113  	// instances.
   114  	InstanceBroker
   115  
   116  	// ConfigGetter allows the retrieval of the configuration data.
   117  	ConfigGetter
   118  
   119  	// SetConfig updates the Environ's configuration.
   120  	//
   121  	// Calls to SetConfig do not affect the configuration of
   122  	// values previously obtained from Storage.
   123  	SetConfig(cfg *config.Config) error
   124  
   125  	// Instances returns a slice of instances corresponding to the
   126  	// given instance ids.  If no instances were found, but there
   127  	// was no other error, it will return ErrNoInstances.  If
   128  	// some but not all the instances were found, the returned slice
   129  	// will have some nil slots, and an ErrPartialInstances error
   130  	// will be returned.
   131  	Instances(ids []instance.Id) ([]instance.Instance, error)
   132  
   133  	EnvironStorage
   134  
   135  	// Destroy shuts down all known machines and destroys the
   136  	// rest of the environment. Note that on some providers,
   137  	// very recently started instances may not be destroyed
   138  	// because they are not yet visible.
   139  	//
   140  	// When Destroy has been called, any Environ referring to the
   141  	// same remote environment may become invalid
   142  	Destroy() error
   143  
   144  	// OpenPorts opens the given ports for the whole environment.
   145  	// Must only be used if the environment was setup with the
   146  	// FwGlobal firewall mode.
   147  	OpenPorts(ports []instance.Port) error
   148  
   149  	// ClosePorts closes the given ports for the whole environment.
   150  	// Must only be used if the environment was setup with the
   151  	// FwGlobal firewall mode.
   152  	ClosePorts(ports []instance.Port) error
   153  
   154  	// Ports returns the ports opened for the whole environment.
   155  	// Must only be used if the environment was setup with the
   156  	// FwGlobal firewall mode.
   157  	Ports() ([]instance.Port, error)
   158  
   159  	// Provider returns the EnvironProvider that created this Environ.
   160  	Provider() EnvironProvider
   161  
   162  	// TODO(axw) 2014-02-11 #pending-review
   163  	//     Embed state.Prechecker, and introduce an EnvironBase
   164  	//     that embeds a no-op prechecker implementation.
   165  }
   166  
   167  // BootstrapContext is an interface that is passed to
   168  // Environ.Bootstrap, providing a means of obtaining
   169  // information about and manipulating the context in which
   170  // it is being invoked.
   171  type BootstrapContext interface {
   172  	GetStdin() io.Reader
   173  	GetStdout() io.Writer
   174  	GetStderr() io.Writer
   175  
   176  	// InterruptNotify starts watching for interrupt signals
   177  	// on behalf of the caller, sending them to the supplied
   178  	// channel.
   179  	InterruptNotify(sig chan<- os.Signal)
   180  
   181  	// StopInterruptNotify undoes the effects of a previous
   182  	// call to InterruptNotify with the same channel. After
   183  	// StopInterruptNotify returns, no more signals will be
   184  	// delivered to the channel.
   185  	StopInterruptNotify(chan<- os.Signal)
   186  }