thin-edge / thin-edge.io

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

tedge connect should service management when the service manager is not available #2172

Closed reubenmiller closed 1 year ago

reubenmiller commented 1 year ago

Is your feature improvement request related to a problem? Please describe.

Connection the bridge using the tedge connect <cloud> command is a container throws an unexpected error when the service manager is not available.

Describe the solution you'd like

If the service manager is not available (as determined via the system.toml is_available key), then any service control should be a no-op. A warning can still be displayed to alert the user that the service was not managed, however the exit code should be set to 0 (instead of non-zero).

Describe alternatives you've considered

Additional context

The service manager abstraction provided by thin-edge.io is already in place to handle the situation when the service manager is not available, however it seems that this logic is not utilized in the tedge connect <cloud> command, though it is used in the tedge disconnect <cloud>.

This logic seems to be the culprit.

connect/command.rs#L473

if let Err(err) = service_manager_result {
    println!("'tedge connect' configured the necessary tedge components, but you will have to start the required services on your own.");
    println!("Start/restart mosquitto and other thin edge components.");
    println!("thin-edge.io works seamlessly with 'systemd'.\n");
    return Err(err.into());
}

Where as the is_available detection in the disconnect command, handles this not available explicitly (and not the overall error):

connect/command.rs#L444

if let Err(SystemServiceError::ServiceManagerUnavailable { cmd: _, name }) =
        &service_manager_result
    {
        println!(
            "Warning: '{}' service manager is not available on the system.\n",
            name
        );
    }
reubenmiller commented 1 year ago

How to test:

  1. Start an alpine docker container (interactively)

    docker run --rm -it alpine:3.18
  2. Install thin-edge.io, create and upload the device certificate

    wget -O - thin-edge.io/install.sh | sh -s
    tedge cert create --device-id $(hostname)
    tedge config set c8y.url mytenant.latest.stage.c8y.io
    tedge cert upload c8y --user myuser@example.com
  3. Connect the mapper to Cumulocity IoT

    tedge connect c8y
    echo $?

    The exit code (printed on the last line) should be zero

gligorisaev commented 1 year ago

QA has thoroughly checked the feature and here are the results: