2018-09-07 19:24:35 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# kas - setup tool for bitbake based projects
|
|
|
|
#
|
2019-08-30 13:41:16 +02:00
|
|
|
# Copyright (c) Siemens AG, 2018-2019
|
2018-09-07 19:24:35 +02:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Jan Kiszka <jan.kiszka@siemens.com>
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
2018-12-14 17:43:05 +01:00
|
|
|
printf "%b" "Usage: $0 [OPTIONS] { build | shell } [KASOPTIONS] KASFILE\n"
|
2018-11-07 14:30:03 +01:00
|
|
|
printf "%b" " $0 [OPTIONS] clean\n"
|
|
|
|
printf "%b" "\nPositional arguments:\n"
|
2019-02-07 08:14:58 +01:00
|
|
|
printf "%b" "build\t\t\tCheck out repositories and build target.\n"
|
|
|
|
printf "%b" "shell\t\t\tRun a shell in the build environment.\n"
|
|
|
|
printf "%b" "clean\t\t\tClean build artifacts, keep downloads.\n"
|
2018-11-07 14:30:03 +01:00
|
|
|
printf "%b" "\nOptional arguments:\n"
|
2019-02-07 08:14:58 +01:00
|
|
|
printf "%b" "--isar\t\t\tUse kas-isar container to build Isar image.\n"
|
2019-08-30 13:41:16 +02:00
|
|
|
printf "%b" "--docker-args\t\tAdditional arguments to pass to docker " \
|
|
|
|
"for running the\n"
|
|
|
|
printf "%b" "\t\t\tbuild.\n"
|
2019-02-07 08:14:58 +01:00
|
|
|
printf "%b" "-v\t\t\tPrint operations.\n"
|
2019-08-30 13:41:16 +02:00
|
|
|
printf "%b" "--ssh-dir\t\tDirectory containing SSH configurations.\n"
|
|
|
|
printf "%b" "\t\t\tAvoid \$HOME/.ssh unless you fully trust the " \
|
|
|
|
"container.\n"
|
2020-05-05 12:02:10 +02:00
|
|
|
printf "%b" "--aws-dir\t\tDirectory containing AWScli configuration.\n"
|
2019-08-30 13:41:16 +02:00
|
|
|
printf "%b" "--no-proxy-from-env\tDo not inherit proxy settings from " \
|
|
|
|
"environment.\n"
|
2018-09-07 19:24:35 +02:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
trace()
|
|
|
|
{
|
2020-03-17 19:58:23 +01:00
|
|
|
[ -n "${VERBOSE}" ] && echo "+ $*"
|
2020-03-17 15:10:56 +01:00
|
|
|
"$@"
|
2018-09-07 19:24:35 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 15:20:17 +02:00
|
|
|
if [ -z "${KAS_IMAGE_VERSION}" ]; then
|
2019-11-20 06:53:15 +01:00
|
|
|
KAS_IMAGE_VERSION="2.0"
|
2019-08-23 15:20:17 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
DOCKER_IMAGE=kasproject/kas:${KAS_IMAGE_VERSION}
|
2018-09-07 19:24:35 +02:00
|
|
|
|
|
|
|
if [ -n "${KAS_WORK_DIR}" ]; then
|
2020-03-17 15:10:56 +01:00
|
|
|
KAS_WORK_DIR=$(readlink -f "${KAS_WORK_DIR}")
|
2018-09-07 19:24:35 +02:00
|
|
|
else
|
2020-03-17 15:10:56 +01:00
|
|
|
KAS_WORK_DIR="$(pwd)"
|
2018-09-07 19:24:35 +02:00
|
|
|
fi
|
|
|
|
|
2018-12-14 17:43:05 +01:00
|
|
|
# parse kas-docker options
|
2018-09-07 19:24:35 +02:00
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
case "$1" in
|
|
|
|
--isar)
|
2019-08-23 15:20:17 +02:00
|
|
|
DOCKER_IMAGE=kasproject/kas-isar:${KAS_IMAGE_VERSION}
|
2018-09-07 19:24:35 +02:00
|
|
|
if ! LOOP_DEV=$(/sbin/losetup -f 2>/dev/null); then
|
|
|
|
if [ $(id -u) -eq 0 ]; then
|
|
|
|
echo "Error: loop device not available!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-11-29 09:06:02 +01:00
|
|
|
sudo_command="/sbin/losetup -f"
|
|
|
|
sudo_message="[sudo] enter password to setup loop"
|
|
|
|
sudo_message="$sudo_message devices by calling"
|
|
|
|
sudo_message="$sudo_message '$sudo_command': "
|
|
|
|
if ! LOOP_DEV=$(sudo -p "$sudo_message" $sudo_command \
|
|
|
|
2>/dev/null); then
|
|
|
|
echo "Error: loop device setup unsuccessful!"
|
|
|
|
echo "try calling '$sudo_command' with root" \
|
|
|
|
"permissions manually."
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-09-07 19:24:35 +02:00
|
|
|
fi
|
|
|
|
ISAR_ARGS="--cap-add=SYS_ADMIN --cap-add=MKNOD --privileged \
|
|
|
|
--device ${LOOP_DEV}"
|
|
|
|
shift 1
|
|
|
|
;;
|
|
|
|
--docker-args)
|
|
|
|
[ $# -gt 0 ] || usage
|
|
|
|
USER_ARGS=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
2018-12-13 10:33:30 +01:00
|
|
|
--ssh-dir)
|
|
|
|
[ $# -gt 2 ] || usage
|
2020-03-17 15:10:56 +01:00
|
|
|
SSH_DIR="$2"
|
2018-12-13 10:33:30 +01:00
|
|
|
shift 2
|
|
|
|
;;
|
2020-05-05 12:02:10 +02:00
|
|
|
--aws-dir)
|
|
|
|
[ $# -gt 2 ] || usage
|
|
|
|
AWS_DIR="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
2019-02-07 08:14:58 +01:00
|
|
|
--no-proxy-from-env)
|
|
|
|
NO_PROXY_FROM_ENV=1
|
|
|
|
shift 1
|
|
|
|
;;
|
2018-09-07 19:24:35 +02:00
|
|
|
-v)
|
|
|
|
VERBOSE=1
|
|
|
|
shift 1
|
|
|
|
;;
|
|
|
|
--*)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
clean)
|
|
|
|
[ $# -eq 1 ] || usage
|
|
|
|
CLEAN_DIR=build/tmp
|
|
|
|
if [ -n "${ISAR_ARGS}" ]; then
|
2020-03-17 15:10:56 +01:00
|
|
|
trace docker run -v "${KAS_WORK_DIR}":/work:rw \
|
2018-09-07 19:24:35 +02:00
|
|
|
--workdir=/work --rm ${ISAR_ARGS} \
|
|
|
|
${DOCKER_IMAGE} \
|
|
|
|
sudo rm -rf ${CLEAN_DIR}
|
|
|
|
else
|
2020-03-17 15:10:56 +01:00
|
|
|
trace rm -rf "${KAS_WORK_DIR}/${CLEAN_DIR}"
|
2018-09-07 19:24:35 +02:00
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
build|shell)
|
|
|
|
CMD=$1
|
2018-12-14 17:43:05 +01:00
|
|
|
shift 1
|
2018-09-07 19:24:35 +02:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
[ -n "${CMD}" ] || usage
|
|
|
|
|
2018-12-14 17:43:05 +01:00
|
|
|
# parse kas sub-command (build or shell) options
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
case "$1" in
|
|
|
|
-h|--help)
|
|
|
|
trace docker run ${DOCKER_IMAGE} ${CMD} --help
|
|
|
|
exit 0
|
|
|
|
;;
|
2019-08-31 12:27:49 +02:00
|
|
|
--skip|--target|--task|-c|--cmd|--command)
|
2020-03-17 15:10:56 +01:00
|
|
|
KAS_OPTIONS="${KAS_OPTIONS} $1 $2"
|
2018-12-14 17:43:05 +01:00
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
KAS_OPTIONS="${KAS_OPTIONS} $1"
|
|
|
|
shift 1
|
|
|
|
;;
|
|
|
|
*)
|
2019-01-02 16:47:59 +01:00
|
|
|
KAS_FILES=
|
|
|
|
for FILE in $(IFS=':'; echo $1); do
|
2020-03-17 15:10:56 +01:00
|
|
|
if ! REAL_FILE="$(realpath -qe "$FILE")"; then
|
2019-01-02 16:47:59 +01:00
|
|
|
echo "Error: configuration file '${FILE}' not found"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ -z "${KAS_FILES}" ]; then
|
2020-03-17 15:10:56 +01:00
|
|
|
FIRST_KAS_FILE="${REAL_FILE}"
|
|
|
|
KAS_FILES="${REAL_FILE}"
|
2019-01-02 16:47:59 +01:00
|
|
|
else
|
2020-03-17 15:10:56 +01:00
|
|
|
KAS_FILES="${KAS_FILES}:${REAL_FILE}"
|
2019-01-02 16:47:59 +01:00
|
|
|
fi
|
|
|
|
done
|
2018-12-14 17:43:05 +01:00
|
|
|
shift 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
[ -n "${FIRST_KAS_FILE}" ] || usage
|
|
|
|
|
2020-03-17 15:10:56 +01:00
|
|
|
KAS_FILE_DIR="$(dirname "${FIRST_KAS_FILE}")"
|
2018-09-07 19:24:35 +02:00
|
|
|
|
2020-03-17 15:10:56 +01:00
|
|
|
REPO_DIR=$(git -C "${KAS_FILE_DIR}" rev-parse --show-toplevel 2>/dev/null) \
|
|
|
|
|| REPO_DIR=$(hg --cwd "${KAS_FILE_DIR}" root 2>/dev/null) \
|
2018-09-07 19:24:35 +02:00
|
|
|
|| REPO_DIR=${KAS_FILE_DIR}
|
|
|
|
|
2020-03-17 15:10:56 +01:00
|
|
|
KAS_FILES=/repo/"$(echo "${KAS_FILES}" | sed 's|'"${REPO_DIR}"'/||g;s|:|:/repo/|g')"
|
2018-09-07 19:24:35 +02:00
|
|
|
|
2020-03-17 15:10:56 +01:00
|
|
|
trace mkdir -p "${KAS_WORK_DIR}"
|
2018-09-07 19:24:35 +02:00
|
|
|
|
2020-05-15 17:15:18 +02:00
|
|
|
if [ $(id -u) -eq 0 ] && [ "${KAS_ALLOW_ROOT}" != "yes" ] ; then
|
|
|
|
echo "Error: Running as root - may break certain recipes."
|
|
|
|
echo "Better give a regular user docker access. Set" \
|
|
|
|
"KAS_ALLOW_ROOT=yes to override."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-03-17 15:10:56 +01:00
|
|
|
set -- -v "${REPO_DIR}":/repo:ro \
|
|
|
|
-v "${KAS_WORK_DIR}":/work:rw --workdir=/work \
|
2020-03-17 15:07:04 +01:00
|
|
|
-e USER_ID=$(id -u) -e GROUP_ID=$(id -g) --rm
|
2018-09-07 19:24:35 +02:00
|
|
|
|
2018-12-13 10:33:30 +01:00
|
|
|
if [ -n "${SSH_DIR}" ] ; then
|
2018-12-14 17:30:51 +01:00
|
|
|
if [ ! -d "${SSH_DIR}" ]; then
|
2020-03-17 16:20:07 +01:00
|
|
|
echo "Passed SSH_DIR '${SSH_DIR}' is not a directory"
|
2018-12-14 17:30:51 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
2020-03-17 15:10:56 +01:00
|
|
|
set -- "$@" -v "$(readlink -f "${SSH_DIR}")":/etc/skel/.ssh:ro
|
2018-12-13 10:33:30 +01:00
|
|
|
fi
|
|
|
|
|
2020-05-05 12:02:10 +02:00
|
|
|
if [ -n "${AWS_DIR}" ] ; then
|
|
|
|
if [ ! -d "${AWS_DIR}" ]; then
|
|
|
|
echo "Passed AWS_DIR '${AWS_DIR}' is not a directory"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
set -- "$@" -v "$(readlink -f "${AWS_DIR}")":/etc/skel/.aws:ro \
|
|
|
|
-e AWS_CONFIG_FILE="${AWS_CONFIG_FILE:-/etc/skel/.aws/config}" \
|
|
|
|
-e AWS_SHARED_CREDENTIALS_FILE="${AWS_SHARED_CREDENTIALS_FILE:-/etc/skel/.aws/credentials}"
|
|
|
|
fi
|
|
|
|
|
2018-09-07 19:24:35 +02:00
|
|
|
if [ -t 1 ]; then
|
2020-03-17 15:07:04 +01:00
|
|
|
set -- "$@" -t -i
|
2018-09-07 19:24:35 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${DL_DIR}" ]; then
|
2020-03-17 15:10:56 +01:00
|
|
|
trace mkdir -p "${DL_DIR}"
|
2020-03-17 15:07:04 +01:00
|
|
|
set -- "$@" \
|
2020-03-17 15:10:56 +01:00
|
|
|
-v "$(readlink -f "${DL_DIR}")":/downloads:rw \
|
2020-03-17 15:07:04 +01:00
|
|
|
-e DL_DIR=/downloads
|
2018-09-07 19:24:35 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${SSTATE_DIR}" ]; then
|
2020-03-17 15:10:56 +01:00
|
|
|
trace mkdir -p "${SSTATE_DIR}"
|
2020-03-17 15:07:04 +01:00
|
|
|
set -- "$@" \
|
2020-03-17 15:10:56 +01:00
|
|
|
-v "$(readlink -f "${SSTATE_DIR}")":/sstate:rw \
|
2020-03-17 15:07:04 +01:00
|
|
|
-e SSTATE_DIR=/sstate
|
2018-09-07 19:24:35 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${KAS_REPO_REF_DIR}" ]; then
|
2020-03-17 15:07:04 +01:00
|
|
|
set -- "$@" \
|
2020-03-17 15:10:56 +01:00
|
|
|
-v "$(readlink -f "${KAS_REPO_REF_DIR}")":/repo-ref:ro \
|
|
|
|
-e KAS_REPO_REF_DIR="${KAS_REPO_REF_DIR}"
|
2018-09-07 19:24:35 +02:00
|
|
|
fi
|
|
|
|
|
2019-08-08 14:47:30 +02:00
|
|
|
for var in TERM KAS_DISTRO KAS_MACHINE KAS_TARGET KAS_TASK \
|
2019-02-18 14:50:24 +01:00
|
|
|
KAS_PREMIRRORS; do
|
2018-09-07 19:24:35 +02:00
|
|
|
if [ -n "$(eval echo \$${var})" ]; then
|
2020-03-17 15:10:56 +01:00
|
|
|
set -- "$@" -e "${var}=$(eval echo \"\$${var}\")"
|
2018-09-07 19:24:35 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2019-08-08 14:47:30 +02:00
|
|
|
# propagate only supported SHELL settings
|
|
|
|
case "$SHELL" in
|
|
|
|
/bin/sh|/bin/bash|/bin/dash)
|
2020-03-17 15:03:38 +01:00
|
|
|
set -- "$@" -e "SHELL=$SHELL"
|
2019-08-08 14:47:30 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2019-02-07 08:14:58 +01:00
|
|
|
if [ -z "${NO_PROXY_FROM_ENV+x}" ]; then
|
|
|
|
for var in http_proxy https_proxy ftp_proxy no_proxy NO_PROXY; do
|
|
|
|
if [ -n "$(eval echo \$${var})" ]; then
|
2020-03-17 15:10:56 +01:00
|
|
|
set -- "$@" -e "${var}=$(eval echo \$${var})"
|
2019-02-07 08:14:58 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2018-12-14 18:00:20 +01:00
|
|
|
trace docker run "$@" ${ISAR_ARGS} ${USER_ARGS} \
|
2019-01-02 16:47:59 +01:00
|
|
|
${DOCKER_IMAGE} ${CMD} ${KAS_OPTIONS} ${KAS_FILES}
|