Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(27)

Side by Side Diff: state/state.go

Issue 5671055: Implementation of the charm state. (Closed)
Patch Set: Created 13 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 (
11 "fmt" 11 "fmt"
12 "launchpad.net/goyaml" 12 "launchpad.net/goyaml"
13 "launchpad.net/gozk/zookeeper" 13 "launchpad.net/gozk/zookeeper"
14 "launchpad.net/juju/go/charm"
14 "strings" 15 "strings"
15 ) 16 )
16 17
17 // State represents the state of an environment 18 // State represents the state of an environment
18 // managed by juju. 19 // managed by juju.
19 type State struct { 20 type State struct {
20 zk *zookeeper.Conn 21 zk *zookeeper.Conn
21 } 22 }
22 23
23 // Add machine creates a new machine. 24 // Add machine creates a new machine.
24 func (s *State) AddMachine() (*Machine, error) { 25 func (s *State) AddMachine() (*Machine, error) {
25 path, err := s.zk.Create("/machines/machine-", "", zookeeper.SEQUENCE, z kPermAll) 26 path, err := s.zk.Create("/machines/machine-", "", zookeeper.SEQUENCE, z kPermAll)
26 if err != nil { 27 if err != nil {
27 return nil, err 28 return nil, err
28 } 29 }
29 key := strings.Split(path, "/")[2] 30 key := strings.Split(path, "/")[2]
30 addMachine := func(t *topology) error { 31 addMachine := func(t *topology) error {
31 return t.AddMachine(key) 32 return t.AddMachine(key)
32 } 33 }
33 if err = retryTopologyChange(s.zk, addMachine); err != nil { 34 if err = retryTopologyChange(s.zk, addMachine); err != nil {
34 return nil, err 35 return nil, err
35 } 36 }
36 return &Machine{s.zk, key}, nil 37 return &Machine{s.zk, key}, nil
37 } 38 }
38 39
40 // AddCharm registers metadata about the provided Charm.
41 func (s *State) AddCharm(id string, ch charm.Charm, url string) (*Charm, error) {
42 data := &charmData{
43 Meta: ch.Meta(),
44 Config: ch.Config(),
45 SHA256: "", // Yet missing.
46 URL: url,
47 }
48 yaml, err := goyaml.Marshal(data)
49 if err != nil {
50 return nil, err
51 }
52 _, err = s.zk.Create(charmPath(id), string(yaml), 0, zkPermAll)
53 if err != nil {
54 return nil, err
55 }
56 return newCharm(s.zk, id, data)
57 }
58
59 // Charm returns a charm by the given id.
60 func (s *State) Charm(id string) (*Charm, error) {
61 yaml, _, err := s.zk.Get(charmPath(id))
62 if err == zookeeper.ZNONODE {
63 return nil, fmt.Errorf("charm %q not found", id)
64 } else if err != nil {
rog 2012/02/16 14:29:47 no else needed here
TheMue 2012/02/17 14:37:21 Done.
65 return nil, err
66 }
67 data := &charmData{}
68 if err = goyaml.Unmarshal([]byte(yaml), data); err != nil {
rog 2012/02/16 14:29:47 since you can, i'd probably use err := ... here an
TheMue 2012/02/17 14:37:21 Done.
69 return nil, err
70 }
71 return newCharm(s.zk, id, data)
72 }
73
39 // AddService creates a new service with the given unique name 74 // AddService creates a new service with the given unique name
40 // and the charm state. 75 // and the charm state.
41 func (s *State) AddService(name string, charm *Charm) (*Service, error) { 76 func (s *State) AddService(name string, ch *Charm) (*Service, error) {
42 » details := map[string]interface{}{"charm": charm.URL().String()} 77 » details := map[string]interface{}{"charm": ch.URL().String()}
43 yaml, err := goyaml.Marshal(details) 78 yaml, err := goyaml.Marshal(details)
44 if err != nil { 79 if err != nil {
45 return nil, err 80 return nil, err
46 } 81 }
47 path, err := s.zk.Create("/services/service-", string(yaml), zookeeper.S EQUENCE, zkPermAll) 82 path, err := s.zk.Create("/services/service-", string(yaml), zookeeper.S EQUENCE, zkPermAll)
48 if err != nil { 83 if err != nil {
49 return nil, err 84 return nil, err
50 } 85 }
51 key := strings.Split(path, "/")[2] 86 key := strings.Split(path, "/")[2]
52 service := &Service{s.zk, key, name} 87 service := &Service{s.zk, key, name}
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 // TODO Create node for bootstrap machine. 204 // TODO Create node for bootstrap machine.
170 205
171 // TODO Setup default global settings information. 206 // TODO Setup default global settings information.
172 207
173 // Finally creation of /initialized as marker. 208 // Finally creation of /initialized as marker.
174 if _, err := s.zk.Create("/initialized", "", 0, zkPermAll); err != nil { 209 if _, err := s.zk.Create("/initialized", "", 0, zkPermAll); err != nil {
175 return err 210 return err
176 } 211 }
177 return nil 212 return nil
178 } 213 }
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b