Fix update system and restart (#77)

* clean code

* fix update and restart button, service needs to run as root for the restart to work
This commit is contained in:
Masoko
2024-02-10 15:23:57 +02:00
committed by GitHub
parent 09c2fa4cb9
commit 9c4ba19cbe
4 changed files with 12 additions and 11 deletions

View File

@@ -165,7 +165,7 @@ set_service(){
print_green "+ Copy rpi-mqtt-monitor.service to /etc/systemd/system/"
sudo cp ${cwd}/rpi-mqtt-monitor.service /etc/systemd/system/
sudo sed -i "s|WorkingDirectory=.*|WorkingDirectory=${cwd}|" /etc/systemd/system/rpi-mqtt-monitor.service
sudo sed -i "s|User=YOUR_USER|User=${user}|" /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 systemctl daemon-reload
sudo systemctl enable rpi-mqtt-monitor.service

View File

@@ -9,8 +9,9 @@ WorkingDirectory=/home/username/git/rpi-mqtt-monitor/
StandardOutput=inherit
StandardError=inherit
Restart=always
RestartSec=1
RestartSec=5
User=YOUR_USER
[Install]
WantedBy=multi-user.target

View File

@@ -305,7 +305,7 @@ def config_json(what_config):
data["state_class"] = "measurement"
data["value_template"] = "{{ 'ON' if value_json.installed_ver != value_json.new_ver else 'OFF' }}"
elif what_config == "update":
version = update.check_git_version_remote(script_dir).strip()
version = update.check_git_version_remote(script_dir)
data["icon"] = "mdi:update"
data["name"] = "RPi MQTT Monitor"
data["title"] = "New Version"
@@ -321,6 +321,7 @@ def config_json(what_config):
data["name"] = "System Restart"
data["command_topic"] = "homeassistant/update/" + hostname + "/command"
data["payload_press"] = "restart"
data["device_class"] = "restart"
else:
return ""
# Return our built discovery config
@@ -445,8 +446,7 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s
if config.discovery_messages:
client.publish("homeassistant/button/" + config.mqtt_topic_prefix + "/" + hostname + "_restart/config",
config_json('restart_button'), qos=config.qos)
# client.publish(config.mqtt_topic_prefix + "/" + hostname + "/rpi5_fan_speed", rpi5_fan_speed, qos=config.qos, retain=config.retain)
# client.publish(config.mqtt_topic_prefix + "/" + hostname + "/git_update", git_update, qos=config.qos, retain=config.retain)
client.loop_stop()
# disconnect from mqtt server
client.disconnect()
@@ -600,9 +600,9 @@ exit_flag = False
# Create a stop event
stop_event = threading.Event()
script_dir = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
script_dir = os.path.dirname(os.path.realpath(__file__))
args = parse_arguments();
if args.service:

View File

@@ -49,15 +49,15 @@ def display_config_differences(current_config, example_config, display=True):
return False
def check_git_version_remote(script_dir):
full_cmd = "git -C {} ls-remote --tags origin | awk -F'/' '{{print $3}}' | sort -V | tail -n 1".format(script_dir)
full_cmd = "/usr/bin/git -C {} ls-remote --tags origin | awk -F'/' '{{print $3}}' | sort -V | tail -n 1".format(script_dir)
try:
result = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8")
except subprocess.CalledProcessError as e:
print("Error: {}".format(e))
return None
return "0"
latest_tag = result.strip()
return latest_tag if latest_tag else None
return latest_tag if latest_tag else "0"
def update_config_version(version, script_dir):