GladysAssistant / Gladys

A privacy-first, open-source home assistant
https://gladysassistant.com
Apache License 2.0
2.55k stars 278 forks source link

Connection to MQTT broker lost on restart #2073

Closed nataloko closed 3 months ago

nataloko commented 3 months ago

Describe the bug I have Gladys running in docker and an external MQTT broker (Mosquitto), everything seems to work OK when I enter the address and login details but configuration gets lost when I reboot (or restart Gladys).

To Reproduce

  1. Install Gladys
  2. Go to Integrations -> MQTT
  3. Set-up EXTERNAL broker (screenshot 1)
  4. Restart Gladys
  5. Go to Integrations -> MQTT
  6. See error (screenshot 2)

Expected behavior I expect the configured broker to stay saved.

Screenshots image image

Your Gladys installation (please complete the following information):

Desktop (please complete the following information):

nataloko commented 3 months ago

I've been playing around a bit more and I think I've found the source: This bad behaviour only happens when I remove Gladys' connection to Docker socket: /var/run/docker.sock:/var/run/docker.sock

When I add it back everything works fine again.

In my particular installation I'm handling containers externally so I don't understand why Gladys needs access to the Docker socket?

Pierre-Gilles commented 3 months ago

@nataloko Hey ! Thanks for your bug report.

There might be a bug indeed.

This is the file initializing the MQTT integration => https://github.com/GladysAssistant/Gladys/blob/master/server/services/mqtt/lib/init.js

I've tried on my side, and for now I can't reproduce the bug...

This is my configuration :

{
  mqttUrl: 'mqtt://localhost',
  mqttUsername: null,
  mqttPassword: null,
  useEmbeddedBroker: false,
  dockerBased: false,
  brokerContainerAvailable: false,
  networkModeValid: false,
  mosquittoVersion: null
}

When restarting, MQTT is connected with success

Did you do a "Gladys based installation" before connecting to an external broker?

nataloko commented 3 months ago

Hi Pierre,

I started to see this on a fresh installation. A difference on my setup is that I've using a user/password to connect to the mqtt broker.

Also I've just checked the locker logs I see this warning when Gladys starts without the docker.sock link (it doesn't show when I add the link back)

2024-05-18T09:21:13+0100 <warn> service.start.js:44 (Service.start) Unable to start service mqtt PlatformNotCompatible [Error]: SYSTEM_NOT_RUNNING_DOCKER

    at System.getNetworkMode (/src/server/lib/system/system.getNetworkMode.js:12:11)

    at MqttHandler.checkDockerNetwork (/src/server/services/mqtt/lib/checkDockerNetwork.js:8:54)

    at MqttHandler.getConfiguration (/src/server/services/mqtt/lib/getConfiguration.js:24:35)

    at MqttHandler.init (/src/server/services/mqtt/lib/init.js:13:25)

    at Object.start (/src/server/services/mqtt/index.js:17:5)

    at Service.start (/src/server/lib/service/service.start.js:33:7)
nataloko commented 3 months ago

I think that warning could be related; I've just tried with the following with a new Gladys installation:

  1. Create docker compose WITHOUT docker.sock volume
  2. Start container
  3. Do Gladys initial setup
  4. Connect Gladys to mqtt broker - SUCCESS
  5. Restart Gladys container
  6. Go to MQTT integration menu - see "MQTT service is not configured" message
  7. Stop Gladys container
  8. Change docker compose to add docker.sock volume
  9. Start Gladys container
  10. Go to MQTT integration menu - see "Connected to the MQTT broker with success !" message

So it seems that the configuration is actually saved but it's just the connection that is not automatically started when there is no docker.sock.

I've also noticed that after step 4 (initial configuration without docker.sock) if I nagivate away from the "setup" screen and then come back to that screen, the configuration details show blank (although message still says "success"). image

Pierre-Gilles commented 3 months ago

@nataloko Thanks for your investigation, it helps a lot.

I understand what's going on now, we check in this integration is "Docker based" on this line: https://github.com/GladysAssistant/Gladys/blob/master/server/services/mqtt/lib/getConfiguration.js#L23

If yes, we get the networkMode of the current container, and in your case it fails because the docker socket is not available.

I'm going to add some safety checks to avoid breaking the MQTT integration in case the Docker socket is not here

Pierre-Gilles commented 3 months ago

Fixed in PR : https://github.com/GladysAssistant/Gladys/pull/2084

Pierre-Gilles commented 3 months ago

@nataloko Fix available in Gladys Assistant 4.42

nataloko commented 2 months ago

Thank you, I can confirm it's working as expected now :)