Use _pkg.txt file for package grain

This commit is contained in:
Megan Wilhite 2023-06-13 13:24:32 -06:00 committed by Pedro Algarvio
parent 34e3f90035
commit e01ceb5284
14 changed files with 63 additions and 24 deletions

1
_pkg.txt Normal file
View file

@ -0,0 +1 @@
pip

View file

@ -1 +1 @@
Added new grain to detect the Salt package type: system or onedir
Added new grain to detect the Salt package type: onedir, pip or system

View file

@ -1 +1 @@
Add salt package type information. Either system or onedir.
Add salt package type information. Either onedir, pip or system.

View file

@ -16,6 +16,13 @@ Docker Containers
The Salt Project uses docker containers to build our deb and rpm packages. If you are building your own packages you can use
the same containers we build with in the Github piplines. These containers are documented `here <https://github.com/saltstack/salt-ci-containers/tree/main/custom/packaging>`_.
Package Grain
=============
In the 3007.0 release a new package grain was added. This detects how Salt was installed using the `_pkg.txt`
in the root of the Salt repo. By default this is set to ``pip``, but it is set to ``onedir`` when ``tools pkg build salt-onedir``
is run in our pipelines when building our onedir packages. If you are building your own custom packages, please ensure you set
``_pkg.txt`` contents to be the type of package you are creating. The options are ``pip``, ``onedir`` or ``system``.
How to build onedir only
========================

View file

@ -16,6 +16,11 @@ Support for python 3.7 has been dropped since it reached end-of-line in 27 Jun 2
Starting from Salt version 3007.0, the Azure functionality previously available in the Salt code base is fully removed. To continue using Salt's features for interacting with Azure resources, users are required to utilize the Azure Salt extension. For more information, refer to the [Azure Salt Extension GitHub repository](https://github.com/salt-extensions/saltext-azurerm).
## New Package Grain
A new ``package`` grain was added in 3007.0 This detects how Salt was installed using the ``_pkg.txt`` in the root of
the directory. If you are building packages of Salt you need to ensure this file is set to the correct package type
that you are building. The options are ``pip``, ``onedir``, or ``system``. By default this file is already set to ``pip``.
<!--
Do not edit the changelog below.
This is auto generated

View file

@ -39,3 +39,12 @@ def test_grains_setval_key_val(salt_cli, salt_minion):
ret = salt_cli.run("grains.setval", "key", "val", minion_tgt=salt_minion.id)
assert ret.data, ret
assert "key" in ret.data
def test_grains_package_onedir(salt_cli, salt_minion):
"""
Test that the package grain returns onedir
"""
ret = salt_cli.run("grains.get", "package", minion_tgt=salt_minion.id)
assert "onedir" == ret.data
assert ret.data, ret

View file

@ -20,6 +20,6 @@ def __virtual__():
def package():
"""
Function to determine if the user is currently using
onedir package or system level package of Salt.
onedir, pip or system level package of Salt.
"""
return {"package": salt.utils.package.pkg_type()}

View file

@ -145,7 +145,7 @@ def _check_bundled():
"""
Gather run-time information to indicate if we are running from source or bundled.
"""
if salt.utils.package.type() == "onedir":
if salt.utils.package.bundled():
return True
return False

View file

@ -1,11 +1,27 @@
import pathlib
import sys
import salt.utils.files
def bundled():
"""
Gather run-time information to indicate if we are running from relenv onedir
"""
if hasattr(sys, "RELENV"):
return True
else:
return False
def pkg_type():
"""
Gather run-time information to indicate if we are running from onedir or .
Utility to find out how Salt was installed.
"""
if hasattr(sys, "RELENV"):
return "onedir"
else:
return "system"
pkg_file = pathlib.Path(__file__).parent.parent.parent / "_pkg.txt"
if pkg_file.is_file():
with salt.utils.files.fopen(pkg_file) as _fp:
content = _fp.read()
if content:
return content.strip()
return None

View file

@ -1,7 +1,6 @@
import logging
import os
import shutil
import sys
import pytest
@ -81,15 +80,13 @@ def test_versions_report(salt_cli):
assert key in expected_keys
expected_keys.remove(key)
assert not expected_keys
if hasattr(sys, "RELENV"):
assert "onedir" in ret_dict["Salt Package Information"]["Package Type"]
else:
assert "system" in ret_dict["Salt Package Information"]["Package Type"]
if os.environ.get("ONEDIR_TESTRUN", "0") == "0":
assert "pip" in ret_dict["Salt Package Information"]["Package Type"]
# Stop any more testing
return
assert "onedir" in ret_dict["Salt Package Information"]["Package Type"]
assert "relenv" in ret_dict["Dependency Versions"]
assert "Salt Extensions" in ret_dict
assert "salt-analytics-framework" in ret_dict["Salt Extensions"]

View file

@ -1,4 +1,4 @@
import sys
import os
import pytest
@ -15,10 +15,10 @@ def test_grains_items(grains):
the return
"""
ret = grains.items()
if hasattr(sys, "RELENV"):
assert ret["package"] == "onedir"
if os.environ.get("ONEDIR_TESTRUN", "0") == "0":
assert ret["package"] == "pip"
else:
assert ret["package"] == "system"
assert ret["package"] == "onedir"
for key in ["num_cpus", "cpu_model", "os_family"]:
assert key in ret.keys()

View file

@ -1,4 +1,4 @@
import sys
import os
import salt.grains.package
@ -8,7 +8,7 @@ def test_grain_package_type(tmp_path):
Test grains.package_type for both package types
"""
ret = salt.grains.package.package()["package"]
if hasattr(sys, "RELENV"):
assert ret == "onedir"
if os.environ.get("ONEDIR_TESTRUN", "0") == "0":
assert ret == "pip"
else:
assert ret == "system"
assert ret == "onedir"

View file

@ -1,11 +1,11 @@
import sys
import os
import salt.utils.package
def test_pkg_type():
ret = salt.utils.package.pkg_type()
if hasattr(sys, "RELENV"):
if os.environ.get("ONEDIR_TESTRUN", "0") == "0":
assert ret == "onedir"
else:
assert ret == "system"

View file

@ -748,6 +748,10 @@ def salt_onedir(
ctx.info(f"Copying '{src.relative_to(tools.utils.REPO_ROOT)}' to '{dst}' ...")
shutil.copyfile(src, dst)
# Add package type file for package grain
with open(pathlib.Path(site_packages) / "_pkg.txt", "w") as fp:
fp.write("onedir")
def _check_pkg_build_files_exist(ctx: Context, **kwargs):
for name, path in kwargs.items():