forked from firewalla/firewalla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildraw
executable file
·142 lines (115 loc) · 5.75 KB
/
buildraw
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
#!/bin/bash
function perr_and_exit()
{
echo "$1" >&2
exit 1
}
function usage()
{
cat << EOM
Usage: buildraw [-n] [-h]"
-n Skip apt dependency installations.
-h Show this help.
EOM
}
function install_dependencies()
{
git pull || perr_and_exit "Failed to pull latest firewalla code."
# TODO: check local apt mirror
echo "Installing node source repository..."
sudo curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - || perr_and_exit "Failed to install node source repo"
echo "Executing apt-get update, please be patient..."
sudo apt-get update || perr_and_exit "apt-get update failed."
apt_pkg_list="net-tools curl redis-server iptables ca-certificates nmap dsniff bluetooth bluez libbluetooth-dev libudev-dev nodejs openvpn easy-rsa python-pip cron"
echo "Installing dependencies, please be patient..."
sudo apt-get install $apt_pkg_list -y || perr_and_exit "Failed to install needed packages."
if [[ -f /.dockerenv ]]; then
echo "Setting special npm configuration for Docker enviornment..."
# a workaround for npm/nodejs bug
# https://github.com/npm/npm/issues/4719
sudo npm config set unsafe-perm true
fi
echo "Installing tools: forever and xml-json..."
sudo npm install forever -g
sudo npm install xml-json -g
echo "Setting up required folders and configures..."
sudo mkdir -p /blog || perr_and_exit "Failed to create folder /blog"
sudo mkdir -p /bspool || perr_and_exit "Failed to create folder /bspool"
echo "Setting up openvpn..."
sudo rm -r -f /etc/openvpn || perr_and_exit "Failed to delete existing openvpn configure folder."
sudo mkdir -p /etc/openvpn || perr_and_exit "Failed to create new openvpn configure folder."
sudo cp -r /usr/share/easy-rsa /etc/openvpn || perr_and_exit "Failed to copy easy_rsa."
echo "Installing shadowsocks..."
sudo pip install shadowsocks || perr_and_exit "Failed to install shadowsocks."
}
function install_walla()
{
echo "Installing node modules..."
FW_NODE_MODULES_PATH=~/.node_modules
branch=$(git rev-parse --abbrev-ref HEAD)
CPU_PLATFORM=$(uname -m)
if [[ $CPU_PLATFORM == "x86_64" ]]; then
NODE_MODULE_REPO=https://github.com/firewalla/firewalla_nodemodules.x86_64.git
git clone $NODE_MODULE_REPO $FW_NODE_MODULES_PATH
elif [[ $CPU_PLATFORM == "armv7l" ]]; then
NODE_MODULE_REPO=https://github.com/firewalla/firewalla_nodemodules.git
git clone --recursive -b $branch --single-branch $NODE_MODULE_REPO $FW_NODE_MODULES_PATH
cd $FW_NODE_MODULES_PATH
git reset -q --hard `cat $FIREWALLA_HOME/scripts/NODE_MODULES_REVISION.$CPU_PLATFORM`
cd -
fi
#no need to install npm any more, they are already provided by .node_modules
# cd $basedir && npm install || echo "[WARNING] There are errors during npm install."
# cd $basedir/api && npm install || echo "[WARNING] There are errors during npm install."
# Skip bro installation if in docker environment, bro in apt repo will be used in Docker
if [[ ! -f /.dockerenv ]]; then
echo "Installing bro..."
BRO_PKG="bro.tar.gz"
if [[ $CPU_PLATFORM == "x86_64" ]]; then
BRO_PKG="/home/$USER/.node_modules/bro.$CPU_PLATFORM.tar.gz"
fi
cd $basedir/imports && tar -zxf $BRO_PKG && sudo cp -r -f $basedir/imports/bro /usr/local/ && rm -r -f $basedir/imports/bro || perr_and_exit "Failed to install bro."
cp $basedir/bin/real/bit* $basedir/bin/
fi
sudo cp $basedir/etc/sysctl.conf /etc/sysctl.conf || perr_and_exit "Failed to replace system sysctl.conf."
sudo cp $basedir/etc/bro-cron /etc/cron.hourly/. || perr_and_exit "Failed to install root bron cronjobs."
crontab $basedir/etc/crontab || perr_and_exit "Failed to install user bro cronjobs."
echo "Setting up encipher..."
sudo mkdir -p /encipher.config || perr_and_exit "Failed to create /encipher.config/"
sudo cp $basedir/config/netbot.config /encipher.config/ || perr_and_exit "Failed top copy encipher config."
sudo mkdir -p /firewalla && sudo chmod 777 /firewalla || perr_and_exit "Failed to create /firewalla."
echo "Setting up brofish and firewalla services..."
sudo cp $basedir/etc/brofish.service /etc/systemd/system/. || perr_and_exit "Failed to copy brofish.servie."
sudo cp $basedir/etc/firewalla.service /etc/systemd/system/. || perr_and_exit "Failed to copy firewalla service."
# ignore systemctl part in docker enviornment, there is some bug that systemctl doesn't work in Docker
if [[ "$BUILD_ONLY" != 1 && ! -f /.dockerenv ]]; then
sudo systemctl daemon-reload || perr_and_exit "Failed to refresh systemd services."
sudo systemctl enable brofish || perr_and_exit "Failed to enable brofish service."
sudo systemctl enable firewalla || perr_and_exit "Failed to enable firewalla service."
fi
sudo setcap cap_net_raw+eip $(eval readlink -f `which nodejs`) || perr_and_exit "Failed setup capabilities for nodejs."
# Set firewalla global environment variable
if ! grep "^FIREWALLA_HOME=" /etc/environment &>/dev/null; then
echo "Adding ENV FIREWALLA_HOME..."
FIREWALLA_HOME=$FIREWALLA_HOME sudo -E bash -c '/bin/echo "FIREWALLA_HOME=$FIREWALLA_HOME" >> /etc/environment' || perr_and_exit "Failed to setup FIREWALLA_HOME env variable."
fi
}
function post_installation() {
source $FIREWALLA_HOME/scripts/utils.sh
setup_folders
}
basedir=`dirname $0`
basedir=`cd $basedir;pwd`
FIREWALLA_HOME=$basedir
while getopts "tnh" opt; do
case $opt in
n) NO_DEPS=1;;
h) usage; exit 0;;
?) perr_and_exit "Invalid option.";;
esac
done
[[ "$NO_DEPS" != 1 ]] && install_dependencies || echo "Skipping dependency installation."
install_walla
post_installation
echo "Installation successful."
exit 0