diff options
author | Christophe Lyon <christophe.lyon@linaro.org> | 2017-02-03 14:14:51 +0000 |
---|---|---|
committer | Christophe Lyon <christophe.lyon@linaro.org> | 2017-02-03 14:30:24 +0000 |
commit | f33002a6126f721fb5e55c0f83f0afe321bb3cb8 (patch) | |
tree | b100f39b405cd5bbc41401b09dab5806fc132aa4 | |
parent | 7dc65c4426b7175b54ab472d57e62d68df8b574e (diff) |
start-container-*.sh: Parameters now require option flags.
This makes it easier/clearer to support optional parameters.
Improve diagnotics and consistency.
start-container-none.sh: exit with error if the requested
arch or distro do not match the host's.
Change-Id: I9c1d27db1ac679da217e7fae4a8cd15773ffec17
-rwxr-xr-x | start-container-docker.sh | 99 | ||||
-rwxr-xr-x | start-container-none.sh | 83 | ||||
-rwxr-xr-x | start-container-schroot.sh | 68 |
3 files changed, 204 insertions, 46 deletions
diff --git a/start-container-docker.sh b/start-container-docker.sh index 0e40d136..45f2ceb0 100755 --- a/start-container-docker.sh +++ b/start-container-docker.sh @@ -12,43 +12,86 @@ set -e # - definition of ${session_host} and ${session_port}, can be used for # a remote connexion to the container -if [ $# -ne 2 -a $# -ne 3 ]; then - echo Usage: $0 arch flavour [host] - echo " arch: architecture (eg: amd64, i386, arm64, armhf)" - echo " flavour: distribution (eg: trusty)" - echo " host: hostname where the container will run, defaults to localhost" - echo " useful if the name resolution does not work correctly" +usage() { + echo Usage: $0 --arch container-arch --distro flavour [--session-host host] [--session-name name] + echo + echo " container-arch: architecture (eg: amd64, i386, arm64, armhf)" + echo " distro: distribution (eg: trusty)" + echo " session-host: hostname where the container will run, defaults to localhost" + echo " useful if the name resolution does not work correctly" + echo " session-name: session, in case the default '$BUILD_NUMBER-$JOB_NAME' is not suitable" exit 1 -fi +} -# BUILD_NUMBER, JOB_NAME and WORKSPACE are set by Jenkins -if [ "x$BUILD_NUMBER" = "x" ]; then - echo "BUILD_NUMBER is not defined. Are you executing this from Jenkins ?" - exit 1 -fi -if [ "x$JOB_NAME" = "x" ]; then - echo "JOB_NAME is not defined. Are you executing this from Jenkins ?" - exit 1 +# Save stdout/stderr file descriptors +exec 3>&1 4>&2 + +# Make sure all output goes to stderr +exec 1>&2 + +container_arch= +distro= +session_host= +session_name= + +while [ $# -ge 1 ] +do + case $1 in + --arch) + container_arch=$2 + [ x${container_arch} = x ] && usage + shift 2 + ;; + --distro) + distro=$2 + [ x${distro} = x ] && usage + shift 2 + ;; + --session-host) + session_host=$2 + [ x${session_host} = x ] && usage + shift 2 + ;; + --session-name) + session_name=$2 + [ x${session_name} = x ] && usage + shift 2 + ;; + *) + echo "Unsupported option: $1" + usage + ;; + esac +done + +[ x${container_arch} = x ] && usage +[ x${distro} = x ] && usage + +[ x"$session_host" = x ] && session_host=localhost +session_host=${session_host}.tcwglab + +if [ x"$session_name" = x ]; then + # Set the default session_name, using BUILD_NUMBER and JOB_NAME, + # as set by Jenkins. + if [ "x$BUILD_NUMBER" = "x" ]; then + echo "BUILD_NUMBER is not defined. Are you executing this from Jenkins ?" + exit 1 + fi + if [ "x$JOB_NAME" = "x" ]; then + echo "JOB_NAME is not defined. Are you executing this from Jenkins ?" + exit 1 + fi + session_name=$(echo $BUILD_NUMBER-$JOB_NAME | sed -e "s#[/=,+]#-#g") fi + +# We want to bind-mount WORKSPACE, so make sure it is defined if [ "x$WORKSPACE" = "x" ]; then echo "WORKSPACE is not defined. Are you executing this from Jenkins ?" exit 1 fi -container_arch=$1 -distro=$2 -session_host=$3 -[ x"$session_host" = x ] && session_host=localhost -session_host=${session_host}.tcwglab - task="build" -# Save stdout/stderr file descriptors -exec 3>&1 4>&2 - -# Make sure all output goes to stderr -exec 1>&2 - case "${distro}:${task}" in "trusty":"build") image=linaro/ci-${container_arch}-tcwg-ubuntu:${distro} @@ -60,7 +103,7 @@ esac DOCKER="docker -H $session_host:2375" $DOCKER pull $image -session_name=$(echo $BUILD_NUMBER-$JOB_NAME | sed -e "s#[/=,+]#-#g") + session_id=$($DOCKER run --name $session_name -dtP \ -v $HOME/snapshots-ref:$HOME/snapshots-ref:ro \ -v $WORKSPACE:$WORKSPACE \ diff --git a/start-container-none.sh b/start-container-none.sh index 9add7459..744810df 100755 --- a/start-container-none.sh +++ b/start-container-none.sh @@ -14,29 +14,96 @@ set -e # - definition of ${session_host} and ${session_port}, can be used for # a remote connexion to the container -if [ $# -ne 2 -a $# -ne 3 ]; then - echo Usage: $0 arch flavour [host] - echo " arch: architecture (eg: amd64, i386, arm64, armhf)" - echo " flavour: distribution (eg: trusty)" - echo " host: hostname where the container will run, defaults to localhost" +usage() { + echo Usage: $0 --arch container-arch --distro flavour [--session-host host] [--session-name name] + echo + echo " container-arch: architecture (eg: amd64, i386, arm64, armhf)" + echo " distro: distribution (eg: trusty)" + echo " session-host: hostname where the container will run, defaults to localhost" + echo " useful if the name resolution does not work correctly" + echo " session-name: session, in case the default '$BUILD_NUMBER-$JOB_NAME' is not suitable" + exit 1 +} + +# Save stdout/stderr file descriptors +exec 3>&1 4>&2 + +# Make sure all output goes to stderr +exec 1>&2 + +container_arch= +distro= +session_host= +session_name= + +while [ $# -ge 1 ] +do + case $1 in + --arch) + container_arch=$2 + [ x${container_arch} = x ] && usage + shift 2 + ;; + --distro) + distro=$2 + [ x${distro} = x ] && usage + shift 2 + ;; + --session-host) + session_host=$2 + [ x${session_host} = x ] && usage + shift 2 + ;; + --session-name) + session_name=$2 + [ x${session_name} = x ] && usage + shift 2 + ;; + *) + echo "Unsupported option: $1" + usage + ;; + esac +done + +[ x${container_arch} = x ] && usage +[ x${distro} = x ] && usage + +case $(uname -m) in + x86_64) my_arch=x86_64 ;; + i686) my_arch=i386 ;; + aarch64) my_arch=arm64 ;; + armv7l|armv8l) my_arch=armhf ;; + *) my_arch=$(uname -m) ;; +esac + +if [ x${container_arch} != x${my_arch} ]; then + echo "Error: requested container arch (${container_arch}) does not match this machine's arch (${my_arch})" exit 1 fi -session_host=$3 +my_distro=$(lsb_release -c | awk '{print $2;}') +if [ x${distro} != x${my_distro} ]; then + echo "Error: requested distro (${distro}) does not match this machine's distro (${my_distro})" + exit 1 +fi if [ x"$DOCKER_HOST" = x"" ]; then [ x"$session_host" = x ] && session_host=localhost session_host=${session_host}.tcwglab - session_port="22" + session_port=22 else session_host="$(echo $DOCKER_HOST | sed -e "s#^tcp://\(.*\):.*#\1#")" session_port="$(ssh $session_host docker port $DOCKER_CONTAINER_ID 22)" session_port="$(echo $session_port | cut -d: -f 2)" fi +# Restore stdout/stderr +exec 1>&3 2>&4 + cat <<EOF ulimit -u 5000 -CONTAINER="bash -c" +CONTAINER="ssh -p ${session_port} -A ${session_host}" session_host=${session_host} session_port=${session_port} EOF diff --git a/start-container-schroot.sh b/start-container-schroot.sh index 6f62e51d..3fdc5727 100755 --- a/start-container-schroot.sh +++ b/start-container-schroot.sh @@ -11,29 +11,77 @@ set -e # to run inside the container. # - definition of ${session_host} and ${session_port}, can be used for # a remote connexion to the container -if [ $# -ne 2 -a $# -ne 3 ]; then - echo Usage: $0 arch flavour [host] - echo " arch: architecture (eg: amd64, i386, arm64, armhf)" - echo " flavour: distribution (eg: trusty)" - echo " host: hostname where the container will run, defaults to localhost" + +usage() { + echo Usage: $0 --arch container-arch --distro flavour [--session-host host] [--session-name name] + echo + echo " container-arch: architecture (eg: amd64, i386, arm64, armhf)" + echo " distro: distribution (eg: trusty)" + echo " session-host: hostname where the container will run, defaults to localhost" + echo " useful if the name resolution does not work correctly" + echo " session-name: session, in case the default '$BUILD_NUMBER-$JOB_NAME' is not suitable" exit 1 -fi +} + +# Save stdout/stderr file descriptors +exec 3>&1 4>&2 + +# Make sure all output goes to stderr +exec 1>&2 + +container_arch= +distro= +session_host= +session_name= + +while [ $# -ge 1 ] +do + case $1 in + --arch) + container_arch=$2 + [ x${container_arch} = x ] && usage + shift 2 + ;; + --distro) + distro=$2 + [ x${distro} = x ] && usage + shift 2 + ;; + --session-host) + session_host=$2 + [ x${session_host} = x ] && usage + shift 2 + ;; + --session-name) + session_name=$2 + [ x${session_name} = x ] && usage + shift 2 + ;; + *) + echo "Unsupported option: $1" + usage + ;; + esac +done + +[ x${container_arch} = x ] && usage +[ x${distro} = x ] && usage -container_arch=$1 -distro=$2 -session_host=$3 [ x"$session_host" = x ] && session_host=localhost session_host=${session_host}.tcwglab session_port=22 schroot_image="tcwg-build-${container_arch}-${distro}" -SCHROOT="ssh ${session_host} schroot" +SCHROOT="ssh -p ${session_port} -A ${session_host} schroot" session_id=$(${SCHROOT} -b -c chroot:$schroot_image --preserve-environment) if [ x"$session_id" = x ]; then exit 1 fi +# Restore stdout/stderr +exec 1>&3 2>&4 + cat <<EOF ulimit -u 5000 # FIXME how to support this remotely? # Sometimes /dev/pts can't get unmounted on the first try. |