~fwereade/juju-core/move-hook-context

« back to all changes in this revision

Viewing changes to worker/firewaller/firewaller.go

state,worker: full lifecycle machines watcher

MachinesWatcher now returns a list of ids that have had their
lifecycle changed in any way. It's up to the consumer to work
out details.

R=TheMue, aram
CC=
https://codereview.appspot.com/6566066

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
                        if !ok {
70
70
                                return watcher.MustErr(fw.machinesWatcher)
71
71
                        }
72
 
                        for _, id := range change.Dead {
73
 
                                machined := fw.machineds[id]
74
 
                                delete(fw.machineds, id)
75
 
                                if err := machined.Stop(); err != nil {
76
 
                                        log.Printf("machine %d watcher returned error when stopping: %v", id, err)
77
 
                                }
78
 
                                log.Debugf("firewaller: stopped watching machine %d", id)
79
 
                        }
80
 
                        for _, id := range change.Alive {
81
 
                                fw.machineds[id] = newMachineData(id, fw)
82
 
                                log.Debugf("firewaller: started watching machine %d", id)
 
72
                        for _, id := range change {
 
73
                                fw.machineLifeChanged(id)
83
74
                        }
84
75
                case change := <-fw.unitsChange:
85
76
                        changed := []*unitData{}
215
206
        return nil
216
207
}
217
208
 
 
209
// machineLifeChanged starts watching new machines when the firewaller
 
210
// is starting, or when new machines come to life, and stops watching
 
211
// machines that are dying.
 
212
func (fw *Firewaller) machineLifeChanged(id int) error {
 
213
        m, err := fw.st.Machine(id)
 
214
        found := !state.IsNotFound(err)
 
215
        if found && err != nil {
 
216
                return err
 
217
        }
 
218
        dead := !found || m.Life() == state.Dead
 
219
        machined, known := fw.machineds[id]
 
220
        if known && dead {
 
221
                return fw.forgetMachine(machined)
 
222
        }
 
223
        if !known && !dead {
 
224
                fw.machineds[id] = newMachineData(id, fw)
 
225
                log.Debugf("firewaller: started watching machine %d", id)
 
226
        }
 
227
        return nil
 
228
}
 
229
 
 
230
func (fw *Firewaller) forgetMachine(machined *machineData) error {
 
231
        for _, unitd := range machined.unitds {
 
232
                unitd.ports = nil
 
233
        }
 
234
        if err := fw.flushMachine(machined); err != nil {
 
235
                return err
 
236
        }
 
237
        for _, unitd := range machined.unitds {
 
238
                fw.forgetUnit(unitd)
 
239
        }
 
240
        delete(fw.machineds, machined.id)
 
241
        if err := machined.Stop(); err != nil {
 
242
                return err
 
243
        }
 
244
        log.Debugf("firewaller: stopped watching machine %d", machined.id)
 
245
        return nil
 
246
}
 
247
 
218
248
// forgetUnit cleans the unit data after the unit is removed.
219
249
func (fw *Firewaller) forgetUnit(unitd *unitData) {
220
250
        name := unitd.unit.Name()
329
359
                        return
330
360
                case change, ok := <-w.Changes():
331
361
                        if !ok {
332
 
                                md.fw.tomb.Kill(watcher.MustErr(w))
 
362
                                _, err := md.machine()
 
363
                                if !state.IsNotFound(err) {
 
364
                                        md.fw.tomb.Kill(watcher.MustErr(w))
 
365
                                }
333
366
                                return
334
367
                        }
335
368
                        select {
388
421
                        return
389
422
                case unit, ok := <-ud.watcher.Changes():
390
423
                        if !ok {
 
424
                                // TODO(niemeyer): Unit watcher shouldn't return a unit.
391
425
                                err := watcher.MustErr(ud.watcher)
392
426
                                if !state.IsNotFound(err) {
393
427
                                        ud.fw.tomb.Kill(err)
468
502
                        return
469
503
                case service, ok := <-sd.watcher.Changes():
470
504
                        if !ok {
 
505
                                // TODO(niemeyer): Service watcher shouldn't return a service.
471
506
                                err := watcher.MustErr(sd.watcher)
472
507
                                if !state.IsNotFound(err) {
473
508
                                        sd.fw.tomb.Kill(err)