This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
/
requires.py
182 lines (158 loc) · 6.31 KB
/
requires.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
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
# Copyright 2018 Canonical Ltd
#
# 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 ipaddress
import json
import socket
# the reactive framework unfortunately does not grok `import as` in conjunction
# with decorators on class instance methods, so we have to revert to `from ...`
# imports
from charms.reactive import (
Endpoint,
all_flags_set,
clear_flag,
set_flag,
when,
when_not,
)
import charmhelpers.contrib.storage.linux.ceph as ch_ceph
class CephRBDMirrorRequires(Endpoint):
def __init__(self, endpoint_name, relation_ids=None, unique_id=None):
"""Initialize unique ID.
This is used when requesting a key from Ceph.
The constructor exists mainly for testing purposes.
"""
if unique_id:
self.unique_id = unique_id
else:
self.unique_id = socket.gethostname()
self.key_name = '{}_key'.format(self.unique_id)
super().__init__(endpoint_name, relation_ids=relation_ids)
@when('endpoint.{endpoint_name}.joined')
def joined(self):
set_flag(self.expand_name('{endpoint_name}.connected'))
@when('endpoint.{endpoint_name}.changed')
def changed(self):
flags = (
self.expand_name(
'endpoint.{endpoint_name}.changed.auth'),
self.expand_name(
'endpoint.{endpoint_name}.changed.' + self.key_name),
self.expand_name(
'endpoint.{endpoint_name}.changed.ceph-public-address'),
)
if all_flags_set(*flags):
for flag in (flags):
clear_flag(flag)
set_flag(self.expand_name('{endpoint_name}.available'))
@when_not('endpoint.{endpoint_name}.joined')
def broken(self):
clear_flag(self.expand_name('{endpoint_name}.available'))
clear_flag(self.expand_name('{endpoint_name}.connected'))
def request_key(self):
for relation in self.relations:
relation.to_publish['unique_id'] = self.unique_id
def get_current_request(self):
"""
Retrieve the current Ceph broker request.
If no request has been created yet then create a new one.
"""
json_rq = self.all_joined_units.received['broker_req']
current_request = ch_ceph.CephBrokerRq()
if json_rq:
try:
j = json.loads(json_rq)
current_request.set_ops(j['ops'])
except (KeyError, json.decoder.JSONDecodeError):
raise
return current_request
def create_replicated_pool(self, name, replicas=3, weight=None,
pg_num=None, group=None, namespace=None,
app_name=None, max_bytes=None,
max_objects=None):
"""Request replicated pool setup.
"""
# Ensure type of numeric values before sending over the wire
replicas = int(replicas) if replicas else None
weight = float(weight) if weight else None
pg_num = int(pg_num) if pg_num else None
max_bytes = int(max_bytes) if max_bytes else None
max_objects = int(max_objects) if max_objects else None
current_request = self.get_current_request()
current_request.add_op_create_replicated_pool(
name="{}".format(name),
replica_count=replicas,
pg_num=pg_num,
weight=weight,
group=group,
namespace=namespace,
app_name=app_name,
max_bytes=max_bytes,
max_objects=max_objects)
ch_ceph.send_request_if_needed(current_request,
relation=self.endpoint_name)
def create_erasure_pool(self, name, erasure_profile=None, weight=None,
group=None, app_name=None, max_bytes=None,
max_objects=None):
"""Request erasure coded pool setup.
"""
# Ensure type of numeric values before sending over the wire
weight = float(weight) if weight else None
max_bytes = int(max_bytes) if max_bytes else None
max_objects = int(max_objects) if max_objects else None
current_request = self.get_current_request()
current_request.add_op_create_erasure_pool(
name="{}".format(name),
erasure_profile=erasure_profile,
weight=weight,
group=group,
app_name=app_name,
max_bytes=max_bytes,
max_objects=max_objects)
ch_ceph.send_request_if_needed(current_request,
relation=self.endpoint_name)
@property
def auth(self):
return self.all_joined_units.received['auth']
@property
def key(self):
return self.all_joined_units.received[self.key_name]
def mon_hosts(self):
"""
NOTE(fnordahl): As much as this should and could have been a property
we have pre-existing interfaces providing this as a function. To be
able to use the same code for relation adaption etc in
``charms.openstack`` we must keep having this as a function unless we
go back and change both to being properties.
"""
for relation in self.relations:
for unit in relation.units:
try:
addr = ipaddress.ip_address(
unit.received.get('ceph-public-address', ''))
except ValueError:
continue
port = 6789
if isinstance(addr, ipaddress.IPv6Address):
yield '[{}]:{}'.format(addr, port)
else:
yield '{}:{}'.format(addr, port)
@property
def public_network(self):
pass
@property
def cluster_network(self):
pass
@property
def pools(self):
return self.all_joined_units.received['pools']