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

Feat: add index_url paramter for setup env #6289

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ Added
each parallel test run would be a maintenance burden. #6282
Contributed by @cognifloyd

* Add new option `index_url` to allow user manually configure python package download url.
Contributed by @yhkl-dev

3.8.1 - December 13, 2023
-------------------------
Fixed
Expand Down
6 changes: 5 additions & 1 deletion contrib/packs/actions/pack_mgmt/setup_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ def __init__(self, config=None, action_service=None):
):
os.environ["no_proxy"] = self.no_proxy

def run(self, packs, update=False, no_download=True):
def run(self, packs, index_url, update=False, no_download=True):
"""
:param packs: A list of packs to create the environment for.
:type: packs: ``list``

:param update: True to update dependencies inside the virtual environment.
:type update: ``bool``

:param index_url: Package index options.
:type index_url: ``str``
"""

for pack_name in packs:
Expand All @@ -96,6 +99,7 @@ def run(self, packs, update=False, no_download=True):
logger=self.logger,
proxy_config=self.proxy_config,
no_download=no_download,
index_url=index_url,
)

message = "Successfully set up virtualenv for the following packs: %s" % (
Expand Down
6 changes: 6 additions & 0 deletions contrib/packs/actions/setup_virtualenv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@
required: false
description: Action timeout in seconds. Action will get killed if it doesn't finish in timeout
type: integer
index_url:
default: "https://pypi.org/simple"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This default means all users that do not use this option will have -i https://pypi.org/simple in the pip command which seems pointless.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for clarification!

required: false
description: Package Index options
type: string

7 changes: 7 additions & 0 deletions st2common/st2common/cmd/setup_pack_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def _register_cli_opts():
"exist, it will create it.."
),
),
cfg.StrOpt(
"index_url",
default=False,
help="Package Index options",
),
]
do_register_cli_opts(cli_opts)

Expand All @@ -64,6 +69,7 @@ def main(argv):

packs = cfg.CONF.pack
update = cfg.CONF.update
index_url = cfg.CONF.index_url

proxy_config = get_and_set_proxy_config()

Expand All @@ -76,6 +82,7 @@ def main(argv):
logger=LOG,
proxy_config=proxy_config,
no_download=True,
index_url=index_url,
)
LOG.info('Successfully set up virtualenv for pack "%s"' % (pack))

Expand Down
25 changes: 23 additions & 2 deletions st2common/st2common/util/virtualenvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def setup_pack_virtualenv(
no_download=True,
force_owner_group=True,
inject_parent_virtualenv_sites=True,
index_url=None,
):

"""
Expand All @@ -78,6 +79,9 @@ def setup_pack_virtualenv(
:param no_download: Do not download and install latest version of pre-installed packages such
as pip and setuptools.
:type no_download: ``bool``

:param index_url: Package index options.
:type index_url: ``str``
"""
logger = logger or LOG

Expand Down Expand Up @@ -137,6 +141,7 @@ def setup_pack_virtualenv(
requirement=requirement,
proxy_config=proxy_config,
logger=logger,
index_url=index_url,
)

# 4. Install pack-specific requirements
Expand All @@ -152,6 +157,7 @@ def setup_pack_virtualenv(
requirements_file_path=requirements_file_path,
proxy_config=proxy_config,
logger=logger,
index_url=index_url,
)
else:
logger.debug("No pack specific requirements found")
Expand Down Expand Up @@ -319,7 +325,11 @@ def inject_st2_pth_into_virtualenv(virtualenv_path: str, logger: Logger = None)


def install_requirements(
virtualenv_path, requirements_file_path, proxy_config=None, logger=None
virtualenv_path,
requirements_file_path,
proxy_config=None,
logger=None,
index_url=None,
):
"""
Install requirements from a file.
Expand All @@ -346,6 +356,8 @@ def install_requirements(
cmd.append("install")
cmd.extend(pip_opts)
cmd.extend(["-U", "-r", requirements_file_path])
if index_url:
cmd.extend(["-i", index_url])

env = get_env_for_subprocess_command()

Expand All @@ -372,7 +384,13 @@ def install_requirements(
return True


def install_requirement(virtualenv_path, requirement, proxy_config=None, logger=None):
def install_requirement(
virtualenv_path,
requirement,
proxy_config=None,
logger=None,
index_url=None,
):
"""
Install a single requirement.

Expand Down Expand Up @@ -400,6 +418,9 @@ def install_requirement(virtualenv_path, requirement, proxy_config=None, logger=
cmd.append("install")
cmd.extend(pip_opts)
cmd.extend([requirement])
if index_url:
cmd.extend(["-i", index_url])

env = get_env_for_subprocess_command()
logger.debug(
"Installing requirement %s with command %s.", requirement, " ".join(cmd)
Expand Down
25 changes: 25 additions & 0 deletions st2common/tests/unit/test_virtualenvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,31 @@ def test_install_requirement_without_proxy(self):
}
virtualenvs.run_command.assert_called_once_with(**expected_args)

@mock.patch.object(
virtualenvs, "run_command", mock.MagicMock(return_value=(0, "", ""))
)
@mock.patch.object(
virtualenvs, "get_env_for_subprocess_command", mock.MagicMock(return_value={})
)
def test_install_requirement_with_index_url(self):
pack_virtualenv_dir = "/opt/stackstorm/virtualenvs/dummy_pack_tests/"
requirement = "six>=1.9.0"
index_url = "https://test.com/sample"
install_requirement(
pack_virtualenv_dir, requirement, proxy_config=None, index_url=index_url
)
expected_args = {
"cmd": [
"/opt/stackstorm/virtualenvs/dummy_pack_tests/bin/pip",
"install",
"six>=1.9.0",
"-i",
index_url,
],
"env": {},
}
virtualenvs.run_command.assert_called_once_with(**expected_args)

@mock.patch.object(
virtualenvs, "run_command", mock.MagicMock(return_value=(0, "", ""))
)
Expand Down
Loading