This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
provides.py
110 lines (96 loc) · 4.5 KB
/
provides.py
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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hashlib
import json
from charms.reactive import hook
from charms.reactive import RelationBase
from charms.reactive import scopes
class ZeppelinProvides(RelationBase):
scope = scopes.SERVICE
@hook('{provides:zeppelin}-relation-changed')
def changed(self):
conv = self.conversation()
requested = set(json.loads(conv.get_remote('requested-notebooks',
'[]')))
registered = set(conv.get_local('registered-notebooks', []))
unregistered = requested - registered
if unregistered:
conv.set_local('unregistered-notebooks', list(unregistered))
conv.set_state('{relation_name}.notebook.registered')
@hook('{provides:zeppelin}-relation-departed')
def departed(self):
conv = self.conversation()
if len(conv.units) == 1: # last unit leaving
# get all notebooks set by the current remote unit
# remove all notebooks for this service
registered = set(conv.get_local('registered-notebooks', []))
removed = set(conv.get_local('removed-notebooks', [])) & registered
conv.set_local('removed-notebooks', list(removed))
conv.set_state('{relation_name}.notebook.removed')
def _notebooks(self, key):
notebooks = []
for conv in self.conversations():
batch = conv.get_local(key, [])
notebooks.extend(conv.get_remote('notebook-{}'.format(notebook))
for notebook in batch)
return notebooks
def unregistered_notebooks(self):
"""
Returns a list containing all of the notebooks pending registration,
as JSON strings.
"""
return self._notebooks('unregistered-notebooks')
def unremoved_notebooks(self):
"""
Returns a list containing all of the notebooks pending removal,
as JSON strings.
"""
return self._notebooks('removed-notebooks')
def accept_notebook(self, notebook):
"""
Acknowledge that the given pending notebook has been registered.
"""
notebook_md5 = hashlib.md5(notebook.encode('utf8')).hexdigest()
for conv in self.conversations():
registered = set(conv.get_local('registered-notebooks', []))
unregistered = set(conv.get_local('unregistered-notebooks', []))
if notebook_md5 in unregistered:
registered.add(notebook_md5)
conv.set_local('registered-notebooks', list(registered))
conv.set_remote('accepted-notebooks',
json.dumps(list(registered)))
unregistered.discard(notebook_md5)
conv.set_local('unregistered-notebooks', list(unregistered))
def reject_notebook(self, notebook):
"""
Inform the client that the given pending notebook has been rejected.
"""
notebook_md5 = hashlib.md5(notebook.encode('utf8')).hexdigest()
for conv in self.conversations():
rejected = set(conv.get_local('rejected-notebooks', []))
unregistered = set(conv.get_local('unregistered-notebooks', []))
if notebook_md5 in unregistered:
rejected.add(notebook_md5)
conv.set_local('rejected-notebooks', list(rejected))
conv.set_remote('rejected-notebooks',
json.dumps(list(rejected)))
unregistered.discard(notebook_md5)
conv.set_local('unregistered-notebooks', list(unregistered))
def remove_notebook(self, notebook):
"""
Acknowledge that the given registered notebook has been removed.
"""
notebook_md5 = hashlib.md5(notebook.encode('utf8')).hexdigest()
for conv in self.conversations():
registered = set(conv.get_local('registered-notebooks', []))
registered.discard(notebook_md5)
conv.set_local('registered-notebooks', list(registered))