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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
# 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: Helper module for misc libs. """
from glusto.core import Glusto as g
import os
import sys
import time
def create_dirs(list_of_nodes, list_of_dir_paths):
"""Creates directories on nodes.
Args:
list_of_nodes (list): Nodes on which dirs has to be created.
list_of_dir_paths (list): List of dirs abs path.
Returns:
bool: True of creation of all dirs on all nodes is successful.
False otherwise.
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
if isinstance(list_of_dir_paths, list):
list_of_dir_paths = ' '.join(list_of_dir_paths)
_rc = True
# Create upload dir on each node
for node in list_of_nodes:
ret, _, err = g.run(node, "mkdir -p %s" % list_of_dir_paths)
if ret != 0:
g.log.error("Failed to create the dirs: %s on node: %s - %s" %
(list_of_dir_paths.split(" "), node, err))
_rc = False
if _rc:
g.log.info("Successfully created dirs: %s on nodes:%s" %
(list_of_dir_paths.split(" "), list_of_nodes))
return _rc
def path_exists(list_of_nodes, list_of_paths):
"""check if paths exists on nodes.
Args:
list_of_nodes (list): List of nodes.
list_of_paths (list): List of abs paths to verify if path exist.
Returns:
bool: True if all paths exists on all nodes. False otherwise.
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
if not isinstance(list_of_paths, list):
list_of_paths = (list_of_paths.split(" "))
_rc = True
for node in list_of_nodes:
for path in list_of_paths:
cmd = "ls -l %s" % path
ret, _, err = g.run(node, cmd)
if ret != 0:
g.log.error("Path: %s not found on node: %s - %s" %
(path, node, err))
_rc = False
if _rc:
g.log.info("Paths: %s exists on nodes: %s" %
(list_of_paths, list_of_nodes))
return _rc
def upload_scripts(list_of_nodes, list_of_scripts_abs_path,
upload_dir="/usr/share/glustolibs/io/scripts/", user=None):
"""Uploads specified scripts to all the nodes.
Args:
list_of_nodes (list): Nodes on which scripts has to be uploaded.
list_of_scripts_abs_path (list): List of absolute path of all
scripts that are to be uploaded from local node.
upload_dir (optional[str]): Name of the dir under which
scripts will be uploaded on remote node.
user (optional[str]): The user to use for the remote connection.
Returns:
bool: True if uploading scripts is sucessful on all nodes.
False otherwise.
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
if not isinstance(list_of_scripts_abs_path, list):
list_of_scripts_abs_path = (
list_of_scripts_abs_path.split(" "))
g.log.info("Scripts to upload: %s" % list_of_scripts_abs_path)
g.log.info("Script upload dir: %s" % upload_dir)
# Create upload dir on each node
if not create_dirs(list_of_nodes, upload_dir):
return False
# Upload scrpts
_rc = True
for script_local_abs_path in list_of_scripts_abs_path:
if not os.path.exists(script_local_abs_path):
g.log.error("Script: %s doesn't exists" % script_local_abs_path)
_rc = False
break
for node in list_of_nodes:
script_name = os.path.basename(script_local_abs_path)
script_upload_path = os.path.join(upload_dir, script_name)
g.upload(node, script_local_abs_path, script_upload_path, user)
if not _rc:
g.log.error("Failed to upload scripts")
return False
# Recursively provide execute permissions to all scripts
for node in list_of_nodes:
ret, _, _ = g.run(node, "chmod -R +x %s" % upload_dir)
if ret != 0:
g.log.error("Unable to provide execute permissions to upload dir "
"'%s' on %s" % (upload_dir, node))
return False
else:
g.log.info("Successfully provided execute permissions to upload "
"dir '%s' on %s" % (upload_dir, node))
ret, out, err = g.run(node, "ls -l %s" % upload_dir)
if ret != 0:
g.log.error("Failed to list the dir: %s on node: %s - %s" %
(upload_dir, node, err))
else:
g.log.info("Listing dir: %s on node: %s - \n%s" %
(upload_dir, node, out))
return True
def yum_add_repos(list_of_nodes, list_of_yum_repos):
"""Add yum repo files on all the nodes.
Args:
list_of_nodes (list): Nodes on which yum repo files has to be added.
list_of_yum_repos (list): List of yum repos
Returns:
bool: True if adding yum repo files is sucessful on all nodes.
False otherwise.
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
if not isinstance(list_of_yum_repos, list):
list_of_yum_repos = list_of_yum_repos.split(" ")
_rc = True
for node in list_of_nodes:
for yum_repo in list_of_yum_repos:
out_file = os.path.basename(yum_repo)
cmd = ("wget %s -O /etc/yum.repos.d/%s" % (yum_repo, out_file))
ret, _, err = g.run(node, cmd)
if ret != 0:
g.log.error("Unable to add repo file: %s on node: %s - %s" %
(yum_repo, node, err))
_rc = False
if _rc:
g.log.info("Successfully added yum repo files: %s on nodes: %s" %
(list_of_yum_repos, list_of_nodes))
return _rc
def yum_install_packages(list_of_nodes, yum_packages):
"""Install the specified yum packages on all nodes.
Args:
list_of_nodes (list): Nodes on which yum packages has to be installed.
yum_packages (list): List of yum packages.
Returns:
bool: True if installation of packages is sucessful on all nodes.
False otherwise.
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
if isinstance(yum_packages, list):
yum_packages = ' '.join(yum_packages)
_rc = True
for node in list_of_nodes:
cmd = "yum -y install %s" % yum_packages
ret, _, err = g.run(node, cmd)
if ret != 0:
g.log.error("Unable to install yum packages: %s on node: %s - %s" %
(yum_packages, node, err))
_rc = False
if _rc:
g.log.info("Successfully installed yum packages: %s on nodes: %s" %
(yum_packages, list_of_nodes))
return _rc
def yum_remove_packages(list_of_nodes, yum_packages):
"""Remove the specified yum packages on all nodes.
Args:
list_of_nodes (list): Nodes on which yum packages has to be removed.
yum_packages (list): List of yum packages.
Returns:
bool: True if removing packages is sucessful on all nodes.
False otherwise.
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
if isinstance(yum_packages, list):
yum_packages = ' '.join(yum_packages)
_rc = True
for node in list_of_nodes:
cmd = "yum -y remove %s" % yum_packages
ret, _, err = g.run(node, cmd)
if ret != 0:
g.log.error("Unable to remove yum packages: %s on node: %s - %s" %
(yum_packages, node, err))
_rc = False
if _rc:
g.log.info("Successfully removed yum packages: %s on nodes: %s" %
(yum_packages, list_of_nodes))
return _rc
def pip_install_packages(list_of_nodes, python_packages):
"""Install the specified python packages on all the specified nodes.
Args:
list_of_nodes (list): Nodes on which python packages has to be
installed.
python_packages (list): List of python packages.
Returns:
bool: True if installation of packages is sucessful on all nodes.
False otherwise.
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
if isinstance(python_packages, list):
python_packages = ' '.join(python_packages)
_rc = True
for node in list_of_nodes:
cmd = "pip install %s" % python_packages
ret, _, err = g.run(node, cmd)
if ret != 0:
g.log.error("Unable to install python packages: %s on node: %s - "
"%s" % (python_packages, node, err))
_rc = False
if _rc:
g.log.info("Successfully installed python packages: %s on nodes: %s" %
(python_packages, list_of_nodes))
return _rc
def install_testing_tools(list_of_nodes, testing_tools):
"""Install the specified testing tools on all nodes.
Args:
list_of_nodes (list): Nodes on which testing tools has to be
installed.
testing_tools(list): List of testing tools to install.
Available tools:
- arequal
Returns:
bool: True if installation of all testing tools is sucessful on
all nodes. False otherwise.
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
if not isinstance(testing_tools, list):
testing_tools = testing_tools.split(" ")
_rc = True
this_module = sys.modules[__name__]
for testing_tool in testing_tools:
func_name = 'install_' + testing_tool
if hasattr(this_module, func_name):
func = getattr(this_module, func_name)
ret = func(list_of_nodes)
if not ret:
_rc = False
else:
g.log.info("Successfully installed tool: %s on nodes: [%s]" %
(testing_tool, ', '.join(list_of_nodes)))
else:
g.log.error("Unable to find the helper function to install %s" %
testing_tool)
_rc = False
return _rc
def install_arequal(list_of_nodes):
"""Installs arequal on nodes in /usr/bin/.
Args:
nodes(list): List of nodes on which arequal-checksum needs to be
installed.
Returns:
bool: Returns True on successful installation of arequal-checksum
on all nodes. False Otherwise.
Note: The arequal repo can be specified in the config file:
dependencies:
testing_tools:
arequal:
repo: "https://abc.def.com/tools/arequal/arequal.repo"
"""
if not isinstance(list_of_nodes, list):
list_of_nodes = [list_of_nodes]
try:
arequal_repo = (g.config['dependencies']['testing_tools']['arequal']
['repo'])
except KeyError:
arequal_repo = ("https://copr.fedorainfracloud.org/coprs/nigelbabu/"
"arequal/repo/epel-7/nigelbabu-arequal-epel-7.repo")
arequal_install_path = "/usr/bin/arequal-checksum"
_rc = True
for node in list_of_nodes:
# check if arequal-checksum is installed
if not path_exists(node, arequal_install_path):
g.log.info("arequal-checksum not installed. Installling...")
# get arequal repo file
if not yum_add_repos(node, arequal_repo):
return False
# Install arequal
if not yum_install_packages(node, "arequal"):
return False
# verify arequal_checksum got installed
if not path_exists(node, arequal_install_path):
raise Exception("%s not found on node %s even after "
"installation" % (arequal_install_path, node))
else:
g.log.info("arequal_checksum is installed on node %s" %
node)
else:
g.log.info("arequal-checksum is already installed on %s" % node)
continue
if _rc:
g.log.info("arequal-checksum is installed on nodes: %s" %
list_of_nodes)
return _rc
def are_nodes_online(nodes):
"""
check whether nodes are online or not
Args:
nodes ( str|list ) : Node/Nodes to check whether online or not
Returns:
tuple : Tuple containing two elements (ret, node_results).
The first element ret is of type 'bool', True if all nodes
are online. False otherwise.
The second element 'node_results' is of type dictonary and it
contains the node and its corresponding result. If node is online
then the result contains True else False.
"""
if isinstance(nodes, str):
nodes = [nodes]
node_results = {}
for node in nodes:
cmd = "ping %s -c1" % node
ret, out, err = g.run_local(cmd)
if ret:
g.log.info("%s is offline" % node)
node_results[node] = False
else:
g.log.info("%s is online" % node)
node_results[node] = True
ret = all(node_results.values())
return ret, node_results
def reboot_nodes(nodes):
"""
reboot the nodes and checks whether nodes are offline or not
Args:
nodes ( str|list ) : Node/Nodes to reboot
Returns:
bool : '_rc' is of type 'bool', True if all nodes
comes offline after reboot. False otherwise.
"""
if isinstance(nodes, str):
nodes = [nodes]
cmd = "reboot"
_rc = False
for node in nodes:
g.log.info("Executing cmd: %s on node %s", cmd, node)
g.log.info("Rebooting the node %s", node)
ret = g.run(node, cmd)
halt = 120
counter = 0
g.log.info("Wait for some seconds for the nodes to go offline")
while counter < halt:
ret, reboot_time = are_nodes_offline(nodes)
if not ret:
g.log.info("Nodes are online, Retry after 2 seconds .....")
time.sleep(2)
counter = counter + 2
else:
_rc = True
g.log.info("All nodes %s are offline", nodes)
break
if not _rc:
g.log.error("Some nodes %s are online", nodes)
return _rc
def reboot_nodes_and_wait_to_come_online(nodes, timeout=300):
"""
reboot the node and wait for node to come online
Args:
nodes ( str|list ) : Node/Nodes to reboot
Kwargs:
timeout (int): timeout value in seconds to wait for node
to come online
Returns:
tuple : Tuple containing two elements (_rc, reboot_results).
The first element '_rc' is of type 'bool', True if all nodes
comes online after reboot. False otherwise.
The second element 'reboot_results' is of type dictonary and it
contains the node and corresponding result for reboot. If reboot is
successfull on node, then result contains True else False.
"""
_rc = reboot_nodes(nodes)
counter = 0
g.log.info("Wait for some seconds for the nodes to come online"
" after reboot")
reboot_results = {}
while counter < timeout:
ret, reboot_results = are_nodes_online(nodes)
if not ret:
g.log.info("Nodes are offline, Retry after 5 seconds ..... ")
time.sleep(5)
counter = counter + 5
else:
_rc = True
break
if not _rc:
for node in reboot_results:
if reboot_results[node]:
g.log.info("Node %s is online", node)
else:
g.log.error("Node %s is offline even after "
"%d minutes", node, timeout/60.0)
else:
g.log.info("All nodes %s are up and running", nodes)
return _rc, reboot_results
def are_nodes_offline(nodes):
"""
check whether nodes are offline or not
Args:
nodes ( str|list ) : Node/Nodes to check whether offline or not
Returns:
tuple : Tuple containing two elements (ret, node_results).
The first element ret is of type 'bool', True if all nodes
are offline. False otherwise.
The second element 'node_results' is of type dictonary and it
contains the node and its corresponding result. If node is offline
then the result contains True else False.
"""
if isinstance(nodes, str):
nodes = [nodes]
node_results = {}
for node in nodes:
cmd = "ping %s -c1" % node
ret, out, err = g.run_local(cmd)
if ret:
g.log.info("%s is offline" % node)
node_results[node] = True
else:
g.log.info("%s is online" % node)
node_results[node] = False
ret = all(node_results.values())
return ret, node_results
|