From b142bd3f8e9dcdd786dc004fe6456afbf00576ad Mon Sep 17 00:00:00 2001 From: Victor Embacher Date: Thu, 21 Oct 2021 15:36:31 +0200 Subject: [PATCH 1/6] Fixed TypErrors causing services not to start and added more precise except blocks. --- webserver/openplc.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/webserver/openplc.py b/webserver/openplc.py index 7582a098..339e9a8b 100644 --- a/webserver/openplc.py +++ b/webserver/openplc.py @@ -167,10 +167,10 @@ def start_modbus(self, port_num): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 43628)) - s.send(b'start_modbusslave(' + str(port_num) + ')\n') + s.send(b'start_modbusslave(' + (b'%d' % port_num) + b')\n') data = s.recv(1000) s.close() - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") def stop_modbus(self): @@ -181,7 +181,7 @@ def stop_modbus(self): s.send(b'stop_modbusslave()\n') data = s.recv(1000) s.close() - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") def start_dnp3(self, port_num): @@ -189,10 +189,10 @@ def start_dnp3(self, port_num): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 43628)) - s.send(b'start_dnp3s(' + str(port_num) + ')\n') + s.send(b'start_dnp3s(' + (b'%d' % port_num) + ')\n') data = s.recv(1000) s.close() - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") def stop_dnp3(self): @@ -203,7 +203,7 @@ def stop_dnp3(self): s.send(b'stop_dnp3s()\n') data = s.recv(1000) s.close() - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") def start_enip(self, port_num): @@ -211,10 +211,10 @@ def start_enip(self, port_num): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 43628)) - s.send(b'start_enip(' + str(port_num) + ')\n') + s.send(b'start_enip(' + (b'%d' % port_num) + b')\n') data = s.recv(1000) s.close() - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") def stop_enip(self): @@ -225,7 +225,7 @@ def stop_enip(self): s.send(b'stop_enip()\n') data = s.recv(1000) s.close() - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") def start_pstorage(self, poll_rate): @@ -233,10 +233,10 @@ def start_pstorage(self, poll_rate): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 43628)) - s.send(b'start_pstorage(' + str(poll_rate) + ')\n') + s.send(b'start_pstorage(' + (b'%d' % poll_rate) + ')\n') data = s.recv(1000) s.close() - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") def stop_pstorage(self): @@ -247,7 +247,7 @@ def stop_pstorage(self): s.send(b'stop_pstorage()\n') data = s.recv(1000) s.close() - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") def logs(self): @@ -259,7 +259,7 @@ def logs(self): data = s.recv(1000000) s.close() return data - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") return "Error connecting to OpenPLC runtime" @@ -275,7 +275,7 @@ def exec_time(self): data = s.recv(10000) s.close() return display_time(int(data), 4) - except: + except ConnectionRefusedError: print("Error connecting to OpenPLC runtime") return "Error connecting to OpenPLC runtime" From bdec3e6336b105560fb85bfaf4e26e0a9f089f53 Mon Sep 17 00:00:00 2001 From: Victor Embacher Date: Thu, 21 Oct 2021 17:12:32 +0200 Subject: [PATCH 2/6] Converted to byte-literal to fix TypeError --- webserver/openplc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webserver/openplc.py b/webserver/openplc.py index 339e9a8b..e42fc0de 100644 --- a/webserver/openplc.py +++ b/webserver/openplc.py @@ -189,7 +189,7 @@ def start_dnp3(self, port_num): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 43628)) - s.send(b'start_dnp3s(' + (b'%d' % port_num) + ')\n') + s.send(b'start_dnp3s(' + (b'%d' % port_num) + b')\n') data = s.recv(1000) s.close() except ConnectionRefusedError: @@ -233,7 +233,7 @@ def start_pstorage(self, poll_rate): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 43628)) - s.send(b'start_pstorage(' + (b'%d' % poll_rate) + ')\n') + s.send(b'start_pstorage(' + (b'%d' % poll_rate) + b')\n') data = s.recv(1000) s.close() except ConnectionRefusedError: From 97a5acb4bba781d607dd7cc3e639db5f8decf8b6 Mon Sep 17 00:00:00 2001 From: Victor Embacher Date: Fri, 22 Oct 2021 13:29:28 +0200 Subject: [PATCH 3/6] Improved Dockerfile caching by removing calls to `install_linux_deps` and `install_py_deps`. Both are now only install via the Dockerfile. --- Dockerfile | 3 ++- background_installer.sh | 4 +--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3d0dffd8..460f7fbd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ automake libtool make git python3 python3-pip \ sqlite3 cmake git COPY . /workdir -RUN cd /workdir && ./install.sh docker WORKDIR /workdir +RUN pip3 install -r requirements.txt +RUN ./install.sh docker ENTRYPOINT ["./start_openplc.sh"] diff --git a/background_installer.sh b/background_installer.sh index 5a28ec0d..c5a2d6ab 100755 --- a/background_installer.sh +++ b/background_installer.sh @@ -128,9 +128,7 @@ elif [ "$1" == "linux" ]; then elif [ "$1" == "docker" ]; then echo "Installing OpenPLC on Linux inside Docker" - linux_install_deps - install_py_deps - + cmake_build_and_test if [ $? -ne 0 ]; then From 07e28213ef1295c5f41c607929e9359bcd0d5a57 Mon Sep 17 00:00:00 2001 From: Victor Embacher Date: Fri, 22 Oct 2021 13:34:46 +0200 Subject: [PATCH 4/6] Added more folders to .dockerignore --- .dockerignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.dockerignore b/.dockerignore index 9f7306b7..39c825cc 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,6 +3,10 @@ .exe/ **/.vs/ .vscode/ +.idea/ +cmake*/ +include/ +lib/ .deps/ bin/ From 15517519995955a5d399bcce7ddc3aab1d7dd5d2 Mon Sep 17 00:00:00 2001 From: Victor Embacher Date: Fri, 22 Oct 2021 13:44:44 +0200 Subject: [PATCH 5/6] fixed cmake folder being ignored by Dockerfile --- .dockerignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.dockerignore b/.dockerignore index 39c825cc..776d6a38 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,7 +4,7 @@ **/.vs/ .vscode/ .idea/ -cmake*/ +cmake-*/ include/ lib/ From a2ac3dcb43266e0df5946b476f9cc60d85467178 Mon Sep 17 00:00:00 2001 From: Victor Embacher Date: Fri, 22 Oct 2021 13:52:07 +0200 Subject: [PATCH 6/6] adhere to principal of least privilege for docker run command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d4c2afa3..2dd2c2e6 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ docker build -t openplc:v3 . _Devices can be passed to the `docker` daemon using the `-v` flag (e.g. `-v /dev/ttyACM0:/dev/ttyACM0`)_ ```bash -docker run -it --rm --privileged -p 8080:8080 openplc:v3 +docker run -it --rm --cap-add=SYS_NICE --cap-add=IPC_LOCK -p 8080:8080 openplc:v3 ``` ## Running Standalone