-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.sh
88 lines (74 loc) · 2.48 KB
/
test.sh
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
#!/bin/bash
# Start the SSH agent
eval "$(ssh-agent -s)"
ssh_key_location="$HOME/.ssh/cacophony-pi"
ssh-add "$ssh_key_location"
# Discover Raspberry Pi services on the network
echo "Discovering Raspberry Pis with service _cacophonator-management._tcp..."
readarray -t services < <(avahi-browse -t -r _cacophonator-management._tcp | grep 'hostname' | awk '{print $3}' | sed 's/\[//' | sed 's/\]//' | sed 's/\.$/.local/')
if [ ${#services[@]} -eq 0 ]; then
echo "No Raspberry Pi services found on the network."
exit 1
fi
# Display the discovered services
echo "Found Raspberry Pi services:"
for i in "${!services[@]}"; do
echo "$((i + 1))) ${services[i]}"
done
# Let the user select a service
read -p "Select a Raspberry Pi to deploy to (1-${#services[@]}): " selection
pi_address=${services[$((selection - 1))]}
if [ -z "$pi_address" ]; then
echo "Invalid selection."
exit 1
fi
echo "Selected Raspberry Pi at: $pi_address"
while true; do
# Deployment
echo "Deploying to Raspberry Pi..."
make
# Stop the service
ssh_stop_command=("ssh" "-i" "$ssh_key_location" "pi@$pi_address" "sudo systemctl stop managementd.service")
echo "Executing: ${ssh_stop_command[*]}"
"${ssh_stop_command[@]}"
if [ $? -ne 0 ]; then
echo "Error: SSH stop command failed"
break
fi
# Copy the file to a temporary location
scp_command=("scp" "-i" "$ssh_key_location" "./managementd" "pi@$pi_address:/home/pi")
echo "Executing: ${scp_command[*]}"
"${scp_command[@]}"
if [ $? -ne 0 ]; then
echo "Error: SCP failed"
break
fi
# Move the file to /usr/bin with sudo
ssh_move_command=("ssh" "-i" "$ssh_key_location" "pi@$pi_address" "sudo mv /home/pi/managementd /usr/bin/")
echo "Executing: ${ssh_move_command[*]}"
"${ssh_move_command[@]}"
if [ $? -ne 0 ]; then
echo "Error: SSH move command failed"
break
fi
# Restart the service
ssh_command=("ssh" "-i" "$ssh_key_location" "pi@$pi_address" "sudo systemctl restart managementd.service")
echo "Executing: ${ssh_command[*]}"
"${ssh_command[@]}"
if [ $? -ne 0 ]; then
echo "Error: SSH command failed"
break
fi
# Stream logs from the service
log_command=("ssh" "-i" "$ssh_key_location" "pi@$pi_address" "sudo journalctl -u managementd.service -f")
echo "Streaming logs from managementd.service... (press Ctrl+C to stop)"
"${log_command[@]}"
echo "Deployment completed. Press any key to deploy again or Ctrl+C to exit."
read -n 1 -s
if [ $? -ne 0 ]; then
break
fi
echo # new line for readability
done
# Kill the SSH agent when done
eval "$(ssh-agent -k)"