github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 "gopkg.in/juju/worker.v1" 10 "gopkg.in/juju/worker.v1/dependency" 11 12 coreagent "github.com/juju/juju/agent" 13 apiagent "github.com/juju/juju/api/agent" 14 "github.com/juju/juju/api/base" 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, err := apiagent.NewState(apiCaller) 53 if err != nil { 54 return nil, errors.Trace(err) 55 } 56 57 // If the machine needs State, grab the state serving info 58 // over the API and write it to the agent configuration. 59 if controller, err := isController(apiState, tag); err != nil { 60 return nil, errors.Annotate(err, "checking controller status") 61 } else if !controller { 62 // Not a controller, nothing to do. 63 return nil, dependency.ErrUninstall 64 } 65 66 info, err := apiState.StateServingInfo() 67 if err != nil { 68 return nil, errors.Annotate(err, "getting state serving info") 69 } 70 err = agent.ChangeConfig(func(config coreagent.ConfigSetter) error { 71 existing, hasInfo := config.StateServingInfo() 72 if hasInfo { 73 // Use the existing cert and key as they appear to 74 // have been already updated by the cert updater 75 // worker to have this machine's IP address as 76 // part of the cert. This changed cert is never 77 // put back into the database, so it isn't 78 // reflected in the copy we have got from 79 // apiState. 80 info.Cert = existing.Cert 81 info.PrivateKey = existing.PrivateKey 82 } 83 config.SetStateServingInfo(info) 84 return nil 85 }) 86 if err != nil { 87 return nil, errors.Trace(err) 88 } 89 90 // All is well - we're done (no actual worker is actually returned). 91 return nil, dependency.ErrUninstall 92 }, 93 } 94 } 95 96 func isController(apiState *apiagent.State, tag names.MachineTag) (bool, error) { 97 machine, err := apiState.Entity(tag) 98 if err != nil { 99 return false, errors.Trace(err) 100 } 101 for _, job := range machine.Jobs() { 102 if job.NeedsState() { 103 return true, nil 104 } 105 } 106 return false, nil 107 }