Skip to content
170 changes: 122 additions & 48 deletions build-scripts/bootstrap-tarballs
Original file line number Diff line number Diff line change
@@ -1,106 +1,180 @@
#!/bin/bash -x
#!/bin/bash

# This script is supposed generate tarballs from the core & masterfiles
# repositories. We also use these tarballs later in the build process to make
# sure that they actually work.
#
# Currently this script does a lot more than bootstrap tarballs.
# It also fetches pull request info and installs PHP and javascript dependencies.
# The history behind this is that this script is run early in the bootstrap-pr job,
# and these extra steps effectively saves compute time for subsequent hub builds.
# Maybe we should rename it to only bootstrap (see ENT-13064).
#
# You will first need to run the autogen script. E.g.:
# PROJECT=community ./buildscripts/build-scripts/autogen
# Then you can run it like this:
# BUILD_TYPE=DEBUG ./buildscripts/build-scripts/bootstrap-tarballs
#
# The script expects the following repositories to be side by side:
# .
# ├── buildscripts
# ├── core
# ├── enterprise
Comment thread
larsewi marked this conversation as resolved.
# ├── nova
# ├── mission-portal
# ├── libntech
# └── masterfiles
#
# ^ When building community you won't need enterprise, nova, mission-portal
#

_dir=$(readlink -e "$(dirname "$0")")
# refactored a few functions into single file scripts for easier development/debugging, see ENT-12741 and ENT-12595
# Easier to add a path to a script than source a file of functions.
export PATH="$_dir"/bin:$PATH
. `dirname "$0"`/functions
. "$(dirname "$0")"/functions
. detect-environment
. compile-options
. version

mkdir -p $BASEDIR/output/tarballs
mkdir -p "$BASEDIR"/output/tarballs

# the first part of the script is not really critical
set +e

# Get information about PRs among the used revisions.
# These PRs will have to be notified of build progress.
#
# Variables such as MISSION_PORTAL_REV may be set by the CI (Jenkins) to build and test multiple PR's together.
# The variables typically hold a branch name or a pull request ID.
# PR IDs can also be preceded by 'pull' or 'origin/pull'.
# E.g.:
# - MISSION_PORTAL_REV=pull/1755
# - MISSION_PORTAL_REV=origin/pull/1755
# where the trailing number is the pull request ID.
#
# Furthermore, they can be suffixed by anything after a subsequent slash (/).
# We usually use:
# - ID alone
# - pull/ID/head
# - pull/ID/merge
#
# This loop fetches information about the PRs if the respective variable is set.
#
for repo_spec in cfengine/buildscripts cfengine/core cfengine/masterfiles cfengine/enterprise cfengine/nova cfengine/mission-portal NorthernTechHQ/libntech; do
# remove organization/ from start of repo_spec
# E.g. 'cfengine/mission-portal' -> 'mission-portal'
repo="${repo_spec#*/}"
rev_param_name="$(echo $repo | tr '[:lower:]-' '[:upper:]_')_REV"
revision="$(echo ${!rev_param_name})" || continue # dereference

# Convert to uppercase, swap hyphens with underscore and append '_REV'
# E.g. 'mission-portal' -> 'MISSION_PORTAL_REV'
rev_param_name="$(echo "$repo" | tr '[:lower:]-' '[:upper:]_')_REV"

# Try to dereference the result from above and skip the rest of the loop
# unless the variable is defined.
revision="${!rev_param_name}" || continue

# remove "origin/" (if any)
revision="${revision##origin/}"

# Check to see if the resolved variable starts with 'pull/'
if expr "$revision" : "pull/" >/dev/null; then
pr_nr="$(echo $revision | cut -d/ -f2)"
get-github-pull-request-info "$repo_spec" "$pr_nr" >> $BASEDIR/output/PRs
# Extract the revision number. E.g. 'pull/1755' -> '1755'
pr_nr="$(echo "$revision" | cut -d/ -f2)"

get-github-pull-request-info "$repo_spec" "$pr_nr" >> "$BASEDIR"/output/PRs
fi
done

# now script failures should fail the script
set -e

cd $BASEDIR/core
rm cfengine-3.*.tar.gz || true
git rev-parse HEAD > $BASEDIR/output/core-commitID
# Build tarball from core repository
cd "$BASEDIR"/core
rm -f cfengine-3.*.tar.gz
git rev-parse HEAD > "$BASEDIR"/output/core-commitID
# Configure in order to run "make dist", deleted later.
./configure -C
make dist
mv cfengine-3.*.tar.gz $BASEDIR/output/tarballs/
make distclean

cd $BASEDIR/masterfiles
rm cfengine-masterfiles*.tar.gz || true
git rev-parse HEAD > $BASEDIR/output/masterfiles-commitID
echo "$(basename "$0"): Debug: Running configure on core repository..."
run_and_print_on_failure ./configure -C
echo "$(basename "$0"): Debug: Running make dist on core repository..."
run_and_print_on_failure make dist
mv cfengine-3.*.tar.gz "$BASEDIR"/output/tarballs/
echo "$(basename "$0"): Debug: Running make distclean on core repository..."
run_and_print_on_failure make distclean

# Build tarballs from masterfiles repository
cd "$BASEDIR"/masterfiles
rm -f cfengine-masterfiles*.tar.gz
git rev-parse HEAD > "$BASEDIR"/output/masterfiles-commitID
# Configure in order to run "make dist", deleted later.
./configure
make dist # source tarball
make tar-package # package tarball
mv cfengine-masterfiles*.tar.gz $BASEDIR/output/tarballs/
make distclean

cd $BASEDIR/output/tarballs
sha256sum *.tar.gz > sha256sums.txt
CKSUM=`sum sha256sums.txt | cut -d ' ' -f 1`
mv sha256sums.txt sha256sums.$CKSUM.txt

echo "$(basename "$0"): Debug: Running configure on masterfiles repository..."
run_and_print_on_failure ./configure
echo "$(basename "$0"): Debug: Running make dist on masterfiles repository..."
run_and_print_on_failure make dist # source tarball
echo "$(basename "$0"): Debug: Running make tar-package on masterfiles repository..."
run_and_print_on_failure make tar-package # package tarball (containing all
# files as if they were installed
# under "prefix".)
mv cfengine-masterfiles*.tar.gz "$BASEDIR"/output/tarballs/
echo "$(basename "$0"): Debug: Running make distclean on masterfiles repository..."
run_and_print_on_failure make distclean

# Compute a checksum list that can be used to verify the integrity of the
# tarballs
cd "$BASEDIR"/output/tarballs
sha256sum -- *.tar.gz > sha256sums.txt
# Add the BSD (16-bit) checksum of the checksum list to it's filename. This way
# you can verify the integrity of the checksum list itself.
CKSUM=$(sum sha256sums.txt | cut -d ' ' -f 1)
mv sha256sums.txt sha256sums."$CKSUM".txt

echo "$(basename "$0"): Debug: Installing javascript npm dependencies..."
(
if test -f "$BASEDIR/mission-portal/public/scripts/package.json"; then
cd $BASEDIR/mission-portal/public/scripts
if test -f "$BASEDIR"/mission-portal/public/scripts/package.json; then
cd "$BASEDIR"/mission-portal/public/scripts
Comment on lines +133 to +134
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change isn't so needed but is fine. I often prefer to avoid the quotes as if more vars enter the expression things get noisy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, shellcheck warns about it

# display node & npm versions
npm --version
node --version
# install dependencies from npmjs
npm ci --prefix $BASEDIR/mission-portal/public/scripts/
run_and_print_on_failure npm ci --prefix "$BASEDIR"/mission-portal/public/scripts/
# build react components
npm run build --prefix $BASEDIR/mission-portal/public/scripts/
run_and_print_on_failure npm run build --prefix "$BASEDIR"/mission-portal/public/scripts/
# remove the packages specified in devDependencies
npm prune --omit=dev --prefix $BASEDIR/mission-portal/public/scripts/

run_and_print_on_failure npm prune --omit=dev --prefix "$BASEDIR"/mission-portal/public/scripts/
fi
)

echo "$(basename "$0"): Debug: Installing PHP composer dependencies from mission-portal repository..."
(
if test -f "$BASEDIR/mission-portal/composer.json"; then
cd $BASEDIR/mission-portal
if test -f "$BASEDIR"/mission-portal/composer.json; then
cd "$BASEDIR"/mission-portal
# install PHP dependencies from composer
php /usr/bin/composer.phar install --no-dev
run_and_print_on_failure php /usr/bin/composer.phar install --no-dev
fi
)

echo "$(basename "$0"): Debug: Installing PHP composer dependencies from nova repository..."
(
if test -f "$BASEDIR/nova/api/http/composer.json"; then
cd $BASEDIR/nova/api/http
if test -f "$BASEDIR"/nova/api/http/composer.json; then
cd "$BASEDIR"/nova/api/http
# install PHP dependencies from composer
php /usr/bin/composer.phar install --no-dev --ignore-platform-reqs
run_and_print_on_failure php /usr/bin/composer.phar install --no-dev --ignore-platform-reqs
fi
)

echo "$(basename "$0"): Debug: Compiling Mission Portal styles..."
(
if test -f "$BASEDIR/mission-portal/public/themes/default/bootstrap/cfengine_theme.less"; then
cd $BASEDIR/mission-portal/public/themes/default/bootstrap
npx -p less lessc --compress ./cfengine_theme.less ./compiled/css/cfengine.less.css
if test -f "$BASEDIR"/mission-portal/public/themes/default/bootstrap/cfengine_theme.less; then
cd "$BASEDIR"/mission-portal/public/themes/default/bootstrap
run_and_print_on_failure npx -p less lessc --compress ./cfengine_theme.less ./compiled/css/cfengine.less.css
fi
)

echo "$(basename "$0"): Debug: Installing LDAP API PHP composer dependencies..."
(
if test -f "$BASEDIR/mission-portal/ldap/composer.json"; then
cd $BASEDIR/mission-portal/ldap
if test -f "$BASEDIR"/mission-portal/ldap/composer.json; then
cd "$BASEDIR"/mission-portal/ldap
# install PHP dependencies from composer
php /usr/bin/composer.phar install --no-dev
run_and_print_on_failure php /usr/bin/composer.phar install --no-dev
fi
)

Loading