LocalClient should not use print()
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 15.7k
- Forks
- 5.6k
- Avg merge
- 2d 44m
- Merged PRs (30d)
- 80
Description
Description of Issue/Question
I want to use salt.client.LocalClient() class in a script which iterates over a bunch of registered environemnts and runs commands.
As we have many Saltmaster it happens that a registered environment does not return data as no minion is connected the current Saltmaster.
Unfortunately LocalClient does not raise an exception which I could catch when no minion is connected for an environment but prints a message directly:
https://github.com/saltstack/salt/blob/1c9ebe2005daf9d79a8d70eca9de882d9ffb9128/salt/client/__init__.py#L282
There are other cases where print is used too:
- https://github.com/saltstack/salt/blob/1c9ebe2005daf9d79a8d70eca9de882d9ffb9128/salt/client/__init__.py#L213
- https://github.com/saltstack/salt/blob/1c9ebe2005daf9d79a8d70eca9de882d9ffb9128/salt/client/__init__.py#L273
- https://github.com/saltstack/salt/blob/1c9ebe2005daf9d79a8d70eca9de882d9ffb9128/salt/client/__init__.py#L1585
- https://github.com/saltstack/salt/blob/1c9ebe2005daf9d79a8d70eca9de882d9ffb9128/salt/client/__init__.py#L1586
- https://github.com/saltstack/salt/blob/1c9ebe2005daf9d79a8d70eca9de882d9ffb9128/salt/client/__init__.py#L1588
IMHO all those should be changed to either use the 'log'-object or to raise an exception based on the situation.
Examples:
- L213: should use 'log.error()' as an error happened but the whole process should not be stopped
- L273: should raise an exception as the call itself did not produce a response but the user should be able to react to this event, which currently is not possible as it's only a message on the stdout
Is there a reason why LocalClient uses print() instead of 'log'-object and exceptions?
Setup
Install salt-master and run with default configuration is sufficient.
Steps to Reproduce Issue
- run this script:
from salt.client import LocalClient
try:
LocalClient().cmd('G@Minions:Empty', 'test.ping', tgt_type='compound', expr_form='compound')
except:
print('error should be exceptable')
Versions Report
All version including 'develop' branch
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in salt/client/init.py at the print() locations linked in the issue, then run the provided LocalClient().cmd reproduction against an empty target. Determine which cases should log and which should raise an exception, and ensure callers can react without messages being written directly to stdout.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100