github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/machiner/manifold.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machiner
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/agent"
    10  	apiagent "github.com/juju/juju/api/agent"
    11  	"github.com/juju/juju/api/base"
    12  	apimachiner "github.com/juju/juju/api/machiner"
    13  	"github.com/juju/juju/cmd/jujud/agent/engine"
    14  	"github.com/juju/juju/worker"
    15  	"github.com/juju/juju/worker/dependency"
    16  	"gopkg.in/juju/names.v2"
    17  )
    18  
    19  // ManifoldConfig defines the names of the manifolds on which a
    20  // Manifold will depend.
    21  type ManifoldConfig engine.AgentAPIManifoldConfig
    22  
    23  // Manifold returns a dependency manifold that runs a machiner worker, using
    24  // the resource names defined in the supplied config.
    25  func Manifold(config ManifoldConfig) dependency.Manifold {
    26  	typedConfig := engine.AgentAPIManifoldConfig(config)
    27  	return engine.AgentAPIManifold(typedConfig, newWorker)
    28  }
    29  
    30  // newWorker non-trivially wraps NewMachiner to specialise a engine.AgentAPIManifold.
    31  //
    32  // TODO(waigani) This function is currently covered by functional tests
    33  // under the machine agent. Add unit tests once infrastructure to do so is
    34  // in place.
    35  func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    36  	currentConfig := a.CurrentConfig()
    37  
    38  	// TODO(fwereade): this functionality should be on the
    39  	// machiner facade instead -- or, better yet, separate
    40  	// the networking concerns from the lifecycle ones and
    41  	// have completey separate workers.
    42  	//
    43  	// (With their own facades.)
    44  	agentFacade := apiagent.NewState(apiCaller)
    45  	modelConfig, err := agentFacade.ModelConfig()
    46  	if err != nil {
    47  		return nil, errors.Errorf("cannot read environment config: %v", err)
    48  	}
    49  
    50  	ignoreMachineAddresses, _ := modelConfig.IgnoreMachineAddresses()
    51  	// Containers only have machine addresses, so we can't ignore them.
    52  	tag := currentConfig.Tag()
    53  	if names.IsContainerMachine(tag.Id()) {
    54  		ignoreMachineAddresses = false
    55  	}
    56  	if ignoreMachineAddresses {
    57  		logger.Infof("machine addresses not used, only addresses from provider")
    58  	}
    59  	accessor := APIMachineAccessor{apimachiner.NewState(apiCaller)}
    60  	w, err := NewMachiner(Config{
    61  		MachineAccessor: accessor,
    62  		Tag:             tag.(names.MachineTag),
    63  		ClearMachineAddressesOnStart: ignoreMachineAddresses,
    64  		NotifyMachineDead: func() error {
    65  			return agent.SetCanUninstall(a)
    66  		},
    67  	})
    68  	if err != nil {
    69  		return nil, errors.Annotate(err, "cannot start machiner worker")
    70  	}
    71  	return w, err
    72  }