Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Trigger tedge multicall with symlinks as well as sub-commands #3313

Merged

Conversation

didier-wenzek
Copy link
Contributor

@didier-wenzek didier-wenzek commented Jan 3, 2025

Proposed changes

Simplify the way tedge-mapper, tedge-agent and tedge-write are launched: it's no more mandatory to create symlinks to tedge with appropriate names.

The following are now equivalent (for the mapper, but also the agent and all tedge services and plugins ):

  • tedge run tedge-mapper c8y
  • tedge-mapper c8y where tedge-mapper is a copy or a symlink of tedge

The former tedge cli can be invoked as before:

$ tedge help
tedge is the cli tool for thin-edge.io

Usage: tedge [OPTIONS] <COMMAND>

Commands:
  init             Initialize Thin Edge
  cert             Create and manage device certificate
  ...
  run              Run thin-edge services and plugins
  ...

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

#2696

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

This PR supersedes #3311

Copy link

codecov bot commented Jan 3, 2025

Codecov Report

Attention: Patch coverage is 72.91667% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/core/tedge/src/main.rs 76.08% 8 Missing and 3 partials ⚠️
crates/core/tedge/src/cli/mod.rs 0.00% 1 Missing ⚠️
plugins/tedge_apt_plugin/src/lib.rs 0.00% 1 Missing ⚠️
Additional details and impacted files

📢 Thoughts on this report? Let us know!

Copy link
Contributor

github-actions bot commented Jan 3, 2025

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
551 0 2 551 100 1h27m53.879757s

fn redirect_if_multicall(cmd: TEdgeOpt, common: CommonArgs) -> TEdgeOptMulticall {
match cmd {
TEdgeOpt::Mapper(opt) => {
TEdgeOptMulticall::Component(Component::TedgeMapper(opt.with_common_args(common)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to be more cautious here: there is a risk of confusion as --tedge-config is provided twice to clap.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you mean --config-dir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a given global argument (as --config-dir) is used twice by the command struct (as here where common can be accessed directly (common) or via the mapper options (opt.common), clap correctly duplicate the user provided info to all the inner components.

  • Hence, there is no need to explicitly copy to common arg.
  • As this clap behavior is not documented, tests have been added, see b735319
  • There is one caveat: clap fails to notice that an argument is provided twice.

crates/core/tedge/src/cli/mod.rs Show resolved Hide resolved
@reubenmiller
Copy link
Contributor

@didier-wenzek Overall the changes look very nice, though I'm just wondering whether the tedge subcommands are being polluted too much by some of the more internal(ish) commands like tedge write...If we have to add a tedge read in the future, then will this just cause unnecessary noise for the user when they look at the --help command?

Do we need to put some of the commands under a some subcommand like utils (though the name is not perfect):

tedge utils write

@reubenmiller
Copy link
Contributor

reubenmiller commented Jan 7, 2025

@didier-wenzek I think it would be useful if we make all of the binaries which can be access via symlinks also available via subcommands, but since there are a few tricker cases (e.g. c8y-firmware-plugin, c8y-remote-access-plugin), so here is the suggested structure (and the associated symlink equivalent in brackets).

  • tedge
    • agent (tedge-agent)
    • mapper (tedge-mapper)
    • watchdog (tedge-watchdog)
    • sm-plugins
      • apt (tedge-apt-plugin)
    • plugins
      • c8y-remote-access-plugin (c8y-remote-access-plugin)
      • c8y-firmware-plugin (c8y-firmware-plugin)
    • utils
      • write (tedge-write)

Alternatively, if we could just do a flat structure and just place all of the binary names under one subcommand:

  • tedge
    • run
      • tedge-agent
      • tedge-mapper
      • tedge-watchdog
      • tedge-apt-plugin
      • c8y-remote-access-plugin
      • c8y-firmware-plugin
      • tedge-write

@didier-wenzek
Copy link
Contributor Author

@didier-wenzek I think it would be useful if we make all of the binaries which can be access via symlinks also available via subcommands, but since there are a few tricker cases (e.g. c8y-firmware-plugin, c8y-remote-access-plugin), so here is the suggested structure (and the associated symlink equivalent in brackets).

* tedge
  
  * agent (tedge-agent)
  * mapper (tedge-mapper)
  * watchdog (tedge-watchdog)
  * sm-plugins
    
    * apt (tedge-apt-plugin)
  * plugins
    
    * c8y-remote-access-plugin (c8y-remote-access-plugin)
    * c8y-firmware-plugin (c8y-firmware-plugin)
  * utils
    
    * write (tedge-write)

Alternatively, if we could just do a flat structure and just place all of the binary names under one subcommand:

* tedge
  
  * run
    
    * tedge-agent
    * tedge-mapper
    * tedge-watchdog
    * tedge-apt-plugin
    * c8y-remote-access-plugin
    * c8y-firmware-plugin
    * tedge-write

The second approach with tedge run xxx is simpler. I will go with that one.

@didier-wenzek
Copy link
Contributor Author

The second approach with tedge run xxx is simpler. I will go with that one.

Done with 2bada64
the docs being updated by 2c73956

Copy link
Contributor

@reubenmiller reubenmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two minor points:

  1. tedge-apt-plugin is missing under tedge run
  2. Can we sort the subcommands under tedge run? I don't mind having help at the end, however the current order seems to be manually defined.

@didier-wenzek
Copy link
Contributor Author

1. `tedge-apt-plugin` is missing under `tedge run`

For some reasons I have to understand better, tedge-apt-plugin is a special case: https://github.com/thin-edge/thin-edge.io/blob/main/crates/core/tedge/src/main.rs#L29

2. Can we sort the subcommands under `tedge run`? I don't mind having `help` at the end, however the current order seems to be manually defined.

Indeed, these sub-commands are listed here: https://github.com/thin-edge/thin-edge.io/blob/main/crates/core/tedge/src/cli/mod.rs#L52

Any order in mind?

@reubenmiller
Copy link
Contributor

1. `tedge-apt-plugin` is missing under `tedge run`

For some reasons I have to understand better, tedge-apt-plugin is a special case: https://github.com/thin-edge/thin-edge.io/blob/main/crates/core/tedge/src/main.rs#L29

2. Can we sort the subcommands under `tedge run`? I don't mind having `help` at the end, however the current order seems to be manually defined.

Indeed, these sub-commands are listed here: https://github.com/thin-edge/thin-edge.io/blob/main/crates/core/tedge/src/cli/mod.rs#L52

Any order in mind?

alphabetical is fine

@didier-wenzek
Copy link
Contributor Author

didier-wenzek commented Jan 8, 2025

1. `tedge-apt-plugin` is missing under `tedge run`

For some reasons I have to understand better, tedge-apt-plugin is a special case: https://github.com/thin-edge/thin-edge.io/blob/main/crates/core/tedge/src/main.rs#L29

This is to exit with a status code of 1 and not 2 when the command line cannot be parsed:
https://github.com/thin-edge/thin-edge.io/blob/main/plugins/tedge_apt_plugin/src/lib.rs#L354

I will handle that case

Copy link
Contributor

@Bravo555 Bravo555 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement, I won't have to manually create dummy binary files for different components to do cargo run anymore!

That said, looking at tedge run --help there are some inconsistencies that could be fixed:

Run thin-edge services and plugins

Usage: tedge run [OPTIONS] <COMMAND>

Commands:
  tedge-mapper              tedge-mapper is the mapper that translates thin-edge.io data model to c8y/az data model.
  tedge-agent               tedge-agent interacts with a Cloud Mapper and one or more Software Plugins
  c8y-firmware-plugin       Thin-edge device firmware management for Cumulocity
  tedge-watchdog            tedge-watchdog checks the health of all the thin-edge.io components/services.
  c8y-remote-access-plugin  Thin-edge.io plugin for the Cumulocity IoT's Cloud Remote Access feature
  tedge-write               tee-like helper for writing to files which `tedge` user does not have write permissions to
  help                      Print this message or the help of the given subcommand(s)

snip...
  • tedge-mapper mentions c8y/az, but not aws
  • the style is inconsistent (capitalization, if the sentence is terminated by a .)

These descriptions are taken from package.description field of Cargo.toml of these crates, so they should either be fixed there, or we could provide the descriptions directly in code by adding doc comments to the Component enum variants; however I think updating Cargo.tomls would be preferable.

Doing this however would be best in another PR, so approving this one.

@didier-wenzek
Copy link
Contributor Author

Just two minor points:

1. `tedge-apt-plugin` is missing under `tedge run`

2. Can we sort the subcommands under `tedge run`? I don't mind having `help` at the end, however the current order seems to be manually defined.

Addressed by 1bdac43

Copy link
Contributor

@reubenmiller reubenmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

didier-wenzek and others added 5 commits January 8, 2025 16:59
The following are now equivalent:

- `tedge mapper c8y`
- `tedge-mapper c8y` where `tedge-mapper` is a copy or a symlink of `tedge`

The former `tedge` cli can be invoked as before:

```
$ tedge help
CLI arguments that should be handled by all thin-edge components

Usage: tedge [OPTIONS] <COMMAND>

Commands:
  init             Initialize Thin Edge
  cert             Create and manage device certificate
  ...
  mapper           Launch a cloud mapper
  agent            Run the agent
  write            Write standard input to a target file
  ...
```

Signed-off-by: Didier Wenzek <[email protected]>
Since the introduction of common arguments,
the header of `tedge help` was wrongly taken by clap
from the `CommonArgs` doc comments
(i.e. "CLI arguments that should be handled by all thin-edge components")

Signed-off-by: Didier Wenzek <[email protected]>
Signed-off-by: Didier Wenzek <[email protected]>
This allows running the tedge binary via `cargo run` without having to specify `--bin tedge`
tedge-apt-plugin specific treatment in tedge/main.rs
was only for exiting with 1 and not 2
when the command line cannot be parsed.

Signed-off-by: Didier Wenzek <[email protected]>
@didier-wenzek didier-wenzek force-pushed the feat/simplify-tedge-multicall branch from 1bdac43 to c80b3d0 Compare January 8, 2025 16:13
@didier-wenzek didier-wenzek added this pull request to the merge queue Jan 8, 2025
Merged via the queue into thin-edge:main with commit bd06fcb Jan 8, 2025
33 checks passed
@didier-wenzek didier-wenzek deleted the feat/simplify-tedge-multicall branch January 8, 2025 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme:cli Theme: cli related topics theme:installation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants