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
|
package state
import (
"errors"
"launchpad.net/gozk/zookeeper"
"launchpad.net/juju-core/trivial"
pathpkg "path"
"sort"
)
var (
// stateChanged is a common error inside the state processing.
stateChanged = errors.New("environment state has changed")
// zkPermAll is a convenience variable for creating new nodes.
zkPermAll = zookeeper.WorldACL(zookeeper.PERM_ALL)
)
// zkRemoveTree recursively removes a zookeeper node and all its
// children. It does not delete "/zookeeper" or the root node itself
// and it does not consider deleting a nonexistent node to be an error.
func zkRemoveTree(zk *zookeeper.Conn, path string) (err error) {
defer trivial.ErrorContextf(&err, "cannot clean up data")
// If we try to delete the zookeeper node (for example when
// calling ZkRemoveTree(zk, "/")) we silently ignore it.
if path == "/zookeeper" {
return
}
// First recursively delete the children.
children, _, err := zk.Children(path)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
return nil
}
return
}
for _, child := range children {
if err = zkRemoveTree(zk, pathpkg.Join(path, child)); err != nil {
return
}
}
// Now delete the path itself unless it's the root node.
if path == "/" {
return nil
}
err = zk.Delete(path, -1)
if err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
return err
}
return nil
}
// diff returns all the elements that exist in A but not B.
func diff(A, B []string) (missing []string) {
next:
for _, a := range A {
for _, b := range B {
if a == b {
continue next
}
}
missing = append(missing, a)
}
return
}
type portSlice []Port
func (p portSlice) Len() int { return len(p) }
func (p portSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p portSlice) Less(i, j int) bool {
p1 := p[i]
p2 := p[j]
if p1.Protocol != p2.Protocol {
return p1.Protocol < p2.Protocol
}
return p1.Number < p2.Number
}
// SortPorts sorts the given ports, first by protocol,
// then by number.
func SortPorts(ports []Port) {
sort.Sort(portSlice(ports))
}
|