#!/bin/bash set -e tool_uid="$(id -u tool)" tool_gid="$(id -g tool)" adjust_tool_uid_gid() { declare usermod_args=() groupmod_args=() declare -a new_groups=() if [[ $MAP_UID ]]; then [[ $(id -u tool) -ne $MAP_UID ]] && usermod_args+=(-u $MAP_UID) fi if [[ $MAP_GROUPS ]]; then set -- $MAP_GROUPS if [[ $tool_gid -ne $1 ]]; then groupmod_args=(-g "$1") fi shift for group in "$@"; do groupadd -g $group -o tool_$group new_groups+=(tool_$group) done IFS=, if [[ ${#new_groups[*]} -gt 0 ]]; then usermod_args+=(-aG "${new_groups[*]}") fi fi if [[ ${#groupmod_args[*]} -gt 0 ]]; then groupmod "${groupmod_args[@]}" tool fi if [[ ${#usermod_args[*]} -gt 0 ]]; then usermod "${usermod_args[@]}" tool fi } configure_ssh() { if [[ ! -d /home/tool/.ssh ]]; then mkdir /home/tool/.ssh chmod 700 /home/tool/.ssh fi if [[ ! -d $run_state/ssh_known_hosts ]]; then touch "$run_state/ssh_known_hosts" fi known_hosts_file="/home/tool/.ssh/known_hosts" ssh_config_file="/home/tool/.ssh/config" ln -sf "$run_state/ssh_known_hosts" "$known_hosts_file" if [[ $SSH_KNOWN_HOSTS ]]; then # If known_hosts was kept between runs, this loop would be faster. for host in $SSH_KNOWN_HOSTS; do ssh-keygen -q -F $host -f "$known_hosts_file" 1>/dev/null || ssh-keyscan $host >> ""$known_hosts_file"" 2>/dev/null done fi { echo "Host *" echo " PubkeyAcceptedAlgorithms +ssh-rsa" echo " StrictHostKeyChecking accept-new" } > "/home/tool/.ssh/config" chown tool:tool -R "/home/tool/.ssh" } configure_bash() { if [[ ! -e $run_state/bash_history ]]; then touch "$run_state/bash_history" fi ln -sf "$run_state/bash_history" "/home/tool/.bash_history" chown -h tool:tool "/home/tool/.bash_history" } configure_cache() { if [[ ! -d $run_state/cache ]]; then mkdir "$run_state/cache" fi ln -sf "$run_state/cache" /home/tool/.cache chown -h tool:tool "/home/tool/.cache" } # TODO: Save /run/user between calls mkdir -p /run/user/$tool_uid adjust_tool_uid_gid tool_uid="$(id -u tool)" tool_gid="$(id -g tool)" run_state="/run/user/$tool_uid/inkluster-dev" if [[ ! -d $run_state ]]; then mkdir -p "$run_state" fi configure_ssh configure_bash configure_cache chown -R tool:tool /run/user/$tool_uid [[ $# -eq 0 ]] && set -- "bash" cmd="$(which "$1")" shift start-stop-daemon --chuid tool:tool --start -d $PWD -u tool --exec "$cmd" "$@"