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

_find_chocolatey() failes due to renaming of chocolatey.exe #64427

Closed
TiHiNi opened this issue Jun 6, 2023 · 7 comments · Fixed by #64428
Closed

_find_chocolatey() failes due to renaming of chocolatey.exe #64427

TiHiNi opened this issue Jun 6, 2023 · 7 comments · Fixed by #64428
Assignees
Labels
Bug broken, incorrect, or confusing behavior dependency underlying Salt dependency issue Windows

Comments

@TiHiNi
Copy link
Contributor

TiHiNi commented Jun 6, 2023

Description of Issue

State chocolatey.installed search for "chocolatey.exe" but Chocolatey has renamed it to "choco.exe"

image

Function: chocolatey.installed Result: False Comment: An exception occurred in this state: Traceback (most recent call last): File "salt\state.py", line 2276, in call ret = self.states[cdata["full"]]( File "salt\loader\lazy.py", line 149, in __call__ return self.loader.run(run_func, *args, **kwargs) File "salt\loader\lazy.py", line 1228, in run return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs) File "salt\loader\lazy.py", line 1243, in _run_as return _func_or_method(*args, **kwargs) File "salt\loader\lazy.py", line 1276, in wrapper return f(*args, **kwargs) File "C:\Program Files\Salt Project\salt\salt\salt\states\chocolatey.py", line 106, in installed pre_install = __salt__["chocolatey.list"](local_only=True) File "salt\loader\lazy.py", line 149, in __call__ return self.loader.run(run_func, *args, **kwargs) File "salt\loader\lazy.py", line 1228, in run return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs) File "salt\loader\lazy.py", line 1243, in _run_as return _func_or_method(*args, **kwargs) File "C:\Program Files\Salt Project\salt\salt\salt\modules\chocolatey.py", line 460, in list_ choc_path = _find_chocolatey() File "C:\Program Files\Salt Project\salt\salt\salt\modules\chocolatey.py", line 127, in _find_chocolatey raise CommandExecutionError(err) salt.exceptions.CommandExecutionError: Chocolatey not installed. Use chocolatey.bootstrap to install the Chocolatey package manager.

salt/modules/chocolatey.py (line 95 to 128)

# Check context
if "chocolatey._path" in __context__:
    return __context__["chocolatey._path"]

# Check the path
choc_path = __salt__["cmd.which"]("chocolatey.exe")
if choc_path:
    __context__["chocolatey._path"] = choc_path
    return __context__["chocolatey._path"]

# Check in common locations
choc_defaults = [
    os.path.join(
        os.environ.get("ProgramData"), "Chocolatey", "bin", "chocolatey.exe"
    ),
    os.path.join(
        os.environ.get("SystemDrive"), "Chocolatey", "bin", "chocolatey.bat"
    ),
]
for choc_exe in choc_defaults:
    if os.path.isfile(choc_exe):
        __context__["chocolatey._path"] = choc_exe
        return __context__["chocolatey._path"]

# Not installed, raise an error
err = (
    "Chocolatey not installed. Use chocolatey.bootstrap to "
    "install the Chocolatey package manager."
)
raise CommandExecutionError(err)

Steps to Reproduce Issue

salt 'XXXXXX' chocolatey.bootstrap
salt 'XXXXXX' chocolatey.install 'notepadplusplus'

Versions Report

salt-call --versions-report
Salt Version:
Salt: 3005.1

Dependency Versions:
cffi: 1.14.6
cherrypy: 18.6.1
dateutil: 2.8.1
docker-py: Not Installed
gitdb: 4.0.7
gitpython: Not Installed
Jinja2: 3.1.0
libgit2: Not Installed
M2Crypto: Not Installed
Mako: 1.1.4
msgpack: 1.0.2
msgpack-pure: Not Installed
mysql-python: Not Installed
pycparser: 2.21
pycrypto: Not Installed
pycryptodome: 3.10.1
pygit2: Not Installed
Python: 3.8.16 (tags/v3.8.16:1e3d2d5, Dec 9 2022, 17:48:54) [MSC v.1916 64 bit (AMD64)]
python-gnupg: 0.4.8
PyYAML: 5.4.1
PyZMQ: 22.0.3
smmap: 4.0.0
timelib: 0.2.4
Tornado: 4.5.3
ZMQ: 4.3.4

System Versions:
dist:
locale: cp1252
machine: AMD64
release: 2019Server
system: Windows
version: 2019Server 10.0.17763 SP0 Multiprocessor Free

@welcome
Copy link

welcome bot commented Jun 6, 2023

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey.
Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
If you have additional questions, email us at saltproject@vmware.com. We’re glad you’ve joined our community and look forward to doing awesome things with you!

@TiHiNi
Copy link
Contributor Author

TiHiNi commented Jun 6, 2023

I worked around for the moment with adding this lines:

salt/modules/chocolatey.py (line 108 to 119)

choc_defaults = [
    os.path.join(
        os.environ.get("ProgramData"), "Chocolatey", "bin", "chocolatey.exe"
    ),
    os.path.join(
        os.environ.get("ProgramData"), "Chocolatey", "bin", "choco.exe"
    ),
    os.path.join(
        os.environ.get("SystemDrive"), "Chocolatey", "bin", "chocolatey.bat"
    ),
]

@OrangeDog OrangeDog added Bug broken, incorrect, or confusing behavior Windows needs-triage dependency underlying Salt dependency issue labels Jun 8, 2023
@adrianschneider94
Copy link

I just did the following until the patch arrives:

# Is chocolatey installed?
Chocolatey is installed without symlink:
  file.exists:
    - name: 'C:\ProgramData\chocolatey\choco.exe'

# Bootstrap Chocolatey package manager
Bootstrap Chocolatey:
  module.run:
    - chocolatey.bootstrap: []
    - onfail:
      - Chocolatey is installed without symlink

# Ensure there is a symlink from chocolatey.exe to choco.exe
Chocolatey is installed:
  file.symlink:
    - name: 'C:\ProgramData\chocolatey\chocolatey.exe'
    - target: 'C:\ProgramData\chocolatey\choco.exe'
    - require:
      - Chocolatey is installed without symlink

@TiHiNi
Copy link
Contributor Author

TiHiNi commented Jun 21, 2023

@adrianschneider94 Good idea. But in my case chocolatey.upgraded search in %programdata%\chocolatey\bin\ for chocolatey.exe

From chocolatey.py:

 os.environ.get("ProgramData"), "Chocolatey", "bin", "chocolatey.exe"

That is how it works for me:

# Is chocolatey installed?
Chocolatey is installed without symlink:
  file.exists:
    - name: 'C:\ProgramData\chocolatey\bin\choco.exe'

# Bootstrap Chocolatey package manager
Bootstrap Chocolatey:
  module.run:
    - chocolatey.bootstrap: []
    - onfail:
      - Chocolatey is installed without symlink

# Ensure there is a symlink from chocolatey.exe to choco.exe
Chocolatey is installed:
  file.symlink:
    - name: 'C:\ProgramData\chocolatey\bin\chocolatey.exe'
    - target: 'C:\ProgramData\chocolatey\bin\choco.exe'
    - require:
      - Chocolatey is installed without symlink

@jmcmillan89
Copy link

@TimHiCe @adrianschneider94 great solution, this a clever workaround. I'd like to propose one additional enhancement, which is moving the file.exists state into an unless requisite:

Install Chocolatey with bootstrap:
  module.run:
    - chocolatey.bootstrap:
      - force: True
    - unless:
      - fun: file.file_exists
        path: 'C:\ProgramData\chocolatey\bin\choco.exe'

Symlink chocolatey.exe to choco.exe:
  file.symlink:
    - name: 'C:\ProgramData\chocolatey\bin\chocolatey.exe'
    - target: 'C:\ProgramData\chocolatey\bin\choco.exe'
    - force: True

This prevents that state from always showing as a failure but accomplishes the same behavior.

@rcmoutinho
Copy link

It seems related to breaking change from chocolatey 1.x and 2.x. This documentation shows that all the previous expected exe are now removed: https://docs.chocolatey.org/en-us/guides/upgrading-to-chocolatey-v2-v6#chocolatey-shortcut-shims-removed

In the end, the Salt module might need to adapt to both cases or have a different implementation depending on the installed version.

@TiHiNi
Copy link
Contributor Author

TiHiNi commented Jun 22, 2023

From my code review every install-, update- etc. method is build on _find_chocolatey(). If this method find the right choco(latey).exe every think should be fine. That is what I have done in the linked pull request.
If you see more problems, please let me know.

By the way, another workaround could be setting an environment variable to install an older version (not testet by me):
https://docs.chocolatey.org/en-us/guides/upgrading-to-chocolatey-v2-v6#installing-a-specific-chocolatey-version

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 dependency underlying Salt dependency issue Windows
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants