-
Notifications
You must be signed in to change notification settings - Fork 304
/
general_utils.py
1618 lines (1294 loc) · 56.1 KB
/
general_utils.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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
(C) Copyright 2018-2022 Intel Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
"""
# pylint: disable=too-many-lines
from logging import getLogger
import grp
import os
import pwd
import re
import random
import string
import time
import ctypes
from getpass import getuser
from importlib import import_module
from socket import gethostname
from avocado.core.settings import settings
from avocado.core.version import MAJOR
from avocado.utils import process
from ClusterShell.Task import task_self
from ClusterShell.NodeSet import NodeSet, NodeSetParseError
from pydaos.raw import IORequest, DaosObjClass
class DaosTestError(Exception):
"""DAOS API exception class."""
class SimpleProfiler():
"""Simple profiler class.
Counts the number of times a function is called and measure its execution
time.
"""
def __init__(self):
"""Initialize a SimpleProfiler object."""
self._stats = {}
self._logger = getLogger()
def clean(self):
"""Clean the metrics collect so far."""
self._stats = {}
def run(self, fn, tag, *args, **kwargs):
"""Run a function and update its stats.
Args:
fn (function): Function to be executed
args (tuple): Argument list
kwargs (dict): Keyworded, variable-length argument list
"""
self._logger.info("Running function: %s()", fn.__name__)
start_time = time.time()
ret = fn(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
self._logger.info(
"Execution time: %s", self._pretty_time(elapsed_time))
if tag not in self._stats:
self._stats[tag] = [0, []]
self._stats[tag][0] += 1
self._stats[tag][1].append(elapsed_time)
return ret
def get_stat(self, tag):
"""Retrieve the stats of a function.
Args:
tag (str): Tag to be query
Returns:
tuple: A tuple of the fastest (max), slowest (min), and average
execution times.
"""
data = self._stats.get(tag, [0, []])
return self._calculate_metrics(data[1])
def set_logger(self, fn):
"""Assign the function to be used for logging.
Set the function that will be used to print the elapsed time on each
function call. If this value is not set, the profiling will be
performed silently.
Parameters:
fn (function): Function to be used for logging.
"""
self._logger = fn
def print_stats(self):
"""Print all the stats collected so far.
If the logger has not been set, the stats will be printed by using the
built-in print function.
"""
self._logger.info("{0:20} {1:5} {2:10} {3:10} {4:10}".format(
"Function Tag", "Hits", "Max", "Min", "Average"))
for fname, data in list(self._stats.items()):
max_time, min_time, avg_time = self._calculate_metrics(data[1])
self._logger.info(
"{0:20} {1:5} {2:10} {3:10} {4:10}".format(
fname,
data[0],
self._pretty_time(max_time),
self._pretty_time(min_time),
self._pretty_time(avg_time)))
@classmethod
def _pretty_time(cls, ftime):
"""Convert to pretty time string."""
return time.strftime("%H:%M:%S", time.gmtime(ftime))
@classmethod
def _calculate_metrics(cls, data):
"""Calculate the maximum, minimum and average values of a given list."""
max_time = max(data)
min_time = min(data)
avg_time = sum(data) / len(data) if data else 0
return max_time, min_time, avg_time
def human_to_bytes(size):
"""Convert a human readable size value to respective byte value.
Args:
size (str): human readable size value to be converted.
Raises:
DaosTestError: when an invalid human readable size value is provided
Returns:
int: value translated to bytes.
"""
conversion_sizes = ("", "k", "m", "g", "t", "p", "e")
conversion = {
1000: ["{}b".format(item) for item in conversion_sizes],
1024: ["{}ib".format(item) for item in conversion_sizes],
}
match = re.findall(r"([0-9.]+)\s*([a-zA-Z]+|)", size)
try:
multiplier = 1
if match[0][1]:
multiplier = -1
unit = match[0][1].lower()
for item in conversion:
if unit in conversion[item]:
multiplier = item ** conversion[item].index(unit)
break
if multiplier == -1:
raise DaosTestError(
"Invalid unit detected, not in {}: {}".format(
conversion[1000] + conversion[1024][1:], unit))
value = float(match[0][0]) * multiplier
except IndexError as error:
raise DaosTestError(
"Invalid human readable size format: {}".format(size)) from error
return int(value) if value.is_integer() else value
def bytes_to_human(size, digits=2, binary=True):
"""Convert a byte value to the largest (> 1.0) human readable size.
Args:
size (int): byte size value to be converted.
digits (int, optional): number of digits used to round the converted
value. Defaults to 2.
binary (bool, optional): convert to binary (True) or decimal (False)
units. Defaults to True.
Returns:
str: value translated to a human readable size.
"""
units = 1024 if binary else 1000
conversion = ["B", "KB", "MB", "GB", "TB", "PB", "EB"]
index = 0
value = [size if isinstance(size, (int, float)) else 0, conversion.pop(0)]
while value[0] > units and conversion:
index += 1
value[0] = float(size) / (units ** index)
value[1] = conversion.pop(0)
if units == 1024 and len(value[1]) > 1:
value[1] = "{}i{}".format(*value[1])
return "".join([str(round(value[0], digits)), value[1]])
def run_command(command, timeout=60, verbose=True, raise_exception=True,
output_check="both", env=None):
"""Run the command on the local host.
This method uses the avocado.utils.process.run() method to run the specified
command string on the local host using subprocess.Popen(). Even though the
command is specified as a string, since shell=False is passed to process.run
it will use shlex.spit() to break up the command into a list before it is
passed to subprocess.Popen. The shell=False is forced for security. As a
result typically any command containing ";", "|", "&&", etc. will fail.
Args:
command (str): command to run.
timeout (int, optional): command timeout. Defaults to 60 seconds.
verbose (bool, optional): whether to log the command run and
stdout/stderr. Defaults to True.
raise_exception (bool, optional): whether to raise an exception if the
command returns a non-zero exit status. Defaults to True.
output_check (str, optional): whether to record the output from the
command (from stdout and stderr) in the test output record files.
Valid values:
"stdout" - standard output *only*
"stderr" - standard error *only*
"both" - both standard output and error in separate files
"combined" - standard output and error in a single file
"none" - disable all recording
Defaults to "both".
env (dict, optional): dictionary of environment variable names and
values to set when running the command. Defaults to None.
Raises:
DaosTestError: if there is an error running the command
Returns:
CmdResult: an avocado.utils.process CmdResult object containing the
result of the command execution. A CmdResult object has the
following properties:
command - command string
exit_status - exit_status of the command
stdout - the stdout
stdout_text - decoded stdout
stderr - the stderr
stderr_text - decoded stderr
duration - command execution time
interrupted - whether the command completed within timeout
pid - command's pid
"""
log = getLogger()
msg = None
kwargs = {
"cmd": command,
"timeout": timeout,
"verbose": verbose,
"ignore_status": not raise_exception,
"allow_output_check": output_check,
"shell": False,
"env": env,
}
if verbose:
log.info("Command environment vars:\n %s", env)
try:
# Block until the command is complete or times out
return process.run(**kwargs)
except (TypeError, FileNotFoundError) as error:
# Can occur if using env with a non-string dictionary values
msg = "Error running '{}': {}".format(command, error)
if env is not None:
msg = "\n".join([
msg,
"Verify env values are defined as strings: {}".format(env)])
except process.CmdError as error:
# Report if the command timed out or failed
if error.result.interrupted:
msg = "Timeout detected running '{}' with a {}s timeout".format(
command, timeout)
elif verbose:
msg = "Error occurred running '{}': {}".format(command, error)
else:
msg = "Error occurred running '{}':\n{}".format(
command, error.result)
if msg is not None:
log.info(msg)
raise DaosTestError(msg)
def run_task(hosts, command, timeout=None, verbose=False):
"""Create a task to run a command on each host in parallel.
Args:
hosts (NodeSet): hosts on which to run the command
command (str): the command to run in parallel
timeout (int, optional): command timeout in seconds. Defaults to None.
verbose (bool, optional): display message for command execution. Defaults to False.
Returns:
Task: a ClusterShell.Task.Task object for the executed command
"""
if not isinstance(hosts, NodeSet):
hosts = NodeSet.fromlist(hosts)
task = task_self()
# Enable forwarding of the ssh authentication agent connection
task.set_info("ssh_options", "-oForwardAgent=yes")
kwargs = {"command": command, "nodes": hosts}
if timeout is not None:
kwargs["timeout"] = timeout
if verbose:
log = getLogger()
log.info("Running on %s: %s", hosts, command)
task.run(**kwargs)
return task
def check_task(task, logger=None):
"""Check the results of the executed task and get the output.
Args:
task (Task): a ClusterShell.Task.Task object for the executed command
Returns:
bool: if the command returned an 0 exit status on every host
"""
def check_task_log(message):
"""Log the provided text if a logger is present.
Args:
message (str): text to display
"""
if logger:
logger.info(message)
# Create a dictionary of hosts for each unique return code
results = dict(task.iter_retcodes())
# Display the command output
for code in sorted(results):
output_data = list(task.iter_buffers(results[code]))
if not output_data:
output_data = [["<NONE>", results[code]]]
for output, o_hosts in output_data:
node_set = NodeSet.fromlist(o_hosts)
lines = list(output.splitlines())
if len(lines) > 1:
# Print the sub-header for multiple lines of output
check_task_log(" {}: rc={}, output:".format(node_set, code))
for number, line in enumerate(lines):
if isinstance(line, bytes):
line = line.decode("utf-8")
if len(lines) == 1:
# Print the sub-header and line for one line of output
check_task_log(" {}: rc={}, output: {}".format(node_set, code, line))
continue
try:
check_task_log(" {}".format(line))
except IOError:
# DAOS-5781 Jenkins doesn't like receiving large amounts of data in a short
# space of time so catch this and retry.
check_task_log(
"*** DAOS-5781: Handling IOError detected while processing line {}/{} with "
"retry ***".format(*number + 1, len(lines)))
time.sleep(5)
check_task_log(" {}".format(line))
# List any hosts that timed out
timed_out = [str(hosts) for hosts in task.iter_keys_timeout()]
if timed_out:
check_task_log(" {}: timeout detected".format(NodeSet.fromlist(timed_out)))
# Determine if the command completed successfully across all the hosts
return len(results) == 1 and 0 in results
def display_task(task):
"""Display the output for the executed task.
Args:
task (Task): a ClusterShell.Task.Task object for the executed command
Returns:
bool: if the command returned an 0 exit status on every host
"""
log = getLogger()
return check_task(task, log)
def log_task(hosts, command, timeout=None):
"""Display the output of the command executed on each host in parallel.
Args:
hosts (list): list of hosts
command (str): the command to run in parallel
timeout (int, optional): command timeout in seconds. Defaults to None.
Returns:
bool: if the command returned an 0 exit status on every host
"""
return display_task(run_task(hosts, command, timeout, True))
def run_pcmd(hosts, command, verbose=True, timeout=None, expect_rc=0):
"""Run a command on each host in parallel and get the results.
Args:
hosts (NodeSet): hosts on which to run the command
command (str): the command to run in parallel
verbose (bool, optional): display command output. Defaults to True.
timeout (int, optional): command timeout in seconds. Defaults to None.
expect_rc (int, optional): display output if the command return code
does not match this value. Defaults to 0. A value of None will
bypass this feature.
Returns:
list: a list of dictionaries with each entry containing output, exit
status, and interrupted status common to each group of hosts, e.g.:
[
{
"command": "ls my_dir",
"hosts": NodeSet(wolf-[1-3]),
"exit_status": 0,
"interrupted": False,
"stdout": ["file1.txt", "file2.json"],
},
{
"command": "ls my_dir",
"hosts": NodeSet(wolf-[4]),
"exit_status": 1,
"interrupted": False,
"stdout": ["No such file or directory"],
},
{
"command": "ls my_dir",
"hosts": NodeSet(wolf-[5-6]),
"exit_status": 255,
"interrupted": True,
"stdout": [""]
},
]
"""
log = getLogger()
results = []
# Run the command on each host in parallel
task = run_task(hosts, command, timeout)
# Get the exit status of each host
host_exit_status = {str(host): None for host in hosts}
for exit_status, host_list in task.iter_retcodes():
for host in host_list:
host_exit_status[host] = exit_status
# Get a list of any interrupted hosts
host_interrupted = []
if timeout and task.num_timeout() > 0:
host_interrupted.extend(list(task.iter_keys_timeout()))
# Iterate through all the groups of common output
output_data = list(task.iter_buffers())
if not output_data:
output_data = [["", hosts]]
for output, host_list in output_data:
# Determine the unique exit status for each host with the same output
output_exit_status = {}
for host in host_list:
if host_exit_status[host] not in output_exit_status:
output_exit_status[host_exit_status[host]] = NodeSet()
output_exit_status[host_exit_status[host]].add(host)
# Determine the unique interrupted state for each host with the same
# output and exit status
for exit_status in output_exit_status:
output_interrupted = {}
for host in list(output_exit_status[exit_status]):
is_interrupted = host in host_interrupted
if is_interrupted not in output_interrupted:
output_interrupted[is_interrupted] = NodeSet()
output_interrupted[is_interrupted].add(host)
# Add a result entry for each group of hosts with the same output,
# exit status, and interrupted status
for interrupted in output_interrupted:
results.append({
"command": command,
"hosts": output_interrupted[interrupted],
"exit_status": exit_status,
"interrupted": interrupted,
"stdout": [
line.decode("utf-8").rstrip(os.linesep)
for line in output],
})
# Display results if requested or there is an unexpected exit status
bad_exit_status = [
item["exit_status"]
for item in results
if expect_rc is not None and item["exit_status"] != expect_rc]
if verbose or bad_exit_status:
log.info(colate_results(command, results))
return results
def colate_results(command, results):
"""Colate the output of run_pcmd.
Args:
command (str): command used to obtain the data on each server
results (list): list: a list of dictionaries with each entry
containing output, exit status, and interrupted
status common to each group of hosts (see run_pcmd()'s
return for details)
Returns:
str: a string colating run_pcmd()'s results
"""
res = ""
res += "Command: %s\n" % command
res += "Results:\n"
for result in results:
res += " %s: exit_status=%s, interrupted=%s:" % (
result["hosts"], result["exit_status"], result["interrupted"])
for line in result["stdout"]:
res += " %s\n" % line
return res
def get_host_data(hosts, command, text, error, timeout=None):
"""Get the data requested for each host using the specified command.
Args:
hosts (NodeSet): hosts on which to run the command
command (str): command used to obtain the data on each server
text (str): data identification string
error (str): data error string
Returns:
list: a list of dictionaries containing the following key/value pairs:
"hosts": NodeSet containing the hosts with this data
"data": data requested for the group of hosts
"""
log = getLogger()
host_data = []
DATA_ERROR = "[ERROR]"
# Find the data for each specified servers
log.info(" Obtaining %s data on %s", text, hosts)
results = run_pcmd(hosts, command, False, timeout, None)
errors = [
item["exit_status"]
for item in results if item["exit_status"] != 0]
if errors:
log.info(" %s on the following hosts:", error)
for result in results:
if result["exit_status"] in errors:
log.info(
" %s: rc=%s, interrupted=%s, command=\"%s\":",
result["hosts"], result["exit_status"],
result["interrupted"], result["command"])
for line in result["stdout"]:
log.info(" %s", line)
host_data.append({"hosts": hosts, "data": DATA_ERROR})
else:
for result in results:
host_data.append(
{"hosts": result["hosts"], "data": "\n".join(result["stdout"])})
return host_data
def pcmd(hosts, command, verbose=True, timeout=None, expect_rc=0):
"""Run a command on each host in parallel and get the return codes.
Args:
hosts (NodeSet): hosts on which to run the command
command (str): the command to run in parallel
verbose (bool, optional): display command output. Defaults to True.
timeout (int, optional): command timeout in seconds. Defaults to None.
expect_rc (int, optional): expected return code. Defaults to 0.
Returns:
dict: a dictionary of return codes keys and accompanying NodeSet
values indicating which hosts yielded the return code.
"""
# Run the command on each host in parallel
results = run_pcmd(hosts, command, verbose, timeout, expect_rc)
exit_status = {}
for result in results:
if result["exit_status"] not in exit_status:
exit_status[result["exit_status"]] = NodeSet()
exit_status[result["exit_status"]].add(result["hosts"])
return exit_status
def check_file_exists(hosts, filename, user=None, directory=False,
sudo=False):
"""Check if a specified file exist on each specified hosts.
If specified, verify that the file exists and is owned by the user.
Args:
hosts (NodeSet): hosts on which to run the command
filename (str): file to check for the existence of on each host
user (str, optional): owner of the file. Defaults to None.
sudo (bool, optional): whether to run the command via sudo. Defaults to
False.
Returns:
(bool, NodeSet): A tuple of:
- True if the file exists on each of the hosts; False otherwise
- A NodeSet of hosts on which the file does not exist
"""
missing_file = NodeSet()
command = "test -e {0}".format(filename)
if user is not None and not directory:
command = "test -O {0}".format(filename)
elif user is not None and directory:
command = "test -O {0} && test -d {0}".format(filename)
elif directory:
command = "test -d '{0}'".format(filename)
if sudo:
command = "sudo " + command
task = run_task(hosts, command, verbose=True)
for ret_code, node_list in task.iter_retcodes():
if ret_code != 0:
missing_file.add(NodeSet.fromlist(node_list))
return len(missing_file) == 0, missing_file
def check_for_pool(host, uuid):
"""Check if pool folder exist on server.
Args:
host (NodeSet): Server host name
uuid (str): Pool uuid to check if exists
Returns:
bool: True if pool folder exists, False otherwise
"""
pool_dir = "/mnt/daos/{}".format(uuid)
result = check_file_exists(host, pool_dir, directory=True, sudo=True)
if result[0]:
print("{} exists on {}".format(pool_dir, host))
else:
print("{} does not exist on {}".format(pool_dir, host))
return result[0]
def process_host_list(hoststr):
"""
Convert a slurm style host string into a list of individual hosts.
e.g. server-[26-27] becomes a list with entries server-26, server-27
This works for every thing that has come up so far but I don't know what
all slurm finds acceptable so it might not parse everything possible.
"""
# 1st split into cluster name and range of hosts
split_loc = hoststr.index('-')
cluster = hoststr[0:split_loc]
num_range = hoststr[split_loc + 1:]
# if its just a single host then nothing to do
if num_range.isdigit():
return [hoststr]
# more than 1 host, remove the brackets
host_list = []
num_range = re.sub(r'\[|\]', '', num_range)
# differentiate between ranges and single numbers
hosts_and_ranges = num_range.split(',')
for item in hosts_and_ranges:
if item.isdigit():
hostname = cluster + '-' + item
host_list.append(hostname)
else:
# split the two ends of the range
host_range = item.split('-')
for hostnum in range(int(host_range[0]), int(host_range[1]) + 1):
hostname = "{}-{}".format(cluster, hostnum)
host_list.append(hostname)
return host_list
def get_random_string(length, exclude=None, include=None):
"""Create a specified length string of random ascii letters and numbers.
Optionally exclude specific random strings from being returned.
Args:
length (int): length of the string to return
exclude (list, optional): list of strings to not return. Defaults to
None.
include (list, optional): list of characters to use in the random
string. Defaults to None, in which case use all upper case and
digits.
Returns:
str: a string of random ascii letters and numbers
"""
exclude = exclude if isinstance(exclude, list) else []
if include is None:
include = string.ascii_uppercase + string.digits
random_string = None
while not isinstance(random_string, str) or random_string in exclude:
random_string = "".join(random.choice(include) for _ in range(length)) # nosec
return random_string
def get_random_bytes(length, exclude=None, encoding="utf-8"):
"""Create a specified length string of random ascii letters and numbers.
Optionally exclude specific random strings from being returned.
Args:
length (int): length of the string to return
exclude (list, optional): list of strings to not return. Defaults to
None.
encoding (str, optional): bytes encoding. Defaults to "utf-8"
Returns:
bytes : a string of random ascii letters and numbers converted to
bytes object
"""
return get_random_string(length, exclude).encode(encoding)
def check_pool_files(log, hosts, uuid):
"""Check if pool files exist on the specified list of hosts.
Args:
log (logging): logging object used to display messages
hosts (NodeSet): list of hosts
uuid (str): uuid file name to look for in /mnt/daos.
Returns:
bool: True if the files for this pool exist on each host; False
otherwise
"""
status = True
log.info("Checking for pool data on %s", hosts)
pool_files = [uuid, "superblock"]
for filename in ["/mnt/daos/{}".format(item) for item in pool_files]:
result = check_file_exists(hosts, filename, sudo=True)
if not result[0]:
log.error("%s: %s not found", result[1], filename)
status = False
return status
def convert_list(value, separator=","):
"""Convert a list into a separator-separated string of its items.
Examples:
convert_list([1,2,3]) -> '1,2,3'
convert_list([1,2,3], " ") -> '1 2 3'
convert_list([1,2,3], ", ") -> '1, 2, 3'
Args:
value (list): list to convert into a string
separator (str, optional): list item separator. Defaults to ",".
Returns:
str: a single string containing all the list items separated by the
separator.
"""
return separator.join([str(item) for item in value])
def dump_engines_stacks(hosts, verbose=True, timeout=60, added_filter=None):
"""Signal the engines on each hosts to generate their ULT stacks dump.
Args:
hosts (NodeSet): hosts on which to signal the engines
verbose (bool, optional): display command output. Defaults to True.
timeout (int, optional): command timeout in seconds. Defaults to 60
seconds.
added_filter (str, optional): negative filter to better identify
engines.
Returns:
dict: a dictionary of return codes keys and accompanying NodeSet
values indicating which hosts yielded the return code.
Return code keys:
0 No engine matched the criteria / No engine signaled.
1 One or more engine matched the criteria and a signal was
sent.
"""
result = {}
log = getLogger()
log.info("Dumping ULT stacks of engines on %s", hosts)
if added_filter:
ps_cmd = "/usr/bin/ps xa | grep daos_engine | grep -vE {}".format(
added_filter)
else:
ps_cmd = "/usr/bin/pgrep --list-full daos_engine"
if hosts is not None:
commands = [
"rc=0",
"if " + ps_cmd,
"then rc=1",
"sudo pkill --signal USR2 daos_engine",
# leave some time for ABT info/stacks dump to complete.
# at this time there is no way to know when Argobots ULTs stacks
# has completed, see DAOS-1452/DAOS-9942.
"sleep 30",
"fi",
"exit $rc",
]
result = pcmd(hosts, "; ".join(commands), verbose, timeout, None)
return result
def stop_processes(hosts, pattern, verbose=True, timeout=60, added_filter=None):
"""Stop the processes on each hosts that match the pattern.
Args:
hosts (NodeSet): hosts on which to stop the processes
pattern (str): regular expression used to find process names to stop
verbose (bool, optional): display command output. Defaults to True.
timeout (int, optional): command timeout in seconds. Defaults to 60
seconds.
added_filter (str, optional): negative filter to better identify
processes.
Returns:
dict: a dictionary of return codes keys and accompanying NodeSet
values indicating which hosts yielded the return code.
Return code keys:
0 No processes matched the criteria / No processes killed.
1 One or more processes matched the criteria and a kill was
attempted.
"""
result = {}
log = getLogger()
log.info("Killing any processes on %s that match: %s", hosts, pattern)
if added_filter:
ps_cmd = "/usr/bin/ps xa | grep -E {} | grep -vE {}".format(
pattern, added_filter)
else:
ps_cmd = "/usr/bin/pgrep --list-full {}".format(pattern)
if hosts is not None:
commands = [
"rc=0",
"if " + ps_cmd,
"then rc=1",
"sudo /usr/bin/pkill {}".format(pattern),
"sleep 5",
"if " + ps_cmd,
"then sudo /usr/bin/pkill --signal ABRT {}".format(pattern),
"sleep 1",
"if " + ps_cmd,
"then sudo /usr/bin/pkill --signal KILL {}".format(pattern),
"fi",
"fi",
"fi",
"exit $rc",
]
result = pcmd(hosts, "; ".join(commands), verbose, timeout, None)
return result
def get_partition_hosts(partition, reservation=None):
"""Get a list of hosts in the specified slurm partition and reservation.
Args:
partition (str): name of the partition
reservation (str): name of reservation
Returns:
list: list of hosts in the specified partition
"""
log = getLogger()
hosts = []
if partition is not None:
# Get the partition name information
cmd = "scontrol show partition {}".format(partition)
try:
result = process.run(cmd, timeout=10)
except process.CmdError as error:
log.warning(
"Unable to obtain hosts from the %s slurm "
"partition: %s", partition, error)
result = None
if result:
# Get the list of hosts from the partition information
output = result.stdout_text
try:
hosts = list(NodeSet(re.findall(r"\s+Nodes=(.*)", output)[0]))
except (NodeSetParseError, IndexError):
log.warning(
"Unable to obtain hosts from the %s slurm partition "
"output: %s", partition, output)
hosts = []
if hosts and reservation is not None:
# Get the list of hosts from the reservation information
cmd = "scontrol show reservation {}".format(reservation)
try:
result = process.run(cmd, timeout=10)
except process.CmdError as error:
log.warning(
"Unable to obtain hosts from the %s slurm "
"reservation: %s", reservation, error)
result = None
hosts = []
if result:
# Get the list of hosts from the reservation information
output = result.stdout_text
try:
reservation_hosts = list(
NodeSet(re.findall(r"\sNodes=(\S+)", output)[0]))
except (NodeSetParseError, IndexError):
log.warning(
"Unable to obtain hosts from the %s slurm "
"reservation output: %s", reservation, output)
reservation_hosts = []
is_subset = set(reservation_hosts).issubset(set(hosts))
if reservation_hosts and is_subset:
hosts = reservation_hosts
else:
hosts = []
return hosts
def get_log_file(name):
"""Get the full log file name and path.
Prepends the DAOS_TEST_LOG_DIR path (or /tmp) to the specified file name.
Args:
name (str): log file name
Returns:
str: full log file name including path
"""
return os.path.join(os.environ.get("DAOS_TEST_LOG_DIR", "/tmp"), name)
def check_uuid_format(uuid):
"""Check for a correct UUID format.
Args:
uuid (str): Pool or Container UUID.
Returns:
bool: status of valid or invalid uuid
"""
pattern = re.compile("([0-9a-fA-F-]+)")
return bool(len(uuid) == 36 and pattern.match(uuid))
def get_numeric_list(numeric_range):
"""Convert a string of numeric ranges into an expanded list of integers.
Example: "0-3,7,9-13,15" -> [0, 1, 2, 3, 7, 9, 10, 11, 12, 13, 15]
Args:
numeric_range (str): the string of numbers and/or ranges of numbers to
convert
Raises:
AttributeError: if the syntax of the numeric_range argument is invalid