~themue/juju-core/go-worker-firewaller-machineunits

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package mstate

import (
	"errors"
	"fmt"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
	"launchpad.net/juju-core/charm"
	"strconv"
)

// Service represents the state of a service.
type Service struct {
	st   *State
	name string
}

// serviceDoc represents the internal state of a service in MongoDB.
type serviceDoc struct {
	Name     string `bson:"_id"`
	CharmURL *charm.URL
	Life     Life
	UnitSeq  int
}

// Name returns the service name.
func (s *Service) Name() string {
	return s.name
}

// CharmURL returns the charm URL this service is supposed to use.
func (s *Service) CharmURL() (url *charm.URL, err error) {
	sdoc := &serviceDoc{}
	sel := bson.D{{"_id", s.name}, {"life", Alive}}
	err = s.st.services.Find(sel).One(sdoc)
	if err != nil {
		return nil, fmt.Errorf("can't get the charm URL of service %q: %v", s, err)
	}
	return sdoc.CharmURL, nil
}

// SetCharmURL changes the charm URL for the service.
func (s *Service) SetCharmURL(url *charm.URL) (err error) {
	change := bson.D{{"$set", bson.D{{"charmurl", url}}}}
	err = s.st.services.Update(bson.D{{"_id", s.name}}, change)
	if err != nil {
		return fmt.Errorf("can't set the charm URL of service %q: %v", s, err)
	}
	return nil
}

// Charm returns the service's charm.
func (s *Service) Charm() (*Charm, error) {
	url, err := s.CharmURL()
	if err != nil {
		return nil, err
	}
	return s.st.Charm(url)
}

// String returns the service name.
func (s *Service) String() string {
	return s.Name()
}

// newUnitName returns the next unit name.
func (s *Service) newUnitName() (string, error) {
	sel := bson.D{{"_id", s.name}, {"life", Alive}}
	change := mgo.Change{Update: bson.D{{"$inc", bson.D{{"unitseq", 1}}}}}
	result := serviceDoc{}
	_, err := s.st.services.Find(sel).Apply(change, &result)
	if err != nil {
		return "", err
	}
	name := s.name + "/" + strconv.Itoa(result.UnitSeq)
	return name, nil
}

// addUnit adds the named unit, which is part of unitSet.
func (s *Service) addUnit(name string, principal string) (*Unit, error) {
	udoc := unitDoc{
		Name:      name,
		Service:   s.name,
		Principal: principal,
		Life:      Alive,
	}
	err := s.st.units.Insert(udoc)
	if err != nil {
		return nil, fmt.Errorf("can't add unit to service %q", s)
	}
	return newUnit(s.st, &udoc), nil
}

// AddUnit adds a new principal unit to the service.
func (s *Service) AddUnit() (unit *Unit, err error) {
	ch, err := s.Charm()
	if err != nil {
		return nil, fmt.Errorf("can't add unit to service %q: %v", err)
	}
	if ch.Meta().Subordinate {
		return nil, fmt.Errorf("cannot directly add units to subordinate service %q", s)
	}
	name, err := s.newUnitName()
	if err != nil {
		return nil, fmt.Errorf("can't add unit to service %q: %v", err)
	}
	return s.addUnit(name, "")
}

// AddUnitSubordinateTo adds a new subordinate unit to the service,
// subordinate to principal.
func (s *Service) AddUnitSubordinateTo(principal *Unit) (*Unit, error) {
	ch, err := s.Charm()
	if err != nil {
		return nil, fmt.Errorf("can't add unit to service %q: %v", err)
	}
	if !ch.Meta().Subordinate {
		return nil, fmt.Errorf("can't add unit of principal service %q as a subordinate of %q", s, principal)
	}
	if !principal.IsPrincipal() {
		return nil, errors.New("a subordinate unit must be added to a principal unit")
	}
	name, err := s.newUnitName()
	if err != nil {
		return nil, fmt.Errorf("can't add unit to service %q: %v", err)
	}
	return s.addUnit(name, principal.Name())
}

// RemoveUnit removes the given unit from s.
func (s *Service) RemoveUnit(unit *Unit) error {
	sel := bson.D{
		{"_id", unit.Name()},
		{"service", s.name},
		{"life", Alive},
	}
	change := bson.D{{"$set", bson.D{{"life", Dying}}}}
	err := s.st.units.Update(sel, change)
	if err != nil {
		return fmt.Errorf("can't remove unit %q: %v", unit, err)
	}
	return nil
}

func (s *Service) unitDoc(name string) (*unitDoc, error) {
	udoc := &unitDoc{}
	sel := bson.D{
		{"_id", name},
		{"service", s.name},
		{"life", Alive},
	}
	err := s.st.units.Find(sel).One(udoc)
	if err != nil {
		return nil, err
	}
	return udoc, nil
}

// Unit returns the service's unit with name.
func (s *Service) Unit(name string) (*Unit, error) {
	udoc, err := s.unitDoc(name)
	if err != nil {
		return nil, fmt.Errorf("can't get unit %q from service %q: %v", name, s.name, err)
	}
	return newUnit(s.st, udoc), nil
}

// AllUnits returns all units of the service.
func (s *Service) AllUnits() (units []*Unit, err error) {
	docs := []unitDoc{}
	sel := bson.D{{"service", s.name}, {"life", Alive}}
	err = s.st.units.Find(sel).All(&docs)
	if err != nil {
		return nil, fmt.Errorf("can't get all units from service %q: %v", err)
	}
	for i := range docs {
		units = append(units, newUnit(s.st, &docs[i]))
	}
	return units, nil
}