github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/jujud/agent/machine/servinginfo_setter.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package machine 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 10 coreagent "github.com/juju/juju/agent" 11 apiagent "github.com/juju/juju/api/agent" 12 "github.com/juju/juju/api/base" 13 "github.com/juju/juju/worker" 14 "github.com/juju/juju/worker/dependency" 15 ) 16 17 // ServingInfoSetterConfig provides the dependencies for the 18 // servingInfoSetter manifold. 19 type ServingInfoSetterConfig struct { 20 AgentName string 21 APICallerName string 22 } 23 24 // ServingInfoSetterManifold defines a simple start function which 25 // runs after the API connection has come up. If the machine agent is 26 // a controller, it grabs the state serving info over the API and 27 // records it to agent configuration, and then stops. 28 func ServingInfoSetterManifold(config ServingInfoSetterConfig) dependency.Manifold { 29 return dependency.Manifold{ 30 Inputs: []string{ 31 config.AgentName, 32 config.APICallerName, 33 }, 34 Start: func(context dependency.Context) (worker.Worker, error) { 35 // Get the agent. 36 var agent coreagent.Agent 37 if err := context.Get(config.AgentName, &agent); err != nil { 38 return nil, err 39 } 40 41 // Grab the tag and ensure that it's for a machine. 42 tag, ok := agent.CurrentConfig().Tag().(names.MachineTag) 43 if !ok { 44 return nil, errors.New("agent's tag is not a machine tag") 45 } 46 47 // Get API connection. 48 var apiCaller base.APICaller 49 if err := context.Get(config.APICallerName, &apiCaller); err != nil { 50 return nil, err 51 } 52 apiState := apiagent.NewState(apiCaller) 53 54 // If the machine needs State, grab the state serving info 55 // over the API and write it to the agent configuration. 56 if controller, err := isController(apiState, tag); err != nil { 57 return nil, errors.Annotate(err, "checking controller status") 58 } else if !controller { 59 // Not a controller, nothing to do. 60 return nil, dependency.ErrUninstall 61 } 62 63 info, err := apiState.StateServingInfo() 64 if err != nil { 65 return nil, errors.Annotate(err, "getting state serving info") 66 } 67 err = agent.ChangeConfig(func(config coreagent.ConfigSetter) error { 68 existing, hasInfo := config.StateServingInfo() 69 if hasInfo { 70 // Use the existing cert and key as they appear to 71 // have been already updated by the cert updater 72 // worker to have this machine's IP address as 73 // part of the cert. This changed cert is never 74 // put back into the database, so it isn't 75 // reflected in the copy we have got from 76 // apiState. 77 info.Cert = existing.Cert 78 info.PrivateKey = existing.PrivateKey 79 } 80 config.SetStateServingInfo(info) 81 return nil 82 }) 83 if err != nil { 84 return nil, errors.Trace(err) 85 } 86 87 // All is well - we're done (no actual worker is actually returned). 88 return nil, dependency.ErrUninstall 89 }, 90 } 91 } 92 93 func isController(apiState *apiagent.State, tag names.MachineTag) (bool, error) { 94 machine, err := apiState.Entity(tag) 95 if err != nil { 96 return false, errors.Trace(err) 97 } 98 for _, job := range machine.Jobs() { 99 if job.NeedsState() { 100 return true, nil 101 } 102 } 103 return false, nil 104 }