forked from juju/juju
/
deployer.go
103 lines (94 loc) · 2.84 KB
/
deployer.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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package deployer
import (
"fmt"
"github.com/juju/names"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state/apiserver/common"
)
// DeployerAPI provides access to the Deployer API facade.
type DeployerAPI struct {
*common.Remover
*common.PasswordChanger
*common.LifeGetter
*common.StateAddresser
*common.APIAddresser
*common.UnitsWatcher
st *state.State
resources *common.Resources
authorizer common.Authorizer
}
// NewDeployerAPI creates a new server-side DeployerAPI facade.
func NewDeployerAPI(
st *state.State,
resources *common.Resources,
authorizer common.Authorizer,
) (*DeployerAPI, error) {
if !authorizer.AuthMachineAgent() {
return nil, common.ErrPerm
}
getAuthFunc := func() (common.AuthFunc, error) {
// Get all units of the machine and cache them.
thisMachineTag := authorizer.GetAuthTag()
units, err := getAllUnits(st, thisMachineTag)
if err != nil {
return nil, err
}
// Then we just check if the unit is already known.
return func(tag string) bool {
for _, unit := range units {
if names.NewUnitTag(unit).String() == tag {
return true
}
}
return false
}, nil
}
getCanWatch := func() (common.AuthFunc, error) {
return authorizer.AuthOwner, nil
}
return &DeployerAPI{
Remover: common.NewRemover(st, true, getAuthFunc),
PasswordChanger: common.NewPasswordChanger(st, getAuthFunc),
LifeGetter: common.NewLifeGetter(st, getAuthFunc),
StateAddresser: common.NewStateAddresser(st),
APIAddresser: common.NewAPIAddresser(st, resources),
UnitsWatcher: common.NewUnitsWatcher(st, resources, getCanWatch),
st: st,
resources: resources,
authorizer: authorizer,
}, nil
}
// ConnectionInfo returns all the address information that the
// deployer task needs in one call.
func (d *DeployerAPI) ConnectionInfo() (result params.DeployerConnectionValues, err error) {
info, err := d.st.DeployerConnectionInfo()
if info != nil {
result = params.DeployerConnectionValues{
StateAddresses: info.StateAddresses,
APIAddresses: info.APIAddresses,
}
}
return result, err
}
// getAllUnits returns a list of all principal and subordinate units
// assigned to the given machine.
func getAllUnits(st *state.State, machineTag string) ([]string, error) {
tag, err := names.ParseTag(machineTag, names.MachineTagKind)
if err != nil {
return nil, err
}
machine, err := st.Machine(tag.Id())
if err != nil {
return nil, err
}
// Start a watcher on machine's units, read the initial event and stop it.
watch := machine.WatchUnits()
defer watch.Stop()
if units, ok := <-watch.Changes(); ok {
return units, nil
}
return nil, fmt.Errorf("cannot obtain units of machine %q: %v", machineTag, watch.Err())
}