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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package reboot
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/utils/clock"
     9  
    10  	"github.com/juju/juju/agent"
    11  	"github.com/juju/juju/api"
    12  	"github.com/juju/juju/api/base"
    13  	"github.com/juju/juju/worker"
    14  	"github.com/juju/juju/worker/dependency"
    15  )
    16  
    17  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    18  type ManifoldConfig struct {
    19  	AgentName       string
    20  	APICallerName   string
    21  	MachineLockName string
    22  	Clock           clock.Clock
    23  }
    24  
    25  // Manifold returns a dependency manifold that runs a reboot worker,
    26  // using the resource names defined in the supplied config.
    27  func Manifold(config ManifoldConfig) dependency.Manifold {
    28  	return dependency.Manifold{
    29  		Inputs: []string{
    30  			config.AgentName,
    31  			config.APICallerName,
    32  		},
    33  		Start: func(context dependency.Context) (worker.Worker, error) {
    34  			var agent agent.Agent
    35  			if err := context.Get(config.AgentName, &agent); err != nil {
    36  				return nil, err
    37  			}
    38  			var apiCaller base.APICaller
    39  			if err := context.Get(config.APICallerName, &apiCaller); err != nil {
    40  				return nil, err
    41  			}
    42  			if config.Clock == nil {
    43  				return nil, errors.NotValidf("missing Clock")
    44  			}
    45  			if config.MachineLockName == "" {
    46  				return nil, errors.NotValidf("missing MachineLockName")
    47  			}
    48  			return newWorker(agent, apiCaller, config.MachineLockName, config.Clock)
    49  		},
    50  	}
    51  }
    52  
    53  // newWorker starts a new reboot worker.
    54  //
    55  // TODO(mjs) - It's not tested at the moment, because the scaffolding
    56  // necessary is too unwieldy/distracting to introduce at this point.
    57  func newWorker(a agent.Agent, apiCaller base.APICaller, machineLockName string, clock clock.Clock) (worker.Worker, error) {
    58  	apiConn, ok := apiCaller.(api.Connection)
    59  	if !ok {
    60  		return nil, errors.New("unable to obtain api.Connection")
    61  	}
    62  	rebootState, err := apiConn.Reboot()
    63  	if err != nil {
    64  		return nil, errors.Trace(err)
    65  	}
    66  	w, err := NewReboot(rebootState, a.CurrentConfig(), machineLockName, clock)
    67  	if err != nil {
    68  		return nil, errors.Annotate(err, "cannot start reboot worker")
    69  	}
    70  	return w, nil
    71  }