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

Allow arguments/subcommands in custom operations #2568

Closed scfx closed 8 months ago

scfx commented 9 months ago

Is your feature improvement request related to a problem? Please describe. If my tests were correct, there is currently no possibility to have any whitespaces in the custom operation commands. So, having custom operations like:

[exec]
  topic = "c8y/s/ds"
  on_message = "511"
  command = "python /etc/tedge/operations/command.py"
  timeout = 10

will not work, as the thin-edge will try to find end execute the file "python3 /etc/tedge/operations/command.py"

Describe the solution you'd like I would like to see an exec structure similar to what Docker uses. So:

[exec]
  topic = "c8y/s/ds"
  on_message = "511"
  command = "python /etc/tedge/operations/command.py"
  timeout = 10

If you try to do this in version (0.13.0), Thin-Edge won't provide any supported operations. I haven't tried it in 1.0.0-rc1, so apologies if that is already fixed.

reubenmiller commented 9 months ago

@scfx Technically this is not needed (though we'd still be open for support multiple arguments anyway). See below for how to make your script executable without having to call python3 before hand.

  1. Add a shebang to the first line of your script
    #!/usr/bin/env python3
    print("Hello world")
  2. Make the script executable

    chmod a+x ./command.py

Then you should be able to use the script like any other binary.

[exec]
  topic = "c8y/s/ds"
  on_message = "511"
  command = "/etc/tedge/operations/command.py"
  timeout = 10

Though just checking that you know there is already a plugin to handle the 511 SmartREST message (and it is available for installation via deb,rpm,apk, c8y-command-plugin.

reubenmiller commented 9 months ago

@didier-wenzek Would you be open to supporting this for 1.0.0?

didier-wenzek commented 9 months ago

@didier-wenzek Would you be open to supporting this for 1.0.0?

Yes, this can be done, using shell-words, to allow plain commands as in command = "python /etc/tedge/operations/command.py"

reubenmiller commented 9 months ago

thin-edge.io now supports commands with multiple arguments (in a string form):

command = "python /etc/tedge/operations/command.py"

System tests

gligorisaev commented 8 months ago

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

AjayItHub commented 8 months ago

Hi @reubenmiller , thank you for addressing this issue and we are looking for something like this. We are evaluating thin-edge to work with Cumulocity. If I am not wrong, seems like we can use command something like for c# "dotnet filename.dll". We are not able to make it work as probably we are using older version, and change seems to be addressed only a week before. Will try with latest version.

reubenmiller commented 8 months ago

@AjayItHub Following this Guide on how to create a Custom operation handler, I just adjusted the command to use the dummy dotnet console app that I built.

[exec]
command = "/opt/dotnet/dotnet /opt/myapp/bin/Debug/net7.0/myapp.dll"
topic = "c8y/s/dc/custom_devmgmt"
on_message = "dm101"

Maybe the part that part you're missing is that you are not using the full paths to the dotnet binary or the .dll?

The debugging section might be worth a read as well.

AjayItHub commented 8 months ago

Thank you @reubenmiller for quick response. Not sure what I am missing, but I tried exactly you mentioned in above comment. My thin-edge version is 1.0.0-rc.1

Works:

[exec] command = "/usr/bin/set-wifi.sh" topic = "c8y/s/dc/custom_devmgmt" on_message = "dm101"

Doesn't Work [exec] command = "/opt/dotnet/dotnet /opt/ConsoleApp5/ConsoleApp5.dll" topic = "c8y/s/dc/custom_devmgmt" on_message = "dm101"

I receive below error

Operation execution failed: No such file or directory (os error 2). Command: /opt/dotnet/dotnet /opt/ConsoleApp5/ConsoleApp5.dll. Operation name: set_wifi

reubenmiller commented 8 months ago

/opt/dotnet/dotnet /opt/ConsoleApp5/ConsoleApp5.dll

Are you sure that dotnet is actually installed at the /opt/dotnet/dotnet location? The error message suggestions that dotnet is not located there on your system.

You can verify by just running the same command from the console, e.g.

/opt/dotnet/dotnet /opt/ConsoleApp5/ConsoleApp5.dll

Thank you @reubenmiller for quick response. Not sure what I am missing, but I tried exactly you mentioned in above comment. My thin-edge version is 1.0.0-rc.1

Though I would also update to 1.0.0 as it was release a few days ago

AjayItHub commented 8 months ago

yes @reubenmiller , I confirm dotnet was installed at /opt/dotnet. So, I try updating to 1.0.0 and see (I was thinking 1.0.0.-rc.1 is actually 1.0.0) dotnet

reubenmiller commented 8 months ago

From this information it looks ok, but try the following steps to gather a bit more info to tell what is going on:

  1. Restart the tedge-agent service (to eliminate the off chance that an older version of the binary is actually running which does not support this feature)

    sudo systemctl restart tedge-agent
  2. Try running the command as the tedge user (maybe it does not have access to those files for some reason)

    sudo -u tedge /opt/dotnet/dotnet /opt/ConsoleApp5/ConsoleApp5.dll
  3. Trigger an operation by publishing on the local MQTT broker (as this is easier/quicker to verify rather than creating an operation on the cloud side)

    tedge mqtt pub 'c8y/s/dc/custom_devmgmt' "dm101,$(tedge config get device.id),example"

    There should of been a log file created under /var/log/tedge/agent, for example you can list the newest files under the directory using:

    ls -ltr /var/log/tedge/agent

    There should be a file like:

    set_wifi-2024-02-08T17:51:06.366509289Z.log

    If you still have an error, then maybe it would be worth while creating a new ticket (and include the log file), so we can track this cleaner.

And it would also help just to confirm the tedge version by showing a print out of the console (copy/pasting to a code block is fine, you don't have to screenshot it)

tedge --version
AjayItHub commented 8 months ago

hi @reubenmiller , thank you so much for your help, upgrading tedge helped resolving the issue.