github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/resumer/manifold.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package resumer 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 10 "github.com/juju/juju/agent" 11 apiagent "github.com/juju/juju/api/agent" 12 "github.com/juju/juju/api/base" 13 apiresumer "github.com/juju/juju/api/resumer" 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 a resumer worker, 24 // using the api connection resource named 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 func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) { 31 cfg := a.CurrentConfig() 32 // Grab the tag and ensure that it's for a machine. 33 tag, ok := cfg.Tag().(names.MachineTag) 34 if !ok { 35 return nil, errors.New("this manifold may only be used inside a machine agent") 36 } 37 38 // Get the machine agent's jobs. 39 // TODO(fwereade): this functionality should be on the 40 // deployer facade instead. 41 agentFacade := apiagent.NewState(apiCaller) 42 entity, err := agentFacade.Entity(tag) 43 if err != nil { 44 return nil, err 45 } 46 47 var isModelManager bool 48 for _, job := range entity.Jobs() { 49 if job == multiwatcher.JobManageModel { 50 isModelManager = true 51 break 52 } 53 } 54 if !isModelManager { 55 return nil, dependency.ErrMissing 56 } 57 58 return NewResumer(apiresumer.NewAPI(apiCaller)), nil 59 }