-
Notifications
You must be signed in to change notification settings - Fork 7
/
NerlnetInstall.sh
executable file
·226 lines (191 loc) · 4.84 KB
/
NerlnetInstall.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
#!/bin/bash
# Global definitions
NERLNET_LIB_DIR="/usr/local/lib/nerlnet-lib"
NERLNET_DIR=$NERLNET_LIB_DIR/NErlNet
NERLNET_LOG_DIR="/usr/local/lib/nerlnet-lib/log"
REBAR3_FILE=src_erl/rebar3/rebar3
REBAR3_SYMLINK=/usr/local/bin/rebar3
if [[ -z "$RUNNING_IN_DOCKER" ]]; then
# Not running inside a docker container
LOGGED_IN_USER=$(logname)
else
# Running inside a docker container
LOGGED_IN_USER="$(whoami)" # Probably root
fi
# arguments parsing
# Implemented by https://github.com/matejak/argbash
# args defaults
InstallAll=false
NumJobs=4
help()
{
echo "-------------------------------------" && echo "Nerlnet Install" && echo "-------------------------------------"
echo "Runnig this script only with sudo priviledges!"
echo "Usage:"
echo "-i or --install installs all required utilities to run Nerlnet "
echo "-j or --jobs number of jobs to build of libraries (erlang and cmake)"
exit 2
}
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}
begins_with_short_option()
{
local first_option all_short_options='ihj'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - OPTIONALS
print_help()
{
printf 'Usage: %s [-i|--install] [-h|--help] [-j|--jobs <arg>]\n' "$0"
printf '\t%s\n' "-i, --install: install erlang and cmake from source"
printf '\t%s\n' "-j, --jobs: number of jobs (default: '4')"
}
parse_commandline()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
-i|--install)
InstallAll=true
;;
-i*)
InstallAll=true
;;
-h|--help)
help
exit 0
;;
-h*)
help
exit 0
;;
-j|--jobs)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
NumJobs="$2"
shift
;;
--jobs=*)
NumJobs="${_key##--jobs=}"
;;
-j*)
NumJobs="${_key##-j}"
;;
*)
_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
;;
esac
shift
done
}
parse_commandline "$@"
# Script startds here
function print()
{
echo "[NERLNET] $1"
}
function install_erlang()
{
print "Warning - Erlang otp is about to be installed from source"
print "Warning - please make sure that former erlang/OTP versions are purged"
sleep 5
print "Compile and install erlang from source"
apt update
apt install -y make gcc libncurses-dev libssl-dev
git clone https://github.com/erlang/otp.git
cd otp
git checkout maint-25
./configure
make -j$NumJobs
make install
cd -
rm -rf otp
}
function install_cmake()
{
print "Compile and install CMake version 3.26.3 from source"
apt update
apt install -y make gcc g++
wget https://github.com/Kitware/CMake/releases/download/v3.26.3/cmake-3.26.3.tar.gz
tar -zxvf cmake-3.26.3.tar.gz
cd cmake-3.26.3
./bootstrap
make -j$NumJobs
make install
cd -
rm -rf cmake-3.26.3
rm cmake-3.26.3.tar.gz
}
function build_rebar3()
{
print "Installing rebar3"
cd src_erl/rebar3
./bootstrap
cd -
chmod 755 $REBAR3_FILE
print "Create symbolic link: $REBAR3_SYMLINK--> `pwd`/$REBAR3_FILE"
if [ -L ${REBAR3_SYMLINK} ] ; then
rm $REBAR3_SYMLINK # remove symlink if exists
ln -s `pwd`/$REBAR3_FILE $REBAR3_SYMLINK
elif [ -e ${REBAR3_SYMLINK} ] ; then
# Not a link
rm $REBAR3_SYMLINK # remove symlink if exists
ln -s `pwd`/$REBAR3_FILE $REBAR3_SYMLINK
else
ln -s `pwd`/$REBAR3_FILE $REBAR3_SYMLINK
fi
}
ARCH_TYPE=`uname -m`
print "Execute this script within NErlNet directory with super user privileges!"
sleep 5
echo "Following commands will be executed with super user privileges:"
if [ "$ARCH_TYPE" = "x86_64" ]; then
print "Arch type: x86_64"
elif [ "$ARCH_TYPE" = "armv7l" ]; then
print "Arch type: armv7l."
NumJobs=1
fi
if [ "$InstallAll" = true ] ; then
install_erlang
install_cmake
fi
build_rebar3
print "Creating NErlNet-lib directory in $NERLNET_LIB_DIR"
mkdir -p $NERLNET_LIB_DIR
mkdir -p $NERLNET_LOG_DIR
print "Adding a sym-link to NErlNet directory"
if [ -L ${NERLNET_DIR} ] ; then
rm $NERLNET_DIR # remove symlink if exists
ln -s `pwd` $NERLNET_DIR
elif [ -e ${NERLNET_DIR} ] ; then
# Not a link
rm $NERLNET_DIR # remove symlink if exists
ln -s `pwd` $NERLNET_DIR
else
ln -s `pwd` $NERLNET_DIR
fi
print "$NERLNET_DIR symbolic link is linked to `pwd`"
echo -e "
[Unit]
After=network.service
[Service]
ExecStart=$NERLNET_DIR/NerlnetStartup.sh
User=$LOGGED_IN_USER
[Install]
WantedBy=default.target" > /etc/systemd/system/nerlnet.service
chmod 744 $NERLNET_DIR/NerlnetRun.sh
chmod 664 /etc/systemd/system/nerlnet.service
if [[ -z "$RUNNING_IN_DOCKER" ]]; then
# Not running in docker
chown -R $LOGGED_IN_USER $NERLNET_LOG_DIR
chown -R $LOGGED_IN_USER $NERLNET_DIR
fi
echo "enable and start nerlnet.service"
systemctl enable nerlnet.service
#systemctl start nerlnet.service