github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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  	"github.com/juju/juju/cloudconfig/instancecfg"
    11  	"github.com/juju/juju/constraints"
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/environs/storage"
    14  	"github.com/juju/juju/instance"
    15  	"github.com/juju/juju/network"
    16  	"github.com/juju/juju/state"
    17  	"github.com/juju/juju/tools"
    18  )
    19  
    20  // A EnvironProvider represents a computing and storage provider.
    21  type EnvironProvider interface {
    22  	// RestrictedConfigAttributes are provider specific attributes stored in
    23  	// the config that really cannot or should not be changed across
    24  	// environments running inside a single juju server.
    25  	RestrictedConfigAttributes() []string
    26  
    27  	// PrepareForCreateEnvironment prepares an environment for creation. Any
    28  	// additional configuration attributes are added to the config passed in
    29  	// and returned.  This allows providers to add additional required config
    30  	// for new environments that may be created in an existing juju server.
    31  	// Note that this is not called in a client context, so environment variables,
    32  	// local files, etc are not available.
    33  	PrepareForCreateEnvironment(cfg *config.Config) (*config.Config, error)
    34  
    35  	// PrepareForBootstrap prepares an environment for use. Any additional
    36  	// configuration attributes in the returned environment should
    37  	// be saved to be used later. If the environment is already
    38  	// prepared, this call is equivalent to Open.
    39  	PrepareForBootstrap(ctx BootstrapContext, cfg *config.Config) (Environ, error)
    40  
    41  	// Open opens the environment and returns it.
    42  	// The configuration must have come from a previously
    43  	// prepared environment.
    44  	Open(cfg *config.Config) (Environ, error)
    45  
    46  	// Validate ensures that config is a valid configuration for this
    47  	// provider, applying changes to it if necessary, and returns the
    48  	// validated configuration.
    49  	// If old is not nil, it holds the previous environment configuration
    50  	// for consideration when validating changes.
    51  	Validate(cfg, old *config.Config) (valid *config.Config, err error)
    52  
    53  	// Boilerplate returns a default configuration for the environment in yaml format.
    54  	// The text should be a key followed by some number of attributes:
    55  	//    `environName:
    56  	//        type: environTypeName
    57  	//        attr1: val1
    58  	//    `
    59  	// The text is used as a template (see the template package) with one extra template
    60  	// function available, rand, which expands to a random hexadecimal string when invoked.
    61  	BoilerplateConfig() string
    62  
    63  	// SecretAttrs filters the supplied configuration returning only values
    64  	// which are considered sensitive. All of the values of these secret
    65  	// attributes need to be strings.
    66  	SecretAttrs(cfg *config.Config) (map[string]string, error)
    67  }
    68  
    69  // EnvironStorage implements storage access for an environment.
    70  type EnvironStorage interface {
    71  	// Storage returns storage specific to the environment.
    72  	Storage() storage.Storage
    73  }
    74  
    75  // ConfigGetter implements access to an environment's configuration.
    76  type ConfigGetter interface {
    77  	// Config returns the configuration data with which the Environ was created.
    78  	// Note that this is not necessarily current; the canonical location
    79  	// for the configuration data is stored in the state.
    80  	Config() *config.Config
    81  }
    82  
    83  // BootstrapParams holds the parameters for bootstrapping an environment.
    84  type BootstrapParams struct {
    85  	// Constraints are used to choose the initial instance specification,
    86  	// and will be stored in the new environment's state.
    87  	Constraints constraints.Value
    88  
    89  	// Placement, if non-empty, holds an environment-specific placement
    90  	// directive used to choose the initial instance.
    91  	Placement string
    92  
    93  	// AvailableTools is a collection of tools which the Bootstrap method
    94  	// may use to decide which architecture/series to instantiate.
    95  	AvailableTools tools.List
    96  
    97  	// ContainerBridgeName, if non-empty, overrides the default
    98  	// network bridge device to use for LXC and KVM containers. See
    99  	// also instancecfg.DefaultBridgeName.
   100  	ContainerBridgeName string
   101  }
   102  
   103  // BootstrapFinalizer is a function returned from Environ.Bootstrap.
   104  // The caller must pass a InstanceConfig with the Tools field set.
   105  type BootstrapFinalizer func(BootstrapContext, *instancecfg.InstanceConfig) error
   106  
   107  // An Environ represents a juju environment as specified
   108  // in the environments.yaml file.
   109  //
   110  // Due to the limitations of some providers (for example ec2), the
   111  // results of the Environ methods may not be fully sequentially
   112  // consistent. In particular, while a provider may retry when it
   113  // gets an error for an operation, it will not retry when
   114  // an operation succeeds, even if that success is not
   115  // consistent with a previous operation.
   116  //
   117  // Even though Juju takes care not to share an Environ between concurrent
   118  // workers, it does allow concurrent method calls into the provider
   119  // implementation.  The typical provider implementation needs locking to
   120  // avoid undefined behaviour when the configuration changes.
   121  type Environ interface {
   122  	// Bootstrap creates a new instance with the series and architecture
   123  	// of its choice, constrained to those of the available tools, and
   124  	// returns the instance's architecture, series, and a function that
   125  	// must be called to finalize the bootstrap process by transferring
   126  	// the tools and installing the initial Juju state server.
   127  	//
   128  	// It is possible to direct Bootstrap to use a specific architecture
   129  	// (or fail if it cannot start an instance of that architecture) by
   130  	// using an architecture constraint; this will have the effect of
   131  	// limiting the available tools to just those matching the specified
   132  	// architecture.
   133  	Bootstrap(ctx BootstrapContext, params BootstrapParams) (arch, series string, _ BootstrapFinalizer, _ error)
   134  
   135  	// InstanceBroker defines methods for starting and stopping
   136  	// instances.
   137  	InstanceBroker
   138  
   139  	// ConfigGetter allows the retrieval of the configuration data.
   140  	ConfigGetter
   141  
   142  	// EnvironCapability allows access to this environment's capabilities.
   143  	state.EnvironCapability
   144  
   145  	// ConstraintsValidator returns a Validator instance which
   146  	// is used to validate and merge constraints.
   147  	ConstraintsValidator() (constraints.Validator, error)
   148  
   149  	// SetConfig updates the Environ's configuration.
   150  	//
   151  	// Calls to SetConfig do not affect the configuration of
   152  	// values previously obtained from Storage.
   153  	SetConfig(cfg *config.Config) error
   154  
   155  	// Instances returns a slice of instances corresponding to the
   156  	// given instance ids.  If no instances were found, but there
   157  	// was no other error, it will return ErrNoInstances.  If
   158  	// some but not all the instances were found, the returned slice
   159  	// will have some nil slots, and an ErrPartialInstances error
   160  	// will be returned.
   161  	Instances(ids []instance.Id) ([]instance.Instance, error)
   162  
   163  	// StateServerInstances returns the IDs of instances corresponding
   164  	// to Juju state servers. If there are no state server instances,
   165  	// ErrNoInstances is returned. If it can be determined that the
   166  	// environment has not been bootstrapped, then ErrNotBootstrapped
   167  	// should be returned instead.
   168  	StateServerInstances() ([]instance.Id, error)
   169  
   170  	// Destroy shuts down all known machines and destroys the
   171  	// rest of the environment. Note that on some providers,
   172  	// very recently started instances may not be destroyed
   173  	// because they are not yet visible.
   174  	//
   175  	// When Destroy has been called, any Environ referring to the
   176  	// same remote environment may become invalid
   177  	Destroy() error
   178  
   179  	// OpenPorts opens the given port ranges for the whole environment.
   180  	// Must only be used if the environment was setup with the
   181  	// FwGlobal firewall mode.
   182  	OpenPorts(ports []network.PortRange) error
   183  
   184  	// ClosePorts closes the given port ranges for the whole environment.
   185  	// Must only be used if the environment was setup with the
   186  	// FwGlobal firewall mode.
   187  	ClosePorts(ports []network.PortRange) error
   188  
   189  	// Ports returns the port ranges opened for the whole environment.
   190  	// Must only be used if the environment was setup with the
   191  	// FwGlobal firewall mode.
   192  	Ports() ([]network.PortRange, error)
   193  
   194  	// Provider returns the EnvironProvider that created this Environ.
   195  	Provider() EnvironProvider
   196  
   197  	state.Prechecker
   198  }
   199  
   200  // InstanceTagger is an interface that can be used for tagging instances.
   201  type InstanceTagger interface {
   202  	// TagInstance tags the given instance with the specified tags.
   203  	//
   204  	// The specified tags will replace any existing ones with the
   205  	// same names, but other existing tags will be left alone.
   206  	TagInstance(id instance.Id, tags map[string]string) error
   207  }
   208  
   209  // BootstrapContext is an interface that is passed to
   210  // Environ.Bootstrap, providing a means of obtaining
   211  // information about and manipulating the context in which
   212  // it is being invoked.
   213  type BootstrapContext interface {
   214  	GetStdin() io.Reader
   215  	GetStdout() io.Writer
   216  	GetStderr() io.Writer
   217  	Infof(format string, params ...interface{})
   218  	Verbosef(format string, params ...interface{})
   219  
   220  	// InterruptNotify starts watching for interrupt signals
   221  	// on behalf of the caller, sending them to the supplied
   222  	// channel.
   223  	InterruptNotify(sig chan<- os.Signal)
   224  
   225  	// StopInterruptNotify undoes the effects of a previous
   226  	// call to InterruptNotify with the same channel. After
   227  	// StopInterruptNotify returns, no more signals will be
   228  	// delivered to the channel.
   229  	StopInterruptNotify(chan<- os.Signal)
   230  
   231  	// ShouldVerifyCredentials indicates whether the caller's cloud
   232  	// credentials should be verified.
   233  	ShouldVerifyCredentials() bool
   234  }