#!/bin/bash # (c) 2014-2016 Freifunk Hochstift # # This script builds the firmware by the environment variables given, the # first two being mandatory: # # BASE = Gluon Version (tag or commit, i.e. v2014.4) # BRANCH = Firmware Branch (stable/testing/experimental) # VERSION = the version tag (can only be empty if BRANCH=experimental) # # optional: # AUTOUPDATER = force Autoupdater Branch (stable/testing/experimental/off) # BROKEN = 0 (default) or 1, build the untested hardware model firmwares, too # BUILD_TS = build timestamp (format: %Y-%m-%d %H:%M:%S) # CLEAN = 'dirclean' to perform "make dirclean" before build or 'clean' to perform "make clean" (default: 'none') # DEVICES = build some specific devices and not the entire target # KEY_DIR = specify directory for gluon-opkg-key # MAKEJOBS = number of compiler processes running in parallel (default: number of CPUs/Cores) # PRIORITY = determines the number of day a rollout phase should last at most # PUBLISH = 0 (default) or 1, publish firmware at the end # SITE_ID = specific site repository commit-id (leave blank to use HEAD) # TARGETS = a space separated list of target platforms (if unset, all platforms will be build) # VERBOSE = 0 (default) or 1, call the make commands with 'V=s' to see actual errors better # VERSIONS_DIR = specify directory for version files # ### includes . functions.sh ### static variables MY_DIR=$(dirname $0) MY_DIR=$(readlink -f "${MY_DIR}") DEFAULT_KEY_DIR="${MY_DIR}/opkg-keys" DEFAULT_VERSIONS_DIR="${MY_DIR}/versions" CODE_DIR="${MY_DIR}/src" GLUON_BUILD_DIR="${CODE_DIR}/lede" SITE_DIR="${CODE_DIR}/site" PATCH_DIR="${SITE_DIR}/patches" OUTPUT_DIR="${MY_DIR}/output" IMAGE_DIR="${CODE_DIR}/output/images" PACKAGES_DIR="${CODE_DIR}/output/packages" MANIFEST_DIR="${CODE_DIR}/output/images/sysupgrade" SITE_GEN_SCRIPT="${CODE_DIR}/packages/ffho/ffho/ffho-site-generate/scripts/gen-site-conf.lua" SRV_URL="firmware.in.ffho.net" SRV_USER="firmware" SRV_PATH="/srv/firmware" BUILD_INFO_FILENAME="build-info.txt" SITE_REPO_URL="https://git.ffho.net/freifunkhochstift/ffho-site.git" GLUON_REPO_URL="https://git.ffho.net/freifunkhochstift/gluon.git" LANG=C pushd ${MY_DIR} > /dev/null ### set reasonable defaults for unset environment variables [ -n "${AUTOUPDATER}" ] || AUTOUPDATER=${BRANCH} if [ -n "${BROKEN}" ]; then if [ "${BROKEN}" -eq "1" ]; then export BROKEN else unset BROKEN fi fi [ -n "${BUILD_TS}" ] || BUILD_TS=$(date +"%Y-%m-%d %H:%M:%S") [ -n "${CLEAN}" ] || CLEAN="none" if [ -n "${KEY_DIR}" ]; then KEY_DIR=$(readlink -f "${KEY_DIR}") [ -e "${KEY_DIR}" ] || abort "Can not find specified key directory: ${KEY_DIR}" else KEY_DIR="${DEFAULT_KEY_DIR}" fi [ -n "${MAKEJOBS}" ] || MAKEJOBS=$(grep -c "^processor" /proc/cpuinfo) [ -n "${PRIORITY}" ] || PRIORITY=0 [ -n "${PUBLISH}" ] || PUBLISH=0 [ -n "${VERBOSE}" ] || VERBOSE=0 if [ -n "${VERSIONS_DIR}" ]; then VERSIONS_DIR=$(readlink -f "${VERSIONS_DIR}") else VERSIONS_DIR="${DEFAULT_VERSIONS_DIR}" fi MAKE_PARAM="" [ "${VERBOSE}" -eq "1" ] && MAKE_PARAM="${MAKE_PARAM} V=s" [ -n "${DEVICE}" ] && MAKE_PARAM="${MAKE_PARAM} DEVICE=${DEVICE}" ### ERROR handling [ -n "${BASE}" ] || abort "Please specify BASE environment variable (Gluon, i.e. 'v2014.3' or commit-id)." [ -n "${BRANCH}" ] || abort "Please specify BRANCH environment variable." [ "${BRANCH}" == "experimental" -o "${BASE}" != "HEAD" ] || abort "HEAD is not an allowed BASE-identifier for non-experimental builds. Either use a tagged commit or the commit-SHA itself." [ -n "${VERSION}" -o "${BRANCH}" == "experimental" ] || abort "Please specify VERSION environment variable (not necessary for experimental branch)." [ "${BRANCH}" == "experimental" -o ! -r "${VERSIONS_DIR}/${VERSION}" ] || abort "There exists a version file for '${VERSION}' ... you are trying to do something really stupid, aren't you?" ### init VERSIONS_DIR if necessary if [ ! -d "${VERSIONS_DIR}" ]; then info "Versions directory does not exist yet - creating it ..." mkdir -p ${VERSIONS_DIR} [ "$?" -eq "0" ] || abort "Unable to create versions directory: ${VERSIONS_DIR}" fi ### init CODE_DIR if necessary if [ ! -d "${CODE_DIR}" ]; then info "Code directory does not exist yet - fetching Gluon ..." git clone "${GLUON_REPO_URL}" "${CODE_DIR}" [ "$?" -eq "0" ] || abort "Failed to fetch Gluon repository." fi ### init SITE_DIR if necessary if [ ! -d "${SITE_DIR}" ]; then info "Site repository does not exist, fetching it ..." git clone "${SITE_REPO_URL}" "${SITE_DIR}" [ "$?" -eq "0" ] || abort "Failed to fetch SITE repository." fi ### CHECKOUT GLUON progress "Checking out GLUON '${BASE}' ..." pushd ${CODE_DIR} > /dev/null # check if gluon got modified and bail out if necessary [ "$(git status --porcelain)" ] && abort "Local changes to peers directory. Cowardly refusing to update gluon repository." >&2 git fetch git checkout -q ${BASE} [ "$?" -eq "0" ] || abort "Failed to checkout '${BASE}' gluon base version, mimimi." >&2 git show-ref --verify --quiet refs/remotes/origin/${BASE} if [ "$?" -eq "0" ]; then git pull [ "$?" -eq "0" ] || abort "Failed to get newest '${BASE}' in gluon repository, mimimi." fi GLUON_COMMIT=$(git rev-list --max-count=1 HEAD) popd > /dev/null #${CODE_DIR} ### CHECKOUT SITE REPO progress "Checking out SITE REPO ..." pushd ${SITE_DIR} > /dev/null if [ $(git remote | wc -l) -ge "1" ]; then git fetch # TODO: check if site got modified locally and bail out if necessary if [ -z "${SITE_ID}" ]; then # no specific site given - get the most current one git checkout -q ${BRANCH} git branch -r | grep ${BRANCH} > /dev/null if [ "$?" -eq "0" ]; then git rebase [ "$?" -eq "0" ] || abort "Failed to get newest '${BRANCH}' in site repository, mimimi." fi else # fetch site repo updates git fetch || true # commit given - use this one git checkout -q ${SITE_ID} [ "$?" -eq "0" ] || abort "Failed to checkout requested site commit '${SITE_ID}', mimimi." fi fi SITE_COMMIT=$(git rev-list --max-count=1 HEAD) popd > /dev/null #${SITE_DIR} pushd ${CODE_DIR} > /dev/null ### DIRCLEAN if [ -d "${GLUON_BUILD_DIR}/" -a "${CLEAN}" == "dirclean" ]; then progress "Cleaning your build environment (make dirclean) ..." make dirclean fi ### PREPARE progress "Preparing the build environment (make update) ..." make update [ "$?" -eq "0" ] || abort "Failed to update the build environment, mimimi." ### set reasonable defaults for ${TARGETS} and ${BRANCH} if unset if [ -z "${TARGETS}" ]; then TARGETS=$(make list-targets | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') info "building all targets: '${TARGETS}'" fi if [ "${BRANCH}" == "experimental" -a -z "${VERSION}" ] ; then VERSION=$(make show-release) info "EXPERIMENTAL FIRMWARE: using version tag '${VERSION}'" fi popd > /dev/null #${CODE_DIR} # we are now ready to produce the firmware images, so let's "save" our state build_info_path="${VERSIONS_DIR}/${VERSION}" progress "Saving build information to: ${build_info_path}" [ -n "${build_info_path}" -a -f "${build_info_path}" ] && rm -f ${build_info_path} mkdir -p $(dirname ${build_info_path}) [ "$?" -eq "0" ] || abort "Unable to create output directory: $(dirname ${build_info_path})" touch $(dirname ${build_info_path}) [ "$?" -eq "0" ] || abort "Cannot create build information file: ${build_info_path}" echo "VERSION=${VERSION}" >> ${build_info_path} echo "GLUON=${GLUON_COMMIT} # ${BASE}" >> ${build_info_path} echo "BRANCH=${BRANCH}" >> ${build_info_path} echo "SITE=${SITE_COMMIT} # ${VERSION}" >> ${build_info_path} echo "TARGETS=${TARGETS}" >> ${build_info_path} echo "TS=${BUILD_TS}" >> ${build_info_path} ### restore opkg-keys if [ -e "${KEY_DIR}" ]; then info "build key already exists, restoring it." mkdir -p ${GLUON_BUILD_DIR}/ [ "$?" -eq "0" ] || abort "Unable to create directory: ${GLUON_BUILD_DIR}/" cp -f ${KEY_DIR}/* ${GLUON_BUILD_DIR}/ [ "$?" -eq "0" ] || abort "Unable to copy build key." fi ### create site.conf if [ -x "${SITE_GEN_SCRIPT}" ]; then progress "Generating defaut site.conf" eval GLUON_SITEDIR=${SITE_DIR} ${SITE_GEN_SCRIPT} fi ### BUILD FIRMWARE progress "Building the firmware - please stand by!" pushd ${CODE_DIR} > /dev/null export GLUON_RELEASE="${VERSION}" [ "${AUTOUPDATER}" != "off" ] && export GLUON_BRANCH="${AUTOUPDATER}" for target in ${TARGETS} ; do # configure build environment for our current target export GLUON_TARGET="${target}" # prepare build environment for our current target if [ "${CLEAN}" == "clean" ]; then progress "${target}: Preparing build environment. (make clean)" make clean [ "$?" -eq "0" ] || abort "${target}: Unable to clean environment." fi # now we can start building the images for the target platform progress "${target}: Building FFHO-flavoured Gluon firmware. You'd better go and fetch some c0ffee!" make -j ${MAKEJOBS} ${MAKE_PARAM} [ "$?" -eq "0" ] || abort "${target}: Unable to build firmware." done # generate manifest progress "Generating manifest ..." GLUON_PRIORITY=${PRIORITY} GLUON_BRANCH=${BRANCH} make manifest [ "$?" -eq "0" ] || abort "Failed to generate the manifest." popd > /dev/null #${CODE_DIR} if [ "${PUBLISH}" -eq "1" ]; then # copying firmware to the server if [ -d "${IMAGE_DIR}" ]; then progress "Copying firmware images ..." rsync -rlutzc --filter="+ */" --filter="+ *-${VERSION}-*" --filter="- *" -e ssh ${IMAGE_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION} [ "$?" -eq "0" ] || abort "Failed to copy firmware images." fi # copying packages to the server if [ -d "${PACKAGES_DIR}" ]; then progress "Copying packages ..." PACKAGES_DIR=$(find ${PACKAGES_DIR}/ -maxdepth 1 -name *${VERSION}) rsync -rlutzc -e ssh ${PACKAGES_DIR}/ ${SRV_USER}@${SRV_URL}:${SRV_PATH}/${VERSION}/packages [ "$?" -eq "0" ] || abort "Failed to copy packages." fi # copy manifest and the build info file progress "Copying manifest and build info file ..." pushd ${OUTPUT_DIR} > /dev/null git fetch git checkout -q signing git merge --ff-only origin/master mkdir -p "${OUTPUT_DIR}/${VERSION}/sysupgrade" cp -f "${MANIFEST_DIR}/${BRANCH}.manifest" "${OUTPUT_DIR}/${VERSION}/sysupgrade/" cp -f "${build_info_path}" "${OUTPUT_DIR}/${VERSION}/${BUILD_INFO_FILENAME}" popd > /dev/null #${OUTPUT_DIR} # Update symlinks progress "Update symlink for ${BRANCH}" pushd ${OUTPUT_DIR} > /dev/null ln -fsn ${VERSION} ${BRANCH} popd > /dev/null #${OUTPUT_DIR} fi # The end. Finally. success "We're done, go and enjoy your new firmware (${VERSION})!" popd > /dev/null #${MY_DIR}