Files
OpenPLC_v3/background_installer.sh

316 lines
9.0 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"
2023-09-06 23:33:41 -03:00
WIRINGPI_VERSION="2.61-1"
2023-09-06 23:48:33 -03:00
VENV_DIR="$OPENPLC_DIR/.venv"
2022-12-01 23:02:27 +02:00
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
2023-09-06 21:56:05 -03:00
function fail {
echo "$*"
echo "OpenPLC was NOT installed!"
exit 1
}
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
2023-09-07 00:12:06 -03:00
$1 yum -q -y install curl make automake gcc gcc-c++ kernel-devel pkg-config bison flex autoconf libtool openssl-devel cmake python3
2022-09-28 16:06:05 -04:00
#Installing dependencies for Ubuntu/Mint/Debian
2023-09-06 22:21:20 -03:00
elif [ -x /usr/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 \
2023-09-06 23:48:33 -03:00
automake libtool make git \
sqlite3 cmake curl python3 python3-venv
#Installing dependencies for opensuse tumbleweed
elif [ -x /usr/bin/zypper ]; then
$1 zypper ref
$1 zypper in -y curl make automake gcc gcc-c++ kernel-devel pkg-config bison flex autoconf libtool openssl-devel cmake libmodbus-devel
2023-12-04 10:23:15 -05:00
$1 zypper in -y python python-xml python3 python3-pip
2023-09-06 20:55:07 -03:00
else
2023-09-06 21:56:05 -03:00
fail "Unsupported linux distro."
2022-09-28 16:06:05 -04:00
fi
2019-06-06 09:06:15 -05:00
}
2023-09-06 23:33:41 -03:00
function install_wiringpi {
echo "[WIRINGPI]"
echo "Trying distribution package..."
sudo apt-get install -y wiringpi && return 0
echo "Falling back to direct download..."
local FILE="wiringpi-$WIRINGPI_VERSION-arm64.deb"
local URL="https://github.com/WiringPi/WiringPi/releases/download/$WIRINGPI_VERSION/$FILE"
(
set -e
wget -c -O "$OPENPLC_DIR/$FILE" "$URL"
sudo dpkg -i "$OPENPLC_DIR/$FILE"
sudo apt install -f
rm -f "$OPENPLC_DIR/$FILE"
) || fail "Failed to install wiringpi."
2019-06-06 09:06:15 -05:00
}
function install_py_deps {
2023-09-06 23:48:33 -03:00
python3 -m venv "$VENV_DIR"
"$VENV_DIR/bin/python3" -m pip install flask==2.3.3 werkzeug==2.3.7 flask-login==0.6.2 pyserial 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 21:56:05 -03:00
cp ./iec2c "$OPENPLC_DIR/webserver/" || fail "Error compiling MatIEC"
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"
2023-09-06 21:56:05 -03:00
g++ st_optimizer.cpp -o "$OPENPLC_DIR/webserver/st_optimizer" || fail "Error compiling ST Optimizer"
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"
2023-09-06 21:56:05 -03:00
g++ -std=c++11 glue_generator.cpp -o "$OPENPLC_DIR/webserver/core/glue_generator" || fail "Error compiling Glue Generator"
cd "$OPENPLC_DIR"
}
2019-06-06 09:06:15 -05:00
function install_ethercat {
echo "[EtherCAT]"
cd "$OPENPLC_DIR/utils/ethercat_src"
./install.sh || fail "Error compiling EtherCAT"
echo ethercat > "$OPENPLC_DIR/webserver/scripts/ethercat"
cd "$OPENPLC_DIR"
}
2022-11-12 15:29:15 +02:00
function disable_ethercat {
echo "" > "$OPENPLC_DIR/webserver/scripts/ethercat"
}
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-11 10:23:42 -03:00
swap_on "$1"
2023-09-06 16:54:17 -03:00
cmake .
2019-06-06 09:06:15 -05:00
make
2023-09-06 21:56:05 -03:00
$1 make install || fail "Error installing OpenDNP3"
2019-06-06 09:06:15 -05:00
$1 ldconfig
2023-09-11 10:23:42 -03:00
swap_off "$1"
cd "$OPENPLC_DIR"
}
2019-06-06 09:06:15 -05:00
2023-09-06 21:58:42 -03:00
function disable_opendnp3 {
echo ""
echo "[OPEN DNP3]"
cd "$OPENPLC_DIR/webserver/core"
if test -f dnp3.cpp; then
mv dnp3.cpp dnp3.disabled || fail "Error disabling OpenDNP3"
fi
if test -f dnp3_dummy.disabled; then
mv dnp3_dummy.disabled dnp3_dummy.cpp || fail "Error disabling OpenDNP3"
fi
cd "$OPENPLC_DIR"
}
function install_libmodbus {
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
2023-09-06 21:56:05 -03:00
$1 make install || fail "Error installing Libmodbus"
2019-06-06 09:06:15 -05:00
$1 ldconfig
cd "$OPENPLC_DIR"
# Fix for RPM-based distros
if [ -x /bin/yum ]; then
sudo cp /usr/local/lib/pkgconfig/libmodbus.pc /usr/share/pkgconfig/
sudo cp /usr/local/lib/lib*.* /lib64/
fi
}
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"
disable_ethercat "$1"
2023-09-06 20:39:58 -03:00
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"
if [ "$1" == "win" ]; then
./change_hardware_layer.sh blank
else
./change_hardware_layer.sh blank_linux
fi
2023-09-06 20:43:57 -03:00
./compile_program.sh blank_program.st
2023-09-06 23:48:33 -03:00
cat > "$OPENPLC_DIR/start_openplc.sh" <<EOF
#!/bin/bash
mkdir -p /persistent/st_files
cp -n /workdir/webserver/dnp3_default.cfg /persistent/dnp3.cfg
cp -n /workdir/webserver/openplc_default.db /persistent/openplc.db
cp -n /workdir/webserver/st_files_default/* /persistent/st_files/
cp -n /dev/null /persistent/persistent.file
cp -n /dev/null /persistent/mbconfig.cfg
cd "$OPENPLC_DIR/webserver"
"$OPENPLC_DIR/.venv/bin/python3" webserver.py
EOF
2023-09-06 23:57:04 -03:00
chmod a+x "$OPENPLC_DIR/start_openplc.sh"
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
apt-cyg install gcc-core gcc-g++ git pkg-config automake autoconf libtool make sqlite3 python3
2021-04-19 14:34:52 -04:00
lynx -source https://bootstrap.pypa.io/pip/get-pip.py > get-pip3.py
2023-09-18 11:05:51 -04:00
#Setting up venv
python3 -m venv "$VENV_DIR"
"$VENV_DIR/bin/python3" get-pip3.py
"$VENV_DIR/bin/python3" -m pip install flask==2.3.3 werkzeug==2.3.7 flask-login==0.6.2 pyserial 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
2023-09-06 21:58:42 -03:00
disable_opendnp3
2023-09-06 20:39:58 -03:00
install_libmodbus
2023-09-18 10:15:20 -04:00
finalize_install win
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"
linux_install_deps sudo
install_py_deps
install_all_libs sudo
[ "$2" == "ethercat" ] && install_ethercat
install_systemd_service sudo
2023-09-18 10:15:20 -04:00
finalize_install linux
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-18 10:15:20 -04:00
finalize_install linux
2019-06-06 09:06:15 -05:00
elif [ "$1" == "rpi" ]; then
echo "Installing OpenPLC on Raspberry Pi"
linux_install_deps sudo
2023-09-06 23:33:41 -03:00
install_wiringpi
2019-06-06 09:06:15 -05:00
install_py_deps
install_all_libs sudo
install_systemd_service sudo
2023-09-18 10:15:20 -04:00
finalize_install linux
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
install_all_libs sudo
install_systemd_service sudo
2023-09-18 10:15:20 -04:00
finalize_install linux
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-18 10:15:20 -04:00
finalize_install linux
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