-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapi.go
411 lines (366 loc) · 12.9 KB
/
gapi.go
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Gd3
// Copyright (C) 2017-2018+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package gapi
// ./gd3 run --hostname h1 --tmp-prefix
// ./gd3 run --hostname h2 --tmp-prefix --seeds http://127.0.0.1:2379 --client-urls http://127.0.0.1:2381 --server-urls http://127.0.0.1:2382
// ./gd3 run --hostname h3 --tmp-prefix --seeds http://127.0.0.1:2379 --client-urls http://127.0.0.1:2383 --server-urls http://127.0.0.1:2384
// ./gd3 run --hostname h4 --tmp-prefix --seeds http://127.0.0.1:2379 --client-urls http://127.0.0.1:2385 --server-urls http://127.0.0.1:2386
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"strings"
"sync"
//"github.com/purpleidea/gd3/resources/brick"
"github.com/purpleidea/mgmt/gapi"
"github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/resources"
systemdUnit "github.com/coreos/go-systemd/unit"
errwrap "github.com/pkg/errors"
)
const (
pkgName0 = "centos-release-gluster39"
pkgName = "glusterfs-server" // package name
pathVarLibGlusterd = "/var/lib/glusterd/" // requires a trailing slash!
// TODO: we should implement per module namespacing at some point...
worldNamespace = "gd3::state" // key name of shared string data
brickBasePort = 49152
)
// Gd3GAPI implements the main GAPI interface.
type Gd3GAPI struct {
Program string // program name
Version string // program name
data gapi.Data
initialized bool
closeChan chan struct{}
wg sync.WaitGroup // sync group for tunnel go routines
}
// Init initializes the Gd3GAPI struct.
func (obj *Gd3GAPI) Init(data gapi.Data) error {
if obj.initialized {
return fmt.Errorf("already initialized")
}
if obj.Program == "" {
return fmt.Errorf("the program name must be specified")
}
obj.data = data // store for later
obj.closeChan = make(chan struct{})
obj.initialized = true
return nil
}
// stageCount takes a map of hostname->stage (as strings) and returns the list
// of hostnames at each stage as a map of stage->[]hostname.
func stageCount(stages map[string]string) map[int][]string {
result := make(map[int][]string)
var s int
for key, val := range stages {
ival, err := strconv.Atoi(val)
if err != nil {
s = 0
}
s = ival // we found this stage
if _, ok := result[s]; !ok {
result[s] = make([]string, 0) // initialize list
}
result[s] = append(result[s], key) // add hostname
}
return result
}
// stageMin takes a map of hostname->stage (as strings) and a minimum stage
// count and returns the list of hosts that meet this minimum value.
func stageMin(stages map[string]string, min int) []string {
stageMap := stageCount(stages)
result := []string{}
for key, val := range stageMap {
if key >= min {
result = append(result, val...) // add those hosts
}
}
return result
}
// Graph returns a current Graph.
func (obj *Gd3GAPI) Graph() (*pgraph.Graph, error) {
if !obj.initialized {
return nil, fmt.Errorf("%s: Gd3GAPI is not initialized", obj.Program)
}
g, err := pgraph.NewGraph(obj.Program)
if err != nil {
return nil, errwrap.Wrapf(err, "error creating graph")
}
defaultMetaParams := resources.DefaultMetaParams
// XXX: hard coded for now, easy to store in etcd and populate from initial peer
brickCount := 2
distribute, replicate := 2, 2
totalHosts := distribute * replicate // 4
// put user services into $XDG_RUNTIME_DIR/systemd/user/
// eg: /run/user/1000/systemd/user/
dir := os.Getenv("XDG_RUNTIME_DIR")
if dir == "" {
return nil, fmt.Errorf("the XDG_RUNTIME_DIR env variable is missing or empty")
}
// TODO: on Fedora 24 this is what is used. On Fedora 25 systemd-run
// uses the .../systemd/transient/ directory, but .../systemd/user/ is
// still seen and it works! Which is the correct dir long term?
userSvcDir := path.Join(dir, "/systemd/user/")
log.Printf("%s: User svc dir is: %s", obj.Program, userSvcDir)
log.Printf("%s: Want %d hosts (%d bricks each) for a dist x repl cluster of %d x %d", obj.Program, totalHosts, brickCount, distribute, replicate)
keyMap, err := obj.data.World.StrMapGet(worldNamespace) // map: hostname->state
if err != nil {
return nil, errwrap.Wrapf(err, "the Graph can't StrMapGet the namespace of %s", worldNamespace)
}
var stageOne, stageTwo, stageThree bool
//sc := stageCount(keyMap) // which hosts are at which stages
sm1 := stageMin(keyMap, 1) // which hosts are at this stage or greater
if len(sm1) >= totalHosts {
stageOne = true
}
sm2 := stageMin(keyMap, 2)
if len(sm2) >= totalHosts {
stageTwo = true
}
sm3 := stageMin(keyMap, 3)
if len(sm3) >= totalHosts {
stageThree = true
}
log.Printf("")
log.Printf("%s: Found %d hosts at stage one or greater: %+v", obj.Program, len(sm1), sm1)
log.Printf("%s: Found %d hosts at stage two or greater: %+v", obj.Program, len(sm2), sm2)
log.Printf("%s: Found %d hosts at stage three or greater: %+v", obj.Program, len(sm3), sm3)
log.Printf("")
// key value state resource
one := "1" // "hello, i'm available for this cluster"
kv1 := &resources.KVRes{
BaseRes: resources.BaseRes{
Name: "kv1",
MetaParams: defaultMetaParams,
},
Key: worldNamespace,
Value: &one,
SkipLessThan: true, // allow upgrades to two
}
g.AddVertex(kv1)
if !stageOne { // wait for everyone to be at stage one for no particular reason
return g, nil
}
//pkg0 := &resources.PkgRes{
// BaseRes: resources.BaseRes{
// Name: pkgName0,
// MetaParams: defaultMetaParams,
// },
// State: "installed",
//}
//g.AddVertex(pkg0)
// NOTE: we should do this in parallel, but let's explicitly hello first
// for the purposes of this demo.
//g.AddEdge(kv1, pkg0, pgraph.NewEdge("kv1->pkg0"))
// glusterfs package which includes the glusterfsd binary
pkg := &resources.PkgRes{
BaseRes: resources.BaseRes{
Name: pkgName,
MetaParams: defaultMetaParams,
},
State: "installed",
}
g.AddVertex(pkg)
//g.AddEdge(pkg0, pkg, pgraph.NewEdge("pkg0->pkg"))
g.AddEdge(kv1, pkg, &resources.Edge{Name: "kv1->pkg"})
// var directory
d0 := &resources.FileRes{
BaseRes: resources.BaseRes{
Name: pathVarLibGlusterd, // directory
MetaParams: defaultMetaParams,
},
//Path: pathVarLibGlusterd,
State: "present",
}
g.AddVertex(d0)
g.AddEdge(pkg, d0, &resources.Edge{Name: "pkg->d0"})
// key value state resource
two := "2"
kv2 := &resources.KVRes{
BaseRes: resources.BaseRes{
Name: "kv2",
MetaParams: defaultMetaParams,
},
Key: worldNamespace,
Value: &two,
SkipLessThan: true, // allow upgrades to three or higher
}
g.AddVertex(kv2)
g.AddEdge(kv1, kv2, &resources.Edge{Name: "kv1->kv2"}) // for safety
three := "3"
kv3 := &resources.KVRes{
BaseRes: resources.BaseRes{
Name: "kv3",
MetaParams: defaultMetaParams,
},
Key: worldNamespace,
Value: &three,
SkipLessThan: true, // allow upgrades to three or higher
}
g.AddVertex(kv3)
g.AddEdge(kv2, kv3, &resources.Edge{Name: "kv2->kv3"}) // for safety
dr := &resources.ExecRes{
BaseRes: resources.BaseRes{
Name: "reload systemd user units",
MetaParams: defaultMetaParams,
},
Cmd: "/usr/bin/systemctl --user daemon-reload",
}
g.AddVertex(dr)
for i := 0; i < brickCount; i++ {
unitName := fmt.Sprintf("glusterfsd%d", i) // unit name!!
brickName := fmt.Sprintf("b%d", i) // eg: /bricks/b0
brickPort := brickBasePort + i // 49152 + 0 ...
fullPath := path.Join(userSvcDir, fmt.Sprintf("%s.service", unitName))
volumeName := "foovol"
someUUID := "cf7359b0-476a-4336-b7ef-26f106f2986d" // same on all bricks
socketID := "bcca2de57a5b2e5e9e9941637d873193" // XXX: different on each brick
cmd := []string{"/usr/sbin/glusterfsd"}
cmd = append(cmd, fmt.Sprintf("-s %s", obj.data.Hostname))
cmd = append(cmd, fmt.Sprintf("--volfile-id %s.%s.bricks-%s", volumeName, obj.data.Hostname, brickName)) // XXX: bricks means /bricks/bX iirc
cmd = append(cmd, fmt.Sprintf("-p /var/lib/glusterd/vols/%s/run/%s-bricks-%s.pid", volumeName, obj.data.Hostname, brickName))
cmd = append(cmd, fmt.Sprintf("-S /var/run/gluster/%s.socket", socketID))
cmd = append(cmd, fmt.Sprintf("--brick-name /bricks/%s", brickName))
cmd = append(cmd, fmt.Sprintf("-l /var/log/glusterfs/bricks/bricks-%s.log", brickName)) // XXX: mkdir
cmd = append(cmd, fmt.Sprintf("--xlator-option *-posix.glusterd-uuid=%s", someUUID)) // XXX some uuid same on all bricks
cmd = append(cmd, fmt.Sprintf("--brick-port %d", brickPort))
cmd = append(cmd, fmt.Sprintf("--xlator-option %s-server.listen-port=%d", volumeName, brickPort))
options := []*systemdUnit.UnitOption{
&systemdUnit.UnitOption{"Unit", "Description", fmt.Sprintf("Gd3 brick %d", i)},
&systemdUnit.UnitOption{"Service", "ExecStart", strings.Join(cmd, " ")},
//&systemdUnit.UnitOption{"Unit", "BindsTo", "bar.service"},
//&systemdUnit.UnitOption{"X-Foo", "Bar", "baz"},
//&systemdUnit.UnitOption{"Service", "ExecStop", "/usr/bin/sleep 1"},
&systemdUnit.UnitOption{"Unit", "Documentation", "https://github.com/purpleidea/gd3"},
}
outReader := systemdUnit.Serialize(options)
outBytes, err := ioutil.ReadAll(outReader)
if err != nil {
return nil, errwrap.Wrapf(err, "can't generate brick %d unit file", i)
}
content := string(outBytes)
// brick unit file
bf := &resources.FileRes{
BaseRes: resources.BaseRes{
Name: fullPath,
MetaParams: defaultMetaParams,
},
Path: fullPath,
State: "present",
Content: &content,
}
g.AddVertex(bf)
g.AddEdge(kv2, bf, &resources.Edge{Name: fmt.Sprintf("kv2->bf%d", i)})
edge := &resources.Edge{Name: fmt.Sprintf("bf%d->dr", i)}
edge.Notify = true // send a notification from brick file to exec reloader
g.AddEdge(bf, dr, edge)
if stageTwo {
// glusterfsd service
// bonus: now we can use cgroups to limit all these!!!
svc := &resources.SvcRes{
BaseRes: resources.BaseRes{
Name: unitName, // no .service postfix
MetaParams: defaultMetaParams,
},
State: "running", // TODO: upstream should use a constant
Startup: "enabled",
Session: true, // user services that don't run as root!
}
g.AddVertex(svc)
g.AddEdge(bf, svc, &resources.Edge{Name: fmt.Sprintf("bf%d->svc%d", i, i)})
g.AddEdge(dr, svc, &resources.Edge{Name: fmt.Sprintf("dr->svc%d", i)}) // reload before svc
g.AddEdge(svc, kv3, &resources.Edge{Name: fmt.Sprintf("svc%d->kv3", i)})
}
// TODO: build a proper brick resource instead
//brickRes := &brick.BrickRes{
//}
//g.AddVertex(brickRes)
}
if stageTwo {
log.Printf("")
log.Printf("%s: The bricks are now started...", obj.Program)
log.Printf("")
}
if stageThree {
log.Printf("")
log.Printf("%s: The volume is now started...", obj.Program)
log.Printf("")
}
//g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
return g, nil
}
// Next returns nil errors every time there could be a new graph.
func (obj *Gd3GAPI) Next() chan gapi.Next {
// TODO: should we use obj.data.NoConfigWatch & obj.data.NoStreamWatch ?
ch := make(chan gapi.Next)
stringChan := obj.data.World.StrMapWatch(worldNamespace) // watch for var changes
obj.wg.Add(1)
go func() {
defer obj.wg.Done()
defer close(ch) // this will run before the obj.wg.Done()
if !obj.initialized {
next := gapi.Next{
Err: fmt.Errorf("%s: Gd3GAPI is not initialized", obj.Program),
Exit: true, // exit, b/c programming error?
}
ch <- next
return
}
startChan := make(chan struct{}) // start signal
close(startChan) // kick it off!
for {
var err error
var ok bool
select {
case <-startChan: // kick the loop once at start
startChan = nil // disable
// will delays here block etcd? (no b/c stringChan is buffered!)
case err, ok = <-stringChan:
if !ok { // channel closed
return
}
case <-obj.closeChan:
return
}
log.Printf("%s: Generating new graph...", obj.Program)
next := gapi.Next{
//Exit: true, // TODO: for permanent shutdown!
Err: err,
}
select {
case ch <- next: // trigger a run
case <-obj.closeChan:
return
}
}
}()
return ch
}
// Close shuts down the Gd3GAPI.
func (obj *Gd3GAPI) Close() error {
if !obj.initialized {
return fmt.Errorf("%s: Gd3GAPI is not initialized", obj.Program)
}
close(obj.closeChan)
obj.wg.Wait()
obj.initialized = false // closed = true
return nil
}