~fwereade/pyjuju/go-place-unit

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
// launchpad.net/juju/state
//
// Copyright (c) 2011-2012 Canonical Ltd.

package state

import (
	"errors"
	"fmt"
	"launchpad.net/goyaml"
	"launchpad.net/gozk/zookeeper"
	pathpkg "path"
	"sort"
)

var (
	// stateChange 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)
)

const (
	ItemAdded = iota + 1
	ItemModified
	ItemDeleted
)

// ItemChange represents the change of an item in a configNode.
type ItemChange struct {
	Type     int
	Key      string
	OldValue interface{}
	NewValue interface{}
}

// String returns the item change in a readable format.
func (ic *ItemChange) String() string {
	switch ic.Type {
	case ItemAdded:
		return fmt.Sprintf("setting added: %v = %v", ic.Key, ic.NewValue)
	case ItemModified:
		return fmt.Sprintf("setting modified: %v = %v (was %v)",
			ic.Key, ic.NewValue, ic.OldValue)
	case ItemDeleted:
		return fmt.Sprintf("setting deleted: %v (was %v)", ic.Key, ic.OldValue)
	}
	return fmt.Sprintf("unknown setting change type %d: %v = %v (was %v)",
		ic.Type, ic.Key, ic.NewValue, ic.OldValue)
}

// itemChangeSlice contains a slice of item changes in a config node. 
// It implements the sort interface to sort the items changes by key.
type itemChangeSlice []ItemChange

func (ics itemChangeSlice) Len() int           { return len(ics) }
func (ics itemChangeSlice) Less(i, j int) bool { return ics[i].Key < ics[j].Key }
func (ics itemChangeSlice) Swap(i, j int)      { ics[i], ics[j] = ics[j], ics[i] }

// A ConfigNode represents the data of a ZooKeeper node
// containing YAML-based settings. It manages changes to
// the data as a delta in memory, and merges them back
// onto the node when explicitly requested.
type ConfigNode struct {
	zk   *zookeeper.Conn
	path string
	// pristineCache holds the values in the config node before
	// any keys have been changed. It is reset on Read and Write
	// operations.
	pristineCache map[string]interface{}
	// cache holds the current values in the config node.
	// The difference between pristineCache and cache
	// determines the delta to be applied when ConfigNode.Write
	// is called.
	cache map[string]interface{}
}

func newConfigNode(zk *zookeeper.Conn, path string) *ConfigNode {
	return &ConfigNode{
		zk:    zk,
		path:  path,
		cache: make(map[string]interface{}),
	}
}

// createConfigNode writes an initial config node.
func createConfigNode(zk *zookeeper.Conn, path string, values map[string]interface{}) (*ConfigNode, error) {
	c := newConfigNode(zk, path)
	c.cache = copyCache(values)
	_, err := c.Write()
	if err != nil {
		return nil, err
	}
	return c, nil
}

// parseConfigNode creates a config node based on a pre-read content.
func parseConfigNode(zk *zookeeper.Conn, path, content string) (*ConfigNode, error) {
	c := newConfigNode(zk, path)
	if err := c.setPristineContent(content); err != nil {
		return nil, err
	}
	c.cache = copyCache(c.pristineCache)
	return c, nil
}

// setPristineContent sets the currently known contents of the node.
// It does not affect the user-set contents.
func (c *ConfigNode) setPristineContent(content string) error {
	if err := goyaml.Unmarshal([]byte(content), &c.pristineCache); err != nil {
		return err
	}
	return nil
}

// readConfigNode returns the ConfigNode for path.
func readConfigNode(zk *zookeeper.Conn, path string) (*ConfigNode, error) {
	c := newConfigNode(zk, path)
	if err := c.Read(); err != nil {
		return nil, err
	}
	return c, nil
}

// Read (re)reads the node data into c.
func (c *ConfigNode) Read() error {
	yaml, _, err := c.zk.Get(c.path)
	if err != nil {
		if !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return err
		}
	}
	if err := c.setPristineContent(yaml); err != nil {
		return err
	}
	c.cache = copyCache(c.pristineCache)
	return nil
}

// Write writes changes made to c back onto its node.
// Changes are written as a delta applied on top of the
// latest version of the node, to prevent overwriting
// unrelated changes made to the node since it was last read.
func (c *ConfigNode) Write() ([]ItemChange, error) {
	// changes is used by applyChanges to return the changes to
	// this scope.
	changes := []ItemChange{}
	// nil is a possible value for a key, so we use missing as
	// a marker to simplify the algorithm below.
	missing := new(bool)
	applyChanges := func(yaml string, stat *zookeeper.Stat) (string, error) {
		changes = changes[:0]
		current := make(map[string]interface{})
		if yaml != "" {
			if err := goyaml.Unmarshal([]byte(yaml), current); err != nil {
				return "", err
			}
		}
		for key := range cacheKeys(c.pristineCache, c.cache) {
			oldValue, ok := c.pristineCache[key]
			if !ok {
				oldValue = missing
			}
			newValue, ok := c.cache[key]
			if !ok {
				newValue = missing
			}
			if oldValue == newValue {
				continue
			}
			var change ItemChange
			if newValue != missing {
				current[key] = newValue
				if oldValue != missing {
					change = ItemChange{ItemModified, key, oldValue, newValue}
				} else {
					change = ItemChange{ItemAdded, key, nil, newValue}
				}
			} else if _, ok := current[key]; ok {
				delete(current, key)
				change = ItemChange{ItemDeleted, key, oldValue, nil}
			}
			changes = append(changes, change)
		}
		currentYaml, err := goyaml.Marshal(current)
		if err != nil {
			return "", err
		}
		return string(currentYaml), nil
	}
	if err := c.zk.RetryChange(c.path, 0, zkPermAll, applyChanges); err != nil {
		return nil, err
	}
	sort.Sort(itemChangeSlice(changes))
	c.pristineCache = copyCache(c.cache)
	return changes, nil
}

// Keys returns the current keys in alphabetical order.
func (c *ConfigNode) Keys() []string {
	keys := []string{}
	for key := range c.cache {
		keys = append(keys, key)
	}
	sort.Strings(keys)
	return keys
}

// Get returns the value of key and whether it was found.
func (c *ConfigNode) Get(key string) (value interface{}, found bool) {
	value, found = c.cache[key]
	return
}

// Map returns all keys and values of the node.
func (c *ConfigNode) Map() map[string]interface{} {
	return copyCache(c.cache)
}

// Set sets key to value
func (c *ConfigNode) Set(key string, value interface{}) {
	c.cache[key] = value
}

// Update sets multiple key/value pairs.
func (c *ConfigNode) Update(kv map[string]interface{}) {
	for key, value := range kv {
		c.cache[key] = value
	}
}

// Delete removes key.
func (c *ConfigNode) Delete(key string) {
	delete(c.cache, key)
}

// 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) error {
	// If we try to delete the zookeeper node (for example when
	// calling ZkRemoveTree(zk, "/")) we silently ignore it.
	if path == "/zookeeper" {
		return nil
	}
	// First recursively delete the children.
	children, _, err := zk.Children(path)
	if err != nil {
		if zookeeper.IsError(err, zookeeper.ZNONODE) {
			return nil
		}
		return err
	}
	for _, child := range children {
		if err := zkRemoveTree(zk, pathpkg.Join(path, child)); err != nil {
			return err
		}
	}
	// 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
}

// copyCache copies the keys and values of one cache into a new one.
func copyCache(in map[string]interface{}) (out map[string]interface{}) {
	out = make(map[string]interface{})
	for key, value := range in {
		out[key] = value
	}
	return
}

// cacheKeys returns the keys of all caches as a key=>true map.
func cacheKeys(caches ...map[string]interface{}) map[string]bool {
	keys := make(map[string]bool)
	for _, cache := range caches {
		for key := range cache {
			keys[key] = true
		}
	}
	return keys
}

// Quote translates an unsafe string into a safe quoted one. ASCII
// letters, ASCII digits, dot and dash stay the same, other bytes
// are translated to their hex representation surrounded by 
// underscores.
func Quote(unsafe string) string {
	safe := make([]byte, 0, len(unsafe)*4)
	for i := 0; i < len(unsafe); i++ {
		b := unsafe[i]
		switch {
		case b >= 'a' && b <= 'z',
			b >= 'A' && b <= 'Z',
			b >= '0' && b <= '9',
			b == '.',
			b == '-':
			safe = append(safe, b)
		default:
			safe = append(safe, fmt.Sprintf("_%02x_", b)...)
		}
	}
	return string(safe)
}