/usr/lib/python3/dist-packages/ClusterShell/NodeUtils.py is in python3-clustershell 1.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# Copyright (C) 2010-2016 CEA/DAM
# Copyright (C) 2010-2016 Aurelien Degremont <aurelien.degremont@cea.fr>
# Copyright (C) 2015-2017 Stephane Thiell <sthiell@stanford.edu>
#
# This file is part of ClusterShell.
#
# ClusterShell is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# ClusterShell 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with ClusterShell; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""
Cluster nodes utility module
The NodeUtils module is a ClusterShell helper module that provides
supplementary services to manage nodes in a cluster. It is primarily
designed to enhance the NodeSet module providing some binding support
to external node groups sources in separate namespaces (example of
group sources are: files, jobs scheduler, custom scripts, etc.).
"""
try:
from configparser import ConfigParser, NoOptionError, NoSectionError
except ImportError:
# Python 2 compat
from ConfigParser import ConfigParser, NoOptionError, NoSectionError
import errno
from functools import wraps
import glob
import logging
import os
import shlex
import time
from string import Template
from subprocess import Popen, PIPE
try:
basestring
except NameError:
basestring = str
LOGGER = logging.getLogger(__name__)
class GroupSourceError(Exception):
"""Base GroupSource error exception"""
def __init__(self, message, group_source):
Exception.__init__(self, message)
self.group_source = group_source
class GroupSourceNoUpcall(GroupSourceError):
"""Raised when upcall or method is not available"""
class GroupSourceQueryFailed(GroupSourceError):
"""Raised when a query failed (eg. no group found)"""
class GroupResolverError(Exception):
"""Base GroupResolver error"""
class GroupResolverSourceError(GroupResolverError):
"""Raised when upcall is not available"""
class GroupResolverIllegalCharError(GroupResolverError):
"""Raised when an illegal group character is encountered"""
class GroupResolverConfigError(GroupResolverError):
"""Raised when a configuration error is encountered"""
_DEFAULT_CACHE_TIME = 3600
class GroupSource(object):
"""ClusterShell Group Source class.
A Group Source object defines resolv_map, resolv_list, resolv_all and
optional resolv_reverse methods for node group resolution. It is
constituting a group resolution namespace.
"""
def __init__(self, name, groups=None, allgroups=None):
"""Initialize GroupSource
:param name: group source name
:param groups: group to nodes dict
:param allgroups: optional _all groups_ result (string)
"""
self.name = name
self.groups = groups or {} # we avoid the use of {} as default argument
self.allgroups = allgroups
self.has_reverse = False
def resolv_map(self, group):
"""Get nodes from group `group`"""
return self.groups.get(group, '')
def resolv_list(self):
"""Return a list of all group names for this group source"""
return list(self.groups)
def resolv_all(self):
"""Return the content of all groups as defined by this GroupSource"""
if self.allgroups is None:
raise GroupSourceNoUpcall("All groups info not available", self)
return self.allgroups
def resolv_reverse(self, node):
"""
Return the group name matching the provided node.
"""
raise GroupSourceNoUpcall("Not implemented", self)
class FileGroupSource(GroupSource):
"""File-based Group Source using loader for file format and cache expiry."""
def __init__(self, name, loader):
"""
Initialize FileGroupSource object.
:param name: group source name (eg. key name of yaml root dict)
:param loader: associated content loader (eg. YAMLGroupLoader object)
"""
# do not call super.__init__ to allow the use of r/o properties
self.name = name
self.loader = loader
self.has_reverse = False
@property
def groups(self):
"""groups property (dict)"""
return self.loader.groups(self.name)
@property
def allgroups(self):
"""allgroups property (string)"""
# FileGroupSource uses the 'all' group to implement resolv_all
return self.groups.get('all')
class UpcallGroupSource(GroupSource):
"""
GroupSource class managing external calls for nodegroup support.
Upcall results are cached for a customizable amount of time. This is
controlled by `cache_time` attribute. Default is 3600 seconds.
"""
def __init__(self, name, map_upcall, all_upcall=None,
list_upcall=None, reverse_upcall=None, cfgdir=None,
cache_time=None):
GroupSource.__init__(self, name)
self.verbosity = 0 # deprecated
self.cfgdir = cfgdir
self.logger = logging.getLogger(__name__)
# Supported external upcalls
self.upcalls = {}
self.upcalls['map'] = map_upcall
if all_upcall:
self.upcalls['all'] = all_upcall
if list_upcall:
self.upcalls['list'] = list_upcall
if reverse_upcall:
self.upcalls['reverse'] = reverse_upcall
self.has_reverse = True
# Cache upcall data
if cache_time is None:
self.cache_time = _DEFAULT_CACHE_TIME
else:
self.cache_time = cache_time
self._cache = {}
self.clear_cache()
def clear_cache(self):
"""
Remove all previously cached upcall results whatever their lifetime is.
"""
self._cache = {
'map': {},
'reverse': {}
}
def _upcall_read(self, cmdtpl, args=dict()):
"""
Invoke the specified upcall command, raise an Exception if
something goes wrong and return the command output otherwise.
"""
cmdline = Template(self.upcalls[cmdtpl]).safe_substitute(args)
self.logger.debug("EXEC '%s'", cmdline)
proc = Popen(cmdline, stdout=PIPE, shell=True, cwd=self.cfgdir,
universal_newlines=True)
output = proc.communicate()[0].strip()
self.logger.debug("READ '%s'", output)
if proc.returncode != 0:
self.logger.debug("ERROR '%s' returned %d", cmdline,
proc.returncode)
raise GroupSourceQueryFailed(cmdline, self)
return output
def _upcall_cache(self, upcall, cache, key, **args):
"""
Look for `key' in provided `cache'. If not found, call the
corresponding `upcall'.
If `key' is missing, it is added to provided `cache'. Each entry in a
cache is kept only for a limited time equal to self.cache_time .
"""
if not self.upcalls.get(upcall):
raise GroupSourceNoUpcall(upcall, self)
# Purge expired data from cache
if key in cache and cache[key][1] < time.time():
self.logger.debug("PURGE EXPIRED (%d)'%s'", cache[key][1], key)
del cache[key]
# Fetch the data if unknown of just purged
if key not in cache:
cache_expiry = time.time() + self.cache_time
# $CFGDIR and $SOURCE always replaced
args['CFGDIR'] = self.cfgdir
args['SOURCE'] = self.name
cache[key] = (self._upcall_read(upcall, args), cache_expiry)
return cache[key][0]
def resolv_map(self, group):
"""
Get nodes from group 'group', using the cached value if
available.
"""
return self._upcall_cache('map', self._cache['map'], group, GROUP=group)
def resolv_list(self):
"""
Return a list of all group names for this group source, using
the cached value if available.
"""
return self._upcall_cache('list', self._cache, 'list')
def resolv_all(self):
"""
Return the content of special group ALL, using the cached value
if available.
"""
return self._upcall_cache('all', self._cache, 'all')
def resolv_reverse(self, node):
"""
Return the group name matching the provided node, using the
cached value if available.
"""
# Cast node to string as cache key must be hashable
node_str = str(node)
return self._upcall_cache('reverse', self._cache['reverse'], node_str,
NODE=node_str)
class YAMLGroupLoader(object):
"""
YAML group file loader/reloader.
Load or reload a YAML multi group sources file:
- create GroupSource objects
- gather groups dict content on load
- reload the file once cache_time has expired
"""
def __init__(self, filename, cache_time=None):
"""
Initialize YAMLGroupLoader and load file.
:param filename: YAML file path
:param cache_time: cache time (seconds)
"""
if cache_time is None:
self.cache_time = _DEFAULT_CACHE_TIME
else:
self.cache_time = cache_time
self.cache_expiry = 0
self.filename = filename
self.sources = {}
self._groups = {}
# must be loaded after initialization so self.sources is set
self._load()
def _load(self):
"""Load or reload YAML group file to create GroupSource objects."""
with open(self.filename) as yamlfile:
try:
import yaml
sources = yaml.load(yamlfile)
except ImportError as exc:
msg = "Disable autodir or install PyYAML!"
raise GroupResolverConfigError("%s (%s)" % (str(exc), msg))
except yaml.YAMLError as exc:
raise GroupResolverConfigError("%s: %s" % (self.filename, exc))
# NOTE: change to isinstance(sources, collections.Mapping) with py2.6+
if not isinstance(sources, dict):
fmt = "%s: invalid content (base is not a dict)"
raise GroupResolverConfigError(fmt % self.filename)
first = not self.sources
for srcname, groups in sources.items():
# check for valid types returned by PyYAML Loader
if not isinstance(srcname, basestring):
fmt = '%s: group source %s not a string (add quotes?)'
raise GroupResolverConfigError(fmt % (self.filename, srcname))
if not isinstance(groups, dict):
fmt = "%s: invalid content (group source '%s' is not a dict)"
raise GroupResolverConfigError(fmt % (self.filename, srcname))
for grp in groups:
if not isinstance(grp, basestring):
fmt = '%s: %s: group name %s not a string (add quotes?)'
raise GroupResolverConfigError(fmt % (self.filename,
srcname, grp))
if first:
self._groups[srcname] = groups
self.sources[srcname] = FileGroupSource(srcname, self)
elif srcname in self.sources:
# update groups of existing source
self._groups[srcname] = groups
# else: cannot add new source on reload - just ignore it
# groups are loaded, set cache expiry
self.cache_expiry = time.time() + self.cache_time
def __iter__(self):
"""Iterate over GroupSource objects."""
# safe as long as self.sources is set at init (once)
return iter(self.sources.values())
def groups(self, sourcename):
"""
Groups dict accessor for sourcename.
This method is called by associated FileGroupSource objects and simply
returns dict content, after reloading file if cache_time has expired.
"""
if self.cache_expiry < time.time():
# reload whole file if cache time expired
self._load()
return self._groups[sourcename]
class GroupResolver(object):
"""
Base class GroupResolver that aims to provide node/group resolution
from multiple GroupSources.
A GroupResolver object might be initialized with a default
GroupSource object, that is later used when group resolution is
requested with no source information. As of version 1.7, a set of
illegal group characters may also be provided for sanity check
(raising GroupResolverIllegalCharError when found).
"""
def __init__(self, default_source=None, illegal_chars=None):
"""Lazy initialization of a new GroupResolver object."""
self._sources = {}
self._default_source = default_source
self._initialized = False
self.illegal_chars = illegal_chars or set()
def _late_init(self):
"""Override method to initialize object just before it is needed."""
if self._default_source:
self._sources[self._default_source.name] = self._default_source
self._initialized = True # overriding methods should call super
def init(func):
@wraps(func)
def wrapper(self, *args):
if not self._initialized:
self._late_init()
return func(self, *args)
return wrapper
@init
def set_verbosity(self, value):
"""Set debugging verbosity value (DEPRECATED: use logging.DEBUG)."""
for source in self._sources.values():
source.verbosity = value
@init
def add_source(self, group_source):
"""Add a GroupSource to this resolver."""
if group_source.name in self._sources:
raise ValueError("GroupSource '%s': name collision" % \
group_source.name)
self._sources[group_source.name] = group_source
@init
def sources(self):
"""Get the list of all resolver source names. """
srcs = list(self._sources)
if srcs and srcs[0] is not self._default_source:
srcs.remove(self._default_source.name)
srcs.insert(0, self._default_source.name)
return srcs
@init
def _get_default_source_name(self):
"""Get default source name of resolver."""
if self._default_source is None:
return None
return self._default_source.name
@init
def _set_default_source_name(self, sourcename):
"""Set default source of resolver (by name)."""
try:
self._default_source = self._sources[sourcename]
except KeyError:
raise GroupResolverSourceError(sourcename)
default_source_name = property(_get_default_source_name,
_set_default_source_name)
def _list_nodes(self, source, what, *args):
"""Helper method that returns a list of results (nodes) when
the source is defined."""
result = []
assert source
raw = getattr(source, 'resolv_%s' % what)(*args)
for line in raw.splitlines():
[result.append(x) for x in line.strip().split()]
return result
def _list_groups(self, source, what, *args):
"""Helper method that returns a list of results (groups) when
the source is defined."""
result = []
assert source
raw = getattr(source, 'resolv_%s' % what)(*args)
try:
grpiter = raw.splitlines()
except AttributeError:
grpiter = raw
for line in grpiter:
for grpstr in line.strip().split():
if self.illegal_chars.intersection(grpstr):
errmsg = ' '.join(self.illegal_chars.intersection(grpstr))
raise GroupResolverIllegalCharError(errmsg)
result.append(grpstr)
return result
@init
def _source(self, namespace):
"""Helper method that returns the source by namespace name."""
if not namespace:
source = self._default_source
else:
source = self._sources.get(namespace)
if not source:
raise GroupResolverSourceError(namespace or "<default>")
return source
def group_nodes(self, group, namespace=None):
"""
Find nodes for specified group name and optional namespace.
"""
source = self._source(namespace)
return self._list_nodes(source, 'map', group)
def all_nodes(self, namespace=None):
"""
Find all nodes. You may specify an optional namespace.
"""
source = self._source(namespace)
return self._list_nodes(source, 'all')
def grouplist(self, namespace=None):
"""
Get full group list. You may specify an optional
namespace.
"""
source = self._source(namespace)
return self._list_groups(source, 'list')
def has_node_groups(self, namespace=None):
"""
Return whether finding group list for a specified node is
supported by the resolver (in optional namespace).
"""
try:
return self._source(namespace).has_reverse
except GroupResolverSourceError:
return False
def node_groups(self, node, namespace=None):
"""
Find group list for specified node and optional namespace.
"""
source = self._source(namespace)
return self._list_groups(source, 'reverse', node)
class GroupResolverConfig(GroupResolver):
"""
GroupResolver class that is able to automatically setup its
GroupSource's from a configuration file. This is the default
resolver for NodeSet.
"""
SECTION_MAIN = 'Main'
def __init__(self, filenames, illegal_chars=None):
"""
Lazy init GroupResolverConfig object from filenames.
"""
GroupResolver.__init__(self, illegal_chars=illegal_chars)
self.filenames = filenames
self.config = None
def _late_init(self):
"""
Initialize object when needed. Only the first accessible config
filename is loaded.
"""
GroupResolver._late_init(self)
# support single or multiple config filenames
self.config = ConfigParser()
parsed = self.config.read(self.filenames)
# check if at least one parsable config file has been found, otherwise
# continue with an empty self._sources
if parsed:
# for proper $CFGDIR selection, take last parsed configfile only
self._parse_config(os.path.dirname(parsed[-1]))
def _parse_config(self, cfg_dirname):
"""parse config using relative dir cfg_dirname"""
# parse Main.confdir
try:
if self.config.has_option(self.SECTION_MAIN, 'groupsdir'):
opt_confdir = 'groupsdir'
else:
opt_confdir = 'confdir'
# keep track of loaded confdirs
loaded_confdirs = set()
confdirstr = self.config.get(self.SECTION_MAIN, opt_confdir)
for confdir in shlex.split(confdirstr):
# substitute $CFGDIR, set to the highest priority clustershell
# configuration directory that has been found
confdir = Template(confdir).safe_substitute(CFGDIR=cfg_dirname)
confdir = os.path.normpath(confdir)
if confdir in loaded_confdirs:
continue # load each confdir only once
loaded_confdirs.add(confdir)
if not os.path.isdir(confdir):
if not os.path.exists(confdir):
continue
raise GroupResolverConfigError("Defined confdir %s is not"
" a directory" % confdir)
# add sources declared in groups.conf.d file parts
for groupsfn in sorted(glob.glob('%s/*.conf' % confdir)):
grpcfg = ConfigParser()
grpcfg.read(groupsfn) # ignore files that cannot be read
self._sources_from_cfg(grpcfg, confdir)
except (NoSectionError, NoOptionError):
pass
# parse Main.autodir
try:
# keep track of loaded autodirs
loaded_autodirs = set()
autodirstr = self.config.get(self.SECTION_MAIN, 'autodir')
for autodir in shlex.split(autodirstr):
# substitute $CFGDIR, set to the highest priority clustershell
# configuration directory that has been found
autodir = Template(autodir).safe_substitute(CFGDIR=cfg_dirname)
autodir = os.path.normpath(autodir)
if autodir in loaded_autodirs:
continue # load each autodir only once
loaded_autodirs.add(autodir)
if not os.path.isdir(autodir):
if not os.path.exists(autodir):
continue
raise GroupResolverConfigError("Defined autodir %s is not"
" a directory" % autodir)
# add auto sources declared in groups.d YAML files
for autosfn in sorted(glob.glob('%s/*.yaml' % autodir)):
try:
self._sources_from_yaml(autosfn)
except IOError as exc: # same as OSError in Python 3
# in Python 3 only, we could just catch PermissionError
if exc.errno in (errno.EACCES, errno.EPERM):
# ignore YAML files that we don't have access to
LOGGER.debug(exc)
continue
except (NoSectionError, NoOptionError):
pass
# add sources declared directly in groups.conf
self._sources_from_cfg(self.config, cfg_dirname)
# parse Main.default
try:
def_sourcename = self.config.get('Main', 'default')
# warning: default_source_name is a property
self.default_source_name = def_sourcename
except (NoSectionError, NoOptionError):
pass
except GroupResolverSourceError:
if def_sourcename: # allow empty Main.default
fmt = 'Default group source not found: "%s"'
raise GroupResolverConfigError(fmt % self.config.get('Main',
'default'))
# pick random default source if not provided by config
if not self.default_source_name and self._sources:
self.default_source_name = list(self._sources)[0]
def _sources_from_cfg(self, cfg, cfgdir):
"""
Instantiate as many UpcallGroupSources needed from cfg object,
cfgdir (CWD for callbacks) and cfg filename.
"""
try:
for section in cfg.sections():
# Support grouped sections: section1,section2,section3
for srcname in section.split(','):
if srcname != self.SECTION_MAIN:
# only map is a mandatory upcall
map_upcall = cfg.get(section, 'map', raw=True)
all_upcall = list_upcall = reverse_upcall = ctime = None
if cfg.has_option(section, 'all'):
all_upcall = cfg.get(section, 'all', raw=True)
if cfg.has_option(section, 'list'):
list_upcall = cfg.get(section, 'list', raw=True)
if cfg.has_option(section, 'reverse'):
reverse_upcall = cfg.get(section, 'reverse',
raw=True)
if cfg.has_option(section, 'cache_time'):
ctime = float(cfg.get(section, 'cache_time',
raw=True))
# add new group source
self.add_source(UpcallGroupSource(srcname, map_upcall,
all_upcall,
list_upcall,
reverse_upcall,
cfgdir, ctime))
except (NoSectionError, NoOptionError, ValueError) as exc:
raise GroupResolverConfigError(str(exc))
def _sources_from_yaml(self, filepath):
"""Load source(s) from YAML file."""
for source in YAMLGroupLoader(filepath):
self.add_source(source)
|