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