#!/usr/bin/env bash # vim: ft=sh set -e function headIsDetached() { [[ $(git rev-parse --abbrev-ref --symbolic-full-name HEAD) == "HEAD" ]]; } function binaryExists() { ls build/fluffy_${COMMIT} 2>&1 1>/dev/null } function fetchChanges() { # We cannot use "git pull" in here, because history may be changed upstream git fetch git reset --hard "origin/${BRANCH}" } function buildBinaries() { # Control number of jobs used to lower impact on running nodes export MAKEFLAGS="-j{{ nimbus_fluffy_build_jobs | int }}" make update OVERRIDE=1 make fluffy \ LOG_LEVEL="{{ nimbus_fluffy_build_log_level }}" \ NIMFLAGS="{{ nimbus_fluffy_build_nim_flags | join(' ') }}" # Rename binaries to match commit they were built from. mv "build/fluffy" "build/fluffy_${COMMIT}" # Create a symbolic link to the latest version ln -frs build/fluffy_${COMMIT} build/fluffy # Delete copies that are older than N days find build -mtime +{{ nimbus_fluffy_build_days_kept }} -exec rm '{}' \+ } #------------------------------------------------------------------------------- BRANCH="{{ nimbus_fluffy_repo_branch }}" SERVICE="{{ nimbus_fluffy_build_restart_service }}" echo " >>> Build Start: $(date -u +"%Y-%m-%dT%H:%M:%SZ")" if [[ "${USER}" != "{{ nimbus_fluffy_user }}" ]]; then echo "Incorrect user: ${USER}" >&2 echo "Expected: {{ nimbus_fluffy_user }}" >&2 exit 1 fi # Detached HEAD means we're probably on a tag if headIsDetached; then echo " >>> Deatached HEAD, nothing to fetch." else echo " >>> Fetching changes..." fetchChanges fi COMMIT=$(git rev-parse --short=8 HEAD) if binaryExists && [[ "$1" != "--force" ]]; then echo " >>> Binary already built" exit 0 else echo " >>> Building binaries..." buildBinaries fi {% if nimbus_fluffy_build_restarts_service %} # Avoid faiure on first Ansible run due to missing service. if [[ $(systemctl is-active "${SERVICE}" || true) == "inactive" ]]; then echo " !!! No service to restart!" exit else echo " >>> Restarting service..." sudo systemctl restart "${SERVICE}" fi {% endif %} echo " >>> SUCCESS"