From 233a0651ccb046bc17f9db659d9656ed38c4ccda Mon Sep 17 00:00:00 2001 From: MarianRapahel <73583313+MarianRaphael@users.noreply.github.com> Date: Tue, 12 Sep 2023 21:44:12 +0200 Subject: [PATCH 1/5] Create raspbian-install-device-agent.sh --- service/raspbian-install-device-agent.sh | 109 +++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 service/raspbian-install-device-agent.sh diff --git a/service/raspbian-install-device-agent.sh b/service/raspbian-install-device-agent.sh new file mode 100644 index 0000000..1deeb8c --- /dev/null +++ b/service/raspbian-install-device-agent.sh @@ -0,0 +1,109 @@ +#!/bin/bash + +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root" + exit 1 +fi + +# Node.js version +MIN_NODEJS=16 + +# Update package list and upgrade installed packages +apt-get update + +# Helper functions to test for existence of npm +function HAS_NPM { + if [ -x "$(command -v npm)" ]; then return 0; else return 1; fi +} +# Install Node.js and npm if they do not exist +if [ -x "$(command -v node)" ]; then + echo "NodeJS found" + VERSION=$(node --version | cut -d "." -f1 | cut -d "v" -f2) + + if [[ $VERSION -ge $MIN_NODEJS ]]; then + echo "**************************************************************" + echo " NodeJS Version $MIN_NODEJS or newer found " + echo "**************************************************************" + else + echo "**************************************************************" + echo " You need NodeJS $MIN_NODEJS or newer, please upgrade " + echo "**************************************************************" + exit 1 + fi +else + echo "**************************************************************" + echo " No NodeJS found" + echo " Do you want to install NodeJS $MIN_NODEJS?" + echo "**************************************************************" + read -p "y/N: " yn + [ -z "$yn" ] && yn="n" + + if [[ "$yn" == "y" ]] || [[ "$yn" == "Y" ]]; then + if [ ! -x "$(command -v curl)" ]; then + sudo apt-get install -y curl + fi + sudo apt-get install -y ca-certificates curl gnupg + sudo mkdir -p /etc/apt/keyrings + curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg + echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$MIN_NODEJS.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list + sudo apt-get update + sudo apt-get install nodejs -y + else + echo "**************************************************************" + echo " You will need to manually install NodeJS first. " + echo " Exiting." + echo "**************************************************************" + exit 1 + fi +fi + +if ! HAS_NPM; then + # User confirmation for installation + read -p "Do you wish to proceed with the installation of npm? (y/n): " proceed + if [[ $proceed != "y" && $proceed != "Y" ]]; then + echo "Installation cancelled." + exit 1 + fi + echo "Installing npm..." + apt-get install -y npm +fi + +# Install Device Agent +npm install -g @flowforge/flowforge-device-agent + +# Create the working directory for the Device Agent +mkdir -p /opt/flowforge-device +chown -R $SUDO_USER /opt/flowforge-device + +# Create systemd service file for Device Agent +echo "[Unit] +Description=FlowForge Device Agent +Wants=network.target +Documentation=https://flowforge.com/docs + +[Service] +Type=simple +User=$SUDO_USER +WorkingDirectory=/opt/flowforge-device + +Environment="NODE_OPTIONS=--max_old_space_size=512" +ExecStart=/usr/bin/env flowforge-device-agent +# Use SIGINT to stop +KillSignal=SIGINT +# Auto restart on crash +Restart=on-failure +RestartSec=20 +# Tag things in the log +SyslogIdentifier=FlowForgeDevice +#StandardOutput=syslog + +[Install] +WantedBy=multi-user.target" | sudo tee /etc/systemd/system/flowforge-device-agent.service >/dev/null + +# Reload systemd, enable and start the service +sudo systemctl daemon-reload +sudo systemctl enable flowforge-device-agent.service +sudo systemctl start flowforge-device-agent.service + +# Output status of the service +sudo systemctl status flowforge-device-agent.service From c873346dac990316573e65352aab4a6c2725b330 Mon Sep 17 00:00:00 2001 From: Marian <73583313+MarianRaphael@users.noreply.github.com> Date: Wed, 4 Oct 2023 09:53:26 +0200 Subject: [PATCH 2/5] Apply suggestions from code review --- service/raspbian-install-device-agent.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/service/raspbian-install-device-agent.sh b/service/raspbian-install-device-agent.sh index 1deeb8c..20452b6 100644 --- a/service/raspbian-install-device-agent.sh +++ b/service/raspbian-install-device-agent.sh @@ -6,7 +6,7 @@ if [[ $EUID -ne 0 ]]; then fi # Node.js version -MIN_NODEJS=16 +MIN_NODEJS=14 # Update package list and upgrade installed packages apt-get update @@ -33,7 +33,7 @@ if [ -x "$(command -v node)" ]; then else echo "**************************************************************" echo " No NodeJS found" - echo " Do you want to install NodeJS $MIN_NODEJS?" + echo " Do you want to install NodeJS 18?" echo "**************************************************************" read -p "y/N: " yn [ -z "$yn" ] && yn="n" @@ -45,7 +45,7 @@ else sudo apt-get install -y ca-certificates curl gnupg sudo mkdir -p /etc/apt/keyrings curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg - echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$MIN_NODEJS.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list + echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list sudo apt-get update sudo apt-get install nodejs -y else From 0fb2eac669107100c6211ff69a0a3f0fb308a1fb Mon Sep 17 00:00:00 2001 From: MarianRapahel <73583313+MarianRaphael@users.noreply.github.com> Date: Wed, 4 Oct 2023 09:57:04 +0200 Subject: [PATCH 3/5] added sudo --- service/raspbian-install-device-agent.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/service/raspbian-install-device-agent.sh b/service/raspbian-install-device-agent.sh index 20452b6..cac1359 100644 --- a/service/raspbian-install-device-agent.sh +++ b/service/raspbian-install-device-agent.sh @@ -9,7 +9,7 @@ fi MIN_NODEJS=14 # Update package list and upgrade installed packages -apt-get update +sudo apt-get update # Helper functions to test for existence of npm function HAS_NPM { @@ -65,15 +65,15 @@ if ! HAS_NPM; then exit 1 fi echo "Installing npm..." - apt-get install -y npm + sudo apt-get install -y npm fi # Install Device Agent -npm install -g @flowforge/flowforge-device-agent +sudo npm install -g @flowforge/flowforge-device-agent # Create the working directory for the Device Agent -mkdir -p /opt/flowforge-device -chown -R $SUDO_USER /opt/flowforge-device +sudo mkdir -p /opt/flowforge-device +sudo chown -R $SUDO_USER /opt/flowforge-device # Create systemd service file for Device Agent echo "[Unit] From be9fad23a00c255cb67c902e66ab33485f113f6c Mon Sep 17 00:00:00 2001 From: MarianRapahel <73583313+MarianRaphael@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:32:09 +0200 Subject: [PATCH 4/5] Update raspbian-install-device-agent.sh --- service/raspbian-install-device-agent.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/service/raspbian-install-device-agent.sh b/service/raspbian-install-device-agent.sh index cac1359..e8a8d0a 100644 --- a/service/raspbian-install-device-agent.sh +++ b/service/raspbian-install-device-agent.sh @@ -1,9 +1,16 @@ #!/bin/bash -if [[ $EUID -ne 0 ]]; then - echo "This script must be run as root" - exit 1 -fi +echo "**************************************************************" +echo " FlowFuse Device Agent Installer " +echo " " +echo " Warning: " +echo " The install need root priviledges at times, it uses " +echo " sudo so may ask for your password. " +echo " root access is used to install NodeJS if needed, to set " +echo " directory pemissions and to install the FlowFuse Device " +echo " Agent and register the Device Agent as serivce " +echo " " +echo "**************************************************************" # Node.js version MIN_NODEJS=14 From c0c6d4498ebcb00d926d4b4250cd3b299605fbca Mon Sep 17 00:00:00 2001 From: MarianRapahel <73583313+MarianRaphael@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:35:32 +0200 Subject: [PATCH 5/5] spelling --- service/raspbian-install-device-agent.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/service/raspbian-install-device-agent.sh b/service/raspbian-install-device-agent.sh index e8a8d0a..4c41b49 100644 --- a/service/raspbian-install-device-agent.sh +++ b/service/raspbian-install-device-agent.sh @@ -4,11 +4,11 @@ echo "**************************************************************" echo " FlowFuse Device Agent Installer " echo " " echo " Warning: " -echo " The install need root priviledges at times, it uses " +echo " The install need root privileges at times, it uses " echo " sudo so may ask for your password. " -echo " root access is used to install NodeJS if needed, to set " -echo " directory pemissions and to install the FlowFuse Device " -echo " Agent and register the Device Agent as serivce " +echo " Root access is used to install Node.js if needed, to set " +echo " directory permissions, to install the FlowFuse Device " +echo " Agent and register the Device Agent as service " echo " " echo "**************************************************************"