diff options
author | 2016-11-30 20:33:52 +0100 | |
---|---|---|
committer | 2016-11-30 20:37:12 +0100 | |
commit | 6643e51c254d2dfed65f03568d1ce11e000f01c3 (patch) | |
tree | 21e49ccd23ea04e241cb5b7cd52d80164e2a3d2d /docker | |
parent | 46067f331f1acb9512795a04b6f2f095b5470ecd (diff) |
Initial content
Change-Id: I330ad2e8b08a4743e752b8fe9d1ccf9fa1215f8a
Signed-off-by: Stephane Desneux <stephane.desneux@iot.bzh>
Diffstat (limited to 'docker')
-rw-r--r-- | docker/firstrun.sh | 37 | ||||
-rw-r--r-- | docker/image.conf | 19 | ||||
-rwxr-xr-x | docker/setup_image.sh | 104 | ||||
-rwxr-xr-x | docker/wait_for_net.sh | 19 |
4 files changed, 179 insertions, 0 deletions
diff --git a/docker/firstrun.sh b/docker/firstrun.sh new file mode 100644 index 0000000..2b70a92 --- /dev/null +++ b/docker/firstrun.sh @@ -0,0 +1,37 @@ +#!/bin/bash -x + +# fail at first error ! +set -e + +# redirect outputs to log +LOGFILE=/var/log/firstrun.log +exec >$LOGFILE 2>&1 + +echo $(date +"%Y%m%d %H:%M:%S") Starting firstrun script + +# source same file with .conf extension +. ${0%.sh}.conf + +# run the scripts found in /root/firstrun.d + +for x in /root/firstrun.d/*; do + case $(basename $x) in + [0-9][0-9]_*) + echo "--------------------- start script $x ---------------------" + . $x + echo "--------------------- end of script $x ---------------------" + ;; + *) + ;; + esac + +done + +######################### cleanup ##################### + +# remove firstrun +rm -rf /root/firstrun.* +rm -rf /etc/systemd/system/multi-user.target.wants/firstrun.* +systemctl daemon-reload + +rm -f $LOGFILE diff --git a/docker/image.conf b/docker/image.conf new file mode 100644 index 0000000..27e173f --- /dev/null +++ b/docker/image.conf @@ -0,0 +1,19 @@ +FIRSTRUN=no + +TIMEZONE=Europe/Paris + +XDT_DIR=/xdt + +DEVUSER=devel +DEVUSER_UID=1664 +DEVUSER_PASSWORD=$DEVUSER +DEVUSER_HOME=/home/$DEVUSER + +XDT_META=$XDT_DIR/meta +XDT_DOWNLOADCACHE=$XDT_DIR/downloads +XDT_SSTATECACHE=$XDT_DIR/sstate-cache +XDT_CCACHE=$XDT_DIR/ccache +XDT_BUILD=$XDT_DIR/build +XDT_WORKSPACE=$XDT_DIR/workspace +XDT_SOURCES=$XDT_DIR/sources +XDT_SDK=$XDT_DIR/sdk diff --git a/docker/setup_image.sh b/docker/setup_image.sh new file mode 100755 index 0000000..77a35da --- /dev/null +++ b/docker/setup_image.sh @@ -0,0 +1,104 @@ +#!/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 +} +trap debug ERR # on error, run a sleep tp debug container + +# get the INSTALL dir (the one where we were launched) +INSTDIR=$(cd $(dirname $0)/.. && pwd -P) +echo "Detected container install dir = $INSTDIR" + +################################## variables ################################# + +# source variables in conf file +. $INSTDIR/docker/image.conf + +################################## install docker endpoint ##################### + +# install the entrypoint script in /usr/bin +install --mode=755 $INSTDIR/docker/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/docker/firstrun.sh /root/firstrun.sh + install --mode=644 $INSTDIR/docker/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 <<EOF >/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 ############## + +for x in $INSTDIR/setup.d/*; do + case $(basename $x) in + [0-9][0-9]_*) + echo "--------------------- start script $x ---------------------" + . $x + echo "--------------------- end of script $x ---------------------" + ;; + *) + ;; + esac +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 -----------------------" + diff --git a/docker/wait_for_net.sh b/docker/wait_for_net.sh new file mode 100755 index 0000000..354e76a --- /dev/null +++ b/docker/wait_for_net.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +### this script is used as entrypoint of the docker container to wait for network to be up ### + +IFACE="veth0 eth0" + +function wait_net() { + for i in $IFACE; do + [[ "$(cat /sys/class/net/$i/operstate 2>/dev/null)" == "up" ]] && return 1 + done + return 0 +} + +while wait_net; do + sleep 1 +done + +[[ $# > 0 ]] && exec "$@" +exec /bin/bash -l |