Packaging: reimplement packagecloud release number script as a pants rule #6323
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.
This PR is working towards doing packaging via pantsbuild. Eventually, I hope to archive and stop using st2-packages.git.
One of the key parts of packaging is assigning a release number for a given build. The only way to do that is to query packagecloud to see what the last release was and add 1 to that. We use a shell script to do that in st2-packages.git:
https://github.com/StackStorm/st2-packages/blob/master/.circle/packagecloud.sh
In this PR I rewrote that shell script in python in
pants-plugins/releaseas pantsbuild@rules. Then this builds on #6321 by Injecting the release number from packagecloud as thereleasefield onnfpm_deb_packageandnfpm_rpm_packagetargets.Comparison of old packagecloud shell script with the python functions in this PR
latest_revisionis the primary function for looking up the release number in the old script. This function calls:get_repo_name,get_pkg_os, andget_revision(which callsget_versions_url).The old script recalculates
PKG_IS_UNSTABLEin several places. In this PR, it is calculated only once in the@ruleinject_nfpm_fields:st2/pants-plugins/release/rules.py
Lines 232 to 237 in 0df575c
To replace the
get_repo_namefunction, I used a simple f-string (we do not useenterpriserepos any more, so I didn't bother to copy that):st2/pants-plugins/release/packagecloud_rules.py
Line 123 in 0df575c
To replace the
get_pkg_osfunction, I used some dictionaries instead of functions to link an os-release to its package type. These include all OS-versions that have release in packagecloud (It should always include that so we can look up old versions if we need to), as well as a few more that we will or hope to support. In particular, these dictionaries:DISTROS_BY_PKG_TYPEwhich has{pkg_type: {distro: {distro_id: distro_version}}}. Herepkg_typeisdeborrpm;distrois the OS (debian,ubuntu,el);distro_idis the name we use internally to identify a particular os-version (el8,el9,focal,jammy, etc); anddistro_versionis an actual version number.st2/pants-plugins/release/packagecloud_rules.py
Line 38 in 0df575c
DISTRO_INFOis a dictionary comprehension that reorganizes the metadata inDISTROS_BY_PKG_TYPEto make it easier to look it up using our internaldistro_idname.st2/pants-plugins/release/packagecloud_rules.py
Lines 65 to 74 in 0df575c
DISTRO_INFOis used in the@rulepackagecloud_get_next_releasehere:st2/pants-plugins/release/packagecloud_rules.py
Lines 117 to 118 in 0df575c
The
get_revisionfunction callsget_versions_urlwhich is reimplemented with an f-string and arequestssession (with some additional safety checks around the API call):st2/pants-plugins/release/packagecloud_rules.py
Lines 130 to 133 in 0df575c
Finally, we reimplement the packcloud API calls in the
get_revisionfunction (with some additional safety checks around the API call) in the@rulepackagecloud_get_next_release.st2/pants-plugins/release/packagecloud_rules.py
Lines 137 to 149 in 0df575c
Additional safety checks around API calls and paging in those API calls are implemented here:
st2/pants-plugins/release/packagecloud_rules.py
Lines 105 to 115 in 0df575c
Overview of changes to
pants-plugins/releaseThis uses
requeststo make packagecloud API calls, so tell pants about our plugin's requirement by adding arequirements.txtfile in the plugin directory (pants-plugins/release/requirements.txt):st2/pants-plugins/release/requirements.txt
Line 1 in 0df575c
Then add that requirements.txt file in this BUILD file entry so we can regenerate
lockfiles/pants-plugins.lock:st2/pants-plugins/release/BUILD
Lines 7 to 9 in 0df575c
And implement the packagecloud release lookup in the
@rulepackagecloud_get_next_releasehere (Note that we use@_uncacheable_ruleto make sure this gets recalculated every time we build a package. It is uncacheable since we depend on state in an external system.):st2/pants-plugins/release/packagecloud_rules.py
Lines 91 to 94 in 0df575c
Then we call the
@rulepackagecloud_get_next_releasehere (Note theTODO: add field for distro IDwill be addressed in a follow-up PR after this PR gets merged.):st2/pants-plugins/release/rules.py
Lines 239 to 249 in 0df575c
This rule has to be registered with pants, so do that here:
st2/pants-plugins/release/rules.py
Lines 259 to 261 in 0df575c
And finally, we inject the nfpm
releasefield intonfpm_deb_packageandnfpm_rpm_packagetargets here:st2/pants-plugins/release/rules.py
Line 254 in 0df575c