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:
@@ -165,7 +165,7 @@ set_service(){
|
|||||||
print_green "+ Copy rpi-mqtt-monitor.service to /etc/systemd/system/"
|
print_green "+ Copy rpi-mqtt-monitor.service to /etc/systemd/system/"
|
||||||
sudo cp ${cwd}/rpi-mqtt-monitor.service /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|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 sed -i "s|ExecStart=.*|ExecStart=${exec_start}|" /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
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ WorkingDirectory=/home/username/git/rpi-mqtt-monitor/
|
|||||||
StandardOutput=inherit
|
StandardOutput=inherit
|
||||||
StandardError=inherit
|
StandardError=inherit
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=1
|
RestartSec=5
|
||||||
User=YOUR_USER
|
User=YOUR_USER
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|
||||||
@@ -305,7 +305,7 @@ def config_json(what_config):
|
|||||||
data["state_class"] = "measurement"
|
data["state_class"] = "measurement"
|
||||||
data["value_template"] = "{{ 'ON' if value_json.installed_ver != value_json.new_ver else 'OFF' }}"
|
data["value_template"] = "{{ 'ON' if value_json.installed_ver != value_json.new_ver else 'OFF' }}"
|
||||||
elif what_config == "update":
|
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["icon"] = "mdi:update"
|
||||||
data["name"] = "RPi MQTT Monitor"
|
data["name"] = "RPi MQTT Monitor"
|
||||||
data["title"] = "New Version"
|
data["title"] = "New Version"
|
||||||
@@ -321,6 +321,7 @@ def config_json(what_config):
|
|||||||
data["name"] = "System Restart"
|
data["name"] = "System Restart"
|
||||||
data["command_topic"] = "homeassistant/update/" + hostname + "/command"
|
data["command_topic"] = "homeassistant/update/" + hostname + "/command"
|
||||||
data["payload_press"] = "restart"
|
data["payload_press"] = "restart"
|
||||||
|
data["device_class"] = "restart"
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
# Return our built discovery config
|
# 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:
|
if config.discovery_messages:
|
||||||
client.publish("homeassistant/button/" + config.mqtt_topic_prefix + "/" + hostname + "_restart/config",
|
client.publish("homeassistant/button/" + config.mqtt_topic_prefix + "/" + hostname + "_restart/config",
|
||||||
config_json('restart_button'), qos=config.qos)
|
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()
|
client.loop_stop()
|
||||||
# disconnect from mqtt server
|
# disconnect from mqtt server
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
@@ -600,9 +600,9 @@ exit_flag = False
|
|||||||
|
|
||||||
# Create a stop event
|
# Create a stop event
|
||||||
stop_event = threading.Event()
|
stop_event = threading.Event()
|
||||||
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
||||||
args = parse_arguments();
|
args = parse_arguments();
|
||||||
|
|
||||||
if args.service:
|
if args.service:
|
||||||
|
|||||||
@@ -49,15 +49,15 @@ def display_config_differences(current_config, example_config, display=True):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def check_git_version_remote(script_dir):
|
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:
|
try:
|
||||||
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 None
|
return "0"
|
||||||
|
|
||||||
latest_tag = result.strip()
|
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):
|
def update_config_version(version, script_dir):
|
||||||
|
|||||||
Reference in New Issue
Block a user