Skip to content

Commit 38a24aa

Browse files
add tools for syncing files between local repo and remote blueos on the pi
1 parent 94e77ea commit 38a24aa

File tree

2 files changed

+453
-0
lines changed

2 files changed

+453
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Default values
6+
USER="pi"
7+
HOST=""
8+
SERVICE=""
9+
CONTAINER_NAME="blueos-core"
10+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
SERVICES_DIR="$(cd "$SCRIPT_DIR/../services" && pwd)"
12+
13+
# Colors for output
14+
RED='\033[0;31m'
15+
YELLOW='\033[1;33m'
16+
GREEN='\033[0;32m'
17+
BLUE='\033[0;34m'
18+
NC='\033[0m' # No Color
19+
20+
usage() {
21+
cat << EOF
22+
Usage: $0 [OPTIONS]
23+
24+
Copy service files from local services folder to a remote BlueOS container.
25+
26+
OPTIONS:
27+
-u, --user USER SSH user (default: pi)
28+
-h, --host HOST Remote host (required)
29+
-s, --service SERVICE Specific service to copy (optional, copies all if not specified)
30+
--help Show this help message
31+
32+
EXAMPLES:
33+
# Copy a specific service
34+
$0 --host blueos.local --service ardupilot_manager
35+
36+
# Copy all services
37+
$0 --host 192.168.2.2 --user pi
38+
39+
SSH KEY SETUP:
40+
This script requires SSH key-based authentication.
41+
To set up SSH keys on the remote host, run:
42+
43+
ssh-copy-id pi@blueos.local
44+
45+
Or if using a different user:
46+
47+
ssh-copy-id <user>@<host>
48+
49+
This will copy your public SSH key to the remote host and allow
50+
password-less authentication.
51+
52+
EOF
53+
exit 1
54+
}
55+
56+
log_info() {
57+
echo -e "${BLUE}[INFO]${NC} $1"
58+
}
59+
60+
log_success() {
61+
echo -e "${GREEN}[SUCCESS]${NC} $1"
62+
}
63+
64+
log_warning() {
65+
echo -e "${YELLOW}[WARNING]${NC} $1"
66+
}
67+
68+
log_error() {
69+
echo -e "${RED}[ERROR]${NC} $1"
70+
}
71+
72+
# Parse command line arguments
73+
while [[ $# -gt 0 ]]; do
74+
case $1 in
75+
-u|--user)
76+
USER="$2"
77+
shift 2
78+
;;
79+
-h|--host)
80+
HOST="$2"
81+
shift 2
82+
;;
83+
-s|--service)
84+
SERVICE="$2"
85+
shift 2
86+
;;
87+
-p|--password)
88+
log_warning "Password authentication is not supported. Please use SSH keys instead."
89+
log_info "Run: ssh-copy-id $USER@$HOST"
90+
exit 1
91+
;;
92+
--help)
93+
usage
94+
;;
95+
*)
96+
log_error "Unknown option: $1"
97+
usage
98+
;;
99+
esac
100+
done
101+
102+
# Validate required arguments
103+
if [ -z "$HOST" ]; then
104+
log_error "Remote host is required"
105+
usage
106+
fi
107+
108+
# Validate services directory exists
109+
if [ ! -d "$SERVICES_DIR" ]; then
110+
log_error "Services directory not found: $SERVICES_DIR"
111+
exit 1
112+
fi
113+
114+
# Validate specific service if provided
115+
if [ -n "$SERVICE" ]; then
116+
if [ ! -d "$SERVICES_DIR/$SERVICE" ]; then
117+
log_error "Service '$SERVICE' not found in $SERVICES_DIR"
118+
log_info "Available services:"
119+
find "$SERVICES_DIR" -mindepth 1 -maxdepth 1 -type d -printf " - %f\n"
120+
exit 1
121+
fi
122+
log_info "Will copy service: $SERVICE"
123+
else
124+
log_warning "No specific service specified. Will copy ALL services."
125+
log_warning "This may take some time and overwrite existing files in the container."
126+
log_info "Available services:"
127+
find "$SERVICES_DIR" -mindepth 1 -maxdepth 1 -type d -printf " - %f\n"
128+
echo ""
129+
read -p "Continue? (y/N) " -n 1 -r
130+
echo
131+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
132+
log_info "Aborted by user"
133+
exit 0
134+
fi
135+
fi
136+
137+
# Test SSH connection
138+
log_info "Testing SSH connection to $USER@$HOST..."
139+
if ! ssh -o BatchMode=yes -o ConnectTimeout=5 "$USER@$HOST" "exit" 2>/dev/null; then
140+
log_error "Cannot connect to $USER@$HOST using SSH keys"
141+
log_info "Please set up SSH key authentication first:"
142+
log_info " ssh-copy-id $USER@$HOST"
143+
exit 1
144+
fi
145+
log_success "SSH connection successful"
146+
147+
# Check if container exists and is running
148+
log_info "Checking if container '$CONTAINER_NAME' exists on remote host..."
149+
# shellcheck disable=SC2029
150+
if ! ssh "$USER@$HOST" "docker ps --format '{{.Names}}' | grep -q '^$CONTAINER_NAME\$'" 2>/dev/null; then
151+
log_error "Container '$CONTAINER_NAME' is not running on remote host"
152+
log_info "Available containers:"
153+
ssh "$USER@$HOST" "docker ps --format 'table {{.Names}}\t{{.Status}}'"
154+
exit 1
155+
fi
156+
log_success "Container '$CONTAINER_NAME' is running"
157+
158+
# Create temporary directory on remote host
159+
TEMP_DIR="/tmp/blueos-services-$$"
160+
log_info "Creating temporary directory on remote host: $TEMP_DIR"
161+
# shellcheck disable=SC2029
162+
ssh "$USER@$HOST" "mkdir -p $TEMP_DIR"
163+
164+
# Cleanup function
165+
cleanup() {
166+
log_info "Cleaning up temporary directory on remote host..."
167+
# shellcheck disable=SC2029
168+
ssh "$USER@$HOST" "rm -rf $TEMP_DIR" 2>/dev/null || true
169+
}
170+
trap cleanup EXIT
171+
172+
# Copy services to remote host
173+
if [ -n "$SERVICE" ]; then
174+
log_info "Copying service '$SERVICE' to remote host..."
175+
rsync -avz --progress --exclude='__pycache__' --exclude='*.pyc' --exclude='*.pyo' --exclude='.pytest_cache' -e ssh "$SERVICES_DIR/$SERVICE/" "$USER@$HOST:$TEMP_DIR/$SERVICE/"
176+
SERVICES_TO_COPY="$SERVICE"
177+
else
178+
log_info "Copying all services to remote host..."
179+
rsync -avz --progress --exclude='__pycache__' --exclude='*.pyc' --exclude='*.pyo' --exclude='.pytest_cache' -e ssh "$SERVICES_DIR/" "$USER@$HOST:$TEMP_DIR/"
180+
SERVICES_TO_COPY=$(find "$SERVICES_DIR" -mindepth 1 -maxdepth 1 -type d -printf "%f\n")
181+
fi
182+
183+
# Copy from temp directory into container
184+
log_info "Copying files into container '$CONTAINER_NAME'..."
185+
for svc in $SERVICES_TO_COPY; do
186+
log_info " Copying $svc..."
187+
# shellcheck disable=SC2029
188+
ssh "$USER@$HOST" "docker exec $CONTAINER_NAME mkdir -p /home/pi/services/$svc"
189+
# shellcheck disable=SC2029
190+
ssh "$USER@$HOST" "docker cp $TEMP_DIR/$svc/. $CONTAINER_NAME:/home/pi/services/$svc/"
191+
done
192+
193+
log_success "All files copied successfully!"
194+
log_info "Files are now in the container at: /home/pi/services/"
195+
196+
# Offer to restart services
197+
if [ -n "$SERVICE" ]; then
198+
echo ""
199+
log_info "You may need to restart the '$SERVICE' service in the container for changes to take effect."
200+
else
201+
echo ""
202+
log_info "You may need to restart services in the container for changes to take effect."
203+
fi
204+

0 commit comments

Comments
 (0)