Skip to content

Commit

Permalink
ROX-5894: Allow generation of latest support pkg links (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
misberner authored Nov 10, 2020
1 parent 8b62e3a commit 523d5d8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
34 changes: 25 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ commands:
EOF
[[ -f ~/workspace/shared-env ]] && cat ~/workspace/shared-env >>"$BASH_ENV"
read-pr-metadata:
steps:
- run:
name: Read PR metadata
command: |
~/workspace/go/src/github.com/stackrox/collector/.circleci/read-labels.sh pr-metadata/labels
gcloud-init:
steps:
- run:
Expand Down Expand Up @@ -115,10 +122,7 @@ jobs:
export PATH="${PATH}:${GOPATH}/bin:${WORKSPACE_ROOT}/bin"
EOF
- run:
name: Read PR metadata
command: |
~/workspace/go/src/github.com/stackrox/collector/.circleci/read-labels.sh pr-metadata/labels
- read-pr-metadata

- run:
name: Set COLLECTOR_VERSION
Expand Down Expand Up @@ -1233,6 +1237,15 @@ jobs:

steps:
- initcommand
- read-pr-metadata
- run:
name: Check whether to run job
command: |
if [[ "$CIRCLE_BRANCH" != "master" && ! -f ~/workspace/pr-metadata/labels/test-support-packages ]]; then
echo "On a PR without the test-support-packages label. Not building support packages."
circleci step halt
fi
- setup_remote_docker
- run:
name: Docker login to collector.stackrox.io
Expand All @@ -1247,9 +1260,13 @@ jobs:
- run:
name: Initialize environment
command: |
cci-export BASE_URL "${DOWNLOAD_BASE_URL}/${RELATIVE_PATH}"
relative_path="$RELATIVE_PATH"
if [[ "$CIRCLE_BRANCH" != "master" ]]; then
relative_path="${relative_path}/.test-${CIRCLE_BUILD_NUM}"
fi
cci-export BASE_URL "${DOWNLOAD_BASE_URL}/${relative_path}"
cci-export SUPPORT_PKG_SRC_ROOT "${SOURCE_ROOT}/kernel-modules/support-packages"
cci-export GCLOUD_TARGET "${GCLOUD_BUCKET}/${RELATIVE_PATH}"
cci-export GCLOUD_TARGET "${GCLOUD_BUCKET}/${relative_path}"
- run:
name: Install Pip
Expand Down Expand Up @@ -1314,6 +1331,8 @@ jobs:
# entire `sr-roxc` bucket (!!!). Hence have a hardcoded check for the path looking
# valid.
[[ "$GCLOUD_TARGET" =~ ^gs://[^/]+/.*collector.*/.*support-packages.*$ ]]
# On non-master, additionally test that we have a .test indicator in the URL
[[ "$CIRCLE_BRANCH" == "master" || "$GCLOUD_TARGET" = *.test* ]]
gsutil -m rsync -n -r -d /tmp/support-packages/output "$GCLOUD_TARGET"
reload-released-images:
Expand Down Expand Up @@ -1667,8 +1686,5 @@ workflows:
only: /.*/
- update-support-packages:
context: docker-io-and-stackrox-io-push
filters:
branches:
only: master
requires:
- upload-modules
2 changes: 2 additions & 0 deletions kernel-modules/support-packages/04-create-support-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ for mod_ver_dir in "${MD_DIR}/module-versions"/*; do
package_out_dir="${OUT_DIR}/${mod_ver}"
mkdir -p "$package_out_dir"
filename="support-pkg-${mod_ver::6}-$(date '+%Y%m%d%H%M%S').zip"
latest_filename="support-pkg-${mod_ver::6}-latest.zip"

( cd "$package_root" ; zip -r "${package_out_dir}/${filename}" . )
cp "${package_out_dir}/${filename}" "${package_out_dir}/${latest_filename}"
rm -rf "$package_root" || true
done
25 changes: 20 additions & 5 deletions kernel-modules/support-packages/05-create-index.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@
VersionRange = namedtuple('VersionRange', 'min max')

class SupportPackage(object):
def __init__(self, module_version, rox_version_ranges, file_name, last_update_time):
def __init__(self, module_version, rox_version_ranges, file_name, latest_file_name, last_update_time):
self.module_version = module_version
self.rox_version_ranges = rox_version_ranges
self.file_name = file_name
self.latest_file_name = latest_file_name
self.last_update_time = last_update_time

@property
def download_url(self):
return '%s/%s/%s' % (os.getenv('BASE_URL'), self.module_version, self.file_name)

@property
def download_url_latest(self):
return '%s/%s/%s' % (os.getenv('BASE_URL'), self.module_version, self.latest_file_name) \
if self.latest_file_name is not None else None

def __repr__(self):
return 'SupportPackage(module_version=%s, rox_version_ranges=%s, file_name=%s, last_update_time=%d)' % (
self.module_version, self.rox_version_ranges, self.file_name, self.last_update_time,
)
return 'SupportPackage(' + \
'module_version=%s, rox_version_ranges=%s, file_name=%s, latest_file_name=%s, last_update_time=%d)' % (
self.module_version, self.rox_version_ranges, self.file_name, self.latest_file_name,
self.last_update_time
)

def render_index(packages, out_dir, template_file='index.html'):
curr_dir = str(Path(__file__).parent.absolute().resolve())
Expand Down Expand Up @@ -56,7 +64,14 @@ def load_support_packages(output_dir, mod_md_map):
st = os.stat(os.path.join(mod_out_dir, support_pkg_file))
last_mod_time = datetime.utcfromtimestamp(st.st_mtime).strftime('%Y/%m/%d, %H:%M:%S')

support_packages.append(SupportPackage(mod_ver, rox_version_ranges, support_pkg_file, last_mod_time))
support_pkg_file_latest = 'support-pkg-%s-latest.zip' % (mod_ver[:6])
try:
os.stat(os.path.join(mod_out_dir, support_pkg_file_latest))
except:
support_pkg_file_latest = None

support_packages.append(SupportPackage(
mod_ver, rox_version_ranges, support_pkg_file, support_pkg_file_latest, last_mod_time))

support_packages.sort(key=lambda p: p.rox_version_ranges[0].max, reverse=True)
return support_packages
Expand Down
5 changes: 4 additions & 1 deletion kernel-modules/support-packages/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3240,6 +3240,9 @@ <h2 class="text-tertiary-800 mb-4 border-b border-base-300 pb-4">Collector Suppo
from this page to ensure that Collectors have access to the files required at runtime. Please refer to the documentation on how to install support packages.</p>
<p></p>
{% if support_packages %}
<em>Note:</em> The "stable link" always points to the latest support package for a given version. It will retain
its validity when a support package is updated, but the contents of the downloaded file change over time.
<p></p>
<table>
<thead>
<tr><th>StackRox Version(s)</th><th>Last updated</th><th>Download Link</th></tr>
Expand All @@ -3258,7 +3261,7 @@ <h2 class="text-tertiary-800 mb-4 border-b border-base-300 pb-4">Collector Suppo
{% endfor %}
</td>
<td>{{pkg.last_update_time}}</td>
<td><a href="{{pkg.download_url}}">Download</a></td>
<td><a href="{{pkg.download_url}}">Download</a>{% if pkg.download_url_latest %} (<a href="{{pkg.download_url_latest}}">stable link</a>){% endif %}</td>
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit 523d5d8

Please sign in to comment.