-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain
More file actions
executable file
·113 lines (103 loc) · 3.59 KB
/
main
File metadata and controls
executable file
·113 lines (103 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# Get the directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd $DIR
# Open Xhost
xhost +local: > /dev/null 2>&1
cd ./docker
# Environment variables
export ROS_DOMAIN_ID=${ROS_DOMAIN_ID:=100}
# Create or overwrite the .env file with static settings
# Overwrite the .env file with the desired content
cat <<EOL > .env
# Docker Compose Settings
COMPOSE_PROJECT_NAME=main
COMPOSE_BAKE=true
# ROS Settings
ROS_DISTRO=humble
# Dynamic UID and GID
LOCAL_USER_ID=$(id -u)
LOCAL_GROUP_ID=$(id -g)
EOL
# Ensure cleanup on script exit
cleanup() {
if [[ "$ACTION" != "close" && "$ACTION" != "build" && "$ACTION" != "run" && "$ACTION" != "groot" ]]; then
if docker ps --filter "name=main" --filter "status=running" | grep -q "main"; then
echo "Container is still running. Use 'close' to stop it."
fi
elif [[ "$ACTION" == "close" ]]; then
if docker ps --filter "name=main" --filter "status=running" | grep -q "main"; then
docker compose down
fi
fi
xhost -local: > /dev/null 2>&1
}
trap cleanup EXIT
# Argument handling
ACTION=${1:-}
CUSTOM_COMMAND=${2:-}
print_usage() {
echo -e "\033[1;32m----- [ MAIN Usage ] ------------------------\033[0m"
echo -e "\033[1;32m|\033[0m build - Build the ROS workspace"
echo -e "\033[1;32m|\033[0m run - Run the main program"
echo -e "\033[1;32m|\033[0m groot - Launch Groot2"
echo -e "\033[1;32m|\033[0m enter - Enter the container"
echo -e "\033[1;32m|\033[0m close - Stop the container"
echo -e "\033[1;32m|\033[0m --command <cmd> - Run a custom command"
echo -e "\033[1;32m---------------------------------------------\033[0m"
}
if [[ "$ACTION" == "--command" ]]; then
if [[ -z "$CUSTOM_COMMAND" ]]; then
echo "Error: No command specified for --command."
exit 1
fi
if ! docker ps --filter "name=main" --filter "status=running" | grep -q "main"; then
echo "Container 'main' is not running. Starting it..."
docker compose up -d
fi
docker compose exec main bash -c "$CUSTOM_COMMAND"
exit 0
fi
case $ACTION in
build)
export AUTO_BUILD=true AUTO_RUN=false
docker compose up
;;
run)
export AUTO_BUILD=false AUTO_RUN=true
echo "Running the container..."
docker compose up
docker compose down
;;
groot)
export AUTO_BUILD=false AUTO_RUN=false
echo "Launching Groot..."
docker compose up -d
docker compose exec main bash -c "/home/ros/Eurobot-2025-Main/groot/groot.AppImage"
echo "Groot process exited. Stopping the container..."
docker compose down
;;
enter)
export AUTO_BUILD=false AUTO_RUN=false
echo "Entering the container..."
if ! docker ps --filter "name=main" --filter "status=running" | grep -q "main"; then
echo -e "Container 'main' is not running.\nStarting without build or run..."
docker compose up -d
fi
docker exec -it --user ros main bash || echo "Exited container without shutting it down."
;;
close)
export AUTO_BUILD=false AUTO_RUN=false
if docker ps --filter "name=main" --filter "status=running" | grep -q "main" || \
docker ps --filter "name=main" --filter "status=exited" | grep -q "main"; then
echo "Stopping the container..."
docker compose down
else
echo "Container is already stopped."
fi
;;
*)
print_usage
exit 1
;;
esac