-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploy.sh
109 lines (91 loc) · 3.08 KB
/
deploy.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/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
# Create the service file if it doesn't exist
ssh -i "$ssh_key_location" "pi@$pi_address" "if [ ! -f /etc/systemd/system/cacophony-config-sync.service ]; then
sudo tee /etc/systemd/system/cacophony-config-sync.service > /dev/null << 'EOF'
[Unit]
Description=Cacophony Config Sync Service
After=network.target
[Service]
ExecStart=/usr/bin/cacophony-config-sync
Restart=always
User=pi
Group=pi
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable cacophony-config-sync.service
fi"
# Stop the service
ssh_stop_command=("ssh" "-i" "$ssh_key_location" "pi@$pi_address" "sudo systemctl stop cacophony-config-sync.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" "./cacophony-config-sync" "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/cacophony-config-sync /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 cacophony-config-sync.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 -fu cacophony-config-sync.service ")
echo "Streaming logs from cacophony-config-sync.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)"