OLD | NEW |
1 package juju | 1 package juju |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | 4 "bytes" |
5 "crypto/sha256" | 5 "crypto/sha256" |
6 "encoding/hex" | 6 "encoding/hex" |
7 "fmt" | 7 "fmt" |
8 "io" | 8 "io" |
9 "launchpad.net/juju-core/charm" | 9 "launchpad.net/juju-core/charm" |
10 "launchpad.net/juju-core/state" | 10 "launchpad.net/juju-core/state" |
11 "net/url" | 11 "net/url" |
12 "os" | 12 "os" |
13 ) | 13 ) |
14 | 14 |
15 // AddService creates a new service with the given name to run the given | 15 // AddService creates a new service with the given name to run the given |
16 // charm. If svcName is empty, the charm name will be used. | 16 // charm. If svcName is empty, the charm name will be used. |
17 func (conn *Conn) AddService(name string, ch *state.Charm) (*state.Service, erro
r) { | 17 func (conn *Conn) AddService(name string, ch *state.Charm) (*state.Service, erro
r) { |
18 » st, err := conn.State() | 18 » if name == "" { |
| 19 » » name = ch.URL().Name // TODO ch.Meta().Name ? |
| 20 » } |
| 21 » svc, err := conn.State.AddService(name, ch) |
19 if err != nil { | 22 if err != nil { |
20 return nil, err | 23 return nil, err |
21 } | 24 } |
22 if name == "" { | |
23 name = ch.URL().Name // TODO sch.Meta().Name ? | |
24 } | |
25 svc, err := st.AddService(name, ch) | |
26 if err != nil { | |
27 return nil, err | |
28 } | |
29 meta := ch.Meta() | 25 meta := ch.Meta() |
30 for rname, rel := range meta.Peers { | 26 for rname, rel := range meta.Peers { |
31 ep := state.RelationEndpoint{ | 27 ep := state.RelationEndpoint{ |
32 name, | 28 name, |
33 rel.Interface, | 29 rel.Interface, |
34 rname, | 30 rname, |
35 state.RolePeer, | 31 state.RolePeer, |
36 rel.Scope, | 32 rel.Scope, |
37 } | 33 } |
38 » » if _, err := st.AddRelation(ep); err != nil { | 34 » » if _, err := conn.State.AddRelation(ep); err != nil { |
39 return nil, fmt.Errorf("cannot add peer relation %q to s
ervice %q: %v", rname, name, err) | 35 return nil, fmt.Errorf("cannot add peer relation %q to s
ervice %q: %v", rname, name, err) |
40 } | 36 } |
41 } | 37 } |
42 return svc, nil | 38 return svc, nil |
43 } | 39 } |
44 | 40 |
45 // PutCharm uploads the given charm to provider storage, and adds a | 41 // PutCharm uploads the given charm to provider storage, and adds a |
46 // state.Charm to the state. The charm is not uploaded if a charm with | 42 // state.Charm to the state. The charm is not uploaded if a charm with |
47 // the same URL already exists in the state. | 43 // the same URL already exists in the state. |
48 // If bumpRevision is true, the charm must be a local directory, | 44 // If bumpRevision is true, the charm must be a local directory, |
(...skipping 18 matching lines...) Expand all Loading... |
67 if bumpRevision { | 63 if bumpRevision { |
68 chd, ok := ch.(*charm.Dir) | 64 chd, ok := ch.(*charm.Dir) |
69 if !ok { | 65 if !ok { |
70 return nil, fmt.Errorf("cannot increment version of char
m %q: not a directory", curl) | 66 return nil, fmt.Errorf("cannot increment version of char
m %q: not a directory", curl) |
71 } | 67 } |
72 if err = chd.SetDiskRevision(chd.Revision() + 1); err != nil { | 68 if err = chd.SetDiskRevision(chd.Revision() + 1); err != nil { |
73 return nil, fmt.Errorf("cannot increment version of char
m %q: %v", curl, err) | 69 return nil, fmt.Errorf("cannot increment version of char
m %q: %v", curl, err) |
74 } | 70 } |
75 curl = curl.WithRevision(chd.Revision()) | 71 curl = curl.WithRevision(chd.Revision()) |
76 } | 72 } |
77 » st, err := conn.State() | 73 » if sch, err := conn.State.Charm(curl); err == nil { |
78 » if err != nil { | |
79 » » return nil, err | |
80 » } | |
81 » if sch, err := st.Charm(curl); err == nil { | |
82 return sch, nil | 74 return sch, nil |
83 } | 75 } |
84 var buf bytes.Buffer | 76 var buf bytes.Buffer |
85 switch ch := ch.(type) { | 77 switch ch := ch.(type) { |
86 case *charm.Dir: | 78 case *charm.Dir: |
87 if err := ch.BundleTo(&buf); err != nil { | 79 if err := ch.BundleTo(&buf); err != nil { |
88 return nil, fmt.Errorf("cannot bundle charm: %v", err) | 80 return nil, fmt.Errorf("cannot bundle charm: %v", err) |
89 } | 81 } |
90 case *charm.Bundle: | 82 case *charm.Bundle: |
91 f, err := os.Open(ch.Path) | 83 f, err := os.Open(ch.Path) |
(...skipping 16 matching lines...) Expand all Loading... |
108 return nil, fmt.Errorf("cannot put charm: %v", err) | 100 return nil, fmt.Errorf("cannot put charm: %v", err) |
109 } | 101 } |
110 ustr, err := storage.URL(name) | 102 ustr, err := storage.URL(name) |
111 if err != nil { | 103 if err != nil { |
112 return nil, fmt.Errorf("cannot get storage URL for charm: %v", e
rr) | 104 return nil, fmt.Errorf("cannot get storage URL for charm: %v", e
rr) |
113 } | 105 } |
114 u, err := url.Parse(ustr) | 106 u, err := url.Parse(ustr) |
115 if err != nil { | 107 if err != nil { |
116 return nil, fmt.Errorf("cannot parse storage URL: %v", err) | 108 return nil, fmt.Errorf("cannot parse storage URL: %v", err) |
117 } | 109 } |
118 » sch, err := st.AddCharm(ch, curl, u, digest) | 110 » sch, err := conn.State.AddCharm(ch, curl, u, digest) |
119 if err != nil { | 111 if err != nil { |
120 return nil, fmt.Errorf("cannot add charm: %v", err) | 112 return nil, fmt.Errorf("cannot add charm: %v", err) |
121 } | 113 } |
122 return sch, nil | 114 return sch, nil |
123 } | 115 } |
124 | 116 |
125 // AddUnits starts n units of the given service and allocates machines | 117 // AddUnits starts n units of the given service and allocates machines |
126 // to them as necessary. | 118 // to them as necessary. |
127 func (conn *Conn) AddUnits(svc *state.Service, n int) ([]*state.Unit, error) { | 119 func (conn *Conn) AddUnits(svc *state.Service, n int) ([]*state.Unit, error) { |
128 st, err := conn.State() | |
129 if err != nil { | |
130 return nil, err | |
131 } | |
132 units := make([]*state.Unit, n) | 120 units := make([]*state.Unit, n) |
133 // TODO what do we do if we fail half-way through this process? | 121 // TODO what do we do if we fail half-way through this process? |
134 for i := 0; i < n; i++ { | 122 for i := 0; i < n; i++ { |
135 policy := conn.Environ.AssignmentPolicy() | 123 policy := conn.Environ.AssignmentPolicy() |
136 unit, err := svc.AddUnit() | 124 unit, err := svc.AddUnit() |
137 if err != nil { | 125 if err != nil { |
138 return nil, fmt.Errorf("cannot add unit %d/%d to service
%q: %v", i+1, n, svc.Name(), err) | 126 return nil, fmt.Errorf("cannot add unit %d/%d to service
%q: %v", i+1, n, svc.Name(), err) |
139 } | 127 } |
140 » » if err := st.AssignUnit(unit, policy); err != nil { | 128 » » if err := conn.State.AssignUnit(unit, policy); err != nil { |
141 return nil, fmt.Errorf("cannot assign machine to unit %s
of service %q: %v", unit.Name(), svc.Name(), err) | 129 return nil, fmt.Errorf("cannot assign machine to unit %s
of service %q: %v", unit.Name(), svc.Name(), err) |
142 } | 130 } |
143 units[i] = unit | 131 units[i] = unit |
144 } | 132 } |
145 return units, nil | 133 return units, nil |
146 } | 134 } |
OLD | NEW |