-
-
Notifications
You must be signed in to change notification settings - Fork 750
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
Packaging: Add pex_binary
BUILD metadata for building st2 venv
#6307
Open
cognifloyd
wants to merge
11
commits into
master
Choose a base branch
from
packaging-pex_binary
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−1
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Now we can treat st2.pex as a self-extracting venv installer. Use a preamble file, which pex executes before its bootstrap code, to make the pex just build /opt/stackstorm/st2 instead of teaching all the installers what a pex-tools are.
Other packaging metadata will go in packaging/BUILD later.
bff1553
to
7915779
Compare
The preamble method was hacky and had some uncomfortable sharp edges. The pex cache does not know about the preamble which can break running the pex to unpack itself in some cases (like if you use PEX_TOOLS to inspect the pex before unpacking it). So, switch to a python script that will run when the pex is executed. This script has access to the full venv, thanks to pex, before we have even unpacked it. So, we can use deps like oslo.config safely. However, using a pex entry_point/script/executable causes the script to be included in the extracted venv. If we import any st2 code, then that can make pants add other sources or conf files that we do not want in the pex (other than via the wheels of our sources). So, I left a note warning against importing st2 code.
7915779
to
5c91817
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
enhancement
pantsbuild
size/L
PR that changes 100-499 lines. Requires some effort to review.
st2-packages
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
st2-packages uses a couple of methods for creating the
/opt/stackstorm/st2
venv: dh_virtualenv on debian and other tools on EL.With the move to Pants using lockfiles generated by PEX (Python EXecutable), PEX is the simplest way to build the
/opt/stackstorm/st2
venv. (Note: "pex" is both the name of the tool and the archive files it produces.) Pex packages, or files, are essentially executable archives that contain wheels and run python code. For us, the pex package will basically be a self-extracting virtualenv.The pants docs have a nice overview of PEX, including how to use it via pants.
In future PRs, I plan to add BUILD metadata to embed this pex package in rpm/deb files (built by pants+nfpm). The rpm/deb will use a post-install scriptlet that runs the pex file to generate the venv.
pex_binary(...)
BUILD metadataTo generate a pex package, we need to add a
pex_binary
target that depends on all of ourpython_distribution
targets. This list of dependencies replaces thein-requirements.txt
file in st2-packages and thest2client
dep which gets injected in the Makefile.st2/packaging/BUILD.venv
Lines 35 to 57 in 5a2ad11
There is config to control various aspects of the generated pex. I tried to document what/why some of these options are set the way they are. See the
pex_binary
docs for more about each of these options.st2/packaging/BUILD.venv
Lines 59 to 67 in 5a2ad11
The
include_tools=True
option is especially important in allowing the pex to generate a virtualenv. We configure the pex to run a python script, and in that script we usePEX_TOOLS
to build the virtualenv. The script is configured with theexecutable
option here:st2/packaging/BUILD.venv
Line 58 in 5a2ad11
This is the part of that script that uses
PEX_TOOLS
to build the venv:st2/packaging/build_st2_venv.py
Lines 58 to 75 in 5a2ad11
The
build_st2_venv.py
script actually has access to the all st2 deps. Technically, we could import st2 code as well, but I ran into issues doing that because pants then inferred dependencies on conf files that should not be in the pex. I usedoslo_config
to extract the[system].base_path
from our config file(s) or the env var. I added the bare minimum to accomplish this here. Note that this explicitly supports building the venv before st2 conf files are in place - the code in st2common does not do that.st2/packaging/build_st2_venv.py
Lines 33 to 51 in 5a2ad11
Finally, the unpacked virtualenv ends up with copies of the
build_st2_venv.py
script thanks to how pants runs pex. There isn't a good way to configure that behavior, so I added some logic to remove the script immediately after unpacking the venv.st2/packaging/build_st2_venv.py
Lines 87 to 103 in 5a2ad11
I believe the st2-packages repo also adds another metadata file in
/opt/stackstorm/st2
, but we can add that later if we actually need it. This script will be the place to add any st2-specific metadata files to the venv.The last thing to point out is how the BUILD metadata "parametrizes" the
pex_binary
BUILD metadata so that there is a separate pex file for each python minor version we support (python 3.8, 3.9, 3.10, and 3.11). I tried several ways to add this parametrization before landing on this way, which feels the most concise / most grokable to me. This calls a custom function that defines the parametrization with as little repetition as possible:st2/packaging/BUILD.venv
Lines 68 to 72 in 5a2ad11
This is our function that calls the pants
parametrize()
function. The first arg defines the name of the parametrization group (py38
,py39
,py310
,py311
). Then theoutput_path
determines where underdist/
the pex package will be placed once built. And theinterpreter_constraints
option narrows our constraints to one minor version of python.st2/packaging/BUILD.venv
Lines 25 to 32 in 5a2ad11
Building the pex packages
Finally, to build all pex packages (which end up under
dist/packaging/
), run:Or to build just the one for python 3.11 (which ends up as
dist/packaging/st2-py311.pex
), run:Then to use the pex, just run it (sudo might not be required if your user has write permissions in
/opt/stackstorm
):