/
glusterfs.py
285 lines (236 loc) · 8.1 KB
/
glusterfs.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
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
# -*- coding: utf-8 -*-
'''
Manage glusterfs pool.
'''
# Import python libs
from __future__ import generators
from __future__ import absolute_import
import logging
import socket
# Import salt libs
import salt.utils.cloud as suc
from salt.exceptions import SaltCloudException
log = logging.getLogger(__name__)
RESULT_CODES = [
'Peer {0} added successfully.',
'Probe on localhost not needed',
'Host {0} is already in the peer group',
'Host {0} is already part of another cluster',
'Volume on {0} conflicts with existing volumes',
'UUID of {0} is the same as local uuid',
'{0} responded with "unknown peer". This could happen if {0} doesn\'t have localhost defined',
'Failed to add peer. Information on {0}\'s logs',
'Cluster quorum is not met. Changing peers is not allowed.',
'Failed to update list of missed snapshots from {0}',
'Conflict comparing list of snapshots from {0}',
'Peer is already being detached from cluster.']
def __virtual__():
'''
Only load this module if the gluster command exists
'''
return 'glusterfs' if 'glusterfs.list_volumes' in __salt__ else False
def peered(name):
'''
Check if node is peered.
name
The remote host with which to peer.
.. code-block:: yaml
peer-cluster:
glusterfs.peered:
- name: two
peer-clusters:
glusterfs.peered:
- names:
- one
- two
- three
- four
'''
ret = {'name': name,
'changes': {},
'comment': '',
'result': False}
try:
suc.check_name(name, 'a-zA-Z0-9._-')
except SaltCloudException as e:
ret['comment'] = 'Invalid characters in peer name.'
ret['result'] = False
return ret
peers = __salt__['glusterfs.list_peers']()
if peers:
if name in peers or any([name in peers[x] for x in peers]):
ret['result'] = True
ret['comment'] = 'Host {0} already peered'.format(name)
return ret
result = __salt__['glusterfs.peer'](name)
ret['comment'] = ''
if 'exitval' in result:
if int(result['exitval']) <= len(RESULT_CODES):
ret['comment'] = RESULT_CODES[int(result['exitval'])].format(name)
else:
if 'comment' in result:
ret['comment'] = result['comment']
newpeers = __salt__['glusterfs.list_peers']()
# if newpeers was null, we know something didn't work.
if newpeers and name in newpeers or any([name in newpeers[x] for x in newpeers]):
ret['result'] = True
ret['changes'] = {'new': newpeers, 'old': peers}
# In case the hostname doesn't have any periods in it
elif name == socket.gethostname():
ret['result'] = True
return ret
# In case they have a hostname like "example.com"
elif name == socket.gethostname().split('.')[0]:
ret['result'] = True
return ret
elif 'on localhost not needed' in ret['comment']:
ret['result'] = True
ret['comment'] = 'Peering with localhost is not needed'
else:
ret['result'] = False
return ret
def created(name, bricks, stripe=False, replica=False, device_vg=False,
transport='tcp', start=False, force=False):
'''
Check if volume already exists
name
name of the volume
.. code-block:: yaml
myvolume:
glusterfs.created:
- bricks:
- host1:/srv/gluster/drive1
- host2:/srv/gluster/drive2
Replicated Volume:
glusterfs.created:
- name: volume2
- bricks:
- host1:/srv/gluster/drive2
- host2:/srv/gluster/drive3
- replica: 2
- start: True
'''
ret = {'name': name,
'changes': {},
'comment': '',
'result': False}
volumes = __salt__['glusterfs.list_volumes']()
if name in volumes:
if start:
if isinstance(__salt__['glusterfs.status'](name), dict):
ret['result'] = True
cmnt = 'Volume {0} already exists and is started.'.format(name)
else:
result = __salt__['glusterfs.start_volume'](name)
if 'started' in result:
ret['result'] = True
cmnt = 'Volume {0} started.'.format(name)
ret['changes'] = {'new': 'started', 'old': 'stopped'}
else:
ret['result'] = False
cmnt = result
else:
ret['result'] = True
cmnt = 'Volume {0} already exists.'.format(name)
ret['comment'] = cmnt
return ret
elif __opts__['test']:
if start and isinstance(__salt__['glusterfs.status'](name), dict):
comment = 'Volume {0} will be created and started'.format(name)
else:
comment = 'Volume {0} will be created'.format(name)
ret['comment'] = comment
ret['result'] = None
return ret
if suc.check_name(name, 'a-zA-Z0-9._-'):
ret['comment'] = 'Invalid characters in volume name.'
ret['result'] = False
return ret
ret['comment'] = __salt__['glusterfs.create'](name, bricks, stripe,
replica, device_vg,
transport, start, force)
old_volumes = volumes
volumes = __salt__['glusterfs.list_volumes']()
if name in volumes:
ret['changes'] = {'new': volumes, 'old': old_volumes}
ret['result'] = True
return ret
def started(name):
'''
Check if volume has been started
name
name of the volume
.. code-block:: yaml
mycluster:
glusterfs.started: []
'''
ret = {'name': name,
'changes': {},
'comment': '',
'result': False}
volumes = __salt__['glusterfs.list_volumes']()
if name not in volumes:
ret['result'] = False
ret['comment'] = 'Volume {0} does not exist'.format(name)
return ret
if isinstance(__salt__['glusterfs.status'](name), dict):
ret['comment'] = 'Volume {0} is already started'.format(name)
ret['result'] = True
return ret
elif __opts__['test']:
ret['comment'] = 'Volume {0} will be started'.format(name)
ret['result'] = None
return ret
ret['comment'] = __salt__['glusterfs.start_volume'](name)
if 'started' in ret['comment']:
ret['result'] = True
ret['change'] = {'new': 'started', 'old': 'stopped'}
else:
ret['result'] = False
return ret
def add_volume_bricks(name, bricks):
'''
Add brick(s) to an existing volume
name
Volume name
bricks
List of bricks to add to the volume
.. code-block:: yaml
myvolume:
glusterfs.add_volume_bricks:
- bricks:
- host1:/srv/gluster/drive1
- host2:/srv/gluster/drive2
Replicated Volume:
glusterfs.add_volume_bricks:
- name: volume2
- bricks:
- host1:/srv/gluster/drive2
- host2:/srv/gluster/drive3
'''
ret = {'name': name,
'changes': {},
'comment': '',
'result': False}
current_bricks = __salt__['glusterfs.status'](name)
if 'does not exist' in current_bricks:
ret['result'] = False
ret['comment'] = current_bricks
return ret
if 'is not started' in current_bricks:
ret['result'] = False
ret['comment'] = current_bricks
return ret
add_bricks = __salt__['glusterfs.add_volume_bricks'](name, bricks)
ret['comment'] = add_bricks
if 'bricks successfully added' in add_bricks:
old_bricks = current_bricks
new_bricks = __salt__['glusterfs.status'](name)
ret['result'] = True
ret['changes'] = {'new': list(new_bricks['bricks'].keys()), 'old': list(
old_bricks['bricks'].keys())}
return ret
if 'Bricks already in volume' in add_bricks:
ret['result'] = True
return ret
return ret