github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  
     9  	"github.com/juju/juju/agent"
    10  	"github.com/juju/juju/api"
    11  	"github.com/juju/juju/api/base"
    12  	"github.com/juju/juju/cmd/jujud/agent/util"
    13  	cmdutil "github.com/juju/juju/cmd/jujud/util"
    14  	"github.com/juju/juju/worker"
    15  	"github.com/juju/juju/worker/dependency"
    16  )
    17  
    18  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    19  type ManifoldConfig util.AgentApiManifoldConfig
    20  
    21  // Manifold returns a dependency manifold that runs a reboot worker,
    22  // using the resource names defined in the supplied config.
    23  func Manifold(config ManifoldConfig) dependency.Manifold {
    24  	typedConfig := util.AgentApiManifoldConfig(config)
    25  	return util.AgentApiManifold(typedConfig, newWorker)
    26  }
    27  
    28  // newWorker trivially wraps NewReboot for use in a util.AgentApiManifold.
    29  //
    30  // TODO(mjs) - It's not tested at the moment, because the scaffolding
    31  // necessary is too unwieldy/distracting to introduce at this point.
    32  func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    33  	apiConn, ok := apiCaller.(api.Connection)
    34  	if !ok {
    35  		return nil, errors.New("unable to obtain api.Connection")
    36  	}
    37  	rebootState, err := apiConn.Reboot()
    38  	if err != nil {
    39  		return nil, errors.Trace(err)
    40  	}
    41  	lock, err := cmdutil.HookExecutionLock(cmdutil.DataDir)
    42  	if err != nil {
    43  		return nil, errors.Trace(err)
    44  	}
    45  	w, err := NewReboot(rebootState, a.CurrentConfig(), lock)
    46  	if err != nil {
    47  		return nil, errors.Annotate(err, "cannot start reboot worker")
    48  	}
    49  	return w, nil
    50  }