fix service by adding env variables to make update work properly (#80)

This commit is contained in:
Masoko
2024-02-11 15:51:56 +02:00
committed by GitHub
parent 35f15f9799
commit b36961fada
4 changed files with 20 additions and 9 deletions

View File

@@ -73,7 +73,11 @@ create_venv(){
# Activate the virtual environment # Activate the virtual environment
source rpi_mon_env/bin/activate source rpi_mon_env/bin/activate
if [[ $(python3 --version) ]]; then
python=$(which python3)
else
python=$(which python) python=$(which python)
fi
print_green "+ Activated virtual environment" print_green "+ Activated virtual environment"
} }
@@ -173,6 +177,8 @@ set_service(){
sudo sed -i "s|WorkingDirectory=.*|WorkingDirectory=${cwd}|" /etc/systemd/system/rpi-mqtt-monitor.service sudo sed -i "s|WorkingDirectory=.*|WorkingDirectory=${cwd}|" /etc/systemd/system/rpi-mqtt-monitor.service
sudo sed -i "s|User=YOUR_USER|User=root|" /etc/systemd/system/rpi-mqtt-monitor.service sudo sed -i "s|User=YOUR_USER|User=root|" /etc/systemd/system/rpi-mqtt-monitor.service
sudo sed -i "s|ExecStart=.*|ExecStart=${exec_start}|" /etc/systemd/system/rpi-mqtt-monitor.service sudo sed -i "s|ExecStart=.*|ExecStart=${exec_start}|" /etc/systemd/system/rpi-mqtt-monitor.service
home_dir=$(eval echo ~$user)
sudo sed -i "s|Environment=\"HOME=/home/username\"|Environment=\"HOME=${home_dir}\"|" /etc/systemd/system/rpi-mqtt-monitor.service
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl enable rpi-mqtt-monitor.service sudo systemctl enable rpi-mqtt-monitor.service
sudo systemctl start rpi-mqtt-monitor.service sudo systemctl start rpi-mqtt-monitor.service

View File

@@ -5,6 +5,7 @@ Wants=network-online.target
[Service] [Service]
ExecStart=/home/username/git/rpi-mqtt-monitor/rpi_mon_env/bin/python /home/username/git/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py --service ExecStart=/home/username/git/rpi-mqtt-monitor/rpi_mon_env/bin/python /home/username/git/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py --service
Environment="HOME=/home/username"
WorkingDirectory=/home/username/git/rpi-mqtt-monitor/ WorkingDirectory=/home/username/git/rpi-mqtt-monitor/
StandardOutput=inherit StandardOutput=inherit
StandardError=inherit StandardError=inherit

View File

@@ -16,7 +16,7 @@ import argparse
import threading import threading
import update import update
import config import config
import logging
# get device host name - used in mqtt topic # get device host name - used in mqtt topic
hostname = socket.gethostname() hostname = socket.gethostname()
@@ -148,6 +148,7 @@ def get_manufacturer():
def check_git_update(script_dir): def check_git_update(script_dir):
remote_version = update.check_git_version_remote(script_dir) remote_version = update.check_git_version_remote(script_dir)
logging.info("git_update value:" + remote_version)
if config.version == remote_version: if config.version == remote_version:
git_update = { git_update = {
"installed_ver": config.version, "installed_ver": config.version,
@@ -356,13 +357,14 @@ def publish_update_status_to_mqtt(git_update):
client = create_mqtt_client() client = create_mqtt_client()
if client is None: if client is None:
print("Error: Unable to connect to MQTT broker") print("Error: Unable to connect to MQTT broker")
return
client.loop_start() client.loop_start()
if config.git_update: if config.git_update:
if config.discovery_messages: if config.discovery_messages:
client.publish("homeassistant/binary_sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_git_update/config", client.publish("homeassistant/binary_sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_git_update/config",
config_json('git_update'), qos=config.qos) config_json('git_update'), qos=config.qos)
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/git_update", git_update, qos=config.qos, retain=config.retain) client.publish(config.mqtt_topic_prefix + "/" + hostname + "/git_update", git_update, qos=1, retain=config.retain)
if config.update: if config.update:
if config.discovery_messages: if config.discovery_messages:
@@ -610,7 +612,7 @@ exit_flag = False
stop_event = threading.Event() stop_event = threading.Event()
script_dir = os.path.dirname(os.path.realpath(__file__)) script_dir = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
args = parse_arguments(); args = parse_arguments();
if args.service: if args.service:
@@ -629,14 +631,14 @@ if __name__ == '__main__':
client.loop_start() # Start the MQTT client loop in a new thread client.loop_start() # Start the MQTT client loop in a new thread
# Start the gather_and_send_info function in a new thread # Start the gather_and_send_info function in a new thread
thread1 = threading.Thread(target=gather_and_send_info) thread1 = threading.Thread(target=gather_and_send_info)
thread1.daemon = True # Set the daemon attribute to True # thread1.daemon = True # Set the daemon attribute to True
thread1.start() thread1.start()
if config.update: if config.update:
# Start the update_status function in a new thread # Start the update_status function in a new thread
thread2 = threading.Thread(target=update_status) thread2 = threading.Thread(target=update_status)
thread2.daemon = True # Set the daemon attribute to True # thread2.daemon = True # Set the daemon attribute to True
thread2.start() thread2.start()
# Check the exit flag in the main thread # Check the exit flag in the main thread

View File

@@ -2,6 +2,7 @@ import ast
import os import os
import subprocess import subprocess
import config import config
import logging
def get_assignments(filename): def get_assignments(filename):
with open(filename) as f: with open(filename) as f:
@@ -54,9 +55,10 @@ def check_git_version_remote(script_dir):
result = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8") result = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8")
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print("Error: {}".format(e)) print("Error: {}".format(e))
return "0" return config.version
latest_tag = result.strip() latest_tag = result.strip()
logging.info("git update:" + result + script_dir)
return latest_tag if latest_tag else "0" return latest_tag if latest_tag else "0"