#!/usr/bin/env bash

# Create Docker group to be able to access /var/run/docker.sock
DOCKER_SOCKET=/var/run/docker.sock
DOCKER_GROUP=docker

if [ -S ${DOCKER_SOCKET} ]; then
    DOCKER_GID=$(stat -c '%g' ${DOCKER_SOCKET})
    groupadd -for -g ${DOCKER_GID} ${DOCKER_GROUP}
    usermod -aG ${DOCKER_GROUP} ${AIRFLOW_USER}
    echo "Created group '${DOCKER_GROUP}' (GID ${DOCKER_GID}) and added '${AIRFLOW_USER}' to it."
fi

# Switch to a non-root user and continue
sudo -E -u ${AIRFLOW_USER} -- /usr/bin/env bash $AIRFLOW_HOME/runner.sh ${@}

#!/usr/bin/env bash

# Create Docker group to be able to access /var/run/docker.sock
DOCKER_SOCKET=/var/run/docker.sock
DOCKER_GROUP=docker

if [ -S ${DOCKER_SOCKET} ]; then
    DOCKER_GID=$(stat -c '%g' ${DOCKER_SOCKET})
    groupadd -for -g ${DOCKER_GID} ${DOCKER_GROUP}
    usermod -aG ${DOCKER_GROUP} ${AIRFLOW_USER}
    echo "Created group '${DOCKER_GROUP}' (GID ${DOCKER_GID}) and added '${AIRFLOW_USER}' to it."
fi

# Switch to a non-root user and continue
sudo -E -H -u ${AIRFLOW_USER} -- /usr/bin/env bash $AIRFLOW_HOME/runner.sh ${@}

Fix bug where we ran Airflow using root's $HOME
Fix bug where we ran Airflow using root's $HOME

This causes problems because `pip` tries to check for a cache on the user's
`$HOME/.local`. As it's checking on root's $HOME, this fails because the
airflow user has no permissions to read that folder.

This is caused because we're calling airflow using `sudo -E`, which pass all of
our current user's environment variables. This is needed because we have to
pass forward things like S3 keys received via the environment. However, we also
end up passing the $HOME forward.

The fix is to add the `-H` flag to our `sudo` call, which changes the $HOME to
the target user's home.
