-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_rpi.sh
141 lines (116 loc) · 4.26 KB
/
setup_rpi.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# === Load Config ===
# Source variables from the variables file
source rpi_config.env
# === Adding some authorized public keys ===
echo "=== Adding some authorized public keys ==="
for pubkey_file in $SSH_KEYS_DIR_PATH/*.pub; do
echo $pubkey_file
ssh-copy-id -f -i $pubkey_file pi@$PI_HOST
if [ $? -eq 0 ]; then
echo "Public key $pubkey_file successfully added."
else
echo "Error adding Public key $pubkey_file."
fi
done
# === Running main apt install commands ===
echo "=== Running main apt install commands ==="
commands=(
"sudo apt-get update -y && sudo apt-get --allow-releaseinfo-change -y update"
"sudo apt-get install -y python3-dev python3-pip git openvpn"
"curl -sSL https://get.docker.com | sh && sudo usermod -aG docker pi"
)
for cmd in "${commands[@]}"; do
ssh pi@$PI_HOST "$cmd"
if [ $? -eq 0 ]; then
echo "Command \"$cmd\" executed successfully."
else
echo "Error while executing command \"$cmd\"."
fi
done
# === VPN setup steps ===
echo "=== VPN setup steps ==="
scp $OPENVPN_CONFIG_FILE_PATH pi@$PI_HOST:/home/pi/client.conf
if [ $? -eq 0 ]; then
echo "VPN Configuration file sent successfully."
else
echo "Error sending VPN configuration file"
fi
commands=(
"sudo mv /home/pi/client.conf /etc/openvpn/client.conf"
"sudo echo askpass /etc/openvpn/auth.txt >> /etc/openvpn/client.conf"
"sudo touch /etc/openvpn/auth.txt"
"echo "$OPEN_VPN_PASSWORD" | sudo tee -a /etc/openvpn/auth.txt >/dev/null"
)
for cmd in "${commands[@]}"; do
ssh pi@$PI_HOST "$cmd"
if [ $? -eq 0 ]; then
echo "Command \"$cmd\" executed successfully."
else
echo "Error while executing command \"$cmd\"."
fi
done
# === PYRO ENGINE setup steps ===
echo "=== PYRO ENGINE setup steps ==="
# clone pyro-engine main branch
ssh pi@$PI_HOST sudo git clone --branch main https://github.com/pyronear/pyro-engine.git
# Transfer env file & move it to pyro-engine folder
scp $PYROENGINE_ENV_FILE_PATH pi@$PI_HOST:.env
ssh pi@$PI_HOST sudo mv /home/pi/.env /home/pi/pyro-engine/.env
# Transfer credentials file & move it to pyro-engine folder
scp $PYROENGINE_CREDENTIALS_LOCAL_PATH pi@$PI_HOST:credentials.json
commands=(
"sudo mkdir -p /home/pi/pyro-engine/data/"
"sudo mv /home/pi/credentials.json /home/pi/pyro-engine/data/credentials.json"
)
for cmd in "${commands[@]}"; do
ssh pi@$PI_HOST "$cmd"
if [ $? -eq 0 ]; then
echo "Command \"$cmd\" executed successfully."
else
echo "Error while executing command \"$cmd\"."
fi
done
# === CRONTAB setup ===
echo "=== CRONTAB setup ==="
NEW_CRON_TAB=$(cat << EOF
# Update from github
0 * * * * bash /home/pi/pyro-engine/scripts/update_script.sh
# Check internet connection
*/10 * * * * bash /home/pi/pyro-engine/scripts/check_internet_connection.sh
EOF
)
echo "$NEW_CRON_TAB" | ssh pi@$PI_HOST "crontab -"
# === reboot ===
echo "=== reboot ==="
ssh pi@$PI_HOST sudo reboot
echo "Waiting for the Raspberry Pi to be restarted"
sleep 10
while ! ping -c 1 $PI_HOST &> /dev/null; do
sleep 1
done
echo "The raspberry Pi is now available "
# === Start pyro-engine services ===
echo "=== Start pyro-engine services ==="
ssh pi@$PI_HOST "cd /home/pi/pyro-engine/ && make run"
# === Network setup: wifi & static ethernet ===
echo "=== Network setup: wifi(optionnal) & static ethernet ==="
if [[ -n "${WIFI_SSID// }" ]]; then
echo "WIFI SSID provided, setting up wifi"
ssh pi@$PI_HOST sudo nmcli con add type wifi ifname wlan0 con-name $WIFI_SSID ssid $WIFI_SSID -- wifi-sec.key-mgmt wpa-psk wifi-sec.psk $WIFI_PASSWORD connection.autoconnect yes
fi
echo "Setting up static ethernet "
echo "If you are connected to the raspbeery via ethernet, you may lose the connection in a few moments. This is normal, as you have configured a fixed ip address."
commands=(
"sudo nmcli connection add type ethernet ifname eth0 con-name static-eth0 ipv4.addresses $STATIC_ETHERNET_IP/16 ipv4.method manual"
"sudo nmcli connection modify static-eth0 ipv4.dns \"$DEFAULT_DNS\""
"sudo nmcli connection up static-eth0"
)
for cmd in "${commands[@]}"; do
ssh pi@$PI_HOST "$cmd"
if [ $? -eq 0 ]; then
echo "Command \"$cmd\" executed successfully."
else
echo "Error while executing command \"$cmd\"."
fi
done