-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribution.go
98 lines (89 loc) · 2.73 KB
/
distribution.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/environs/context"
)
// distributeUnit takes a unit and set of clean, possibly empty, instances
// and asks the InstanceDistributor policy (if any) which ones are suitable
// for assigning the unit to. If there is no InstanceDistributor, or the
// distribution group is empty, then all of the candidates will be returned.
func distributeUnit(u *Unit, candidates []instance.Id, limitZones []string) ([]instance.Id, error) {
if len(candidates) == 0 {
return nil, nil
}
if u.st.policy == nil {
return candidates, nil
}
distributor, err := u.st.policy.InstanceDistributor()
if errors.IsNotImplemented(err) {
return candidates, nil
} else if err != nil {
return nil, err
}
if distributor == nil {
return nil, fmt.Errorf("policy returned nil instance distributor without an error")
}
distributionGroup, err := ApplicationInstances(u.st, u.doc.Application)
if err != nil {
return nil, err
}
if len(distributionGroup) == 0 {
return candidates, nil
}
return distributor.DistributeInstances(context.CallContext(u.st), candidates, distributionGroup, limitZones)
}
// ApplicationInstances returns the instance IDs of provisioned
// machines that are assigned units of the specified application.
func ApplicationInstances(st *State, application string) ([]instance.Id, error) {
units, err := allUnits(st, application)
if err != nil {
return nil, err
}
instanceIds := make([]instance.Id, 0, len(units))
for _, unit := range units {
machineId, err := unit.AssignedMachineId()
if errors.IsNotAssigned(err) {
continue
} else if err != nil {
return nil, err
}
machine, err := st.Machine(machineId)
if err != nil {
return nil, err
}
instanceId, err := machine.InstanceId()
if err == nil {
instanceIds = append(instanceIds, instanceId)
} else if errors.IsNotProvisioned(err) {
continue
} else {
return nil, err
}
}
return instanceIds, nil
}
// ApplicationMachines returns the machine IDs of machines which have
// the specified application listed as a principal.
func ApplicationMachines(st *State, application string) ([]string, error) {
machines, err := st.AllMachines()
if err != nil {
return nil, err
}
applicationName := unitAppName(application)
var machineIds []string
for _, machine := range machines {
principalSet := set.NewStrings()
for _, principal := range machine.Principals() {
principalSet.Add(unitAppName(principal))
}
if principalSet.Contains(applicationName) {
machineIds = append(machineIds, machine.Id())
}
}
return machineIds, nil
}