saltstack / saltstack/salt

Specifying external python binary for 'python' parameter and 'venv' module for 'venv_bin' parameter fails in creation of managed virtualenv

Open
#68,282 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

The documentation is no longer correct. It says the default is to use virtualenv, but it now is not if not installed.

It also appears that there's no way to use the venv module of a different Python (e.g. the system Python)?

People will have to workaround by creating custom pyvenv wrappers.

Originally posted by @OrangeDog in https://github.com/saltstack/salt/issues/66673#issuecomment-2383672191

I encountered this issue as well. The only way to use the venv module appears to be to run it using the particular python executable that will interact with it.
Output of highstate when attempting to create virtualenv in minion using state file that specifies python and venv_bin:

    ID: app_venv
    Function: virtualenv.managed
    Name: C:\Users\user\Repos\salt\test_env
    Result: False
    Comment: An exception occurred in this state: Traceback (most recent call last):
                File "C:\Users\user\Repos\salt\salt\state.py", line 2229, in call
                  ret = self.states[cdata["full"]](
                File "C:\Users\user\Repos\salt\salt\loader\lazy.py", line 163, in __call__
                  ret = self.loader.run(run_func, *args, **kwargs)
                File "C:\Users\user\Repos\salt\salt\loader\lazy.py", line 1276, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File "C:\Users\user\Repos\salt\salt\loader\lazy.py", line 1291, in _run_as
                  ret = _func_or_method(*args, **kwargs)
                File "C:\Users\user\Repos\salt\salt\loader\lazy.py", line 1327, in wrapper
                  return f(*args, **kwargs)
                File "C:\Users\user\Repos\salt\salt\states\virtualenv_mod.py", line 199, in managed
                  venv_ret = __salt__["virtualenv.create"](
                File "C:\Users\user\Repos\salt\salt\loader\lazy.py", line 163, in __call__
                  ret = self.loader.run(run_func, *args, **kwargs)
                File "C:\Users\user\Repos\salt\salt\loader\lazy.py", line 1276, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File "C:\Users\user\Repos\salt\salt\loader\lazy.py", line 1291, in _run_as
                  ret = _func_or_method(*args, **kwargs)
                File "C:\Users\user\Repos\salt\salt\modules\virtualenv_mod.py", line 242, in create
                  raise CommandExecutionError(
              salt.exceptions.CommandExecutionError: The `python`(`--python`) option is not supported by 'venv'
     Started: 19:34:35.579902
    Duration: 0.0 ms
     Changes:

The following patch enabled the creation of the virtual environment on my setup without issue to the extent I was able to test it (version 3007.6 running on a Windows 10 Minion):

--- a/salt/modules/virtualenv_mod.py
+++ b/salt/modules/virtualenv_mod.py
@@ -177,7 +177,10 @@ def create(
         venv_bin = __pillar__.get("venv_bin") or __opts__.get("venv_bin")

     if venv_bin == "venv":
-        cmd = [sys.executable, "-m", "venv"]
+        if python is not None and python.strip() != "":
+            cmd = [python, "-m", "venv"]
+        else:
+            cmd = [sys.executable, "-m", "venv"]
     else:
         cmd = [venv_bin]

@@ -238,13 +241,7 @@ def create(
         # ----- Stop the user if virtualenv only options are being used ----->
         # If any of the following values are not None, it means that the user
         # is actually passing a True or False value. Stop Him!
-        if python is not None and python.strip() != "":
-            raise CommandExecutionError(
-                "The `python`(`--python`) option is not supported by '{}'".format(
-                    venv_bin
-                )
-            )
-        elif extra_search_dir is not None and extra_search_dir.strip() != "":
+        if extra_search_dir is not None and extra_search_dir.strip() != "":
             raise CommandExecutionError(
                 "The `extra_search_dir`(`--extra-search-dir`) option is not "
                 "supported by '{}'".format(venv_bin)

This was tested with minimal required alteration to master and minion config files, and the following state files:
/srv/salt/top.sls

base:
  '*':
    - minionstate

/srv/salt/minionstate.sls

app_venv:
  virtualenv.managed:
    - name: 'C:\Users\user\Repos\salt\test_env'
    - venv_bin: 'venv'
    - python: py
    - pip_pkgs:
      - opencv-python # Arbitrary package to test installation of virtual environment

I am still learning how to use SaltStack, and so am not yet familiar with what would be needed to adhere to the project's standards. If this modification could be useful, then any assistance in how the process should be worked through properly, such as the creation of a correctly formulated pull request, would be greatly appreciated.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in salt/modules/virtualenv_mod.py at create and review how python and venv_bin are converted into the command. Reproduce the supplied Windows state using venv_bin: 'venv' and python: py, then verify that the venv module can use the selected Python without rejecting the python option.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.