Skip to content

Commit

Permalink
actually show plugin config warnings/deprecations
Browse files Browse the repository at this point in the history
previouslly we recorded but did not show to avoid spam
since we could not dedup from forks, that was already
fixed in another PR so now we can show/display them.

Also:
  * funcitonalize deprecation msg construct from docs
  * reuse formatting func in cli
  * normalize alternatives: most of the code used intended plural
    but some and most data/tests used the singular
  • Loading branch information
bcoca committed Feb 1, 2024
1 parent e458cba commit 7824434
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 47 deletions.
15 changes: 6 additions & 9 deletions lib/ansible/cli/doc.py
Expand Up @@ -1350,16 +1350,13 @@ def get_man_text(doc, collection_name='', plugin_type=''):
if doc.get('deprecated', False):
text.append(_format("DEPRECATED: ", 'bold', 'DEP'))
if isinstance(doc['deprecated'], dict):
if 'removed_at_date' in doc['deprecated']:
text.append(
"\tReason: %(why)s\n\tWill be removed in a release after %(removed_at_date)s\n\tAlternatives: %(alternative)s" % doc.pop('deprecated')
)
else:
if 'version' in doc['deprecated'] and 'removed_in' not in doc['deprecated']:
doc['deprecated']['removed_in'] = doc['deprecated']['version']
text.append("\tReason: %(why)s\n\tWill be removed in: Ansible %(removed_in)s\n\tAlternatives: %(alternative)s" % doc.pop('deprecated'))
if 'removed_at_date' not in doc['deprecated'] and 'version' in doc['deprecated'] and 'removed_in' not in doc['deprecated']:
doc['deprecated']['removed_in'] = doc['deprecated']['version']
text.append('\t' + C.config.get_deprecated_msg_from_config(doc['deprecated'], True))
else:
text.append("%s" % doc.pop('deprecated'))
text.append("%s" % doc['deprecated'])
del doc['deprecated']
text.append("\n")

if doc.pop('has_action', False):
text.append("")
Expand Down
14 changes: 14 additions & 0 deletions lib/ansible/config/manager.py
Expand Up @@ -606,3 +606,17 @@ def initialize_plugin_configuration_definitions(self, plugin_type, name, defs):
self._plugins[plugin_type] = {}

self._plugins[plugin_type][name] = defs

@staticmethod
def get_deprecated_msg_from_config(dep_docs, include_removal=False):

removal = ''
if include_removal:
if 'removed_at_date' in dep_docs:
removal = "Will be removed in a release after %(removed_at_date)s\n\t"
else:
removal = "Will be removed in: Ansible %(removed_in)s\n\t"

msg = "Reason: %(why)s\n\t" + removal + "Alternatives: %(alternatives)s"

return msg % dep_docs
31 changes: 26 additions & 5 deletions lib/ansible/constants.py
Expand Up @@ -15,6 +15,10 @@
from ansible.release import __version__
from ansible.utils.fqcn import add_internal_fqcns

# initialize config manager/config data to read/store global settings
# and generate 'pseudo constants' for app consumption.
config = ConfigManager()


def _warning(msg):
''' display is not guaranteed here, nor it being the full class, but try anyways, fallback to sys.stderr.write '''
Expand All @@ -36,6 +40,26 @@ def _deprecated(msg, version):
sys.stderr.write(' [DEPRECATED] %s, to be removed in %s\n' % (msg, version))


def handle_config_noise(display=None):

if display is not None:
w = display.warning
d = display.deprecated
else:
w = _warning
d = _deprecated

while config.WARNINGS:
warn = config.WARNINGS.pop()
w(warn)

while config.DEPRECATED:
# tuple with name and options
dep = config.DEPRECATED.pop()
msg = config.get_deprecated_msg_from_config(dep[1])
d(msg, version=dep[1]['version'])


def set_constant(name, value, export=vars()):
''' sets constants and returns resolved options dict '''
export[name] = value
Expand Down Expand Up @@ -178,11 +202,8 @@ def __getitem__(self, y):
)

# POPULATE SETTINGS FROM CONFIG ###
config = ConfigManager()

# Generate constants from config
for setting in config.get_configuration_definitions():
set_constant(setting, config.get_config_value(setting, variables=vars()))

for warn in config.WARNINGS:
_warning(warn)
# emit any warnings or deprecations
handle_config_noise()
2 changes: 2 additions & 0 deletions lib/ansible/plugins/__init__.py
Expand Up @@ -92,6 +92,7 @@ def get_options(self, hostvars=None):

def set_option(self, option, value):
self._options[option] = C.config.get_config_value(option, plugin_type=self.plugin_type, plugin_name=self._load_name, direct={option: value})
C.handle_config_noise(display)

def set_options(self, task_keys=None, var_options=None, direct=None):
'''
Expand All @@ -108,6 +109,7 @@ def set_options(self, task_keys=None, var_options=None, direct=None):
if self.allow_extras and var_options and '_extras' in var_options:
# these are largely unvalidated passthroughs, either plugin or underlying API will validate
self._options['_extras'] = var_options['_extras']
C.handle_config_noise(display)

def has_option(self, option):
if not self._options:
Expand Down
Expand Up @@ -23,7 +23,7 @@
- key: fact_caching_connection
section: defaults
deprecated:
alternative: none
alternatives: none
why: Test deprecation
version: '2.0.0'
_prefix:
Expand All @@ -35,7 +35,7 @@
- key: fact_caching_prefix
section: defaults
deprecated:
alternative: none
alternatives: none
why: Another test deprecation
removed_at_date: '2050-01-01'
_timeout:
Expand All @@ -50,7 +50,7 @@
- name: notsjonfile_fact_caching_timeout
version_added: 1.5.0
deprecated:
alternative: do not use a variable
alternatives: do not use a variable
why: Test deprecation
version: '3.0.0'
type: integer
Expand Down
Expand Up @@ -12,7 +12,7 @@
description:
- this is a noop
deprecated:
alternative: Use some other lookup
alternatives: Use some other lookup
why: Test deprecation
removed_in: '3.0.0'
extends_documentation_fragment:
Expand Down
Expand Up @@ -12,7 +12,7 @@
- Ansible Core Team
version_added: 1.0.0
deprecated:
alternative: Use some other module
alternatives: Use some other module
why: Test deprecation
removed_in: '3.0.0'
options:
Expand Down Expand Up @@ -41,7 +41,7 @@
- name: TEST_ENV
version_added: 1.0.0
deprecated:
alternative: none
alternatives: none
why: Test deprecation
removed_in: '2.0.0'
version: '2.0.0'
Expand Down
Expand Up @@ -22,7 +22,7 @@
- key: fact_caching_connection
section: defaults
deprecated:
alternative: none
alternatives: none
why: Test deprecation
version: '2.0.0'
_prefix:
Expand All @@ -34,7 +34,7 @@
- key: fact_caching_prefix
section: defaults
deprecated:
alternative: none
alternatives: none
why: Another test deprecation
removed_at_date: '2050-01-01'
_timeout:
Expand All @@ -49,7 +49,7 @@
- name: notsjonfile_fact_caching_timeout
version_added: 1.5.0
deprecated:
alternative: do not use a variable
alternatives: do not use a variable
why: Test deprecation
version: '3.0.0'
type: integer
Expand Down
Expand Up @@ -11,7 +11,7 @@
description:
- this is a noop
deprecated:
alternative: Use some other lookup
alternatives: Use some other lookup
why: Test deprecation
removed_in: '3.0.0'
extends_documentation_fragment:
Expand Down
Expand Up @@ -17,7 +17,7 @@
- Ansible Core Team
version_added: 1.0.0
deprecated:
alternative: Use some other module
alternatives: Use some other module
why: Test deprecation
removed_in: '3.0.0'
options:
Expand Down Expand Up @@ -46,7 +46,7 @@
- name: TEST_ENV
version_added: 1.0.0
deprecated:
alternative: none
alternatives: none
why: Test deprecation
removed_in: '2.0.0'
version: '2.0.0'
Expand Down
Expand Up @@ -9,7 +9,7 @@ class ModuleDocFragment(object):
DOCUMENTATION = r'''
options: {}
deprecated:
alternative: Use some other module
alternatives: Use some other module
why: Test deprecation
removed_in: '3.0.0'
'''
Expand Up @@ -18,29 +18,29 @@ class ModuleDocFragment(object):
section: bar
version_added: 1.1.0
deprecated:
alternative: none
alternatives: none
why: Test deprecation
version: '3.0.0'
env:
- name: FOO_BAR
version_added: 1.2.0
deprecated:
alternative: none
alternatives: none
why: Test deprecation
removed_at_date: 2020-01-31
vars:
- name: foobar
version_added: 1.3.0
deprecated:
alternative: none
alternatives: none
why: Test deprecation
removed_at_date: 2040-12-31
testcol2depr:
description:
- A plugin option taken from testcol2 that is deprecated
type: str
deprecated:
alternative: none
alternatives: none
why: Test option deprecation
version: '2.0.0'
'''
Expand Up @@ -11,7 +11,7 @@
author:
- Ansible Core Team
deprecated:
alternative: new_module
alternatives: new_module
why: Updated module released with more functionality
removed_at_date: '2022-06-01'
removed_in: '2.14'
Expand Down
Expand Up @@ -12,7 +12,7 @@
version_added: "2.14"
deprecated:
why: reasons
alternative: other thing
alternatives: other thing
removed_in: "2.16"
removed_from_collection: "ansible.legacy"
options: {}
Expand Down
Expand Up @@ -6,7 +6,7 @@ DOCUMENTATION:
version_added: "2.14"
deprecated:
why: reasons
alternative: use other thing
alternatives: use other thing
removed_in: "2.16"
removed_from_collection: "ansible.legacy"
options: {}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/targets/ansible-doc/noop.output
Expand Up @@ -4,7 +4,7 @@
"author": "Ansible core team",
"collection": "testns.testcol",
"deprecated": {
"alternative": "Use some other lookup",
"alternatives": "Use some other lookup",
"removed_from_collection": "testns.testcol",
"removed_in": "3.0.0",
"why": "Test deprecation"
Expand Down
Expand Up @@ -3,7 +3,7 @@
"doc": {
"collection": "testns.testcol",
"deprecated": {
"alternative": "Use some other module",
"alternatives": "Use some other module",
"removed_from_collection": "testns.testcol2",
"removed_in": "3.0.0",
"why": "Test deprecation"
Expand Down
14 changes: 7 additions & 7 deletions test/integration/targets/ansible-doc/notjsonfile.output
Expand Up @@ -11,7 +11,7 @@
"options": {
"_prefix": {
"deprecated": {
"alternative": "none",
"alternatives": "none",
"collection_name": "testns.testcol",
"removed_at_date": "2050-01-01",
"why": "Another test deprecation"
Expand Down Expand Up @@ -49,7 +49,7 @@
"vars": [
{
"deprecated": {
"alternative": "do not use a variable",
"alternatives": "do not use a variable",
"collection_name": "testns.testcol",
"version": "3.0.0",
"why": "Test deprecation"
Expand All @@ -74,7 +74,7 @@
"ini": [
{
"deprecated": {
"alternative": "none",
"alternatives": "none",
"collection_name": "testns.testcol",
"version": "2.0.0",
"why": "Test deprecation"
Expand All @@ -87,7 +87,7 @@
},
"testcol2depr": {
"deprecated": {
"alternative": "none",
"alternatives": "none",
"collection_name": "testns.testcol2",
"version": "2.0.0",
"why": "Test option deprecation"
Expand All @@ -104,7 +104,7 @@
"env": [
{
"deprecated": {
"alternative": "none",
"alternatives": "none",
"collection_name": "testns.testcol2",
"removed_at_date": "2020-01-31",
"why": "Test deprecation"
Expand All @@ -117,7 +117,7 @@
"ini": [
{
"deprecated": {
"alternative": "none",
"alternatives": "none",
"collection_name": "testns.testcol2",
"version": "3.0.0",
"why": "Test deprecation"
Expand All @@ -132,7 +132,7 @@
"vars": [
{
"deprecated": {
"alternative": "none",
"alternatives": "none",
"collection_name": "testns.testcol2",
"removed_at_date": "2040-12-31",
"why": "Test deprecation"
Expand Down
Expand Up @@ -14,7 +14,7 @@ OPTIONS (= inicates it is required):
env:
- added_in: version 1.0.0 of ansible-core
deprecated:
alternative: none
alternatives: none
removed_in: 2.0.0
version: 2.0.0
why: Test deprecation
Expand Down
Expand Up @@ -20,7 +20,7 @@ OPTIONS (= inicates it is required):
set_via:
env:
- deprecated:
alternative: none
alternatives: none
removed_in: 2.0.0
version: 2.0.0
why: Test deprecation
Expand Down

0 comments on commit 7824434

Please sign in to comment.