Last active
March 12, 2025 00:55
-
-
Save aswinkumar1999/d68b3169f7f842af59963808d209336f to your computer and use it in GitHub Desktop.
One-script-setup-for-Husarnet-Ubuntu-RPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# husarnet-ros2-setup.sh | |
# Script to automate Husarnet installation, configuration, and cleanup | |
# Handles existing installations and allows peer updates | |
# Function to use sudo for a command if not already root | |
run_with_sudo() { | |
if [ "$EUID" -ne 0 ]; then | |
echo "Running with sudo: $1" | |
sudo bash -c "$1" | |
return $? | |
else | |
bash -c "$1" | |
return $? | |
fi | |
} | |
# Save the original user for running non-privileged commands | |
if [ "$EUID" -eq 0 ]; then | |
ORIGINAL_USER=$(logname || echo ${SUDO_USER}) | |
if [ -z "$ORIGINAL_USER" ]; then | |
# Fallback if we can't detect the user | |
ORIGINAL_USER=$(who am i | awk '{print $1}') | |
fi | |
else | |
ORIGINAL_USER=$(whoami) | |
fi | |
ORIGINAL_HOME=$(eval echo ~$ORIGINAL_USER) | |
echo "Script will run as current user ($ORIGINAL_USER) and use sudo only when needed." | |
# Function to show help | |
show_help() { | |
echo "Usage: $0 [OPTIONS] [JOINCODE] [HOSTNAME]" | |
echo "" | |
echo "Options:" | |
echo " --install Install and configure Husarnet and ROS 2 (default action)" | |
echo " --update-peers Only update the ROS_STATIC_PEERS configuration" | |
echo " --uninstall Disconnect, clean up, and uninstall Husarnet" | |
echo " --list-peers List all available peers in the network (for testing)" | |
echo " --help Show this help message" | |
echo "" | |
echo "Arguments:" | |
echo " JOINCODE The Husarnet join code (required for installation)" | |
echo " HOSTNAME Custom hostname for Husarnet (optional, defaults to system hostname)" | |
echo "" | |
echo "Examples:" | |
echo " $0 fc94:b01d:1803:8dd8:b293:5c7d:7639:932a/XXXX" | |
echo " $0 --update-peers" | |
echo " $0 --list-peers" | |
echo " $0 --uninstall" | |
exit 0 | |
} | |
# Function to check if Husarnet is already installed | |
check_husarnet_installed() { | |
if command -v husarnet &> /dev/null; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
# Function to check if device is already joined to a Husarnet network | |
check_husarnet_joined() { | |
if husarnet status 2>/dev/null | grep -q "Is joined.*yes"; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
# Function to install RMW Cyclone DDS dependency | |
install_cyclone_dds() { | |
# First check if we can detect ROS 2 and Cyclone DDS | |
echo "Checking for ROS 2 and Cyclone DDS..." | |
# Check for ros2 command | |
if command -v ros2 &>/dev/null; then | |
echo "ros2 command found in environment." | |
# Check for cyclonedds | |
if ros2 pkg list 2>/dev/null | grep -q rmw_cyclonedds_cpp; then | |
echo "Cyclone DDS RMW implementation is already installed." | |
return 0 | |
else | |
echo "Cyclone DDS RMW implementation not found." | |
fi | |
else | |
echo "ros2 command not found in environment." | |
echo "Continuing without ROS 2 package verification." | |
return 1 | |
fi | |
# Try to determine ROS distribution from environment | |
ROS_DISTRO=$(printenv ROS_DISTRO || echo "") | |
if [ -z "$ROS_DISTRO" ]; then | |
# Try to detect from common paths as fallback | |
if [ -d "/opt/ros/jazzy" ]; then | |
ROS_DISTRO="jazzy" | |
elif [ -d "/opt/ros/noble" ]; then | |
ROS_DISTRO="noble" | |
fi | |
fi | |
if [ ! -z "$ROS_DISTRO" ]; then | |
echo "Detected ROS 2 distribution: $ROS_DISTRO" | |
echo "Attempting to install rmw_cyclonedds_cpp for ROS 2 $ROS_DISTRO..." | |
run_with_sudo "apt-get update && apt-get install -y ros-$ROS_DISTRO-rmw-cyclonedds-cpp" | |
if [ $? -ne 0 ]; then | |
echo "Failed to install ros-$ROS_DISTRO-rmw-cyclonedds-cpp." | |
echo "NOTE: This is not critical for Husarnet operation. You can continue using the script." | |
echo "If needed, you can install it manually later with:" | |
echo " sudo apt install ros-$ROS_DISTRO-rmw-cyclonedds-cpp" | |
fi | |
else | |
echo "Could not determine ROS 2 distribution." | |
echo "NOTE: rmw_cyclonedds_cpp is recommended for ROS 2 communication over Husarnet." | |
echo "Please install it manually using your ROS 2 installation method." | |
fi | |
} | |
# Function to add environment setup to bashrc | |
add_env_to_bashrc() { | |
local domain_id=$1 | |
if [ -z "$ORIGINAL_USER" ]; then | |
echo "Warning: Could not determine the original user. Environment variables may not be set correctly." | |
return | |
fi | |
local user_bashrc="$ORIGINAL_HOME/.bashrc" | |
if [ ! -f "$user_bashrc" ]; then | |
echo "Warning: Could not find .bashrc for user $ORIGINAL_USER at $user_bashrc" | |
return | |
fi | |
echo "Adding ROS 2 over Husarnet environment settings to $user_bashrc" | |
# Check if environment variables are already set | |
if ! grep -q "RMW_IMPLEMENTATION=rmw_cyclonedds_cpp" "$user_bashrc"; then | |
echo "" >> "$user_bashrc" | |
echo "# ROS 2 over Husarnet environment setup" >> "$user_bashrc" | |
echo "export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp" >> "$user_bashrc" | |
echo "export CYCLONEDDS_URI=file:///var/tmp/cyclonedds.xml" >> "$user_bashrc" | |
echo "export ROS_AUTOMATIC_DISCOVERY_RANGE=LOCALHOST" >> "$user_bashrc" | |
if [ ! -z "$domain_id" ]; then | |
echo "export ROS_DOMAIN_ID=$domain_id" >> "$user_bashrc" | |
fi | |
echo "Added ROS 2 environment settings to .bashrc for user $ORIGINAL_USER" | |
else | |
# Update ROS_DOMAIN_ID if it's different or not set | |
if [ ! -z "$domain_id" ]; then | |
if grep -q "export ROS_DOMAIN_ID=" "$user_bashrc"; then | |
# Replace existing ROS_DOMAIN_ID | |
sed -i "s/export ROS_DOMAIN_ID=[0-9]*/export ROS_DOMAIN_ID=$domain_id/" "$user_bashrc" | |
else | |
# Add ROS_DOMAIN_ID after the ROS environment block | |
sed -i "/export ROS_AUTOMATIC_DISCOVERY_RANGE=LOCALHOST/a export ROS_DOMAIN_ID=$domain_id" "$user_bashrc" | |
fi | |
fi | |
echo "ROS 2 environment settings already exist in .bashrc for user $ORIGINAL_USER" | |
fi | |
} | |
# Function to extract current peers from bashrc | |
get_current_peers() { | |
if [ -z "$ORIGINAL_USER" ]; then | |
echo "" | |
return | |
fi | |
local user_bashrc="$ORIGINAL_HOME/.bashrc" | |
if [ ! -f "$user_bashrc" ]; then | |
echo "" | |
return | |
fi | |
local peers=$(grep "export ROS_STATIC_PEERS=" "$user_bashrc" 2>/dev/null | sed -E "s/export ROS_STATIC_PEERS='(.*)'/\1/") | |
if [ ! -z "$peers" ]; then | |
echo "$peers" | |
else | |
echo "" | |
fi | |
} | |
# Function to get current ROS_DOMAIN_ID from bashrc | |
get_current_domain_id() { | |
if [ -z "$ORIGINAL_USER" ]; then | |
echo "" | |
return | |
fi | |
local user_bashrc="$ORIGINAL_HOME/.bashrc" | |
if [ ! -f "$user_bashrc" ]; then | |
echo "" | |
return | |
fi | |
local domain_id=$(grep "export ROS_DOMAIN_ID=" "$user_bashrc" 2>/dev/null | sed -E "s/export ROS_DOMAIN_ID=([0-9]*)/\1/") | |
if [ ! -z "$domain_id" ]; then | |
echo "$domain_id" | |
else | |
echo "" | |
fi | |
} | |
# Extract available peers from husarnet status | |
get_available_peers() { | |
husarnet status 2>/dev/null | grep -v "localhost" | grep -v "websetup" | grep -o -P "fc94:[a-f0-9:]+\s+\K[^\s]+" | tr '\n' ',' | sed 's/,$//' | |
} | |
# Function to get all peer IP addresses from husarnet status (excluding websetup) | |
get_all_peer_ips() { | |
# Get all IPv6 addresses and filter out websetup | |
husarnet status 2>/dev/null | grep "fc94:" | grep -v "(websetup)" | grep -o -P "fc94:[a-f0-9:]+\b" | tr '\n' ';' | sed 's/;$//' | |
} | |
# Function to update ROS_STATIC_PEERS in bashrc | |
update_peers() { | |
local peers_list=$1 | |
if [ -z "$ORIGINAL_USER" ]; then | |
echo "Warning: Could not determine the original user. Peers may not be updated correctly." | |
return | |
fi | |
local user_bashrc="$ORIGINAL_HOME/.bashrc" | |
if [ ! -f "$user_bashrc" ]; then | |
echo "Warning: Could not find .bashrc for user $ORIGINAL_USER at $user_bashrc" | |
return | |
fi | |
# Remove any existing ROS_STATIC_PEERS line from bashrc | |
sed -i '/export ROS_STATIC_PEERS=/d' "$user_bashrc" | |
# Add new ROS_STATIC_PEERS line | |
echo "export ROS_STATIC_PEERS='$peers_list'" >> "$user_bashrc" | |
echo "Updated .bashrc for user $ORIGINAL_USER" | |
} | |
# Function to list all available peers on the network | |
list_peers() { | |
echo "===== Husarnet Peers =====" | |
# Check if Husarnet is installed and joined | |
if ! check_husarnet_installed; then | |
echo "Husarnet is not installed. Unable to list peers." | |
exit 1 | |
fi | |
if ! check_husarnet_joined; then | |
echo "This device is not joined to a Husarnet network. Unable to list peers." | |
exit 1 | |
fi | |
echo "All Husarnet IP addresses in network (excluding websetup):" | |
ALL_PEER_IPS=$(get_all_peer_ips) | |
if [ -z "$ALL_PEER_IPS" ]; then | |
echo "No peer IP addresses found." | |
else | |
# Convert semicolons to newlines for better readability | |
echo "$ALL_PEER_IPS" | tr ';' '\n' | while read -r ip; do | |
# Try to find the hostname for this IP | |
hostname=$(husarnet status 2>/dev/null | grep -F "$ip" | grep -v "websetup" | grep -o -P "(?<=\s)[^\s]+(?=\s|$)" | grep -v "fc94:" | head -1) | |
if [ ! -z "$hostname" ]; then | |
echo "$ip - $hostname" | |
else | |
echo "$ip" | |
fi | |
done | |
fi | |
echo "" | |
echo "Current ROS_STATIC_PEERS configuration:" | |
CURRENT_PEERS=$(get_current_peers) | |
if [ -z "$CURRENT_PEERS" ]; then | |
echo "No peers configured in ROS_STATIC_PEERS." | |
else | |
echo "$CURRENT_PEERS" | tr ';' '\n' | |
fi | |
exit 0 | |
} | |
# Function to uninstall Husarnet and clean up ROS 2 configuration | |
uninstall_husarnet() { | |
echo "===== Uninstalling Husarnet and cleaning up ROS 2 configuration =====" | |
# Check if Husarnet is installed | |
if ! check_husarnet_installed; then | |
echo "Husarnet is not installed. Nothing to uninstall." | |
return | |
fi | |
# Check if device is joined to a network and disconnect | |
if check_husarnet_joined; then | |
echo "Disconnecting from Husarnet network..." | |
run_with_sudo "husarnet daemon stop" | |
echo "Disconnected from Husarnet network." | |
fi | |
# Remove Husarnet | |
echo "Removing Husarnet client..." | |
if [ -f /etc/apt/sources.list.d/husarnet.list ]; then | |
run_with_sudo "apt-get -y remove husarnet && apt-get -y autoremove && rm -f /etc/apt/sources.list.d/husarnet.list" | |
echo "Husarnet client removed." | |
else | |
echo "Unable to find Husarnet APT repository. Trying alternative uninstall method..." | |
if [ -f /usr/bin/husarnet ]; then | |
run_with_sudo "rm -f /usr/bin/husarnet" | |
echo "Husarnet binary removed." | |
fi | |
fi | |
# Remove CycloneDDS configuration | |
echo "Removing CycloneDDS configuration..." | |
if [ -f /var/tmp/cyclonedds.xml ]; then | |
run_with_sudo "rm -f /var/tmp/cyclonedds.xml" | |
echo "CycloneDDS configuration removed." | |
fi | |
# Skip removing the Cyclone DDS package - we'll keep it for future use | |
echo "Skipping rmw_cyclonedds_cpp removal as it may be needed later." | |
# Remove environment variables from bashrc | |
if [ -z "$ORIGINAL_USER" ]; then | |
echo "Warning: Could not determine the original user. Environment variables may not be cleaned up." | |
return | |
fi | |
local user_bashrc="$ORIGINAL_HOME/.bashrc" | |
if [ ! -f "$user_bashrc" ]; then | |
echo "Warning: Could not find .bashrc for user $ORIGINAL_USER at $user_bashrc" | |
return | |
fi | |
echo "Removing ROS 2 environment settings from .bashrc for user $ORIGINAL_USER..." | |
# Remove environment variables | |
sed -i '/# ROS 2 over Husarnet environment setup/d' "$user_bashrc" | |
sed -i '/export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp/d' "$user_bashrc" | |
sed -i '/export CYCLONEDDS_URI=file:\/\/\/var\/tmp\/cyclonedds.xml/d' "$user_bashrc" | |
sed -i '/export ROS_AUTOMATIC_DISCOVERY_RANGE=LOCALHOST/d' "$user_bashrc" | |
sed -i '/export ROS_STATIC_PEERS=/d' "$user_bashrc" | |
sed -i '/export ROS_DOMAIN_ID=/d' "$user_bashrc" | |
echo "Removed ROS 2 environment settings from .bashrc for user $ORIGINAL_USER" | |
echo "Uninstallation complete." | |
echo "You may need to restart your system to complete the cleanup." | |
} | |
# Function for updating peers configuration - used in both install and update-peers actions | |
configure_peers() { | |
# Get current peers configuration | |
CURRENT_PEERS=$(get_current_peers) | |
if [ ! -z "$CURRENT_PEERS" ]; then | |
echo "Current peers configuration: $CURRENT_PEERS" | |
fi | |
# Get all peer IP addresses (excluding websetup) | |
ALL_PEER_IPS=$(get_all_peer_ips) | |
echo "Available options for peer configuration:" | |
echo "1. Use all IP addresses automatically (recommended, excluding websetup)" | |
if [ ! -z "$ALL_PEER_IPS" ]; then | |
echo " → This will use: $ALL_PEER_IPS" | |
else | |
echo " → No other devices found in your Husarnet network" | |
fi | |
echo "2. Specify peer hostnames manually" | |
echo "3. Keep current configuration" | |
read -p "Select an option (1-3, default: 1): " PEER_OPTION | |
# Default to option 1 if empty | |
if [ -z "$PEER_OPTION" ]; then | |
PEER_OPTION=1 | |
fi | |
case $PEER_OPTION in | |
1) | |
# Use all peer IPs automatically | |
if [ -z "$ALL_PEER_IPS" ]; then | |
echo "No peer IP addresses found in your Husarnet network." | |
echo "You may need to add devices to your Husarnet network first." | |
PEERS_LIST="" | |
else | |
echo "Using all available IP addresses: $ALL_PEER_IPS" | |
PEERS_LIST=$ALL_PEER_IPS | |
fi | |
;; | |
2) | |
# Manual configuration | |
# Get available peer hostnames | |
AVAILABLE_PEERS=$(get_available_peers) | |
if [ ! -z "$AVAILABLE_PEERS" ]; then | |
echo "Available peers in your Husarnet network: $AVAILABLE_PEERS" | |
fi | |
read -p "Enter comma-separated list of peers: " NEW_PEERS_INPUT | |
# If input is empty, keep current peers | |
if [ -z "$NEW_PEERS_INPUT" ] && [ ! -z "$CURRENT_PEERS" ]; then | |
NEW_PEERS_INPUT=$CURRENT_PEERS | |
fi | |
# Convert comma-separated list to semicolon-separated list for ROS_STATIC_PEERS | |
PEERS_LIST=$(echo $NEW_PEERS_INPUT | tr ',' ';') | |
;; | |
3) | |
# Keep current configuration | |
echo "Keeping current peer configuration." | |
PEERS_LIST=$CURRENT_PEERS | |
;; | |
*) | |
echo "Invalid option. Using all peer IP addresses automatically." | |
PEERS_LIST=$ALL_PEER_IPS | |
;; | |
esac | |
# Update peers in bashrc only if we have a list | |
if [ ! -z "$PEERS_LIST" ]; then | |
update_peers $PEERS_LIST | |
echo "ROS_STATIC_PEERS updated to: '$PEERS_LIST'" | |
else | |
echo "No peers configured. Communication may be limited to localhost only." | |
fi | |
} | |
# Parse command line arguments | |
JOINCODE="" | |
HOSTNAME="" | |
ACTION="install" | |
# Process options | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--help) | |
show_help | |
;; | |
--install) | |
ACTION="install" | |
shift | |
;; | |
--update-peers) | |
ACTION="update-peers" | |
shift | |
;; | |
--uninstall) | |
ACTION="uninstall" | |
shift | |
;; | |
--list-peers) | |
ACTION="list-peers" | |
shift | |
;; | |
--*) | |
echo "Unknown option: $1" | |
show_help | |
;; | |
*) | |
# First non-option argument is joincode | |
if [ -z "$JOINCODE" ]; then | |
JOINCODE="$1" | |
# Second non-option argument is hostname | |
elif [ -z "$HOSTNAME" ]; then | |
HOSTNAME="$1" | |
else | |
echo "Too many arguments" | |
show_help | |
fi | |
shift | |
;; | |
esac | |
done | |
# Use system hostname if none provided | |
if [ -z "$HOSTNAME" ]; then | |
HOSTNAME=$(hostname) | |
fi | |
# Handle actions | |
case "$ACTION" in | |
uninstall) | |
uninstall_husarnet | |
exit 0 | |
;; | |
list-peers) | |
list_peers | |
exit 0 | |
;; | |
update-peers) | |
echo "===== Updating ROS 2 Peers Configuration =====" | |
# Install Cyclone DDS if needed | |
install_cyclone_dds | |
# Create CycloneDDS configuration if it doesn't exist | |
if [ ! -f /var/tmp/cyclonedds.xml ]; then | |
echo "Creating CycloneDDS configuration..." | |
run_with_sudo "mkdir -p /var/tmp && touch /var/tmp/cyclonedds.xml && chmod 666 /var/tmp/cyclonedds.xml" | |
run_with_sudo "cat > /var/tmp/cyclonedds.xml << 'EOL' | |
<?xml version=\"1.0\" encoding=\"UTF-8\" ?> | |
<CycloneDDS xmlns=\"https://cdds.io/config\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"https://cdds.io/config https://raw.githubusercontent.com/eclipse-cyclonedds/cyclonedds/master/etc/cyclonedds.xsd\"> | |
<Domain> | |
<General> | |
<Interfaces> | |
<NetworkInterface name=\"hnet0\"/> | |
</Interfaces> | |
<AllowMulticast>false</AllowMulticast> | |
<FragmentSize>1194B</FragmentSize><!-- default: 1344 B minus Husarnet metadata (~150 B) --> | |
<Transport>udp6</Transport> | |
</General> | |
</Domain> | |
</CycloneDDS> | |
EOL" | |
fi | |
# Get current ROS_DOMAIN_ID | |
CURRENT_DOMAIN_ID=$(get_current_domain_id) | |
if [ ! -z "$CURRENT_DOMAIN_ID" ]; then | |
echo "Current ROS_DOMAIN_ID: $CURRENT_DOMAIN_ID" | |
read -p "Enter ROS_DOMAIN_ID (0-232, leave empty to keep current): " DOMAIN_ID | |
if [ -z "$DOMAIN_ID" ]; then | |
DOMAIN_ID=$CURRENT_DOMAIN_ID | |
fi | |
else | |
read -p "Enter ROS_DOMAIN_ID (0-232, default: 0): " DOMAIN_ID | |
if [ -z "$DOMAIN_ID" ]; then | |
DOMAIN_ID=0 | |
fi | |
fi | |
# Validate domain ID | |
if ! [[ "$DOMAIN_ID" =~ ^[0-9]+$ ]] || [ "$DOMAIN_ID" -lt 0 ] || [ "$DOMAIN_ID" -gt 232 ]; then | |
echo "Invalid ROS_DOMAIN_ID. Using default value (0)." | |
DOMAIN_ID=0 | |
fi | |
# Ensure ROS 2 environment variables are set | |
add_env_to_bashrc $DOMAIN_ID | |
# Configure peers using our new function | |
configure_peers | |
echo "" | |
echo "IMPORTANT: Make sure to use the same ROS_DOMAIN_ID ($DOMAIN_ID) on all machines in your ROS 2 network." | |
echo "To apply changes for your current session, run: source ~/.bashrc" | |
exit 0 | |
;; | |
install) | |
# Continue with the rest of the script for installation | |
;; | |
*) | |
echo "Unknown action: $ACTION" | |
show_help | |
;; | |
esac | |
echo "===== Husarnet and ROS 2 Setup =====" | |
# Check for existing Husarnet installation | |
if check_husarnet_installed; then | |
echo "Husarnet client is already installed." | |
HUSARNET_INSTALLED=true | |
else | |
HUSARNET_INSTALLED=false | |
if [ -z "$JOINCODE" ]; then | |
echo "Husarnet is not installed and no join code was provided." | |
echo "Usage: $0 <joincode> [custom_hostname]" | |
echo "Example: $0 fc94:b01d:1803:8dd8:b293:5c7d:7639:932a/XXXXXXXXXXXXXXXXXXXXXX" | |
exit 1 | |
fi | |
fi | |
# Check if already joined to a Husarnet network | |
if check_husarnet_joined; then | |
echo "This device is already joined to a Husarnet network." | |
HUSARNET_JOINED=true | |
else | |
HUSARNET_JOINED=false | |
if [ -z "$JOINCODE" ]; then | |
echo "Husarnet client is not connected to a network and no join code was provided." | |
echo "Usage: $0 <joincode> [custom_hostname]" | |
echo "Example: $0 fc94:b01d:1803:8dd8:b293:5c7d:7639:932a/XXXXXXXXXXXXXXXXXXXXXX" | |
exit 1 | |
fi | |
fi | |
# Install Husarnet if not installed | |
if [ "$HUSARNET_INSTALLED" = false ]; then | |
echo "Installing Husarnet Client..." | |
run_with_sudo "curl -s https://install.husarnet.com/install.sh | bash" | |
if [ $? -ne 0 ]; then | |
echo "Failed to install Husarnet. Please check your internet connection and try again." | |
exit 1 | |
fi | |
echo "Husarnet Client installed successfully." | |
fi | |
# Join Husarnet network if not joined | |
if [ "$HUSARNET_JOINED" = false ]; then | |
echo "Joining Husarnet network as '$HOSTNAME'..." | |
run_with_sudo "husarnet join $JOINCODE $HOSTNAME" | |
if [ $? -ne 0 ]; then | |
echo "Failed to join Husarnet network. Please check your join code and try again." | |
exit 1 | |
fi | |
echo "Successfully joined Husarnet network as '$HOSTNAME'." | |
fi | |
# Install Cyclone DDS RMW implementation | |
install_cyclone_dds | |
# Create CycloneDDS configuration for IPv6 | |
echo "Configuring CycloneDDS for IPv6 support..." | |
run_with_sudo "mkdir -p /var/tmp && touch /var/tmp/cyclonedds.xml && chmod 666 /var/tmp/cyclonedds.xml" | |
run_with_sudo "cat > /var/tmp/cyclonedds.xml << 'EOL' | |
<?xml version=\"1.0\" encoding=\"UTF-8\" ?> | |
<CycloneDDS xmlns=\"https://cdds.io/config\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"https://cdds.io/config https://raw.githubusercontent.com/eclipse-cyclonedds/cyclonedds/master/etc/cyclonedds.xsd\"> | |
<Domain> | |
<General> | |
<Interfaces> | |
<NetworkInterface name=\"hnet0\"/> | |
</Interfaces> | |
<AllowMulticast>false</AllowMulticast> | |
<FragmentSize>1194B</FragmentSize><!-- default: 1344 B minus Husarnet metadata (~150 B) --> | |
<Transport>udp6</Transport> | |
</General> | |
</Domain> | |
</CycloneDDS> | |
EOL" | |
# Get current ROS_DOMAIN_ID | |
CURRENT_DOMAIN_ID=$(get_current_domain_id) | |
if [ ! -z "$CURRENT_DOMAIN_ID" ]; then | |
echo "Current ROS_DOMAIN_ID: $CURRENT_DOMAIN_ID" | |
read -p "Enter ROS_DOMAIN_ID (0-232, leave empty to keep current): " DOMAIN_ID | |
if [ -z "$DOMAIN_ID" ]; then | |
DOMAIN_ID=$CURRENT_DOMAIN_ID | |
fi | |
else | |
read -p "Enter ROS_DOMAIN_ID (0-232, default: 0): " DOMAIN_ID | |
if [ -z "$DOMAIN_ID" ]; then | |
DOMAIN_ID=0 | |
fi | |
fi | |
# Validate domain ID | |
if ! [[ "$DOMAIN_ID" =~ ^[0-9]+$ ]] || [ "$DOMAIN_ID" -lt 0 ] || [ "$DOMAIN_ID" -gt 232 ]; then | |
echo "Invalid ROS_DOMAIN_ID. Using default value (0)." | |
DOMAIN_ID=0 | |
fi | |
# Add ROS 2 environment settings to .bashrc | |
add_env_to_bashrc $DOMAIN_ID | |
# Ask for peer update | |
read -p "Do you want to update the ROS_STATIC_PEERS configuration? (y/n): " UPDATE_PEERS | |
if [[ "$UPDATE_PEERS" =~ ^[Yy]$ ]]; then | |
# Use our new peer configuration function | |
configure_peers | |
fi | |
# Get hostname or IPv6 address for sharing with others | |
HUSARNET_HOSTNAME=$(hostname) | |
HUSARNET_IPV6=$(husarnet status 2>/dev/null | grep -oP 'Husarnet IP:.*\n\K[a-f0-9:]+' || echo "Not available") | |
# Print completion message | |
echo "" | |
echo "===== Setup Complete =====" | |
echo "" | |
echo "Husarnet Information:" | |
echo " Hostname: $HUSARNET_HOSTNAME" | |
echo " IPv6 Address: $HUSARNET_IPV6" | |
echo "" | |
echo "ROS 2 Configuration:" | |
echo " CycloneDDS XML: /var/tmp/cyclonedds.xml" | |
echo " ROS_DOMAIN_ID: $DOMAIN_ID" | |
echo " ROS environment variables have been added to your .bashrc" | |
echo "" | |
echo "IMPORTANT: Make sure to use the same ROS_DOMAIN_ID ($DOMAIN_ID) on all machines" | |
echo "in your ROS 2 network for proper communication." | |
echo "" | |
echo "To apply changes for your current session, run:" | |
echo " source ~/.bashrc" | |
echo "" | |
echo "To run this script again to update peers configuration:" | |
echo " $0 --update-peers" | |
echo "" | |
echo "To list all peers in your network:" | |
echo " $0 --list-peers" | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated code to Automatically discover peers, avoiding Human errors/typos.