Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Lego3 opened this issue Feb 22, 2024 · 7 comments
Assignees
Labels
Bug broken, incorrect, or confusing behavior

Comments

@Lego3
Copy link
Contributor

Lego3 commented Feb 22, 2024

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: 8223a27#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 Lego3 added Bug broken, incorrect, or confusing behavior needs-triage labels Feb 22, 2024
@max-arnold
Copy link
Contributor

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/

@kiniou
Copy link

kiniou commented Feb 22, 2024

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.

@dwoz
Copy link
Contributor

dwoz commented Feb 22, 2024

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

@Lego3
Copy link
Contributor Author

Lego3 commented Feb 23, 2024

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

@kiniou
Copy link

kiniou commented Feb 23, 2024

I confirm this is working fine 👍 🎉 (I did almost the same by forcing the fileclient in my ext_pillar to workaround this issue 😉)

@dwoz
Copy link
Contributor

dwoz commented Mar 1, 2024

This fix will be in 3007.0 and 3006.8

@dwoz dwoz closed this as completed Mar 1, 2024
@Lego3
Copy link
Contributor Author

Lego3 commented Mar 1, 2024

Great! Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior
Projects
None yet
Development

No branches or pull requests

4 participants