From 91d46acefc2daf521cfe086a407c66264b150874 Mon Sep 17 00:00:00 2001 From: qzhhhi Date: Sun, 24 Mar 2024 13:56:43 +0000 Subject: [PATCH] Add remote debugging tool --- .gitignore | 7 ++- .script/service-remote.sh | 3 ++ .script/service.sh | 48 +++++++++++++++++++ .script/set-remote.py | 32 +++++++++++++ .script/ssh-remote.sh | 3 ++ .script/sync-remote.sh | 6 +++ .ssh/config | 6 +++ Dockerfile | 98 ++++++++++++++++++++++++++++----------- rmcs_ws/src/ugas | 2 +- 9 files changed, 176 insertions(+), 29 deletions(-) create mode 100755 .script/service-remote.sh create mode 100755 .script/service.sh create mode 100755 .script/set-remote.py create mode 100755 .script/ssh-remote.sh create mode 100755 .script/sync-remote.sh create mode 100644 .ssh/config diff --git a/.gitignore b/.gitignore index e5d369049..d007824c8 100644 --- a/.gitignore +++ b/.gitignore @@ -56,4 +56,9 @@ CATKIN_IGNORE # Hik camera log files MvFGSdkLog -MvSdkLog \ No newline at end of file +MvSdkLog + +# SSH key files +id_rsa +id_rsa.pub + diff --git a/.script/service-remote.sh b/.script/service-remote.sh new file mode 100755 index 000000000..74c94c174 --- /dev/null +++ b/.script/service-remote.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +/bin/ssh remote "service rmcs $1" \ No newline at end of file diff --git a/.script/service.sh b/.script/service.sh new file mode 100755 index 000000000..a61306bde --- /dev/null +++ b/.script/service.sh @@ -0,0 +1,48 @@ +#! /bin/bash + +set -e + +start() { + if [[ -z "$HOME" ]]; then + export HOME=/root + fi + source /opt/ros/humble/setup.bash + source /rmcs_install/setup.bash + nohup ros2 launch rmcs_bringup rmcs.launch.py &> rmcs.launch.out 2>&1 & echo $! > /tmp/rmcs.pid +} + +stop() { + if [ -f "/tmp/rmcs.pid" ]; then + pkill -P "$(cat /tmp/rmcs.pid)" + fi +} + +check_running() { + if [ -f "/tmp/rmcs.pid" ]; then + if ps -p "$(cat /tmp/rmcs.pid)" > /dev/null + then + echo 'Fatal: RMCS daemon is still running!' + exit 1 + fi + fi +} + +case "$1" in + start) + check_running + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + *) + echo 'Usage: /etc/init.d/rmcs {start|stop|restart}' + exit 1 + ;; +esac + +exit 0 \ No newline at end of file diff --git a/.script/set-remote.py b/.script/set-remote.py new file mode 100755 index 000000000..ab80b10aa --- /dev/null +++ b/.script/set-remote.py @@ -0,0 +1,32 @@ +#!/bin/python3 + +import sys +import re +import os + +if len(sys.argv) == 1: + print("Fatal: No arguments provided. Please provide the IP of the remote host.") + sys.exit(1) +elif len(sys.argv) > 2: + print( + "Fatal: Too many arguments provided. Please provide only the IP of the remote host." + ) + sys.exit(1) + +ssh_path = os.path.join(os.getenv("HOME"), ".ssh", "config") +try: + with open(ssh_path, "r") as file: + config = file.read() + updated_config, updated_count = re.subn( + r"Host remote\n HostName [^\n]+", + f"Host remote\n HostName {sys.argv[1]}", + config, + ) + if updated_count == 0: + print("Fatal: Cannot find any place to modify in SSH config.") + sys.exit(1) + with open(ssh_path, "w") as file: + file.write(updated_config) +except FileNotFoundError: + print("Fatal: SSH config file not found.") + sys.exit(1) diff --git a/.script/ssh-remote.sh b/.script/ssh-remote.sh new file mode 100755 index 000000000..7799ec0e2 --- /dev/null +++ b/.script/ssh-remote.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +ssh remote \ No newline at end of file diff --git a/.script/sync-remote.sh b/.script/sync-remote.sh new file mode 100755 index 000000000..1187119bd --- /dev/null +++ b/.script/sync-remote.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +SRC_DIR=rmcs_ws/install +DST_DIR=ssh://remote//rmcs_install + +unison -auto -batch -repeat watch -times ${SRC_DIR} ${DST_DIR} -force ${SRC_DIR} \ No newline at end of file diff --git a/.ssh/config b/.ssh/config new file mode 100644 index 000000000..84d4dd1c6 --- /dev/null +++ b/.ssh/config @@ -0,0 +1,6 @@ +Host remote + HostName 127.0.0.1 + Port 2022 + User root + PreferredAuthentications publickey + IdentityFile ~/.ssh/id_rsa diff --git a/Dockerfile b/Dockerfile index ebd9f8afb..9ca17a98b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,11 @@ +# 23.0 is the minimum docker engine version that required to build. +# If your docker engine lower than this version, please upgrade it or manually enable the BuildKit. + + + # Base container, provides a runtime environment FROM ros:humble AS rmcs-base -WORKDIR / - # Change bash as default shell instead of sh SHELL ["/bin/bash", "-c"] @@ -28,54 +31,95 @@ RUN wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCT rm -rf /var/lib/apt/lists/* && \ rm ./GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB -# Download RMCS source, fix dependencies and compile -RUN git clone --recurse-submodules https://github.com/Alliance-Algorithm/RMCS && \ - cd RMCS/rmcs_ws && \ - source /etc/profile && \ +# Mount rmcs source, fix dependencies and compile +RUN --mount=type=bind,target=/tmp/rmcs_ws/src,source=rmcs_ws/src,readonly \ + cd /tmp/rmcs_ws && \ source /opt/ros/humble/local_setup.bash && \ apt-get update && \ rosdep install --from-paths src --ignore-src -r -y && \ rm -rf /var/lib/apt/lists/* && \ - colcon build && \ - mv ./install /rmcs-install && cd / && \ - rm -rf RMCS - - -# Runtime container, will automatically launch the main program -FROM rmcs-base AS rmcs-runtime + colcon build --install-base /rmcs_install --merge-install && \ + rm -rf build log -RUN echo '#!/usr/bin/bash' > /entrypoint.bash && \ - echo 'source /opt/ros/humble/setup.bash' >> /entrypoint.bash && \ - echo 'source /rmcs-install/setup.bash' >> /entrypoint.bash && \ - echo 'ros2 launch rmcs_bringup rmcs.launch.py' >> /entrypoint.bash && \ - chmod +x /entrypoint.bash +# Install unison to allow file synchronization +RUN cd /tmp && \ + wget -O unison.tar.gz https://github.com/bcpierce00/unison/releases/download/v2.53.4/unison-2.53.4-ubuntu-x86_64.tar.gz && \ + mkdir -p unison && tar -zxf unison.tar.gz -C unison && \ + cp unison/bin/* /usr/local/bin && \ + rm -rf unison unison.tar.gz -ENTRYPOINT [ "/entrypoint.bash" ] # Developing container, works with devcontainer FROM rmcs-base AS rmcs-develop -# Install develop tools (clangd/zsh/etc...) +# Install develop tools (ssh/clangd/zsh/etc...) RUN apt-get update && apt-get -y install \ - lsb-release software-properties-common gnupg zsh sudo && \ + openssh-client \ + lsb-release software-properties-common gnupg zsh sudo \ + libcanberra-gtk-module libcanberra-gtk3-module && \ + rm -rf /var/lib/apt/lists/* \ echo -e "\n" | bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" && \ ln -s /usr/bin/clangd-* /usr/bin/clangd -RUN apt-get -y install libcanberra-gtk-module libcanberra-gtk3-module - # Add user RUN useradd -m developer --shell /bin/zsh && echo "developer:developer" | chpasswd && adduser developer sudo && \ echo "developer ALL=(ALL:ALL) NOPASSWD:ALL" >> /etc/sudoers && \ gpasswd --add developer dialout - -# Set user -USER developer WORKDIR /home/developer ENV USER=developer ENV WORKDIR=/home/developer +# Generate ssh secret key & profile unison +RUN --mount=type=bind,target=/tmp/.ssh,source=.ssh,readonly \ + mkdir -p .ssh && \ + if [ ! -f "/tmp/.ssh/id_rsa.pub" ]; then ssh-keygen -N "" -f ".ssh/id_rsa"; fi && \ + cp -r /tmp/.ssh/* .ssh && \ + mkdir -p .unison && \ + echo 'confirmbigdel = false' >> ".unison/default.prf" && \ + chown -R 1000:1000 .ssh .unison + +USER developer + # Install oh my zsh & change theme to af-magic RUN set -eo pipefail && \ curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh && \ - sed -i 's/ZSH_THEME=\"[a-z0-9\-]*\"/ZSH_THEME="af-magic"/g' .zshrc \ No newline at end of file + sed -i 's/ZSH_THEME=\"[a-z0-9\-]*\"/ZSH_THEME="af-magic"/g' .zshrc + +COPY --chown=root:root .script/set-remote.py /usr/local/bin/set-remote +COPY --chown=root:root .script/ssh-remote.sh /usr/local/bin/ssh-remote +COPY --chown=root:root .script/sync-remote.sh /usr/local/bin/sync-remote +COPY --chown=root:root .script/service-remote.sh /usr/local/bin/service-remote + + + +# Runtime container, will automatically launch the main program +FROM rmcs-base AS rmcs-runtime + +# Install ssh server +RUN apt-get update && \ + apt-get install -y openssh-server && \ + rm -rf /var/lib/apt/lists/* && \ + echo 'Port 2022' >> /etc/ssh/sshd_config && \ + echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config && \ + echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config + +# Add tini +RUN wget -O /tini https://github.com/krallin/tini/releases/download/v0.19.0/tini && \ + chmod +x /tini + +# Entrypoint +RUN echo '#!/usr/bin/bash' > /entrypoint.sh && \ + echo 'rm -rf /tmp/*' >> /entrypoint.sh && \ + echo 'service ssh restart' >> /entrypoint.sh && \ + echo 'service rmcs restart' >> /entrypoint.sh && \ + echo 'sleep infinity' >> /entrypoint.sh && \ + chmod +x /entrypoint.sh + +COPY --chown=root:root .script/service.sh /etc/init.d/rmcs +COPY --from=rmcs-develop --chmod=600 --chown=root:root /home/developer/.ssh/id_rsa.pub /root/.ssh/authorized_keys + +WORKDIR /root/ + +ENTRYPOINT ["/tini", "--"] +CMD [ "/entrypoint.sh" ] \ No newline at end of file diff --git a/rmcs_ws/src/ugas b/rmcs_ws/src/ugas index f8d43634a..270ba667b 160000 --- a/rmcs_ws/src/ugas +++ b/rmcs_ws/src/ugas @@ -1 +1 @@ -Subproject commit f8d43634a6d20041774dbe9c3f31e1bce92b6937 +Subproject commit 270ba667bd07a62e51e76334aea77ce6595dc529