salt icon indicating copy to clipboard operation
salt copied to clipboard

[BUG] 3006.7 broke salt.cp.get_file_str (and probably other functions in the cp module)

Open Lego3 opened this issue 2 years ago • 7 comments

Description I updated to 3006.7, and now at least the function salt.cp.get_file_str doesn't work when run in a pillar as jinja code. Let's say this is my_pillar.sls:

{% set some_path = "/mnt/my_file.txt" %}

{% set file_content = salt.cp.get_file_str(some_path ) %}

Then, if I try to do e.g. salt my_machine state.apply I will get

my_machine:
    Data failed to compile:
----------
    Pillar failed to render with the following messages:
----------
    Rendering SLS 'my_pillar' failed. Please see master log for details.

In the master log there is

Traceback (most recent call last):
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/templates.py", line 476, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/opt/saltstack/salt/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 6, in top-level template code
  File "/opt/saltstack/salt/lib/python3.10/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 159, in __call__
    ret = self.loader.run(run_func, *args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 1245, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 1260, in _run_as
    return _func_or_method(*args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/cp.py", line 459, in get_file_str
    fn_ = cache_file(path, saltenv)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/cp.py", line 555, in cache_file
    with _client() as client:
AttributeError: __enter__

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/pillar/__init__.py", line 944, in render_pstate
    state = compile_template(
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/template.py", line 99, in compile_template
    ret = render(input_data, saltenv, sls, **render_kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 159, in __call__
    ret = self.loader.run(run_func, *args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 1245, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 1260, in _run_as
    return _func_or_method(*args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/renderers/jinja.py", line 62, in render
    tmp_data = salt.utils.templates.JINJA(
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/templates.py", line 219, in render_tmpl
    output = render_str(tmplstr, context, tmplpath)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/templates.py", line 522, in render_jinja_tmpl
    raise SaltRenderError(
salt.exceptions.SaltRenderError: Jinja error: __enter__
Traceback (most recent call last):
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/templates.py", line 476, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/opt/saltstack/salt/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 6, in top-level template code
  File "/opt/saltstack/salt/lib/python3.10/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 159, in __call__
    ret = self.loader.run(run_func, *args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 1245, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/loader/lazy.py", line 1260, in _run_as
    return _func_or_method(*args, **kwargs)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/cp.py", line 459, in get_file_str
    fn_ = cache_file(path, saltenv)
  File "/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/cp.py", line 555, in cache_file
    with _client() as client:
AttributeError: __enter__

In 3006.6 this worked just fine. I looked in the source code and found this change: https://github.com/saltstack/salt/commit/8223a27948eacd5d51743a302415a35022e3838f#diff-63034e752393e64b82defaf83baabfd9b9431cc9c4826f35216fa7267dd894c5L166

By undoing that particular change everything works again, but the error message is put in the log all the time, so I suspect that is not the root cause. I did see in the changelog for example "Fixed an issue where fileclient requests during Pillar rendering cause fileserver backends to be needlessly refreshed. #65990". Might that be the cause?

Setup Salt-master and salt-minion running on the same machine, and thus on the same version.

Steps to Reproduce the behavior See description above.

Expected behavior Everything works fine in 3006.6 and I expected it to still work in 3006.7. Unfortunately the Salt releases are very aggressively pushed to repos, removing all older versions. Is there any place I can still find 3006.6, or that is completely gone now?

Versions Report Yesterday 3006.6 Today 3006.7 via dnf update

Lego3 avatar Feb 22 '24 13:02 Lego3

Is there any place I can still find 3006.6, or that is completely gone now?

https://repo.saltproject.io/salt/py3/ https://repo.saltproject.io/salt/py3/redhat/8/x86_64/minor/

max-arnold avatar Feb 22 '24 16:02 max-arnold

I also stumble upon this exact same missing __enter__ exception and after debuging a bit with pdb, it seems the fileclient is a PillarClient which is missing proper contextmanager's __enter__ and __exit__ methods.

After adding those methods, i've managed to go a bit further but the file i was looking for is not found by this client because it will look only in pillar_roots directories but my file is located under file_roots directories.

kiniou avatar Feb 22 '24 16:02 kiniou

There is a potential fix in #66128. Is it possible for someone to apply this patch to a minion and validate it?

dwoz avatar Feb 22 '24 22:02 dwoz

Yes, it worked for me with that patch applied :-)

Lego3 avatar Feb 23 '24 07:02 Lego3

I confirm this is working fine :+1: :tada: (I did almost the same by forcing the fileclient in my ext_pillar to workaround this issue :wink:)

kiniou avatar Feb 23 '24 10:02 kiniou

This fix will be in 3007.0 and 3006.8

dwoz avatar Mar 01 '24 00:03 dwoz

Great! Thank you

Lego3 avatar Mar 01 '24 07:03 Lego3