| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 package machiner | 1 package machiner |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "launchpad.net/juju-core/container" | 4 "launchpad.net/juju-core/container" |
| 5 "launchpad.net/juju-core/log" | 5 "launchpad.net/juju-core/log" |
| 6 "launchpad.net/juju-core/state" | 6 "launchpad.net/juju-core/state" |
| 7 "launchpad.net/juju-core/state/watcher" | 7 "launchpad.net/juju-core/state/watcher" |
| 8 "launchpad.net/tomb" | 8 "launchpad.net/tomb" |
| 9 ) | 9 ) |
| 10 | 10 |
| 11 // NewMachiner starts a machine agent running. | 11 // NewMachiner starts a machine agent running that |
| 12 // deploys agents in the given directory. | |
| 12 // The Machiner dies when it encounters an error. | 13 // The Machiner dies when it encounters an error. |
| 13 func NewMachiner(machine *state.Machine) *Machiner { | 14 func NewMachiner(machine *state.Machine, varDir string) *Machiner { |
| 14 » m := &Machiner{} | 15 » cont := &container.Simple{VarDir: varDir} |
| 16 » return newMachiner(machine, cont) | |
| 17 } | |
| 18 | |
| 19 func newMachiner(machine *state.Machine, cont container.Container) *Machiner { | |
| 20 » m := &Machiner{container: cont} | |
| 15 go m.loop(machine) | 21 go m.loop(machine) |
| 16 return m | 22 return m |
| 17 } | 23 } |
| 18 | 24 |
| 19 // Machiner represents a running machine agent. | 25 // Machiner represents a running machine agent. |
| 20 type Machiner struct { | 26 type Machiner struct { |
| 21 » tomb tomb.Tomb | 27 » tomb tomb.Tomb |
| 28 » container container.Container | |
|
fwereade
2012/09/10 06:58:30
Wait, what? A machiner can only deploy to one cont
rog
2012/09/10 19:53:10
When LXC is added, we'll probably have "localConta
niemeyer
2012/09/11 18:35:21
This is true, but the decision is made considering
rog
2012/09/12 16:46:17
reverted after discussion on line.
| |
| 22 } | 29 } |
| 23 | 30 |
| 24 func (m *Machiner) loop(machine *state.Machine) { | 31 func (m *Machiner) loop(machine *state.Machine) { |
| 25 defer m.tomb.Done() | 32 defer m.tomb.Done() |
| 26 w := machine.WatchUnits() | 33 w := machine.WatchUnits() |
| 27 defer watcher.Stop(w, &m.tomb) | 34 defer watcher.Stop(w, &m.tomb) |
| 28 | 35 |
| 29 // TODO read initial units, check if they're running | 36 // TODO read initial units, check if they're running |
| 30 // and restart them if not. Also track units so | 37 // and restart them if not. Also track units so |
| 31 // that we don't deploy units that are already running. | 38 // that we don't deploy units that are already running. |
| 32 for { | 39 for { |
| 33 select { | 40 select { |
| 34 case <-m.tomb.Dying(): | 41 case <-m.tomb.Dying(): |
| 35 return | 42 return |
| 36 case change, ok := <-w.Changes(): | 43 case change, ok := <-w.Changes(): |
| 37 if !ok { | 44 if !ok { |
| 38 m.tomb.Kill(watcher.MustErr(w)) | 45 m.tomb.Kill(watcher.MustErr(w)) |
| 39 return | 46 return |
| 40 } | 47 } |
| 41 for _, u := range change.Removed { | 48 for _, u := range change.Removed { |
| 42 if u.IsPrincipal() { | 49 if u.IsPrincipal() { |
| 43 » » » » » if err := container.Simple.Destroy(u); e rr != nil { | 50 » » » » » if err := m.container.Destroy(u); err != nil { |
| 44 log.Printf("cannot destroy unit %s: %v", u.Name(), err) | 51 log.Printf("cannot destroy unit %s: %v", u.Name(), err) |
| 45 } | 52 } |
| 46 } | 53 } |
| 47 } | 54 } |
| 48 for _, u := range change.Added { | 55 for _, u := range change.Added { |
| 49 if u.IsPrincipal() { | 56 if u.IsPrincipal() { |
| 50 » » » » » if err := container.Simple.Deploy(u); er r != nil { | 57 » » » » » if err := m.container.Deploy(u); err != nil { |
| 51 // TODO put unit into a queue to retry the deploy. | 58 // TODO put unit into a queue to retry the deploy. |
| 52 log.Printf("cannot deploy unit % s: %v", u.Name(), err) | 59 log.Printf("cannot deploy unit % s: %v", u.Name(), err) |
| 53 } | 60 } |
| 54 } | 61 } |
| 55 } | 62 } |
| 56 } | 63 } |
| 57 } | 64 } |
| 58 } | 65 } |
| 59 | 66 |
| 60 // Wait waits until the Machiner has died, and returns the error encountered. | 67 // Wait waits until the Machiner has died, and returns the error encountered. |
| 61 func (m *Machiner) Wait() error { | 68 func (m *Machiner) Wait() error { |
| 62 return m.tomb.Wait() | 69 return m.tomb.Wait() |
| 63 } | 70 } |
| 64 | 71 |
| 65 // Stop terminates the Machiner and returns any error that it encountered. | 72 // Stop terminates the Machiner and returns any error that it encountered. |
| 66 func (m *Machiner) Stop() error { | 73 func (m *Machiner) Stop() error { |
| 67 m.tomb.Kill(nil) | 74 m.tomb.Kill(nil) |
| 68 return m.tomb.Wait() | 75 return m.tomb.Wait() |
| 69 } | 76 } |
| OLD | NEW |