"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "salt/cli/salt.py" between
salt-3002.1.tar.gz and salt-3002.2.tar.gz

About: SaltStack is a systems management software for data center automation, cloud orchestration, server provisioning, configuration management and more. Community version.

salt.py  (salt-3002.1):salt.py  (salt-3002.2)
# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import, print_function, unicode_literals
import os import os
import sys import sys
# Import Salt libs
import salt.defaults.exitcodes import salt.defaults.exitcodes
import salt.log import salt.log
import salt.utils.job import salt.utils.job
import salt.utils.parsers import salt.utils.parsers
import salt.utils.stringutils import salt.utils.stringutils
from salt.exceptions import ( from salt.exceptions import (
AuthenticationError, AuthenticationError,
AuthorizationError, AuthorizationError,
EauthAuthenticationError, EauthAuthenticationError,
LoaderError, LoaderError,
SaltClientError, SaltClientError,
SaltInvocationError, SaltInvocationError,
SaltSystemExit, SaltSystemExit,
) )
# Import 3rd-party libs
from salt.ext import six
from salt.utils.args import yamlify_arg from salt.utils.args import yamlify_arg
from salt.utils.verify import verify_log from salt.utils.verify import verify_log
sys.modules["pkg_resources"] = None sys.modules["pkg_resources"] = None
class SaltCMD(salt.utils.parsers.SaltCMDOptionParser): class SaltCMD(salt.utils.parsers.SaltCMDOptionParser):
""" """
The execution of a salt command happens here The execution of a salt command happens here
""" """
skipping to change at line 61 skipping to change at line 52
# We don't need to bail on config file permission errors # We don't need to bail on config file permission errors
# if the CLI process is run with the -a flag # if the CLI process is run with the -a flag
skip_perm_errors = self.options.eauth != "" skip_perm_errors = self.options.eauth != ""
self.local_client = salt.client.get_local_client( self.local_client = salt.client.get_local_client(
self.get_config_file_path(), self.get_config_file_path(),
skip_perm_errors=skip_perm_errors, skip_perm_errors=skip_perm_errors,
auto_reconnect=True, auto_reconnect=True,
) )
except SaltClientError as exc: except SaltClientError as exc:
self.exit(2, "{0}\n".format(exc)) self.exit(2, "{}\n".format(exc))
return return
if self.options.batch or self.options.static: if self.options.batch or self.options.static:
# _run_batch() will handle all output and # _run_batch() will handle all output and
# exit with the appropriate error condition # exit with the appropriate error condition
# Execution will not continue past this point # Execution will not continue past this point
# in batch mode. # in batch mode.
self._run_batch() self._run_batch()
return return
skipping to change at line 97 skipping to change at line 88
} }
if "token" in self.config: if "token" in self.config:
import salt.utils.files import salt.utils.files
try: try:
with salt.utils.files.fopen( with salt.utils.files.fopen(
os.path.join(self.config["cachedir"], ".root_key"), "r" os.path.join(self.config["cachedir"], ".root_key"), "r"
) as fp_: ) as fp_:
kwargs["key"] = fp_.readline() kwargs["key"] = fp_.readline()
except IOError: except OSError:
kwargs["token"] = self.config["token"] kwargs["token"] = self.config["token"]
kwargs["delimiter"] = self.options.delimiter kwargs["delimiter"] = self.options.delimiter
if self.selected_target_option: if self.selected_target_option:
kwargs["tgt_type"] = self.selected_target_option kwargs["tgt_type"] = self.selected_target_option
else: else:
kwargs["tgt_type"] = "glob" kwargs["tgt_type"] = "glob"
# If batch_safe_limit is set, check minions matching target and # If batch_safe_limit is set, check minions matching target and
skipping to change at line 161 skipping to change at line 152
kwargs["token"] = tok.get("token", "") kwargs["token"] = tok.get("token", "")
if not res: if not res:
sys.stderr.write("ERROR: Authentication failed\n") sys.stderr.write("ERROR: Authentication failed\n")
sys.exit(2) sys.exit(2)
kwargs.update(res) kwargs.update(res)
kwargs["eauth"] = self.options.eauth kwargs["eauth"] = self.options.eauth
if self.config["async"]: if self.config["async"]:
jid = self.local_client.cmd_async(**kwargs) jid = self.local_client.cmd_async(**kwargs)
salt.utils.stringutils.print_cli( salt.utils.stringutils.print_cli(
"Executed command with job ID: {0}".format(jid) "Executed command with job ID: {}".format(jid)
) )
return return
# local will be None when there was an error # local will be None when there was an error
if not self.local_client: if not self.local_client:
return return
retcodes = [] retcodes = []
errors = [] errors = []
skipping to change at line 238 skipping to change at line 229
sys.stderr.write("ERROR: Minions returned with non-zero exit cod e\n") sys.stderr.write("ERROR: Minions returned with non-zero exit cod e\n")
sys.exit(salt.defaults.exitcodes.EX_GENERIC) sys.exit(salt.defaults.exitcodes.EX_GENERIC)
except ( except (
AuthenticationError, AuthenticationError,
AuthorizationError, AuthorizationError,
SaltInvocationError, SaltInvocationError,
EauthAuthenticationError, EauthAuthenticationError,
SaltClientError, SaltClientError,
) as exc: ) as exc:
ret = six.text_type(exc) ret = str(exc)
self._output_ret(ret, "", retcode=1) self._output_ret(ret, "", retcode=1)
def _preview_target(self): def _preview_target(self):
""" """
Return a list of minions from a given target Return a list of minions from a given target
""" """
return self.local_client.gather_minions( return self.local_client.gather_minions(
self.config["tgt"], self.selected_target_option or "glob" self.config["tgt"], self.selected_target_option or "glob"
) )
skipping to change at line 303 skipping to change at line 294
self.config["batch"] = self.options.batch self.config["batch"] = self.options.batch
batch = salt.cli.batch.Batch( batch = salt.cli.batch.Batch(
self.config, eauth=eauth, parser=self.options self.config, eauth=eauth, parser=self.options
) )
except SaltClientError: except SaltClientError:
# We will print errors to the console further down the stack # We will print errors to the console further down the stack
sys.exit(1) sys.exit(1)
# Printing the output is already taken care of in run() itself # Printing the output is already taken care of in run() itself
retcode = 0 retcode = 0
for res in batch.run(): for res in batch.run():
for ret in six.itervalues(res): for ret in res.values():
job_retcode = salt.utils.job.get_retcode(ret) job_retcode = salt.utils.job.get_retcode(ret)
if job_retcode > retcode: if job_retcode > retcode:
# Exit with the highest retcode we find # Exit with the highest retcode we find
retcode = job_retcode retcode = job_retcode
sys.exit(retcode) sys.exit(retcode)
def _print_errors_summary(self, errors): def _print_errors_summary(self, errors):
if errors: if errors:
salt.utils.stringutils.print_cli("\n") salt.utils.stringutils.print_cli("\n")
salt.utils.stringutils.print_cli("---------------------------") salt.utils.stringutils.print_cli("---------------------------")
skipping to change at line 333 skipping to change at line 324
return_counter = 0 return_counter = 0
not_return_counter = 0 not_return_counter = 0
not_return_minions = [] not_return_minions = []
not_response_minions = [] not_response_minions = []
not_connected_minions = [] not_connected_minions = []
failed_minions = [] failed_minions = []
for each_minion in ret: for each_minion in ret:
minion_ret = ret[each_minion] minion_ret = ret[each_minion]
if isinstance(minion_ret, dict) and "ret" in minion_ret: if isinstance(minion_ret, dict) and "ret" in minion_ret:
minion_ret = ret[each_minion].get("ret") minion_ret = ret[each_minion].get("ret")
if isinstance(minion_ret, six.string_types) and minion_ret.startswit h( if isinstance(minion_ret, str) and minion_ret.startswith(
"Minion did not return" "Minion did not return"
): ):
if "Not connected" in minion_ret: if "Not connected" in minion_ret:
not_connected_minions.append(each_minion) not_connected_minions.append(each_minion)
elif "No response" in minion_ret: elif "No response" in minion_ret:
not_response_minions.append(each_minion) not_response_minions.append(each_minion)
not_return_counter += 1 not_return_counter += 1
not_return_minions.append(each_minion) not_return_minions.append(each_minion)
else: else:
return_counter += 1 return_counter += 1
if self._get_retcode(ret[each_minion]): if self._get_retcode(ret[each_minion]):
failed_minions.append(each_minion) failed_minions.append(each_minion)
salt.utils.stringutils.print_cli("\n") salt.utils.stringutils.print_cli("\n")
salt.utils.stringutils.print_cli("-------------------------------------- -----") salt.utils.stringutils.print_cli("-------------------------------------- -----")
salt.utils.stringutils.print_cli("Summary") salt.utils.stringutils.print_cli("Summary")
salt.utils.stringutils.print_cli("-------------------------------------- -----") salt.utils.stringutils.print_cli("-------------------------------------- -----")
salt.utils.stringutils.print_cli( salt.utils.stringutils.print_cli(
"# of minions targeted: {0}".format(return_counter + not_return_coun ter) "# of minions targeted: {}".format(return_counter + not_return_count er)
) )
salt.utils.stringutils.print_cli( salt.utils.stringutils.print_cli(
"# of minions returned: {0}".format(return_counter) "# of minions returned: {}".format(return_counter)
) )
salt.utils.stringutils.print_cli( salt.utils.stringutils.print_cli(
"# of minions that did not return: {0}".format(not_return_counter) "# of minions that did not return: {}".format(not_return_counter)
) )
salt.utils.stringutils.print_cli( salt.utils.stringutils.print_cli(
"# of minions with errors: {0}".format(len(failed_minions)) "# of minions with errors: {}".format(len(failed_minions))
) )
if self.options.verbose: if self.options.verbose:
if not_connected_minions: if not_connected_minions:
salt.utils.stringutils.print_cli( salt.utils.stringutils.print_cli(
"Minions not connected: {0}".format(" ".join(not_connected_m inions)) "Minions not connected: {}".format(" ".join(not_connected_mi nions))
) )
if not_response_minions: if not_response_minions:
salt.utils.stringutils.print_cli( salt.utils.stringutils.print_cli(
"Minions not responding: {0}".format(" ".join(not_response_m inions)) "Minions not responding: {}".format(" ".join(not_response_mi nions))
) )
if failed_minions: if failed_minions:
salt.utils.stringutils.print_cli( salt.utils.stringutils.print_cli(
"Minions with failures: {0}".format(" ".join(failed_minions) ) "Minions with failures: {}".format(" ".join(failed_minions))
) )
salt.utils.stringutils.print_cli("-------------------------------------- -----") salt.utils.stringutils.print_cli("-------------------------------------- -----")
def _progress_end(self, out): def _progress_end(self, out):
import salt.output import salt.output
salt.output.progress_end(self.progress_bar) salt.output.progress_end(self.progress_bar)
def _progress_ret(self, progress, out): def _progress_ret(self, progress, out):
""" """
skipping to change at line 422 skipping to change at line 413
sys.stderr.write("ERROR: No return received\n") sys.stderr.write("ERROR: No return received\n")
sys.exit(2) sys.exit(2)
def _format_ret(self, full_ret): def _format_ret(self, full_ret):
""" """
Take the full return data and format it to simple output Take the full return data and format it to simple output
""" """
ret = {} ret = {}
out = "" out = ""
retcode = 0 retcode = 0
for key, data in six.iteritems(full_ret): for key, data in full_ret.items():
ret[key] = data["ret"] ret[key] = data["ret"]
if "out" in data: if "out" in data:
out = data["out"] out = data["out"]
ret_retcode = self._get_retcode(data) ret_retcode = self._get_retcode(data)
if ret_retcode > retcode: if ret_retcode > retcode:
retcode = ret_retcode retcode = ret_retcode
return ret, out, retcode return ret, out, retcode
def _get_retcode(self, ret): def _get_retcode(self, ret):
""" """
Determine a retcode for a given return Determine a retcode for a given return
""" """
retcode = 0 retcode = 0
# if there is a dict with retcode, use that # if there is a dict with retcode, use that
if isinstance(ret, dict) and ret.get("retcode", 0) != 0: if isinstance(ret, dict) and ret.get("retcode", 0) != 0:
if isinstance(ret.get("retcode", 0), dict): if isinstance(ret.get("retcode", 0), dict):
return max(six.itervalues(ret.get("retcode", {0: 0}))) return max(ret.get("retcode", {0: 0}).values())
return ret["retcode"] return ret["retcode"]
# if its a boolean, False means 1 # if its a boolean, False means 1
elif isinstance(ret, bool) and not ret: elif isinstance(ret, bool) and not ret:
return 1 return 1
return retcode return retcode
def _format_error(self, minion_error): def _format_error(self, minion_error):
for minion, error_doc in six.iteritems(minion_error): for minion, error_doc in minion_error.items():
error = "Minion [{0}] encountered exception '{1}'".format( error = "Minion [{}] encountered exception '{}'".format(
minion, error_doc["message"] minion, error_doc["message"]
) )
return error return error
def _print_docs(self, ret): def _print_docs(self, ret):
""" """
Print out the docstrings for all of the functions on the minions Print out the docstrings for all of the functions on the minions
""" """
import salt.output import salt.output
docs = {} docs = {}
if not ret: if not ret:
self.exit(2, "No minions found to gather docs from\n") self.exit(2, "No minions found to gather docs from\n")
if isinstance(ret, six.string_types): if isinstance(ret, str):
self.exit(2, "{0}\n".format(ret)) self.exit(2, "{}\n".format(ret))
for host in ret: for host in ret:
if isinstance(ret[host], six.string_types) and ( if isinstance(ret[host], str) and (
ret[host].startswith("Minion did not return") ret[host].startswith("Minion did not return")
or ret[host] == "VALUE_TRIMMED" or ret[host] == "VALUE_TRIMMED"
): ):
continue continue
for fun in ret[host]: for fun in ret[host]:
if fun not in docs and ret[host][fun]: if fun not in docs and ret[host][fun]:
docs[fun] = ret[host][fun] docs[fun] = ret[host][fun]
if self.options.output: if self.options.output:
for fun in sorted(docs): for fun in sorted(docs):
salt.output.display_output({fun: docs[fun]}, "nested", self.conf ig) salt.output.display_output({fun: docs[fun]}, "nested", self.conf ig)
else: else:
for fun in sorted(docs): for fun in sorted(docs):
salt.utils.stringutils.print_cli("{0}:".format(fun)) salt.utils.stringutils.print_cli("{}:".format(fun))
salt.utils.stringutils.print_cli(docs[fun]) salt.utils.stringutils.print_cli(docs[fun])
salt.utils.stringutils.print_cli("") salt.utils.stringutils.print_cli("")
 End of changes. 22 change blocks. 
30 lines changed or deleted 21 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)