-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·239 lines (209 loc) · 6.09 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env bash
VENDOR_HOME=/opt/candy-line
SERVICE_NAME=ltepi2
GITHUB_ID=CANDY-LINE/ltepi2-service
VERSION=3.0.3
BOOT_APN=${BOOT_APN:-umobile.jp}
NODEJS_VERSIONS="v4"
SERVICE_HOME=${VENDOR_HOME}/${SERVICE_NAME}
SRC_DIR="${SRC_DIR:-/tmp/$(basename ${GITHUB_ID})-${VERSION}}"
CANDY_RED=${CANDY_RED:-1}
KERNEL="${KERNEL:-$(uname -r)}"
CONTAINER_MODE=0
if [ "${KERNEL}" != "$(uname -r)" ]; then
CONTAINER_MODE=1
fi
WELCOME_FLOW_URL=https://git.io/vKhk3
ROUTER_ENABLED=${ROUTER_ENABLED:-1}
LTE_PING_INTERVAL_SEC=${LTE_PING_INTERVAL_SEC:-0}
PRESERVE_APN=${PRESERVE_APN:-0}
REBOOT=0
function err {
echo -e "\033[91m[ERROR] $1\033[0m"
}
function info {
echo -e "\033[92m[INFO] $1\033[0m"
}
function alert {
echo -e "\033[93m[ALERT] $1\033[0m"
}
function setup {
[ "${DEBUG}" ] || rm -fr ${SRC_DIR}
}
function assert_root {
if [[ $EUID -ne 0 ]]; then
alert "This script must be run as root"
exit 1
fi
}
function test_connectivity {
curl -s --head --fail -o /dev/null https://github.com 2>&1
if [ "$?" != 0 ]; then
alert "Internet connection is required"
exit 1
fi
}
function ask_to_unistall_if_installed {
if [ -f "${SERVICE_HOME}/environment" ]; then
alert "Please uninstall ltepi2-service first by 'sudo /opt/candy-line/ltepi2/uninstall.sh'"
exit 1
fi
}
function download {
if [ -d "${SRC_DIR}" ]; then
return
fi
cd /tmp
curl -L https://github.com/${GITHUB_ID}/archive/${VERSION}.tar.gz | tar zx
if [ "$?" != "0" ]; then
err "Make sure internet is available"
exit 1
fi
}
function _ufw_setup {
info "Configuring ufw..."
ufw --force disable
ufw deny in on ppp0
for n in `ls /sys/class/net`
do
if [ "${n}" != "lo" ] && [ "${n}" != "ppp0" ]; then
ufw allow in on ${n}
if [ "$?" != "0" ]; then
err "Failed to configure ufw for the network interface: ${n}"
exit 4
fi
fi
done
ufw --force enable
}
function install_ppp_mode {
if [ "${ROUTER_ENABLED}" != "0" ]; then
info "*** To be configured for ROUTER MODE ***"
return
fi
info "*** To be configured for PPP MODEM MODE ***"
info "Installing ufw and ppp..."
apt-get update -y
apt-get install -y ufw ppp pppconfig
cp -f ${SRC_DIR}/systemd/ltepi2.chatscript /etc/chatscripts/ltepi2
cp -f ${SRC_DIR}/systemd/ltepi2.peers /etc/ppp/peers/ltepi2
_ufw_setup
}
function install_candy_board {
RET=`which pip`
RET=$?
if [ "${RET}" != "0" ]; then
info "Installing pip..."
curl -L https://bootstrap.pypa.io/get-pip.py | /usr/bin/env python
fi
pip install --upgrade candy-board-cli
pip install --upgrade candy-board-amt
}
function install_candy_red {
if [ "${CANDY_RED}" == "0" ]; then
return
fi
NODEJS_VER=`node -v`
NPM_VER=`npm -v`
if [ "$?" == "0" ]; then
for v in ${NODEJS_VERSIONS}
do
echo ${NODEJS_VER} | grep -oE "${v/./\\.}\..*"
if [ "$?" == "0" ]; then
unset NODEJS_VER
fi
done
else
NODEJS_VER="N/A"
fi
apt-get update -y
if [ -n "${NODEJS_VER}" ]; then
info "Installing Node.js..."
MODEL_NAME=`cat /proc/cpuinfo | grep "model name"`
if [ "$?" != "0" ]; then
alert "Unsupported environment"
exit 1
fi
apt-get remove -y nodered nodejs nodejs-legacy npm
echo ${MODEL_NAME} | grep -o "ARMv6"
if [ "$?" == "0" ]; then
cd /tmp
wget http://node-arm.herokuapp.com/node_archive_armhf.deb
dpkg -i node_archive_armhf.deb
else
curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
apt-get install -y nodejs
fi
fi
info "Installing dependencies..."
apt-get install -y python-dev python-rpi.gpio bluez libudev-dev
cd ~
npm cache clean
info "Installing CANDY-RED..."
WELCOME_FLOW_URL=${WELCOME_FLOW_URL} NODE_OPTS=--max-old-space-size=128 npm install -g --unsafe-perm candy-red
REBOOT=1
}
function install_service {
info "Installing system service ..."
RET=`systemctl | grep ${SERVICE_NAME}.service | grep -v not-found`
RET=$?
if [ "${RET}" == "0" ]; then
return
fi
download
if [ ! -f "${SRC_DIR}/systemd/boot-apn.${BOOT_APN}.json" ]; then
err "Invalid BOOT_APN value => ${BOOT_APN}"
exit 1
fi
LIB_SYSTEMD="$(dirname $(dirname $(which systemctl)))"
if [ "${LIB_SYSTEMD}" == "/" ]; then
LIB_SYSTEMD=""
fi
LIB_SYSTEMD="${LIB_SYSTEMD}/lib/systemd"
mkdir -p ${SERVICE_HOME}
cp -f ${SRC_DIR}/systemd/boot-apn.${BOOT_APN}.json ${SERVICE_HOME}/boot-apn.json
cp -f ${SRC_DIR}/systemd/boot-ip.*.json ${SERVICE_HOME}
cp -f ${SRC_DIR}/systemd/environment.txt ${SERVICE_HOME}/environment
sed -i -e "s/%VERSION%/${VERSION//\//\\/}/g" ${SERVICE_HOME}/environment
sed -i -e "s/%ROUTER_ENABLED%/${ROUTER_ENABLED//\//\\/}/g" ${SERVICE_HOME}/environment
sed -i -e "s/%LTE_PING_INTERVAL_SEC%/${LTE_PING_INTERVAL_SEC//\//\\/}/g" ${SERVICE_HOME}/environment
sed -i -e "s/%PRESERVE_APN%/${PRESERVE_APN//\//\\/}/g" ${SERVICE_HOME}/environment
FILES=`ls ${SRC_DIR}/systemd/*.sh`
FILES="${FILES} `ls ${SRC_DIR}/systemd/server_*.py`"
for f in ${FILES}
do
install -o root -g root -D -m 755 ${f} ${SERVICE_HOME}
done
cp -f ${SRC_DIR}/systemd/${SERVICE_NAME}.service.txt ${SRC_DIR}/systemd/${SERVICE_NAME}.service
sed -i -e "s/%VERSION%/${VERSION//\//\\/}/g" ${SRC_DIR}/systemd/${SERVICE_NAME}.service
install -o root -g root -D -m 644 ${SRC_DIR}/systemd/${SERVICE_NAME}.service ${LIB_SYSTEMD}/system/
systemctl enable ${SERVICE_NAME}
install -o root -g root -D -m 755 ${SRC_DIR}/uninstall.sh ${SERVICE_HOME}/uninstall.sh
info "${SERVICE_NAME} service has been installed"
REBOOT=1
}
function teardown {
[ "${DEBUG}" ] || rm -fr ${SRC_DIR}
if [ "${CONTAINER_MODE}" == "0" ] && [ "${REBOOT}" == "1" ]; then
alert "*** Please reboot the system (enter 'sudo reboot') ***"
fi
}
function package {
rm -f $(basename ${GITHUB_ID})-*.tgz
# http://unix.stackexchange.com/a/9865
COPYFILE_DISABLE=1 tar --exclude="./.*" --exclude=Makefile -zcf $(basename ${GITHUB_ID})-${VERSION}.tgz *
}
# main
if [ "$1" == "pack" ]; then
package
exit 0
fi
assert_root
test_connectivity
ask_to_unistall_if_installed
setup
install_candy_board
install_candy_red
install_service
install_ppp_mode
teardown