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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package identityfilewriter
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/juju/names"
    10  
    11  	"github.com/juju/juju/agent"
    12  	apiagent "github.com/juju/juju/api/agent"
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/cmd/jujud/agent/util"
    15  	"github.com/juju/juju/state/multiwatcher"
    16  	"github.com/juju/juju/worker"
    17  	"github.com/juju/juju/worker/dependency"
    18  )
    19  
    20  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    21  type ManifoldConfig util.AgentApiManifoldConfig
    22  
    23  // Manifold returns a dependency manifold that runs an identity file writer worker,
    24  // using the resource names defined in the supplied config.
    25  func Manifold(config ManifoldConfig) dependency.Manifold {
    26  	typedConfig := util.AgentApiManifoldConfig(config)
    27  	return util.AgentApiManifold(typedConfig, newWorker)
    28  }
    29  
    30  // newWorker trivially wraps NewWorker for use in a util.AgentApiManifold.
    31  func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    32  	cfg := a.CurrentConfig()
    33  
    34  	// Grab the tag and ensure that it's for a machine.
    35  	tag, ok := cfg.Tag().(names.MachineTag)
    36  	if !ok {
    37  		return nil, errors.New("this manifold may only be used inside a machine agent")
    38  	}
    39  
    40  	// Get the machine agent's jobs.
    41  	entity, err := apiagent.NewState(apiCaller).Entity(tag)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	var isModelManager bool
    47  	for _, job := range entity.Jobs() {
    48  		if job == multiwatcher.JobManageModel {
    49  			isModelManager = true
    50  			break
    51  		}
    52  	}
    53  
    54  	if !isModelManager {
    55  		return nil, dependency.ErrMissing
    56  	}
    57  
    58  	return NewWorker(cfg)
    59  }
    60  
    61  var NewWorker = func(agentConfig agent.Config) (worker.Worker, error) {
    62  	inner := func(<-chan struct{}) error {
    63  		return agent.WriteSystemIdentityFile(agentConfig)
    64  	}
    65  	return worker.NewSimpleWorker(inner), nil
    66  }