LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2012, 2013 Canonical Ltd. | 1 // Copyright 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 package machiner | 3 package machiner |
4 | 4 |
5 import ( | 5 import ( |
6 "fmt" | |
7 | |
8 "launchpad.net/loggo" | 6 "launchpad.net/loggo" |
9 | 7 |
10 "launchpad.net/juju-core/errors" | 8 "launchpad.net/juju-core/errors" |
11 "launchpad.net/juju-core/state/api" | 9 "launchpad.net/juju-core/state/api" |
12 "launchpad.net/juju-core/state/api/machiner" | 10 "launchpad.net/juju-core/state/api/machiner" |
13 "launchpad.net/juju-core/state/api/params" | 11 "launchpad.net/juju-core/state/api/params" |
14 "launchpad.net/juju-core/worker" | 12 "launchpad.net/juju-core/worker" |
15 ) | 13 ) |
16 | 14 |
17 var logger = loggo.GetLogger("juju.worker.machiner") | 15 var logger = loggo.GetLogger("juju.worker.machiner") |
18 | 16 |
19 // Machiner is responsible for a machine agent's lifecycle. | 17 // Machiner is responsible for a machine agent's lifecycle. |
20 type Machiner struct { | 18 type Machiner struct { |
21 st *machiner.State | 19 st *machiner.State |
22 tag string | 20 tag string |
23 machine *machiner.Machine | 21 machine *machiner.Machine |
24 } | 22 } |
25 | 23 |
26 // NewMachiner returns a Machiner that will wait for the identified machine | 24 // NewMachiner returns a Worker that will wait for the identified machine |
27 // to become Dying and make it Dead; or until the machine becomes Dead by | 25 // to become Dying and make it Dead; or until the machine becomes Dead by |
28 // other means. | 26 // other means. |
29 func NewMachiner(st *machiner.State, tag string) worker.NotifyWorker { | 27 func NewMachiner(st *machiner.State, tag string) worker.Worker { |
30 mr := &Machiner{st: st, tag: tag} | 28 mr := &Machiner{st: st, tag: tag} |
31 return worker.NewNotifyWorker(mr) | 29 return worker.NewNotifyWorker(mr) |
32 } | |
33 | |
34 func (mr *Machiner) String() string { | |
35 return fmt.Sprintf("machiner for %s", mr.tag) | |
36 } | 30 } |
37 | 31 |
38 func isNotFoundOrUnauthorized(err error) bool { | 32 func isNotFoundOrUnauthorized(err error) bool { |
39 return errors.IsNotFoundError(err) || params.ErrCode(err) == params.Code
Unauthorized | 33 return errors.IsNotFoundError(err) || params.ErrCode(err) == params.Code
Unauthorized |
40 } | 34 } |
41 | 35 |
42 func (mr *Machiner) SetUp() (api.NotifyWatcher, error) { | 36 func (mr *Machiner) SetUp() (api.NotifyWatcher, error) { |
43 // Find which machine we're responsible for. | 37 // Find which machine we're responsible for. |
44 m, err := mr.st.Machine(mr.tag) | 38 m, err := mr.st.Machine(mr.tag) |
45 if isNotFoundOrUnauthorized(err) { | 39 if isNotFoundOrUnauthorized(err) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 logger.Errorf("%s falied to set machine to dead: %v", mr, err) | 74 logger.Errorf("%s falied to set machine to dead: %v", mr, err) |
81 return err | 75 return err |
82 } | 76 } |
83 return worker.ErrTerminateAgent | 77 return worker.ErrTerminateAgent |
84 } | 78 } |
85 | 79 |
86 func (mr *Machiner) TearDown() error { | 80 func (mr *Machiner) TearDown() error { |
87 // Nothing to do here. | 81 // Nothing to do here. |
88 return nil | 82 return nil |
89 } | 83 } |
LEFT | RIGHT |