2022-02-14 17:24:12 +01:00
|
|
|
#!/usr/bin/env bash
|
2022-01-23 17:08:46 +01:00
|
|
|
set -o errexit -o pipefail
|
|
|
|
DIR=$(dirname "${0}")
|
|
|
|
MAIN_DIR=$(readlink -f "${DIR}/..")
|
utils/docker-run: fix support for git-worktrees
The docker-run script attempts to support git-new-workdirs and
git-worktrees by resolving the symlink at '$GIT_DIR/config' to get the
true $GIT_DIR. However this does not work for git-worktrees as they do
not use symlinks, instead they change the $GIT_DIR into a regular file
that contains the path to the real $GIT_DIR. To complicate things
further, we actually want the $GIT_COMMON_DIR which is the superset of a
worktree's $GIT_DIR.
git-rev-parse supports the '--git-common-dir' which will resolve the
$GIT_COMMON_DIR for us. However it does not work for git-new-workdirs,
so we still need to detect and handle them.
'--git-common-dir' also appeared only with git 2.10.0, released in 2016,
so it will not be available in older "enterprise-grade" distributions.
In that case, 'git rev-parse --git-common-dir' would return the option
flag '--git-common-dir' as-is, which is incorrect. So, we instruct it to
never return flags.
'--git-common-dir' also returns just '.git' for the main working copy,
but 'docker run' want an absolute path, so we canonicalise it.
Signed-off-by: Brandon Maier <brandon.maier@collins.com>
[yann.morin.1998@free.fr:
- support git versions before --git-common-dir was introduced
- don't mount GIT_DIR if unknown (i.e. not needed)
- fix expanding MAIN_DIR
]
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
2023-07-28 23:32:18 +02:00
|
|
|
if [ -L "${MAIN_DIR}/.git/config" ]; then
|
|
|
|
# Support git-new-workdir
|
|
|
|
GIT_DIR="$(dirname "$(realpath "${MAIN_DIR}/.git/config")")"
|
|
|
|
else
|
|
|
|
# Support git-worktree
|
|
|
|
GIT_DIR="$(cd "${MAIN_DIR}" && git rev-parse --no-flags --git-common-dir)"
|
|
|
|
fi
|
2023-08-20 15:33:32 +02:00
|
|
|
if test -z "${IMAGE}" ; then
|
|
|
|
# shellcheck disable=SC2016
|
|
|
|
IMAGE=$(grep ^image: "${MAIN_DIR}/.gitlab-ci.yml" | \
|
|
|
|
sed -e 's,^image: ,,g' | sed -e 's,\$CI_REGISTRY,registry.gitlab.com,g')
|
|
|
|
fi
|
2022-01-23 17:08:46 +01:00
|
|
|
|
2023-04-15 21:43:41 +02:00
|
|
|
declare -a docker_opts=(
|
|
|
|
-i
|
|
|
|
--rm
|
|
|
|
--user "$(id -u):$(id -g)"
|
2023-08-09 23:24:49 +02:00
|
|
|
--workdir "$(pwd)"
|
2023-08-09 23:24:50 +02:00
|
|
|
--security-opt label=disable
|
2023-10-31 11:22:16 +01:00
|
|
|
--network host
|
2023-04-15 21:43:41 +02:00
|
|
|
)
|
utils/docker-run: fix support for git-worktrees
The docker-run script attempts to support git-new-workdirs and
git-worktrees by resolving the symlink at '$GIT_DIR/config' to get the
true $GIT_DIR. However this does not work for git-worktrees as they do
not use symlinks, instead they change the $GIT_DIR into a regular file
that contains the path to the real $GIT_DIR. To complicate things
further, we actually want the $GIT_COMMON_DIR which is the superset of a
worktree's $GIT_DIR.
git-rev-parse supports the '--git-common-dir' which will resolve the
$GIT_COMMON_DIR for us. However it does not work for git-new-workdirs,
so we still need to detect and handle them.
'--git-common-dir' also appeared only with git 2.10.0, released in 2016,
so it will not be available in older "enterprise-grade" distributions.
In that case, 'git rev-parse --git-common-dir' would return the option
flag '--git-common-dir' as-is, which is incorrect. So, we instruct it to
never return flags.
'--git-common-dir' also returns just '.git' for the main working copy,
but 'docker run' want an absolute path, so we canonicalise it.
Signed-off-by: Brandon Maier <brandon.maier@collins.com>
[yann.morin.1998@free.fr:
- support git versions before --git-common-dir was introduced
- don't mount GIT_DIR if unknown (i.e. not needed)
- fix expanding MAIN_DIR
]
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
2023-07-28 23:32:18 +02:00
|
|
|
|
2023-08-09 23:24:49 +02:00
|
|
|
declare -a mountpoints=(
|
|
|
|
"${MAIN_DIR}"
|
|
|
|
"$(pwd)"
|
|
|
|
)
|
2023-08-09 23:24:46 +02:00
|
|
|
|
utils/docker-run: propagate user's proxy settings
When dealing with enterprise-grade networks, it is more often than not
the case that the wider internet is unreachable but through proxies.
There is a usual set of variables that users can set in the
environment to point various tools (curl, git...) to use those
proxies.
Propagate those variables inside the container.
Note that there are a few tools (e.g. cvs, svn) that may not recognise
those variables; instead, they require custom setup that is too
complex to handle, so is left as an exercise to interested parties.
Similarly, there are other types of proxy, socks4 or socks5, that also
require custom setup that is not trivial to replicate in a container,
so is also left out as an exercise for interested parties.
In the large majority of cases, those few variables are enough to Make
Things Work™.
Signed-off-by: Yann E. MORIN <yann.morin@orange.com>
Cc: Ricardo Martincoski <ricardo.martincoski@datacom.com.br>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2023-10-31 11:22:17 +01:00
|
|
|
# curl lists (and recognises and uses) other types of *_proxy variables,
|
|
|
|
# but only those make sense for Buildroot:
|
|
|
|
for env in all_proxy http_proxy https_proxy ftp_proxy no_proxy; do
|
|
|
|
if [ "${!env}" ]; then
|
|
|
|
docker_opts+=( --env "${env}" )
|
|
|
|
# The lower-case variant takes precedence on the upper-case one
|
|
|
|
# (dixit curl)
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
# http_proxy is only lower-case (dixit curl)
|
|
|
|
if [ "${env}" = http_proxy ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
# All the others also exist in the upper-case variant
|
|
|
|
env="${env^^}"
|
|
|
|
if [ "${!env}" ]; then
|
|
|
|
docker_opts+=( --env "${env}" )
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
utils/docker-run: fix support for git-worktrees
The docker-run script attempts to support git-new-workdirs and
git-worktrees by resolving the symlink at '$GIT_DIR/config' to get the
true $GIT_DIR. However this does not work for git-worktrees as they do
not use symlinks, instead they change the $GIT_DIR into a regular file
that contains the path to the real $GIT_DIR. To complicate things
further, we actually want the $GIT_COMMON_DIR which is the superset of a
worktree's $GIT_DIR.
git-rev-parse supports the '--git-common-dir' which will resolve the
$GIT_COMMON_DIR for us. However it does not work for git-new-workdirs,
so we still need to detect and handle them.
'--git-common-dir' also appeared only with git 2.10.0, released in 2016,
so it will not be available in older "enterprise-grade" distributions.
In that case, 'git rev-parse --git-common-dir' would return the option
flag '--git-common-dir' as-is, which is incorrect. So, we instruct it to
never return flags.
'--git-common-dir' also returns just '.git' for the main working copy,
but 'docker run' want an absolute path, so we canonicalise it.
Signed-off-by: Brandon Maier <brandon.maier@collins.com>
[yann.morin.1998@free.fr:
- support git versions before --git-common-dir was introduced
- don't mount GIT_DIR if unknown (i.e. not needed)
- fix expanding MAIN_DIR
]
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
2023-07-28 23:32:18 +02:00
|
|
|
# Empty GIT_DIR means that we are not in a workdir, *and* git is too old
|
|
|
|
# to know about worktrees, so we're not in a worktree either. So it means
|
|
|
|
# we're in the main git working copy, and thus we don't need to mount the
|
|
|
|
# .git directory.
|
|
|
|
if [ "${GIT_DIR}" ]; then
|
|
|
|
# GIT_DIR in the main working copy (when git supports worktrees) will
|
2023-07-31 20:56:57 +02:00
|
|
|
# be just '.git', but 'docker run' needs an absolute path. If it is
|
|
|
|
# not absolute, GIT_DIR is relative to MAIN_DIR. If it's an absolute
|
|
|
|
# path already (in a wordir), then that's a noop.
|
|
|
|
GIT_DIR="$(cd "${MAIN_DIR}"; readlink -e "${GIT_DIR}")"
|
2023-08-09 23:24:46 +02:00
|
|
|
mountpoints+=( "${GIT_DIR}" )
|
2023-08-09 23:24:48 +02:00
|
|
|
|
|
|
|
# 'repo' stores .git/objects separately.
|
|
|
|
if [ -L "${GIT_DIR}/objects" ]; then
|
|
|
|
# GITDIR is already an absolute path, but for symetry
|
|
|
|
# with the above, keep the same cd+readlink construct.
|
|
|
|
OBJECTS_DIR="$(cd "${MAIN_DIR}"; readlink -e "${GIT_DIR}/objects")"
|
|
|
|
mountpoints+=( "${OBJECTS_DIR}" )
|
|
|
|
fi
|
utils/docker-run: fix support for git-worktrees
The docker-run script attempts to support git-new-workdirs and
git-worktrees by resolving the symlink at '$GIT_DIR/config' to get the
true $GIT_DIR. However this does not work for git-worktrees as they do
not use symlinks, instead they change the $GIT_DIR into a regular file
that contains the path to the real $GIT_DIR. To complicate things
further, we actually want the $GIT_COMMON_DIR which is the superset of a
worktree's $GIT_DIR.
git-rev-parse supports the '--git-common-dir' which will resolve the
$GIT_COMMON_DIR for us. However it does not work for git-new-workdirs,
so we still need to detect and handle them.
'--git-common-dir' also appeared only with git 2.10.0, released in 2016,
so it will not be available in older "enterprise-grade" distributions.
In that case, 'git rev-parse --git-common-dir' would return the option
flag '--git-common-dir' as-is, which is incorrect. So, we instruct it to
never return flags.
'--git-common-dir' also returns just '.git' for the main working copy,
but 'docker run' want an absolute path, so we canonicalise it.
Signed-off-by: Brandon Maier <brandon.maier@collins.com>
[yann.morin.1998@free.fr:
- support git versions before --git-common-dir was introduced
- don't mount GIT_DIR if unknown (i.e. not needed)
- fix expanding MAIN_DIR
]
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
2023-07-28 23:32:18 +02:00
|
|
|
fi
|
|
|
|
|
2023-08-09 23:24:47 +02:00
|
|
|
if [ "${BR2_DL_DIR}" ]; then
|
|
|
|
mountpoints+=( "${BR2_DL_DIR}" )
|
|
|
|
docker_opts+=( --env BR2_DL_DIR )
|
|
|
|
fi
|
|
|
|
|
2023-08-09 23:24:46 +02:00
|
|
|
# shellcheck disable=SC2013 # can't use while-read because of the assignment
|
|
|
|
for dir in $(printf '%s\n' "${mountpoints[@]}" |LC_ALL=C sort -u); do
|
|
|
|
docker_opts+=( --mount "type=bind,src=${dir},dst=${dir}" )
|
|
|
|
done
|
|
|
|
|
2023-04-15 21:43:41 +02:00
|
|
|
if tty -s; then
|
|
|
|
docker_opts+=( -t )
|
|
|
|
fi
|
|
|
|
|
|
|
|
exec docker run "${docker_opts[@]}" "${IMAGE}" "${@}"
|