From 882643908432f10dbb3e931cb614325d4f49ba37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=A0togl?= Date: Mon, 21 Nov 2022 12:45:49 +0100 Subject: [PATCH] Fix docker creation for Ubuntu 20.04 and 22.04 (#83) * Fix docker creation by dynamically adding ROS 1 repositories if Ubuntu 20.04 is used. --- .../nvidia_docker/setup_nvidia_support.rst | 9 +------ .../create_setup_workspace.rst | 2 +- scripts/_RosTeamWs_Defines.bash | 13 ++++++++-- scripts/docker/_RosTeamWs_Docker_Defines.bash | 4 +-- scripts/setup-ros-workspace.bash | 26 +++++++++++++++---- templates/docker/nvidia.dockerfile | 3 --- templates/docker/recreate_docker.sh | 10 +++---- templates/docker/standard.dockerfile | 3 --- 8 files changed, 41 insertions(+), 29 deletions(-) diff --git a/docs/docker/nvidia_docker/setup_nvidia_support.rst b/docs/docker/nvidia_docker/setup_nvidia_support.rst index 345b1b4a..c5270add 100644 --- a/docs/docker/nvidia_docker/setup_nvidia_support.rst +++ b/docs/docker/nvidia_docker/setup_nvidia_support.rst @@ -157,14 +157,7 @@ Change the Dockerfile .. code-block:: bash - .\build_docker_image - - to rebuild your container. After the rebuilt has finished you can recreate it with - - .. code-block:: bash - - .\create_docker_container - + ./recreate_docker.sh You now should have a docker container which exposes your nvidia drivers and can switch to your workspace with ``rtw_switch_to_docker``. diff --git a/docs/use-cases/operating_system/create_setup_workspace.rst b/docs/use-cases/operating_system/create_setup_workspace.rst index 4d66d672..6d868c1c 100644 --- a/docs/use-cases/operating_system/create_setup_workspace.rst +++ b/docs/use-cases/operating_system/create_setup_workspace.rst @@ -52,7 +52,7 @@ If you exited a container an want to reconnect as a user, you have to run the `` Recreate a container """""""""""""""""""""" -If you removed an image, you can recreate it by switching into the ``.rtw_docker_defines`` folder inside your workspace and then executing the ``.\build_docker_image`` command. After the container is rebuilt, you can create it with ``.\create_docker_container`` command. Thereafter, you should be able to normally start, reconnect and stop the container. +If you removed an image, you can recreate it by switching into the ``.rtw_docker_defines`` folder inside your workspace and then executing the ``./recreate_docker.sh`` command. After the process is finished you should be able to normally start, reconnect and stop the container. .. note:: For more general questions on the usage of docker and the limitations of RosTeamWS in interacting with docker have a look at our :ref:`docker related docs`. diff --git a/scripts/_RosTeamWs_Defines.bash b/scripts/_RosTeamWs_Defines.bash index eac5b78e..660e4395 100755 --- a/scripts/_RosTeamWs_Defines.bash +++ b/scripts/_RosTeamWs_Defines.bash @@ -315,16 +315,25 @@ function user_decision { local decision=$1 user_answer=$2 - echo -e "${TERMINAL_COLOR_USER_INPUT_DECISION}${decision}[${rtw_accepted_answers[*]}]${TERMINAL_COLOR_NC}" + echo -e "${TERMINAL_COLOR_USER_INPUT_DECISION}${decision}${TERMINAL_COLOR_NC} [${rtw_accepted_answers[*]}]" read user_answer while ! $(is_accepted_user_answer "$user_answer"); do - echo -e "${TERMINAL_COLOR_USER_INPUT_DECISION}${decision} Please type one of the following: [${rtw_accepted_answers[*]}]${TERMINAL_COLOR_NC}" + echo -e "${TERMINAL_COLOR_USER_INPUT_DECISION}${decision}${TERMINAL_COLOR_NC} ${TERMINAL_COLOR_USER_NOTICE}Please type one of the following:${TERMINAL_COLOR_NC} [${rtw_accepted_answers[*]}]" read user_answer done } +# function which prints a notification in predefined color scheme and wait for user confirmation +# $1 - notification = The message which gets print to the commandline +function user_confirmation { + notification=$1 + + echo -e "${TERMINAL_COLOR_USER_CONFIRMATION}${notification}${TERMINAL_COLOR_NC}" + read +} + function set_framework_default_paths { FRAMEWORK_NAME="ros_team_workspace" # readlink prints resolved symbolic links or canonical file names -> the "dir/dir_2/.." becomes "dir" diff --git a/scripts/docker/_RosTeamWs_Docker_Defines.bash b/scripts/docker/_RosTeamWs_Docker_Defines.bash index 530d405d..1cf2d947 100755 --- a/scripts/docker/_RosTeamWs_Docker_Defines.bash +++ b/scripts/docker/_RosTeamWs_Docker_Defines.bash @@ -40,7 +40,7 @@ build_docker_image () { prev_pwd=$(pwd) cd "$(dirname "$docker_file_path")" || { echo "Could not change directory to new workspace"; return 1; } - echo "Building docker image $docker_image_tag with docker file $docker_file_path. This can take a while..." + notify_user "Building docker image $docker_image_tag with docker file $docker_file_path. This can take a while..." sleep 1 # sleep a second, so that user can read above message docker build \ --build-arg user=$USER \ @@ -89,7 +89,7 @@ create_docker_container() { fi # END: Needed for Nvidia support - echo "Instantiating docker image '$docker_image_tag' and mapping workspace folder to '$ws_folder'." + notify_user "Instantiating docker image '$docker_image_tag' and mapping workspace folder to '$ws_folder'." xhost +local:docker docker run \ --net=host \ diff --git a/scripts/setup-ros-workspace.bash b/scripts/setup-ros-workspace.bash index 9d7090d0..b543a479 100755 --- a/scripts/setup-ros-workspace.bash +++ b/scripts/setup-ros-workspace.bash @@ -122,7 +122,7 @@ setup_new_workspace () { # TODO(destogl): This is only temporarily solution until we offer different base-workspaces support (above commented lines) choice="0" - echo -e "${RTW_COLOR_NOTIFY_USER} The new workspace will base on currently sourced workspace or if none is sourced, '${ros_distro_base_workspace_source_path}' will be sourced." + notify_user "The new workspace will based on currently sourced workspace or if none is sourced, '${ros_distro_base_workspace_source_path}' will be sourced." if [ -z "$choice" ]; then print_and_exit "No workspace is chosen!" "$usage" @@ -145,7 +145,7 @@ setup_new_workspace () { base_ws_path="" if [ -z "$ROS_DISTRO" ]; then - echo -e "${TERMINAL_COLOR_YELLOW}No workspace is sourced, sourcing: '${ros_distro_base_workspace_source_path}'${TERMINAL_COLOR_NC}" + notify_user "No workspace is sourced, sourcing: '${ros_distro_base_workspace_source_path}'" source ${ros_distro_base_workspace_source_path} base_ws_path="${ros_distro_base_workspace_source_path}" fi @@ -163,10 +163,9 @@ setup_new_workspace () { # TODO: Add here output of the WS echo "" - echo -e "${TERMINAL_COLOR_USER_NOTICE}ATTENTION: Creating a new workspace in folder \"${ws_path}\" for ROS \"${ros_distro}\" (ROS$ros_version) (full path: ${new_workspace_location}) using \"${base_ws}\" (${base_ws_path}) as base workspace.${TERMINAL_COLOR_NC}" + notify_user "ATTENTION: Creating a new workspace in folder \"${ws_path}\" for ROS \"${ros_distro}\" (ROS$ros_version) (full path: ${new_workspace_location}) using \"${base_ws}\" (${base_ws_path}) as base workspace." echo "" - echo -e "${TERMINAL_COLOR_USER_CONFIRMATION}If correct press , otherwise +C and start the script again from the package folder and/or with correct controller name.${TERMINAL_COLOR_NC}" - read + user_confirmation "If correct press , otherwise +C and start the script again from the package folder and/or with correct controller name." # TODO(destogl): This part with base workspaces should be updated!!! - this is obsolete logic # Create and initialise ROS-Workspace @@ -362,6 +361,22 @@ create_workspace_docker () { if [[ "${ubuntu_version}" == *20.04* ]]; then DOCKER_FILE="$ws_docker_folder/Dockerfile" TMP_FILE="$ws_docker_folder/.tmp_Dockerfile" + + # Add ROS 1 repositories + mv $DOCKER_FILE "$TMP_FILE" + TEST_LINE=`awk '$1 == "#" && $2 == "ROS2" && $3 == "repository" { print NR }' $TMP_FILE` # get line before `# ROS2 repository` + let CUT_LINE=$TEST_LINE-1 + head -$CUT_LINE $TMP_FILE > $DOCKER_FILE + + echo "# ROS repository" >> $DOCKER_FILE + echo "RUN echo \"deb [arch=\$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros/ubuntu \$(lsb_release -sc) main\" | tee /etc/apt/sources.list.d/ros.list > /dev/null" >> $DOCKER_FILE + echo "" >> $DOCKER_FILE + + # Add last part + let CUT_LINE=$TEST_LINE+0 + tail -n +$CUT_LINE $TMP_FILE >> $DOCKER_FILE + + # Add repositories for Nala mv $DOCKER_FILE "$TMP_FILE" TEST_LINE=`awk '$1 == "#" && $2 == "install" && $3 == "nala" { print NR }' $TMP_FILE` # get line before `# install nala and upgrade` dependency let CUT_LINE=$TEST_LINE-0 @@ -376,6 +391,7 @@ create_workspace_docker () { let CUT_LINE=$TEST_LINE+2 tail -n +$CUT_LINE $TMP_FILE >> $DOCKER_FILE + # Cleanup temp files rm $TMP_FILE fi diff --git a/templates/docker/nvidia.dockerfile b/templates/docker/nvidia.dockerfile index 7322537c..1a7c2483 100644 --- a/templates/docker/nvidia.dockerfile +++ b/templates/docker/nvidia.dockerfile @@ -43,9 +43,6 @@ RUN nala install -y curl gnupg gnupg2 lsb-release software-properties-common && # Add key to keyring RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg -# ROS repository -RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/ros.list > /dev/null - # ROS2 repository RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | tee /etc/apt/sources.list.d/ros2.list > /dev/null diff --git a/templates/docker/recreate_docker.sh b/templates/docker/recreate_docker.sh index 7e1afc54..d98166a8 100755 --- a/templates/docker/recreate_docker.sh +++ b/templates/docker/recreate_docker.sh @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -build_docker_image () { +RTW_WS_build_docker_container_image () { local docker_image_tag=DUMMY_DOCKER_IMAGE_TAG @@ -25,7 +25,7 @@ build_docker_image () { -t "$docker_image_tag" . } -create_docker_container () { +RTW_WS_create_docker_container_instance () { local docker_host_name=DUMMY_DOCKER_HOSTNAME local docker_image_tag=DUMMY_DOCKER_IMAGE_TAG @@ -46,7 +46,7 @@ create_docker_container () { fi # END: Needed for Nvidia support - echo "Instantiating docker image '$docker_image_tag' and mapping workspace folder to '$ws_folder'." + notify_user "Instantiating docker image '$docker_image_tag' and mapping workspace folder to '$ws_folder'." xhost +local:docker docker run \ --net=host \ @@ -64,5 +64,5 @@ create_docker_container () { -it "$docker_image_tag" /bin/bash } -build_docker_image -create_docker_container +RTW_WS_build_docker_container_image +RTW_WS_create_docker_container_instance diff --git a/templates/docker/standard.dockerfile b/templates/docker/standard.dockerfile index 4b2a93c5..05752c3e 100644 --- a/templates/docker/standard.dockerfile +++ b/templates/docker/standard.dockerfile @@ -40,9 +40,6 @@ RUN nala install -y curl gnupg gnupg2 lsb-release software-properties-common && # Add key to keyring RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg -# ROS repository -RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/ros.list > /dev/null - # ROS2 repository RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | tee /etc/apt/sources.list.d/ros2.list > /dev/null