thin-edge / thin-edge.io

The open edge framework for lightweight IoT devices
https://thin-edge.io
Apache License 2.0
221 stars 54 forks source link

inconsistent exit codes between tedge connect and disconnect when service manager is not present #1953

Closed reubenmiller closed 1 year ago

reubenmiller commented 1 year ago

Describe the bug

The tedge connect c8y and tedge disconnect c8y are inconsistent with the handling when the default service manager is not configured.

For example, on an alpine docker container without systemd configured, and without the /etc/tedge/systemtoml file being present, the following command result in difference of behaviour:

Command Actual behaviour
tedge disconnect c8y Exits cleanly with an exit code 0, and a clear message that it is skipping the service manager actions
tedge connect c8y Exits with an exit code 1 (e.g. error), and error message indicating that the service manager actions were the root cause of failure

To Reproduce

On an alpine 3.14 docker container without systemd installed, run the following steps as a root user:

  1. Create a device certificate
  2. Configured the c8y.url and upload the device certificate to Cumulocity IoT
  3. Try to connect thin-edge.io to Cumulocity using tedge connect c8y observe the exit code and log messages.should be 0
  4. Try to connect thin-edge.io to Cumulocity using tedge disconnect c8y, the exit could, e.g. echo $?

Expected behavior

  1. The tedge connect c8y command should exit with a 0 exit code if it was successful creating the bridge configuration and if the service manager is not available.
  2. The tedge connect c8y command should use a similar warning as the tedge disconnect c8y command, see below.
    Service manager 'systemd' is not available, skipping starting/enabling of tedge components.

Use case: Single-process containers

When running thin-edge.io under a single process container setup, there isn't a service manager to coordinate each of the processes as each container will only contain one service. Therefore using the tedge connect c8y is only useful to create the mosquitto bridge configuration file, and it does not need to restart the mosquitto broker as it can be hosted in another container. In this case the tedge connect c8y is used to configure the device, and not used to actually start any of the processes/services.

Screenshots

Below shows the raw command output from each of the tedge connect and disconnect commands.

Command: Disconnect => Returns exit code 0

tedge disconnect c8y
The system config file '/etc/tedge/system.toml' doesn't exist. Use '/bin/systemctl' as a service manager.

Removing Cumulocity bridge.

Service manager 'systemd' is not available, skipping stopping/disabling of tedge components.

Command: Connect => Returns exit code 1

tedge connect c8y
The system config file '/etc/tedge/system.toml' doesn't exist. Use '/bin/systemctl' as a service manager.

Checking if systemd is available.

Warning: 'systemd' service manager is not available on the system.

Checking if configuration for requested bridge already exists.

Validating the bridge certificates.

Creating the device in Cumulocity cloud.

Saving configuration for requested bridge.

'tedge connect' configured the necessary tedge components, but you will have to start the required services on your own.
Start/restart mosquitto and other thin edge components.
thin-edge.io works seamlessly with 'systemd'.

Error: failed to connect Cumulocity cloud.

Caused by:
    Failed to execute '/bin/systemctl --version' to check the service manager availability.
    Service manager 'systemd' is not available on the system or elevated permissions have not been granted.

The command exits with an exit code 1.

Environment (please complete the following information):

Additional context

reubenmiller commented 1 year ago

Resolved by https://github.com/thin-edge/thin-edge.io/pull/2204

reubenmiller commented 1 year ago

Duplicated by https://github.com/thin-edge/thin-edge.io/issues/2172

reubenmiller commented 1 year ago

Example of the new handling of the tedge connect when the service manager is not available.

# tedge connect c8y
The system config file '/etc/tedge/system.toml' doesn't exist. Use '/bin/systemctl' as a service manager.

Checking if systemd is available.

Warning: 'systemd' service manager is not available on the system.

Checking if configuration for requested bridge already exists.

Validating the bridge certificates.

Creating the device in Cumulocity cloud.

Saving configuration for requested bridge.

'tedge connect' configured the necessary tedge components, but you will have to start the required services on your own.
Start/restart mosquitto and other thin edge components.
thin-edge.io works seamlessly with 'systemd'.

/ # echo $?
0