OLD | NEW |
1 // launchpad.net/juju/state | 1 // launchpad.net/juju/state |
2 // | 2 // |
3 // Copyright (c) 2011-2012 Canonical Ltd. | 3 // Copyright (c) 2011-2012 Canonical Ltd. |
4 | 4 |
5 // The state package enables reading, observing, and changing | 5 // The state package enables reading, observing, and changing |
6 // the state stored in ZooKeeper of a whole environment | 6 // the state stored in ZooKeeper of a whole environment |
7 // managed by juju. | 7 // managed by juju. |
8 package state | 8 package state |
9 | 9 |
10 import ( | 10 import ( |
(...skipping 20 matching lines...) Expand all Loading... |
31 key := strings.Split(path, "/")[2] | 31 key := strings.Split(path, "/")[2] |
32 addMachine := func(t *topology) error { | 32 addMachine := func(t *topology) error { |
33 return t.AddMachine(key) | 33 return t.AddMachine(key) |
34 } | 34 } |
35 if err = retryTopologyChange(s.zk, addMachine); err != nil { | 35 if err = retryTopologyChange(s.zk, addMachine); err != nil { |
36 return nil, err | 36 return nil, err |
37 } | 37 } |
38 return &Machine{s, key}, nil | 38 return &Machine{s, key}, nil |
39 } | 39 } |
40 | 40 |
| 41 // RemoveMachine removes the machine with the given id. |
| 42 func (s *State) RemoveMachine(id int) error { |
| 43 key := machineKey(id) |
| 44 removeMachine := func(t *topology) error { |
| 45 if !t.HasMachine(key) { |
| 46 return fmt.Errorf("machine not found") |
| 47 } |
| 48 hasUnits, err := t.MachineHasUnits(key) |
| 49 if err != nil { |
| 50 return err |
| 51 } |
| 52 if hasUnits { |
| 53 return fmt.Errorf("machine has units") |
| 54 } |
| 55 return t.RemoveMachine(key) |
| 56 } |
| 57 if err := retryTopologyChange(s.zk, removeMachine); err != nil { |
| 58 return fmt.Errorf("can't remove machine %d: %v", id, err) |
| 59 } |
| 60 return zkRemoveTree(s.zk, fmt.Sprintf("/machines/%s", key)) |
| 61 } |
| 62 |
| 63 // Machine returns the machine with the given id. |
| 64 func (s *State) Machine(id int) (*Machine, error) { |
| 65 key := machineKey(id) |
| 66 topology, err := readTopology(s.zk) |
| 67 if err != nil { |
| 68 return nil, err |
| 69 } |
| 70 if !topology.HasMachine(key) { |
| 71 return nil, fmt.Errorf("machine %d not found", id) |
| 72 } |
| 73 return &Machine{s, key}, nil |
| 74 } |
| 75 |
| 76 // AllMachines returns all machines in the environment. |
| 77 func (s *State) AllMachines() ([]*Machine, error) { |
| 78 topology, err := readTopology(s.zk) |
| 79 if err != nil { |
| 80 return nil, err |
| 81 } |
| 82 machines := []*Machine{} |
| 83 for _, key := range topology.MachineKeys() { |
| 84 machines = append(machines, &Machine{s, key}) |
| 85 } |
| 86 return machines, nil |
| 87 } |
| 88 |
41 // AddCharm adds the ch charm with curl to the state. | 89 // AddCharm adds the ch charm with curl to the state. |
42 // bundleUrl must be set to a URL where the bundle for ch | 90 // bundleUrl must be set to a URL where the bundle for ch |
43 // may be downloaded from. | 91 // may be downloaded from. |
44 // On success the newly added charm state is returned. | 92 // On success the newly added charm state is returned. |
45 func (s *State) AddCharm(ch charm.Charm, curl *charm.URL, bundleURL *url.URL) (*
Charm, error) { | 93 func (s *State) AddCharm(ch charm.Charm, curl *charm.URL, bundleURL *url.URL) (*
Charm, error) { |
46 data := &charmData{ | 94 data := &charmData{ |
47 Meta: ch.Meta(), | 95 Meta: ch.Meta(), |
48 Config: ch.Config(), | 96 Config: ch.Config(), |
49 BundleURL: bundleURL.String(), | 97 BundleURL: bundleURL.String(), |
50 } | 98 } |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 // TODO Create node for bootstrap machine. | 264 // TODO Create node for bootstrap machine. |
217 | 265 |
218 // TODO Setup default global settings information. | 266 // TODO Setup default global settings information. |
219 | 267 |
220 // Finally creation of /initialized as marker. | 268 // Finally creation of /initialized as marker. |
221 if _, err := s.zk.Create("/initialized", "", 0, zkPermAll); err != nil { | 269 if _, err := s.zk.Create("/initialized", "", 0, zkPermAll); err != nil { |
222 return err | 270 return err |
223 } | 271 } |
224 return nil | 272 return nil |
225 } | 273 } |
OLD | NEW |