github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/machinelock/manifold.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machinelock
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/agent"
    10  	"github.com/juju/juju/cmd/jujud/agent/util"
    11  	cmdutil "github.com/juju/juju/cmd/jujud/util"
    12  	"github.com/juju/juju/worker"
    13  	"github.com/juju/juju/worker/dependency"
    14  )
    15  
    16  // createLock exists to be patched out in export_test.go
    17  var createLock = cmdutil.HookExecutionLock
    18  
    19  // ManifoldConfig specifies the names a machinelock manifold should use to
    20  // address its dependencies.
    21  type ManifoldConfig util.AgentManifoldConfig
    22  
    23  // Manifold returns a dependency.Manifold that governs the construction of
    24  // and access to a machine-wide lock intended to prevent various operations
    25  // from running concurrently and interfering with one another. Examples (are
    26  // not limited to): hook executions, package installation, synchronisation
    27  // of reboots.
    28  // Clients can access the lock by passing a **fslock.Lock into the out param
    29  // of their dependency.Context's Get method.
    30  func Manifold(config ManifoldConfig) dependency.Manifold {
    31  	manifold := util.AgentManifold(util.AgentManifoldConfig(config), newWorker)
    32  	manifold.Output = util.ValueWorkerOutput
    33  	return manifold
    34  }
    35  
    36  // newWorker creates a degenerate worker that provides access to an fslock.
    37  func newWorker(a agent.Agent) (worker.Worker, error) {
    38  	dataDir := a.CurrentConfig().DataDir()
    39  	lock, err := createLock(dataDir)
    40  	if err != nil {
    41  		return nil, errors.Trace(err)
    42  	}
    43  	return util.NewValueWorker(lock)
    44  }