~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/toolsversionchecker/manifold.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package toolsversionchecker
 
5
 
 
6
import (
 
7
        "time"
 
8
 
 
9
        "github.com/juju/errors"
 
10
 
 
11
        "github.com/juju/juju/agent"
 
12
        apiagent "github.com/juju/juju/api/agent"
 
13
        "github.com/juju/juju/api/agenttools"
 
14
        "github.com/juju/juju/api/base"
 
15
        "github.com/juju/juju/cmd/jujud/agent/engine"
 
16
        "github.com/juju/juju/state/multiwatcher"
 
17
        "github.com/juju/juju/worker"
 
18
        "github.com/juju/juju/worker/dependency"
 
19
        "gopkg.in/juju/names.v2"
 
20
)
 
21
 
 
22
// ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
 
23
type ManifoldConfig engine.AgentApiManifoldConfig
 
24
 
 
25
// Manifold returns a dependency manifold that runs a toolsversionchecker worker,
 
26
// using the api connection resource named in the supplied config.
 
27
func Manifold(config ManifoldConfig) dependency.Manifold {
 
28
        typedConfig := engine.AgentApiManifoldConfig(config)
 
29
        return engine.AgentApiManifold(typedConfig, newWorker)
 
30
}
 
31
 
 
32
func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
 
33
        st := apiagent.NewState(apiCaller)
 
34
        isMM, err := isModelManager(a, st)
 
35
        if err != nil {
 
36
                return nil, errors.Trace(err)
 
37
        }
 
38
        if !isMM {
 
39
                return nil, dependency.ErrMissing
 
40
        }
 
41
 
 
42
        // 4 times a day seems a decent enough amount of checks.
 
43
        checkerParams := VersionCheckerParams{
 
44
                CheckInterval: time.Hour * 6,
 
45
        }
 
46
        return New(agenttools.NewFacade(apiCaller), &checkerParams), nil
 
47
}
 
48
 
 
49
func isModelManager(a agent.Agent, st *apiagent.State) (bool, error) {
 
50
        cfg := a.CurrentConfig()
 
51
 
 
52
        // Grab the tag and ensure that it's for a machine.
 
53
        tag, ok := cfg.Tag().(names.MachineTag)
 
54
        if !ok {
 
55
                return false, errors.New("this manifold may only be used inside a machine agent")
 
56
        }
 
57
 
 
58
        entity, err := st.Entity(tag)
 
59
        if err != nil {
 
60
                return false, err
 
61
        }
 
62
 
 
63
        for _, job := range entity.Jobs() {
 
64
                if job == multiwatcher.JobManageModel {
 
65
                        return true, nil
 
66
                }
 
67
        }
 
68
 
 
69
        return false, nil
 
70
}