/
moosefs.py
164 lines (134 loc) · 3.98 KB
/
moosefs.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
# -*- coding: utf-8 -*-
'''
Module for gathering and managing information about MooseFS
'''
from __future__ import absolute_import, print_function, unicode_literals
# Import salt libs
import salt.utils.path
def __virtual__():
'''
Only load if the mfs commands are installed
'''
if salt.utils.path.which('mfsgetgoal'):
return 'moosefs'
return (False, 'The moosefs execution module cannot be loaded: the mfsgetgoal binary is not in the path.')
def dirinfo(path, opts=None):
'''
Return information on a directory located on the Moose
CLI Example:
.. code-block:: bash
salt '*' moosefs.dirinfo /path/to/dir/ [-[n][h|H]]
'''
cmd = 'mfsdirinfo'
ret = {}
if opts:
cmd += ' -' + opts
cmd += ' ' + path
out = __salt__['cmd.run_all'](cmd, python_shell=False)
output = out['stdout'].splitlines()
for line in output:
if not line:
continue
comps = line.split(':')
ret[comps[0].strip()] = comps[1].strip()
return ret
def fileinfo(path):
'''
Return information on a file located on the Moose
CLI Example:
.. code-block:: bash
salt '*' moosefs.fileinfo /path/to/dir/
'''
cmd = 'mfsfileinfo ' + path
ret = {}
chunknum = ''
out = __salt__['cmd.run_all'](cmd, python_shell=False)
output = out['stdout'].splitlines()
for line in output:
if not line:
continue
if '/' in line:
comps = line.split('/')
chunknum = comps[0].strip().split(':')
meta = comps[1].strip().split(' ')
chunk = chunknum[0].replace('chunk ', '')
loc = chunknum[1].strip()
id_ = meta[0].replace('(id:', '')
ver = meta[1].replace(')', '').replace('ver:', '')
ret[chunknum[0]] = {
'chunk': chunk,
'loc': loc,
'id': id_,
'ver': ver,
}
if 'copy' in line:
copyinfo = line.strip().split(':')
ret[chunknum[0]][copyinfo[0]] = {
'copy': copyinfo[0].replace('copy ', ''),
'ip': copyinfo[1].strip(),
'port': copyinfo[2],
}
return ret
def mounts():
'''
Return a list of current MooseFS mounts
CLI Example:
.. code-block:: bash
salt '*' moosefs.mounts
'''
cmd = 'mount'
ret = {}
out = __salt__['cmd.run_all'](cmd)
output = out['stdout'].splitlines()
for line in output:
if not line:
continue
if 'fuse.mfs' in line:
comps = line.split(' ')
info1 = comps[0].split(':')
info2 = info1[1].split('/')
ret[comps[2]] = {
'remote': {
'master': info1[0],
'port': info2[0],
'subfolder': '/' + info2[1],
},
'local': comps[2],
'options': (comps[5].replace('(', '').replace(')', '')
.split(',')),
}
return ret
def getgoal(path, opts=None):
'''
Return goal(s) for a file or directory
CLI Example:
.. code-block:: bash
salt '*' moosefs.getgoal /path/to/file [-[n][h|H]]
salt '*' moosefs.getgoal /path/to/dir/ [-[n][h|H][r]]
'''
cmd = 'mfsgetgoal'
ret = {}
if opts:
cmd += ' -' + opts
else:
opts = ''
cmd += ' ' + path
out = __salt__['cmd.run_all'](cmd, python_shell=False)
output = out['stdout'].splitlines()
if 'r' not in opts:
goal = output[0].split(': ')
ret = {
'goal': goal[1],
}
else:
for line in output:
if not line:
continue
if path in line:
continue
comps = line.split()
keytext = comps[0] + ' with goal'
if keytext not in ret:
ret[keytext] = {}
ret[keytext][comps[3]] = comps[5]
return ret