Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if filepath.endswith('.zip'):
self.log.debug('File Type: zip')
opener, mode = zipfile.ZipFile, 'r'
elif filepath.endswith('.tar.gz') or filepath.endswith('.tgz'):
self.log.debug('File Type: GZip Tar')
opener, mode = tarfile.open, 'r:gz'
elif filepath.endswith('.tar.bz2') or filepath.endswith('.tbz'):
self.log.debug('File Type: Bzip Tar')
opener, mode = tarfile.open, 'r:bz2'
else:
msg = (
'Could not extract "{0}" as no appropriate extractor is found.'
.format(filepath)
)
self.log.critical(msg)
raise WatchmakerException(msg)
if create_dir:
to_directory = os.sep.join((
to_directory,
'.'.join(filepath.split(os.sep)[-1].split('.')[:-1])
))
try:
os.makedirs(to_directory)
except OSError:
if not os.path.isdir(to_directory):
msg = 'Unable create directory - {0}'.format(to_directory)
self.log.critical(msg)
raise
cwd = os.getcwd()
self.log.critical(
'Failed to read /etc/system-release. Cannot determine system '
'distribution!'
)
raise
# Search the release file for a match against _supported_dists
matched = self.DIST_PATTERN.search(release.lower())
if matched is None:
# Release not supported, exit with error
msg = (
'Unsupported OS distribution. OS must be one of: {0}'
.format(', '.join(self.SUPPORTED_DISTS))
)
self.log.critical(msg)
raise WatchmakerException(msg)
# Assign dist,version from the match groups tuple, removing any spaces
dist, version = (x.replace(' ', '') for x in matched.groups())
# Determine el_version
if dist == 'amazon':
el_version = self._get_amazon_el_version(version)
else:
el_version = version.split('.')[0]
if el_version is None:
msg = (
'Unsupported OS version! dist = {0}, version = {1}.'
.format(dist, version)
)
self.log.critical(msg)
Returns:
:obj:`dict`:
Dictionary containing three keys: ``retcode`` (:obj:`int`),
``stdout`` (:obj:`bytes`), and ``stderr`` (:obj:`bytes`).
"""
ret = {
'retcode': 0,
'stdout': b'',
'stderr': b''
}
if not isinstance(cmd, list):
msg = 'Command is not a list: {0}'.format(cmd)
self.log.critical(msg)
raise WatchmakerException(msg)
self.log.debug('Command: %s', ' '.join(cmd))
# If running as a standalone, PyInstaller will have modified the
# LD_LIBRARY_PATH to point to standalone libraries. If there were a
# value at runtime, PyInstaller will create LD_LIBRARY_PATH_ORIG. In
# order for salt to run correctly, LD_LIBRARY_PATH has to be fixed.
kwargs = {}
env = dict(os.environ)
lib_path_key = 'LD_LIBRARY_PATH'
if env.get(lib_path_key) is not None:
lib_path_orig_value = env.get(lib_path_key + '_ORIG')
if lib_path_orig_value is None:
self.config_path = self.default_config
else:
self.log.info('User supplied config being used.')
# Get the raw config data
data = ''
if self._validate_url(self.config_path):
data = self.get_config_data(True, self.config_path)
elif self.config_path and not os.path.exists(self.config_path):
msg = (
'User supplied config {0} does not exist. Please '
'double-check your config path or use the default config '
'path.'.format(self.config_path)
)
self.log.critical(msg)
raise WatchmakerException(msg)
else:
data = self.get_config_data(False, self.config_path)
config_full = yaml.safe_load(data)
try:
config_all = config_full.get('all', [])
config_system = config_full.get(self.system, [])
except AttributeError:
msg = 'Malformed config file. Must be a dictionary.'
self.log.critical(msg)
raise
# If both config and config_system are empty, raise
if not config_system and not config_all:
msg = 'Malformed config file. No workers for this system.'
self.log.critical(msg)
def _set_system_params(self):
"""Set OS-specific attributes."""
if 'linux' in self.system:
self.system_drive = '/'
self.workers_manager = LinuxWorkersManager
self.system_params = self._get_linux_system_params()
elif 'windows' in self.system:
self.system_drive = os.environ['SYSTEMDRIVE']
self.workers_manager = WindowsWorkersManager
self.system_params = self._get_windows_system_params()
else:
msg = 'System, {0}, is not recognized?'.format(self.system)
self.log.critical(msg)
raise WatchmakerException(msg)
if self.log_dir:
self.system_params['logdir'] = self.log_dir
def _validate_config(self):
"""Validate the config is properly formed."""
if not self.yumrepomap:
self.log.warning('`yumrepomap` did not exist or was empty.')
elif not isinstance(self.yumrepomap, list):
msg = '`yumrepomap` must be a list!'
self.log.critical(msg)
raise WatchmakerException(msg)
data = self.get_config_data(False, self.config_path)
config_full = yaml.safe_load(data)
try:
config_all = config_full.get('all', [])
config_system = config_full.get(self.system, [])
except AttributeError:
msg = 'Malformed config file. Must be a dictionary.'
self.log.critical(msg)
raise
# If both config and config_system are empty, raise
if not config_system and not config_all:
msg = 'Malformed config file. No workers for this system.'
self.log.critical(msg)
raise WatchmakerException(msg)
# Merge the config data, preserving the listed order of workers
config = collections.OrderedDict()
for worker in config_system + config_all:
try:
# worker is a single-key dict, where the key is the name of the
# worker and the value is the worker parameters. we need to
# test if the worker is already in the config, but a dict is
# is not hashable so cannot be tested directly with
# `if worker not in config`. this bit of ugliness extracts the
# key and its value so we can use them directly.
worker_name, worker_config = list(worker.items())[0]
if worker_name not in config:
# Add worker to config
config[worker_name] = {'config': worker_config}
self.log.debug('%s config: %s', worker_name, worker_config)
self.log.info(
'Applying the user-defined list of states, states=%s',
states
)
cmd.extend(['state.sls', states])
if exclude:
cmd.extend(['exclude={0}'.format(exclude)])
ret = self.run_salt(cmd, log_pipe='stderr', raise_error=False)
if ret['retcode'] != 0:
failed_states = self._get_failed_states(
ast.literal_eval(ret['stdout'].decode('utf-8')))
if failed_states:
raise WatchmakerException(
yaml.safe_dump(
{
'Salt state execution failed':
failed_states
},
default_flow_style=False,
indent=4
)
)
self.log.info('Salt states all applied successfully!')