#!/bin/bash -x ### this script is called when building the docker image: it's executing in a temp container echo "------------------------ Starting $(basename $0) -----------------------" function debug() { set +x echo "-------------------------" echo "Command failed." echo "Running sleep for 1 day. To proceed:" echo "* run 'killall sleep' to continue" echo "* run 'killall -9 sleep' to abort the build" sig=0 sleep 86400 || sig=$? # if killed -9, then abort [[ $sig == 137 ]] && exit 1 # abort set -x return 0 # continue } function crash() { set +x echo " -------------------------" echo "| CRASH HANDLER TRIGGERED |" echo " -------------------------" exit 1 } # get the INSTALL dir (the one where we were launched) INSTDIR=$(cd $(dirname $0) && pwd -P) echo "Detected container install dir = $INSTDIR" if [[ -f $INSTDIR/DEBUG ]]; then echo "#### INTERACTIVE DEBUG MODE IS ACTIVE ####" trap debug ERR # on error, run a sleep tp debug container else echo "#### JENKINS MODE MODE IS ACTIVE ####" trap crash ERR # on error, simply crash fi ################################## variables ################################# # source variables in conf file . $INSTDIR/image.conf # source flavour config file (generated by top Makefile) . $INSTDIR/flavour.conf if [[ -z "$CONTAINER_TYPE" ]]; then grep -q docker /proc/self/cgroup && CONTAINER_TYPE="docker" fi ################################## install docker endpoint ##################### # install the entrypoint script in /usr/bin install --mode=755 $INSTDIR/wait_for_net.sh /usr/bin/ ################################## install first-run service ################### # all operations requiring runnning daemons (inc. systemd) must be run at first # container instanciation if [[ "$FIRSTRUN" == "yes" ]]; then install --mode=755 $INSTDIR/firstrun.sh /root/firstrun.sh install --mode=644 $INSTDIR/image.conf /root/firstrun.conf [[ -d $INSTDIR/firstrun.d ]] && cp -a $INSTDIR/firstrun.d /root/ mkdir -p /etc/systemd/system/multi-user.target.wants/ cat </etc/systemd/system/multi-user.target.wants/firstrun.service [Unit] Description=Firstrun service After=network.target [Service] Type=oneshot ExecStart=-/bin/bash -c /root/firstrun.sh TimeoutSec=0 StandardInput=null RemainAfterExit=yes [Install] WantedBy=multi-user.target EOF fi # helper func to install a firstrun hook function firstrun_add() { script=$1 level=${2:-50} name=${3:-$(basename $script)} mkdir -p /root/firstrun.d cp $script /root/firstrun.d/${level}_$name } ################################## adjust system timezone ############################ ln -sf ../usr/share/zoneinfo/$TIMEZONE /etc/localtime ################################## run other scripts in turn ############## function enumerate_tasks() { for script in $INSTDIR/common.d/*; do case $(basename $script) in [0-9][0-9]_*) echo $(basename $script):$script ;; esac done for tsk in $(cat $INSTDIR/flavours/$FLAVOUR.tasks | sed 's/#.*$//g'); do if [[ -f $INSTDIR/tasks.d/$tsk ]]; then echo $tsk:$INSTDIR/tasks.d/$tsk else # fail to find task echo "$INSTDIR/flavours/$FLAVOUR.tasks: invalid task '$tsk'" >&2 return 1 fi done return 0 } for script in $(enumerate_tasks | sort -k1 -t':' | cut -f2 -d':'); do echo "--------------------- start script $script ---------------------" . $script echo "--------------------- end of script $script ---------------------" done ############################### cleanup ################################### cd / apt-get clean # clean apt caches rm -rf /var/lib/apt/lists/* rm -rf $INSTDIR # yes, I can auto-terminate myself ! # cleanup /tmp without removing the dir for x in $(find /tmp -mindepth 1); do rm -rf $x || true done echo "------------------------ $(basename $0) finished -----------------------"