Files
OpenPLC_v3/background_installer.sh

348 lines
9.2 KiB
Bash
Raw Normal View History

2019-06-06 09:06:15 -05:00
#!/bin/bash
2023-09-06 16:54:17 -03:00
OPENPLC_DIR="$PWD"
2023-09-06 21:06:02 -03:00
SWAP_FILE="$OPENPLC_DIR/swapfile"
2022-12-01 23:02:27 +02:00
ETHERCAT_INSTALL=""
2023-09-06 21:44:24 -03:00
function print_help_and_exit {
2019-06-06 09:06:15 -05:00
echo ""
echo "Error: You must provide a platform name as argument"
echo ""
echo "Usage: ./install.sh [platform] where [platform] can be"
echo " win Install OpenPLC on Windows over Cygwin"
echo " linux Install OpenPLC on a Debian-based Linux distribution"
echo " docker Install OpenPLC in a Docker container"
echo " rpi Install OpenPLC on a Raspberry Pi"
echo " neuron Install OpenPLC on a UniPi Neuron PLC"
echo " custom Skip all specific package installation and tries to install"
echo " OpenPLC assuming your system already has all dependencies met."
echo " This option can be useful if you're trying to install OpenPLC"
echo " on an unsuported Linux platform or had manually installed"
echo " all the dependency packages before."
echo ""
exit 1
2023-09-06 21:44:24 -03:00
}
[ $# -eq 0 ] && print_help_and_exit
2019-06-06 09:06:15 -05:00
#set -x
# arg1: sudo or blank
function linux_install_deps {
2023-09-06 20:55:07 -03:00
if [ -x /bin/yum ]; then
2022-09-28 16:06:05 -04:00
yum clean expire-cache
yum check-update
$1 yum -q -y install curl make automake gcc gcc-c++ kernel-devel pkg-config bison flex autoconf libtool openssl-devel cmake python3 python3-pip
$1 yum -q -y install python2.7 python2-devel
#Installing dependencies for Ubuntu/Mint/Debian
2023-09-06 20:55:07 -03:00
elif [ -x /bin/apt-get ]; then
2022-09-28 16:06:05 -04:00
$1 apt-get update
$1 apt-get install -y build-essential pkg-config bison flex autoconf \
automake libtool make git python2.7 \
2023-09-06 21:42:26 -03:00
sqlite3 cmake git curl python3 python3-pip libmodbus-dev
2023-09-06 20:55:07 -03:00
else
echo "Unsupported linux distro."
exit 1
2022-09-28 16:06:05 -04:00
fi
2021-03-11 10:47:34 -05:00
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
$1 python2.7 get-pip.py
2019-06-06 09:06:15 -05:00
}
function install_py_deps {
2020-06-05 15:21:52 -04:00
$1 pip2 install flask
$1 pip2 install flask-login
$1 pip2 install pyserial
$1 pip2 install pymodbus
$1 pip3 install pymodbus==2.5.3
2019-06-06 09:06:15 -05:00
}
function swap_on {
2023-09-06 20:59:09 -03:00
echo "creating swapfile..."
2023-09-06 21:13:37 -03:00
# Fallocate is instantaneous. Only use dd as fallback.
$1 touch "$SWAP_FILE"
$1 fallocate -zl 1G "$SWAP_FILE" ||
2023-09-06 21:06:02 -03:00
$1 dd if=/dev/zero of="$SWAP_FILE" bs=1M count=1000
2023-09-06 21:13:37 -03:00
2023-09-06 21:06:02 -03:00
$1 mkswap "$SWAP_FILE"
$1 swapon "$SWAP_FILE"
}
2023-09-06 20:59:09 -03:00
function swap_off {
2023-09-06 20:59:09 -03:00
echo "removing swapfile..."
2023-09-06 21:06:02 -03:00
$1 swapoff "$SWAP_FILE"
$1 rm -f "$SWAP_FILE"
}
2023-09-06 20:59:09 -03:00
function install_matiec {
2019-06-06 09:06:15 -05:00
echo "[MATIEC COMPILER]"
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR/utils/matiec_src"
2019-06-06 09:06:15 -05:00
autoreconf -i
./configure
make
2023-09-06 16:54:17 -03:00
cp ./iec2c "$OPENPLC_DIR/webserver/"
2019-06-06 09:06:15 -05:00
if [ $? -ne 0 ]; then
echo "Error compiling MatIEC"
echo "OpenPLC was NOT installed!"
exit 1
fi
cd "$OPENPLC_DIR"
}
2019-06-06 09:06:15 -05:00
function install_st_optimizer {
2019-06-06 09:06:15 -05:00
echo "[ST OPTIMIZER]"
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR/utils/st_optimizer_src"
2019-06-06 09:06:15 -05:00
g++ st_optimizer.cpp -o st_optimizer
2023-09-06 16:54:17 -03:00
cp ./st_optimizer "$OPENPLC_DIR/webserver/"
2019-06-06 09:06:15 -05:00
if [ $? -ne 0 ]; then
echo "Error compiling ST Optimizer"
echo "OpenPLC was NOT installed!"
exit 1
fi
cd "$OPENPLC_DIR"
}
2019-06-06 09:06:15 -05:00
function install_glue_generator {
2019-06-06 09:06:15 -05:00
echo "[GLUE GENERATOR]"
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR/utils/glue_generator_src"
2019-06-06 09:06:15 -05:00
g++ -std=c++11 glue_generator.cpp -o glue_generator
2023-09-06 16:54:17 -03:00
cp ./glue_generator "$OPENPLC_DIR/webserver/core"
2019-06-06 09:06:15 -05:00
if [ $? -ne 0 ]; then
echo "Error compiling Glue Generator"
echo "OpenPLC was NOT installed!"
exit 1
fi
cd "$OPENPLC_DIR"
}
2019-06-06 09:06:15 -05:00
function install_ethercat {
2022-12-01 23:02:27 +02:00
if [ "$ETHERCAT_INSTALL" == "install" ]; then
echo ""
echo "[EtherCAT]"
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR/utils/ethercat_src"
2022-12-01 23:02:27 +02:00
./install.sh
if [ $? -ne 0 ]; then
echo "Error compiling EtherCAT"
echo "OpenPLC was NOT installed!"
exit 1
fi
2022-11-19 13:49:48 +02:00
fi
cd "$OPENPLC_DIR"
}
2022-11-12 15:29:15 +02:00
function install_opendnp3 {
2019-06-06 09:06:15 -05:00
echo "[OPEN DNP3]"
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR/utils/dnp3_src"
2023-09-06 20:59:09 -03:00
swap_on
2023-09-06 16:54:17 -03:00
cmake .
2019-06-06 09:06:15 -05:00
make
$1 make install
if [ $? -ne 0 ]; then
echo "Error installing OpenDNP3"
echo "OpenPLC was NOT installed!"
exit 1
fi
$1 ldconfig
2023-09-06 20:59:09 -03:00
swap_off
cd "$OPENPLC_DIR"
}
2019-06-06 09:06:15 -05:00
function install_libmodbus {
2023-09-06 21:42:26 -03:00
[ -r /usr/include/modbus/modbus.h ] && return 0
2019-06-06 09:06:15 -05:00
echo "[LIBMODBUS]"
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR/utils/libmodbus_src"
2019-06-06 09:06:15 -05:00
./autogen.sh
./configure
$1 make install
if [ $? -ne 0 ]; then
echo "Error installing Libmodbus"
echo "OpenPLC was NOT installed!"
exit 1
fi
$1 ldconfig
cd "$OPENPLC_DIR"
}
2019-06-06 09:06:15 -05:00
function install_systemd_service() {
2019-06-06 09:06:15 -05:00
if [ "$1" == "sudo" ]; then
echo "[OPENPLC SERVICE]"
$1 tee /lib/systemd/system/openplc.service > /dev/null <<EOF
[Unit]
Description=OpenPLC Service
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
Group=root
WorkingDirectory=$OPENPLC_DIR
ExecStart=$OPENPLC_DIR/start_openplc.sh
[Install]
WantedBy=multi-user.target
EOF
echo "Enabling OpenPLC Service..."
2019-06-06 09:06:15 -05:00
$1 systemctl daemon-reload
$1 systemctl enable openplc
fi
}
2023-09-06 20:39:58 -03:00
function install_all_libs {
install_matiec "$1"
2023-09-06 20:40:59 -03:00
install_st_optimizer "$1"
2023-09-06 20:39:58 -03:00
install_glue_generator "$1"
install_opendnp3 "$1"
install_libmodbus "$1"
2019-06-06 09:06:15 -05:00
}
function finalize_install {
2023-09-06 20:43:57 -03:00
echo "[FINALIZING]"
cd "$OPENPLC_DIR/webserver/scripts"
./change_hardware_layer.sh blank_linux
./compile_program.sh blank_program.st
cp "start_openplc.sh" "$OPENPLC_DIR"
cd "$OPENPLC_DIR"
}
2023-09-06 20:43:57 -03:00
2019-06-06 09:06:15 -05:00
if [ "$1" == "win" ]; then
echo "Installing OpenPLC on Windows"
cp ./utils/apt-cyg/apt-cyg ./
install apt-cyg /bin
apt-cyg update
2019-06-06 09:06:15 -05:00
apt-cyg install lynx
echo "Installing Python 2.7"
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR/utils/python2"
tar -xf python27-2.7.18-4.tar.xz
rsync -a ./etc/ /etc/
rsync -a ./usr/ /usr/
/etc/postinstall/python2.sh
ln -s /usr/bin/python2.7.exe /usr/bin/python2
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR"
2021-01-29 13:07:48 -05:00
# apt-cyg remove gcc-core gcc-g++ pkg-config automake autoconf libtool make python2 python2-pip sqlite3
apt-cyg install gcc-core gcc-g++ git pkg-config automake autoconf libtool make sqlite3 python3
2021-03-11 10:47:34 -05:00
lynx -source https://bootstrap.pypa.io/pip/2.7/get-pip.py > get-pip.py
2021-04-19 14:34:52 -04:00
lynx -source https://bootstrap.pypa.io/pip/get-pip.py > get-pip3.py
/usr/bin/python2 get-pip.py
/usr/bin/python3 get-pip3.py
/usr/bin/python2 -m pip install flask
/usr/bin/python2 -m pip install flask-login
/usr/bin/python2 -m pip install pyserial
/usr/bin/python2 -m pip install pymodbus
/usr/bin/python3 -m pip install pymodbus==2.5.3
2019-06-06 09:06:15 -05:00
echo ""
echo "[MATIEC COMPILER]"
cp ./utils/matiec_src/bin_win32/*.* ./webserver/
if [ $? -ne 0 ]; then
echo "Error compiling MatIEC"
echo "OpenPLC was NOT installed!"
exit 1
fi
2023-09-06 20:40:59 -03:00
install_st_optimizer
2023-09-06 20:39:58 -03:00
install_glue_generator
2019-06-06 09:06:15 -05:00
echo ""
echo "[OPEN DNP3]"
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR/webserver/core"
2019-06-06 09:06:15 -05:00
if test -f dnp3.cpp; then
mv dnp3.cpp dnp3.disabled
if [ $? -ne 0 ]; then
echo "Error disabling OpenDNP3"
echo "OpenPLC was NOT installed!"
exit 1
fi
fi
if test -f dnp3_dummy.disabled; then
mv dnp3_dummy.disabled dnp3_dummy.cpp
if [ $? -ne 0 ]; then
echo "Error disabling OpenDNP3"
echo "OpenPLC was NOT installed!"
exit 1
fi
fi
2023-09-06 16:54:17 -03:00
cd "$OPENPLC_DIR"
2019-06-06 09:06:15 -05:00
2023-09-06 20:39:58 -03:00
install_libmodbus
2023-09-06 20:43:57 -03:00
finalize_install
2019-06-06 09:06:15 -05:00
elif [ "$1" == "linux" ]; then
2022-12-01 23:02:27 +02:00
2019-06-06 09:06:15 -05:00
echo "Installing OpenPLC on Linux"
2022-12-01 23:02:27 +02:00
if [ "$2" == "ethercat" ]; then
echo "including EtherCAT"
ETHERCAT_INSTALL="install"
echo ethercat > webserver/scripts/ethercat
else
echo "" > webserver/scripts/ethercat
fi
2019-06-06 09:06:15 -05:00
linux_install_deps sudo
install_py_deps
2019-08-20 10:05:55 -03:00
install_py_deps "sudo -H"
2019-06-06 09:06:15 -05:00
install_all_libs sudo
install_systemd_service sudo
2023-09-06 20:55:07 -03:00
# Fix for RPM-based distros
if [ -x /bin/yum ]; then
2022-09-28 16:30:08 -04:00
sudo cp /usr/local/lib/pkgconfig/libmodbus.pc /usr/share/pkgconfig/
sudo cp /usr/local/lib/lib*.* /lib64/
fi
2023-09-06 20:43:57 -03:00
finalize_install
2019-06-06 09:06:15 -05:00
elif [ "$1" == "docker" ]; then
echo "Installing OpenPLC on Linux inside Docker"
linux_install_deps
install_py_deps
install_all_libs
2023-09-06 20:43:57 -03:00
finalize_install
2019-06-06 09:06:15 -05:00
elif [ "$1" == "rpi" ]; then
echo "Installing OpenPLC on Raspberry Pi"
linux_install_deps sudo
sudo apt-get install -y wiringpi
install_py_deps
2019-08-20 10:05:55 -03:00
install_py_deps "sudo -H"
2019-06-06 09:06:15 -05:00
install_all_libs sudo
install_systemd_service sudo
2023-09-06 20:43:57 -03:00
finalize_install
2019-06-06 09:06:15 -05:00
elif [ "$1" == "neuron" ]; then
echo "Installing OpenPLC on UniPi Neuron PLC"
echo ""
echo "[DISABLING UNIPI SERVICES]"
sudo systemctl stop neuronhost.service
sudo systemctl disable neuronhost.service
sudo systemctl stop neurontcp.service
sudo systemctl disable neurontcp.service
sudo systemctl stop evok.service
sudo systemctl disable evok.service
linux_install_deps sudo
install_py_deps
2019-08-20 10:05:55 -03:00
install_py_deps "sudo -H"
2019-06-06 09:06:15 -05:00
install_all_libs sudo
install_systemd_service sudo
2023-09-06 20:43:57 -03:00
finalize_install
2019-06-06 09:06:15 -05:00
elif [ "$1" == "custom" ]; then
echo "Installing OpenPLC on Custom Platform"
install_all_libs
install_systemd_service sudo
2023-09-06 20:43:57 -03:00
finalize_install
2019-06-06 09:06:15 -05:00
else
2023-09-06 21:44:24 -03:00
print_help_and_exit
2019-06-06 09:06:15 -05:00
fi