1
// Copyright 2012-2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
"github.com/juju/utils/clock"
9
"github.com/juju/utils/proxy"
10
"gopkg.in/juju/charm.v6-unstable"
11
"gopkg.in/juju/names.v2"
13
"github.com/juju/juju/api/uniter"
14
"github.com/juju/juju/apiserver/params"
15
"github.com/juju/juju/core/leadership"
16
"github.com/juju/juju/worker/uniter/runner/jujuc"
20
ValidatePortRange = validatePortRange
21
TryOpenPorts = tryOpenPorts
22
TryClosePorts = tryClosePorts
32
remoteUnitName string,
33
relations map[int]*ContextRelation,
35
proxySettings proxy.Settings,
37
charmMetrics *charm.Metrics,
38
actionData *ActionData,
39
assignedMachineTag names.MachineTag,
42
) (*HookContext, error) {
49
unitName: unit.Name(),
50
relationId: relationId,
51
remoteUnitName: remoteUnitName,
54
proxySettings: proxySettings,
55
actionData: actionData,
56
pendingPorts: make(map[PortRange]PortRangeInfo),
57
assignedMachineTag: assignedMachineTag,
60
// Get and cache the addresses.
62
ctx.publicAddress, err = unit.PublicAddress()
63
if err != nil && !params.IsCodeNoAddressSet(err) {
66
ctx.privateAddress, err = unit.PrivateAddress()
67
if err != nil && !params.IsCodeNoAddressSet(err) {
70
ctx.availabilityzone, err = unit.AvailabilityZone()
74
ctx.machinePorts, err = state.AllMachinePorts(ctx.assignedMachineTag)
76
return nil, errors.Trace(err)
79
statusCode, statusInfo, err := unit.MeterStatus()
81
return nil, errors.Annotate(err, "could not retrieve meter status for unit")
83
ctx.meterStatus = &meterStatus{
90
// SetEnvironmentHookContextRelation exists purely to set the fields used in hookVars.
91
// It makes no assumptions about the validity of context.
92
func SetEnvironmentHookContextRelation(
94
relationId int, endpointName, remoteUnitName string,
96
context.relationId = relationId
97
context.remoteUnitName = remoteUnitName
98
context.relations = map[int]*ContextRelation{
100
endpointName: endpointName,
101
relationId: relationId,
106
func PatchCachedStatus(ctx jujuc.Context, status, info string, data map[string]interface{}) func() {
107
hctx := ctx.(*HookContext)
108
oldStatus := hctx.status
109
hctx.status = &jujuc.StatusInfo{
115
hctx.status = oldStatus
119
func GetStubActionContext(in map[string]interface{}) *HookContext {
121
actionData: &ActionData{
127
type LeadershipContextFunc func(LeadershipSettingsAccessor, leadership.Tracker) LeadershipContext
129
func PatchNewLeadershipContext(f LeadershipContextFunc) func() {
130
var old LeadershipContextFunc
131
old, newLeadershipContext = newLeadershipContext, f
132
return func() { newLeadershipContext = old }
135
func StorageAddConstraints(ctx *HookContext) map[string][]params.StorageConstraints {
136
return ctx.storageAddConstraints
139
// NewModelHookContext exists purely to set the fields used in rs.
140
// The returned value is not otherwise valid.
141
func NewModelHookContext(
142
id, modelUUID, envName, unitName, meterCode, meterInfo, availZone string,
143
apiAddresses []string, proxySettings proxy.Settings,
144
machineTag names.MachineTag,
151
apiAddrs: apiAddresses,
152
proxySettings: proxySettings,
153
meterStatus: &meterStatus{
158
assignedMachineTag: machineTag,
159
availabilityzone: availZone,
163
func ContextEnvInfo(hctx *HookContext) (name, uuid string) {
164
return hctx.envName, hctx.uuid
167
func ContextMachineTag(hctx *HookContext) names.MachineTag {
168
return hctx.assignedMachineTag
171
func UpdateCachedSettings(cf0 ContextFactory, relId int, unitName string, settings params.Settings) {
172
cf := cf0.(*contextFactory)
173
members := cf.relationCaches[relId].members
174
if members[unitName] == nil {
175
members[unitName] = params.Settings{}
177
for key, value := range settings {
178
members[unitName][key] = value
182
func CachedSettings(cf0 ContextFactory, relId int, unitName string) (params.Settings, bool) {
183
cf := cf0.(*contextFactory)
184
settings, found := cf.relationCaches[relId].members[unitName]
185
return settings, found