github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/runner/export_test.go (about) 1 // Copyright 2012-2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package runner 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 "github.com/juju/utils/proxy" 10 "gopkg.in/juju/charm.v5" 11 12 "github.com/juju/juju/api/uniter" 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/worker/leadership" 15 "github.com/juju/juju/worker/uniter/metrics" 16 "github.com/juju/juju/worker/uniter/runner/jujuc" 17 ) 18 19 var ( 20 MergeWindowsEnvironment = mergeWindowsEnvironment 21 SearchHook = searchHook 22 HookCommand = hookCommand 23 LookPath = lookPath 24 ValidatePortRange = validatePortRange 25 TryOpenPorts = tryOpenPorts 26 TryClosePorts = tryClosePorts 27 ) 28 29 func RunnerPaths(rnr Runner) Paths { 30 return rnr.(*runner).paths 31 } 32 33 type LeadershipContextFunc func(LeadershipSettingsAccessor, leadership.Tracker) LeadershipContext 34 35 func PatchNewLeadershipContext(f LeadershipContextFunc) func() { 36 var old LeadershipContextFunc 37 old, newLeadershipContext = newLeadershipContext, f 38 return func() { newLeadershipContext = old } 39 } 40 41 func UpdateCachedSettings(f0 Factory, relId int, unitName string, settings params.Settings) { 42 f := f0.(*factory) 43 cf := f.contextFactory.(*contextFactory) 44 members := cf.relationCaches[relId].members 45 if members[unitName] == nil { 46 members[unitName] = params.Settings{} 47 } 48 for key, value := range settings { 49 members[unitName][key] = value 50 } 51 } 52 53 func CachedSettings(f0 Factory, relId int, unitName string) (params.Settings, bool) { 54 f := f0.(*factory) 55 cf := f.contextFactory.(*contextFactory) 56 settings, found := cf.relationCaches[relId].members[unitName] 57 return settings, found 58 } 59 60 // PatchMeterStatus changes the meter status of the context. 61 func (ctx *HookContext) PatchMeterStatus(code, info string) func() { 62 oldMeterStatus := ctx.meterStatus 63 ctx.meterStatus = &meterStatus{ 64 code: code, 65 info: info, 66 } 67 return func() { 68 ctx.meterStatus = oldMeterStatus 69 } 70 } 71 72 func ContextEnvInfo(ctx Context) (name, uuid string) { 73 hctx := ctx.(*HookContext) 74 return hctx.envName, hctx.uuid 75 } 76 77 func ContextMachineTag(ctx Context) names.MachineTag { 78 hctx := ctx.(*HookContext) 79 return hctx.assignedMachineTag 80 } 81 82 func GetStubActionContext(in map[string]interface{}) *HookContext { 83 return &HookContext{ 84 actionData: &ActionData{ 85 ResultsMap: in, 86 }, 87 } 88 } 89 90 func PatchCachedStatus(ctx Context, status, info string, data map[string]interface{}) func() { 91 hctx := ctx.(*HookContext) 92 oldStatus := hctx.status 93 hctx.status = &jujuc.StatusInfo{ 94 Status: status, 95 Info: info, 96 Data: data, 97 } 98 return func() { 99 hctx.status = oldStatus 100 } 101 } 102 103 func NewHookContext( 104 unit *uniter.Unit, 105 state *uniter.State, 106 id, 107 uuid, 108 envName string, 109 relationId int, 110 remoteUnitName string, 111 relations map[int]*ContextRelation, 112 apiAddrs []string, 113 proxySettings proxy.Settings, 114 canAddMetrics bool, 115 charmMetrics *charm.Metrics, 116 actionData *ActionData, 117 assignedMachineTag names.MachineTag, 118 paths Paths, 119 ) (*HookContext, error) { 120 ctx := &HookContext{ 121 unit: unit, 122 state: state, 123 id: id, 124 uuid: uuid, 125 envName: envName, 126 unitName: unit.Name(), 127 relationId: relationId, 128 remoteUnitName: remoteUnitName, 129 relations: relations, 130 apiAddrs: apiAddrs, 131 proxySettings: proxySettings, 132 metricsRecorder: nil, 133 definedMetrics: charmMetrics, 134 actionData: actionData, 135 pendingPorts: make(map[PortRange]PortRangeInfo), 136 assignedMachineTag: assignedMachineTag, 137 } 138 if canAddMetrics { 139 charmURL, err := unit.CharmURL() 140 if err != nil { 141 return nil, err 142 } 143 ctx.metricsRecorder, err = metrics.NewJSONMetricRecorder( 144 paths.GetMetricsSpoolDir(), 145 charmMetrics.Metrics, 146 charmURL.String()) 147 if err != nil { 148 return nil, err 149 } 150 } 151 // Get and cache the addresses. 152 var err error 153 ctx.publicAddress, err = unit.PublicAddress() 154 if err != nil && !params.IsCodeNoAddressSet(err) { 155 return nil, err 156 } 157 ctx.privateAddress, err = unit.PrivateAddress() 158 if err != nil && !params.IsCodeNoAddressSet(err) { 159 return nil, err 160 } 161 ctx.availabilityzone, err = unit.AvailabilityZone() 162 if err != nil { 163 return nil, err 164 } 165 ctx.machinePorts, err = state.AllMachinePorts(ctx.assignedMachineTag) 166 if err != nil { 167 return nil, errors.Trace(err) 168 } 169 170 statusCode, statusInfo, err := unit.MeterStatus() 171 if err != nil { 172 return nil, errors.Annotate(err, "could not retrieve meter status for unit") 173 } 174 ctx.meterStatus = &meterStatus{ 175 code: statusCode, 176 info: statusInfo, 177 } 178 return ctx, nil 179 } 180 181 // NewEnvironmentHookContext exists purely to set the fields used in rs. 182 // The returned value is not otherwise valid. 183 func NewEnvironmentHookContext( 184 id, envUUID, envName, unitName, meterCode, meterInfo, availZone string, 185 apiAddresses []string, proxySettings proxy.Settings, 186 machineTag names.MachineTag, 187 ) *HookContext { 188 return &HookContext{ 189 id: id, 190 unitName: unitName, 191 uuid: envUUID, 192 envName: envName, 193 apiAddrs: apiAddresses, 194 proxySettings: proxySettings, 195 meterStatus: &meterStatus{ 196 code: meterCode, 197 info: meterInfo, 198 }, 199 relationId: -1, 200 assignedMachineTag: machineTag, 201 availabilityzone: availZone, 202 } 203 } 204 205 // SetEnvironmentHookContextRelation exists purely to set the fields used in hookVars. 206 // It makes no assumptions about the validity of context. 207 func SetEnvironmentHookContextRelation( 208 context *HookContext, 209 relationId int, endpointName, remoteUnitName string, 210 ) { 211 context.relationId = relationId 212 context.remoteUnitName = remoteUnitName 213 context.relations = map[int]*ContextRelation{ 214 relationId: { 215 endpointName: endpointName, 216 relationId: relationId, 217 }, 218 } 219 } 220 221 func (ctx *HookContext) StorageAddConstraints() map[string][]params.StorageConstraints { 222 return ctx.storageAddConstraints 223 } 224 225 // PatchMetricsRecorder patches the metrics writer used by the context with a new 226 // object. 227 func PatchMetricsRecorder(ctx jujuc.Context, writer MetricsRecorder) func() { 228 hctx := ctx.(*HookContext) 229 oldRecorder := hctx.metricsRecorder 230 hctx.metricsRecorder = writer 231 return func() { 232 hctx.metricsRecorder = oldRecorder 233 } 234 }