~dave-cheney/juju-core/089-state-unit-assigned-machine

« back to all changes in this revision

Viewing changes to juju/deploy.go

juju: always connect to State.

All uses of juju.Conn other than Bootstrap and Destroy require a state
connection, so make State an exported field of Conn and connect to it
when creating the Conn.

The few places that call Bootstrap can easily call Environ.Bootstrap
instead.

Also add environs.NewFromName to make it easier to open an
environment, rename juju.NewConn to juju.NewConnFromName, and add
juju.NewConnFromEnviron to join the dots.

R=TheMue, niemeyer
CC=
https://codereview.appspot.com/6488077

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
// AddService creates a new service with the given name to run the given
16
16
// charm.  If svcName is empty, the charm name will be used.
17
17
func (conn *Conn) AddService(name string, ch *state.Charm) (*state.Service, error) {
18
 
        st, err := conn.State()
19
 
        if err != nil {
20
 
                return nil, err
21
 
        }
22
18
        if name == "" {
23
 
                name = ch.URL().Name // TODO sch.Meta().Name ?
 
19
                name = ch.URL().Name // TODO ch.Meta().Name ?
24
20
        }
25
 
        svc, err := st.AddService(name, ch)
 
21
        svc, err := conn.State.AddService(name, ch)
26
22
        if err != nil {
27
23
                return nil, err
28
24
        }
35
31
                        state.RolePeer,
36
32
                        rel.Scope,
37
33
                }
38
 
                if _, err := st.AddRelation(ep); err != nil {
 
34
                if _, err := conn.State.AddRelation(ep); err != nil {
39
35
                        return nil, fmt.Errorf("cannot add peer relation %q to service %q: %v", rname, name, err)
40
36
                }
41
37
        }
74
70
                }
75
71
                curl = curl.WithRevision(chd.Revision())
76
72
        }
77
 
        st, err := conn.State()
78
 
        if err != nil {
79
 
                return nil, err
80
 
        }
81
 
        if sch, err := st.Charm(curl); err == nil {
 
73
        if sch, err := conn.State.Charm(curl); err == nil {
82
74
                return sch, nil
83
75
        }
84
76
        var buf bytes.Buffer
115
107
        if err != nil {
116
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
111
        if err != nil {
120
112
                return nil, fmt.Errorf("cannot add charm: %v", err)
121
113
        }
125
117
// AddUnits starts n units of the given service and allocates machines
126
118
// to them as necessary.
127
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
120
        units := make([]*state.Unit, n)
133
121
        // TODO what do we do if we fail half-way through this process?
134
122
        for i := 0; i < n; i++ {
137
125
                if err != nil {
138
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
129
                        return nil, fmt.Errorf("cannot assign machine to unit %s of service %q: %v", unit.Name(), svc.Name(), err)
142
130
                }
143
131
                units[i] = unit