-
Notifications
You must be signed in to change notification settings - Fork 210
/
install.sh
executable file
·200 lines (156 loc) · 5.58 KB
/
install.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
# Stop on the first sign of trouble
set -e
if [ $UID != 0 ]; then
echo "ERROR: Operation not permitted. Forgot sudo?"
exit 1
fi
VERSION="master"
if [[ $1 != "" ]]; then VERSION=$1; fi
echo "The Things Network Gateway installer"
echo "Version $VERSION"
# Update the gateway installer to the correct branch (defaults to master)
echo "Updating installer files..."
OLD_HEAD=$(git rev-parse HEAD)
git fetch
git checkout -q $VERSION
git pull
NEW_HEAD=$(git rev-parse HEAD)
if [[ $OLD_HEAD != $NEW_HEAD ]]; then
echo "New installer found. Restarting process..."
exec "./install.sh" "$VERSION"
fi
# Request gateway configuration data
# There are two ways to do it, manually specify everything
# or rely on the gateway EUI and retrieve settings files from remote (recommended)
echo "Gateway configuration:"
# Try to get gateway ID from MAC address
# First try eth0, if that does not exist, try wlan0 (for RPi Zero)
GATEWAY_EUI_NIC="eth0"
if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
GATEWAY_EUI_NIC="wlan0"
fi
if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
echo "ERROR: No network interface found. Cannot set gateway ID."
exit 1
fi
GATEWAY_EUI=$(ip link show $GATEWAY_EUI_NIC | awk '/ether/ {print $2}' | awk -F\: '{print $1$2$3"FFFE"$4$5$6}')
GATEWAY_EUI=${GATEWAY_EUI^^} # toupper
echo "Detected EUI $GATEWAY_EUI from $GATEWAY_EUI_NIC"
read -r -p "Do you want to use remote settings file? [y/N]" response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y) ]]; then
NEW_HOSTNAME="ttn-gateway"
REMOTE_CONFIG=true
else
printf " Host name [ttn-gateway]:"
read NEW_HOSTNAME
if [[ $NEW_HOSTNAME == "" ]]; then NEW_HOSTNAME="ttn-gateway"; fi
printf " Descriptive name [ttn-ic880a]:"
read GATEWAY_NAME
if [[ $GATEWAY_NAME == "" ]]; then GATEWAY_NAME="ttn-ic880a"; fi
printf " Contact email: "
read GATEWAY_EMAIL
printf " Latitude [0]: "
read GATEWAY_LAT
if [[ $GATEWAY_LAT == "" ]]; then GATEWAY_LAT=0; fi
printf " Longitude [0]: "
read GATEWAY_LON
if [[ $GATEWAY_LON == "" ]]; then GATEWAY_LON=0; fi
printf " Altitude [0]: "
read GATEWAY_ALT
if [[ $GATEWAY_ALT == "" ]]; then GATEWAY_ALT=0; fi
fi
# Change hostname if needed
CURRENT_HOSTNAME=$(hostname)
if [[ $NEW_HOSTNAME != $CURRENT_HOSTNAME ]]; then
echo "Updating hostname to '$NEW_HOSTNAME'..."
hostname $NEW_HOSTNAME
echo $NEW_HOSTNAME > /etc/hostname
sed -i "s/$CURRENT_HOSTNAME/$NEW_HOSTNAME/" /etc/hosts
fi
# Check dependencies
echo "Installing dependencies..."
apt-get install swig libftdi-dev python-dev
# Install LoRaWAN packet forwarder repositories
INSTALL_DIR="/opt/ttn-gateway"
if [ ! -d "$INSTALL_DIR" ]; then mkdir $INSTALL_DIR; fi
pushd $INSTALL_DIR
# Build libraries
if [ ! -d libmpsse ]; then
git clone https://github.com/devttys0/libmpsse.git
pushd libmpsse/src
else
pushd libmpsse/src
git reset --hard
git pull
fi
./configure --disable-python
make
make install
ldconfig
popd
# Build LoRa gateway app
if [ ! -d lora_gateway ]; then
git clone -b legacy https://github.com/TheThingsNetwork/lora_gateway.git
pushd lora_gateway
else
pushd lora_gateway
git fetch origin
git checkout legacy
git reset --hard
fi
cp ./libloragw/99-libftdi.rules /etc/udev/rules.d/99-libftdi.rules
sed -i -e 's/CFG_SPI= native/CFG_SPI= ftdi/g' ./libloragw/library.cfg
sed -i -e 's/PLATFORM= kerlink/PLATFORM= lorank/g' ./libloragw/library.cfg
sed -i -e 's/ATTRS{idProduct}=="6010"/ATTRS{idProduct}=="6014"/g' /etc/udev/rules.d/99-libftdi.rules
make
popd
# Build packet forwarder
if [ ! -d packet_forwarder ]; then
git clone -b legacy https://github.com/TheThingsNetwork/packet_forwarder.git
pushd packet_forwarder
else
pushd packet_forwarder
git fetch origin
git checkout legacy
git reset --hard
fi
make
popd
# Symlink poly packet forwarder
if [ ! -d bin ]; then mkdir bin; fi
if [ -f ./bin/poly_pkt_fwd ]; then rm ./bin/poly_pkt_fwd; fi
ln -s $INSTALL_DIR/packet_forwarder/poly_pkt_fwd/poly_pkt_fwd ./bin/poly_pkt_fwd
cp -f ./packet_forwarder/poly_pkt_fwd/global_conf.json ./bin/global_conf.json
LOCAL_CONFIG_FILE=$INSTALL_DIR/bin/local_conf.json
# Remove old config file
if [ -e $LOCAL_CONFIG_FILE ]; then rm $LOCAL_CONFIG_FILE; fi;
if [ "$REMOTE_CONFIG" = true ] ; then
# Get remote configuration repo
if [ ! -d gateway-remote-config ]; then
git clone https://github.com/ttn-zh/gateway-remote-config.git
pushd gateway-remote-config
else
pushd gateway-remote-config
git pull
git reset --hard
fi
ln -s $INSTALL_DIR/gateway-remote-config/$GATEWAY_EUI.json $LOCAL_CONFIG_FILE
popd
else
echo -e "{\n\t\"gateway_conf\": {\n\t\t\"gateway_ID\": \"$GATEWAY_EUI\",\n\t\t\"servers\": [ { \"server_address\": \"router.eu.thethings.network\", \"serv_port_up\": 1700, \"serv_port_down\": 1700, \"serv_enabled\": true } ],\n\t\t\"ref_latitude\": $GATEWAY_LAT,\n\t\t\"ref_longitude\": $GATEWAY_LON,\n\t\t\"ref_altitude\": $GATEWAY_ALT,\n\t\t\"contact_email\": \"$GATEWAY_EMAIL\",\n\t\t\"description\": \"$GATEWAY_NAME\" \n\t}\n}" >$LOCAL_CONFIG_FILE
fi
popd
echo "Gateway EUI is: $GATEWAY_EUI"
echo "The hostname is: $NEW_HOSTNAME"
echo "Open TTN console and register your gateway using your EUI: https://console.thethingsnetwork.org/gateways"
echo
echo "Installation completed."
# Start packet forwarder as a service
cp ./start.sh $INSTALL_DIR/bin/
cp ./ttn-gateway.service /lib/systemd/system/
systemctl enable ttn-gateway.service
echo "The system will reboot in 5 seconds..."
sleep 5
shutdown -r now