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
|
#!/usr/bin/env python
# This file is part of DiSTAF
# Copyright (C) 2015-2016 Red Hat, Inc. <http://www.redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Description: Library for file operations.
"""
import re
import socket
from distaf.util import tc
def write_file(filename, file_contents=" ", create_mode='', filesize='', \
server=''):
"""
This module writes the file along with file contents
@paramater:
* filename - <str> absolute path name of the file to be created
* file_contents - <str> (optional) file content
* create_mode - <str> (optional) mode to create the file
* filesize - <str> (optional) filesize
* server - <str> (optional) name of the server to write the
file. If not given, the function takes the
first node from config file
@Returns: True, on success
False, on failure
"""
if server == '':
server = tc.nodes[0]
if create_mode == '':
create_mode = 'open'
if create_mode != 'open':
try:
conn = tc.get_connection(server, 'root')
if conn == -1:
tc.logger.error("Unable to get connection to 'root' of "
"node %s" % server)
return False
if not conn.modules.os.path.exists(conn.modules.os.path.\
dirname(filename)):
conn.modules.os.makedirs(conn.modules.os.path.\
dirname(filename))
except:
tc.logger.error("Exception occured while creating directory for "
"file %s" % filename)
return False
finally:
conn.close()
if create_mode == 'open':
try:
conn = tc.get_connection(server, 'root')
if conn == -1:
tc.logger.error("Unable to get connection to 'root' of node "
"%s" % server)
return False
if not conn.modules.os.path.exists(conn.modules.os.path.\
dirname(filename)):
conn.modules.os.makedirs(conn.modules.os.path.\
dirname(filename))
with conn.builtin.open(filename, 'w') as _filehandle:
_filehandle.write(file_contents)
except:
tc.logger.error("Exception occured while writing file %s" \
% filename)
return False
finally:
conn.close()
elif create_mode == 'echo':
cmd = "echo " + file_contents + " > " + filename
ret, _, _ = tc.run(server, cmd)
if ret != 0:
return False
elif create_mode == 'touch':
cmd = "touch " + filename
ret, _, _ = tc.run(server, cmd)
if ret != 0:
return False
elif create_mode == 'dd':
if filesize == '':
tc.logger.error("Invalid argument for dd cmd. Pass correct \
number of parameters")
return False
cmd = "dd if=/dev/zero of=%s bs=%s count=1" % (filename, filesize)
ret, _, _ = tc.run(server, cmd)
if ret != 0:
return False
return True
def remove_file(filename, server=''):
"""
This module removes the given file
@paramater:
* filename - <str> absolute path name of the file to be created
* server - <str> (optional) name of the server to write the
file. If not given, the function takes the
first node from config file
@Returns: True, on success
False, on failure
"""
if server == '':
server = tc.nodes[0]
try:
conn = tc.get_connection(server, 'root')
if conn == -1:
tc.logger.error("Unable to get connection to 'root' of node %s" \
% server)
return False
if conn.modules.os.path.exists(filename):
conn.modules.os.remove(filename)
except:
tc.logger.error("Exception occured while removing file %s" % filename)
return False
finally:
conn.close()
return True
def calculate_checksum(file_list, server=''):
"""
This module calculates checksum (sha256sum) for the given file list
@paramater:
* file_list - <list> absolute file names for which checksum to be
calculated
* server - <str> (optional) name of the server.
If not given, the function takes the
first node from config file
@Returns: checksum value in dict format, on success
None, on failure
"""
if server == '':
server = tc.nodes[0]
cmd = "sha256sum %s" % ' '.join(file_list)
ret = tc.run(server, cmd)
if ret[0] != 0:
tc.logger.error("Failed to execute checksum command in server %s" \
% server)
return None
checksum_dict = {}
for line in ret[1].split('\n')[:-1]:
match = re.search(r'^(\S+)\s+(\S+)', line.strip())
if match is None:
tc.logger.error("checksum output is not in \
expected format")
return None
checksum_dict[match.group(2)] = match.group(1)
return checksum_dict
def get_extended_attributes_info(file_list, encoding='hex', attr_name='', \
server=''):
"""
This module gets extended attribute info for the given file list
@paramater:
* file_list - <list> absolute file names
* encoding - <str> (optional) encoding format
* server - <str> (optional) name of the server.
If not given, the function takes the
first node from config file
@Returns: Extended attribute info in dict format, on success
None, on failure
"""
if server == '':
server = tc.nodes[0]
server = socket.gethostbyname(server)
if attr_name == '':
cmd = "getfattr -d -m . -e %s %s" % (encoding, ' '.join(file_list))
else:
cmd = "getfattr -d -m . -n %s %s" % (attr_name, ' '.join(file_list))
ret = tc.run(server, cmd)
if ret[0] != 0:
tc.logger.error("Failed to execute getfattr command in server %s" \
% server)
return None
attr_dict = {}
for each_attr in ret[1].split('\n\n')[:-1]:
for line in each_attr.split('\n'):
if line.startswith('#'):
match = re.search(r'.*file:\s(\S+).*', line)
if match is None:
tc.logger.error("getfattr output isn't in expected format")
return None
key = "/" + match.group(1)
attr_dict[key] = {}
else:
output = line.split('=')
attr_dict[key][output[0]] = output[1]
return attr_dict
def get_filepath_from_rhsnode(filename, server=''):
"""
This module gets filepath of the given file from rhsnode
@paramater:
* filename - <str> absolute name of the file
* server - <str> (optional) name of the server.
If not given, the function takes the
first client from config file
@Returns: file path for the given file in rhs node in list format
on success None, on failure
"""
if server == '':
server = tc.clients[0]
output = get_extended_attributes_info([filename], \
attr_name='trusted.glusterfs.pathinfo', server=server)
if output is None:
tc.logger.error("Failed to get path info")
return None
pathinfo = output[filename]['trusted.glusterfs.pathinfo']
return re.findall(".*?POSIX.*?:(\S+)\>", pathinfo)
|