-
Notifications
You must be signed in to change notification settings - Fork 494
/
unit_agent.go
335 lines (308 loc) · 10.8 KB
/
unit_agent.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package deployer
import (
"sync"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/lumberjack/v2"
"github.com/juju/names/v4"
"github.com/juju/utils/v3/voyeur"
"github.com/juju/version/v2"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/prometheus/client_golang/prometheus"
"github.com/juju/juju/agent"
"github.com/juju/juju/agent/addons"
"github.com/juju/juju/agent/tools"
"github.com/juju/juju/api/agent/uniter"
"github.com/juju/juju/api/base"
"github.com/juju/juju/core/arch"
"github.com/juju/juju/core/machinelock"
coreos "github.com/juju/juju/core/os"
"github.com/juju/juju/core/paths"
jujuversion "github.com/juju/juju/version"
"github.com/juju/juju/worker/introspection"
"github.com/juju/juju/worker/logsender"
)
// UnitAgent wraps the agent config for this unit.
type UnitAgent struct {
tag names.UnitTag
name string
clock clock.Clock
logger Logger
mu sync.Mutex
agentConf agent.ConfigSetterWriter
configChangedVal *voyeur.Value
setupLogging func(*loggo.Context, agent.Config)
unitEngineConfig func() dependency.EngineConfig
unitManifolds func(UnitManifoldsConfig) dependency.Manifolds
prometheusRegistry *prometheus.Registry
// Able to disable running units.
workerRunning bool
}
// UnitAgentConfig is a params struct with the values necessary to
// construct a working unit agent.
type UnitAgentConfig struct {
Name string
DataDir string
Clock clock.Clock
Logger Logger
UnitEngineConfig func() dependency.EngineConfig
UnitManifolds func(UnitManifoldsConfig) dependency.Manifolds
SetupLogging func(*loggo.Context, agent.Config)
}
// Validate ensures all the required values are set.
func (u *UnitAgentConfig) Validate() error {
if u.Name == "" {
return errors.NotValidf("missing Name")
}
if u.DataDir == "" {
return errors.NotValidf("missing DataDir")
}
if u.Clock == nil {
return errors.NotValidf("missing Clock")
}
if u.Logger == nil {
return errors.NotValidf("missing Logger")
}
if u.SetupLogging == nil {
return errors.NotValidf("missing SetupLogging")
}
if u.UnitEngineConfig == nil {
return errors.NotValidf("missing UnitEngineConfig")
}
if u.UnitManifolds == nil {
return errors.NotValidf("missing UnitManifolds")
}
return nil
}
// NewUnitAgent constructs an "agent" that is responsible for
// defining the workers for the unit and wraps access and updates
// to the agent.conf file for the unit. The method expects that there
// is an agent.conf file written in the <datadir>/agents/unit-<name>
// directory. It would be good to remove this need moving forwards
// and have unit agent logging overrides allowable in the machine
// agent config file.
func NewUnitAgent(config UnitAgentConfig) (*UnitAgent, error) {
if err := config.Validate(); err != nil {
return nil, errors.Trace(err)
}
// Create a symlink for the unit "agent" binaries.
// This is used because the uniter is still using the tools directory
// for the unit agent for creating the jujuc symlinks.
config.Logger.Tracef("creating symlink for %q to tools directory for jujuc", config.Name)
current := version.Binary{
Number: jujuversion.Current,
Arch: arch.HostArch(),
Release: coreos.HostOSTypeName(),
}
tag := names.NewUnitTag(config.Name)
toolsDir := tools.ToolsDir(config.DataDir, tag.String())
_, err := tools.ChangeAgentTools(config.DataDir, tag.String(), current)
defer removeOnErr(&err, config.Logger, toolsDir)
if err != nil {
// Any error here is indicative of a disk issue, potentially out of
// space or inodes. Either way, bouncing the deployer and having the
// exponential backoff enter play is the right decision.
return nil, errors.Trace(err)
}
config.Logger.Infof("creating new agent config for %q", config.Name)
conf, err := agent.ReadConfig(agent.ConfigPath(config.DataDir, tag))
if err != nil {
return nil, errors.Trace(err)
}
prometheusRegistry, err := addons.NewPrometheusRegistry()
if err != nil {
return nil, errors.Trace(err)
}
unit := &UnitAgent{
tag: tag,
name: config.Name,
clock: config.Clock,
logger: config.Logger,
agentConf: conf,
configChangedVal: voyeur.NewValue(true),
setupLogging: config.SetupLogging,
unitEngineConfig: config.UnitEngineConfig,
unitManifolds: config.UnitManifolds,
prometheusRegistry: prometheusRegistry,
}
// Update the 'upgradedToVersion' in the agent.conf file if it is
// different to the current version.
if conf.UpgradedToVersion() != jujuversion.Current {
if err := unit.ChangeConfig(func(setter agent.ConfigSetter) error {
setter.SetUpgradedToVersion(jujuversion.Current)
return nil
}); err != nil {
return nil, errors.Trace(err)
}
}
return unit, nil
}
func (a *UnitAgent) start() (worker.Worker, error) {
a.logger.Tracef("starting workers for %q", a.name)
loggingContext, bufferedLogger, closeLogging, err := a.initLogging()
if err != nil {
a.logger.Tracef("init logging failed %s", err)
return nil, errors.Trace(err)
}
updateAgentConfLogging := func(loggingConfig string) error {
return a.ChangeConfig(func(setter agent.ConfigSetter) error {
setter.SetLoggingConfig(loggingConfig)
return nil
})
}
machineLock, err := machinelock.New(machinelock.Config{
AgentName: a.tag.String(),
Clock: a.clock,
Logger: loggingContext.GetLogger("juju.machinelock"),
LogFilename: agent.MachineLockLogFilename(a.agentConf),
})
// There will only be an error if the required configuration
// values are not passed in.
if err != nil {
a.logger.Tracef("creating machine lock failed %s", err)
return nil, errors.Trace(err)
}
// construct unit agent manifold
a.logger.Tracef("creating unit manifolds for %q", a.name)
manifolds := a.unitManifolds(UnitManifoldsConfig{
LoggingContext: loggingContext,
Agent: a,
LogSource: bufferedLogger.Logs(),
LeadershipGuarantee: 30 * time.Second,
AgentConfigChanged: a.configChangedVal,
ValidateMigration: a.validateMigration,
UpdateLoggerConfig: updateAgentConfLogging,
MachineLock: machineLock,
Clock: a.clock,
})
depEngineConfig := a.unitEngineConfig()
// TODO: tweak IsFatal error func, maybe?
depEngineConfig.Logger = loggingContext.GetLogger("juju.worker.dependency")
// Tweak as necessary.
engine, err := dependency.NewEngine(depEngineConfig)
if err != nil {
return nil, err
}
a.logger.Tracef("installing manifolds for %q", a.name)
if err := dependency.Install(engine, manifolds); err != nil {
if err := worker.Stop(engine); err != nil {
a.logger.Errorf("while stopping engine with bad manifolds: %v", err)
}
return nil, err
}
a.mu.Lock()
a.workerRunning = true
a.mu.Unlock()
go func() {
// Wait for the worker to finish, then mark not running.
_ = engine.Wait()
a.mu.Lock()
a.workerRunning = false
closeLogging()
a.mu.Unlock()
}()
if err := addons.StartIntrospection(addons.IntrospectionConfig{
AgentTag: a.CurrentConfig().Tag(),
Engine: engine,
NewSocketName: addons.DefaultIntrospectionSocketName,
PrometheusGatherer: a.prometheusRegistry,
MachineLock: machineLock,
WorkerFunc: introspection.NewWorker,
}); err != nil {
// If the introspection worker failed to start, we just log error
// but continue. It is very unlikely to happen in the real world
// as the only issue is connecting to the abstract domain socket
// and the agent is controlled by by the OS to only have one.
a.logger.Errorf("failed to start introspection worker: %v", err)
}
a.logger.Tracef("engine for %q running", a.name)
return engine, nil
}
func (a *UnitAgent) running() bool {
a.mu.Lock()
defer a.mu.Unlock()
return a.workerRunning
}
func (a *UnitAgent) initLogging() (*loggo.Context, *logsender.BufferedLogWriter, func(), error) {
loggingContext := loggo.NewContext(loggo.INFO)
logFilename := agent.LogFilename(a.agentConf)
if err := paths.PrimeLogFile(logFilename); err != nil {
// This isn't a fatal error so log and continue if priming
// fails.
a.logger.Errorf("unable to prime %s (proceeding anyway): %v", logFilename, err)
}
ljLogger := &lumberjack.Logger{
Filename: logFilename, // eg: "/var/log/juju/unit-mysql-0.log"
MaxSize: a.CurrentConfig().AgentLogfileMaxSizeMB(),
MaxBackups: a.CurrentConfig().AgentLogfileMaxBackups(),
Compress: true,
}
a.logger.Debugf("created rotating log file %q with max size %d MB and max backups %d",
ljLogger.Filename, ljLogger.MaxSize, ljLogger.MaxBackups)
if err := loggingContext.AddWriter(
"file", loggo.NewSimpleWriter(ljLogger, loggo.DefaultFormatter)); err != nil {
a.logger.Errorf("unable to configure file logging for unit %q: %v", a.name, err)
}
bufferedLogger, err := logsender.InstallBufferedLogWriter(loggingContext, 1048576)
if err != nil {
return nil, nil, nil, errors.Annotate(err, "unable to add buffered log writer")
}
closeLogging := func() {
if _, err = loggingContext.RemoveWriter("file"); err != nil {
a.logger.Errorf("%q remove writer: %s", a.name, err)
}
bufferedLogger.Close()
if err = ljLogger.Close(); err != nil {
a.logger.Errorf("%q lumberjack logger close: %s", a.name, err)
}
}
// Add line for starting agent to logging context.
loggingContext.GetLogger("juju").Infof("Starting unit workers for %q", a.name)
a.setupLogging(loggingContext, a.agentConf)
return loggingContext, bufferedLogger, closeLogging, nil
}
// ChangeConfig modifies this configuration using the given mutator.
func (a *UnitAgent) ChangeConfig(change agent.ConfigMutator) error {
a.mu.Lock()
defer a.mu.Unlock()
if err := change(a.agentConf); err != nil {
return errors.Trace(err)
}
if err := a.agentConf.Write(); err != nil {
return errors.Annotate(err, "cannot write agent configuration")
}
a.configChangedVal.Set(true)
return nil
}
// CurrentConfig returns the agent config for this agent.
func (a *UnitAgent) CurrentConfig() agent.Config {
a.mu.Lock()
defer a.mu.Unlock()
return a.agentConf.Clone()
}
// validateMigration is called by the migrationminion to help check
// that the agent will be ok when connected to a new controller.
func (a *UnitAgent) validateMigration(apiCaller base.APICaller) error {
// TODO(mjs) - more extensive checks to come.
facade := uniter.NewState(apiCaller, a.tag)
_, err := facade.Unit(a.tag)
if err != nil {
return errors.Trace(err)
}
model, err := facade.Model()
if err != nil {
return errors.Trace(err)
}
curModelUUID := a.CurrentConfig().Model().Id()
newModelUUID := model.UUID
if newModelUUID != curModelUUID {
return errors.Errorf("model mismatch when validating: got %q, expected %q",
newModelUUID, curModelUUID)
}
return nil
}