Using Tar command in action handler

I a handle on the updating using sub systems and I can update a single file. What I want to do now is update a directory. What I tried was:

  1. Compressing and archiving a directory and gave a name called update.tar.gz
  2. changes my secondaries.json to download the update package
  3. in the action handler script I copy the update.tar.gz from the storage and then try to extract t to the folder I want.

Perform normal processing for this action.

    cp $SECONDARY_FIRMWARE_PATH /home/torizon/update.tar.gz
   	tar -xvzf /home/torizon/update.tar.gz -C /home/torizon/polardelight/updates
    echo '{"status": "ok", "message": "New file updated"}'
    exit 0 
    ;;

I get the following error
(action=install) output could not be parsed (expecting JSON string)

Can what I’m trying to do be done? I assumed that you can put any bash code in the install section.

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Based on the context provided, it seems like you are trying to use a tar command within an action handler script to update a directory by extracting a tar.gz file. The error you are encountering suggests that the output of your script is not being parsed correctly as a JSON string.

Here are a few things you might want to check or try:

  1. Ensure JSON Output: Make sure that the output of your script is a valid JSON string. The error message indicates that the output could not be parsed as JSON. Double-check the format of the JSON string you are echoing. It should be properly formatted, e.g., echo '{"status": "ok", "message": "New file updated"}'.

  2. Error Handling: Consider adding error handling to your script to catch any issues with the cp or tar commands. This can help ensure that any errors are properly reported and do not interfere with the JSON output. For example:

    if ! cp $SECONDARY_FIRMWARE_PATH /home/torizon/update.tar.gz; then
        echo '{"status": "error", "message": "Failed to copy update package"}'
        exit 1
    fi
    
    if ! tar -xvzf /home/torizon/update.tar.gz -C /home/torizon/polardelight/updates; then
        echo '{"status": "error", "message": "Failed to extract update package"}'
        exit 1
    fi
    
    echo '{"status": "ok", "message": "New file updated"}'
    exit 0
    
  3. Script Execution: Ensure that the script is being executed in an environment where all necessary variables (like $SECONDARY_FIRMWARE_PATH) are correctly set and accessible.

If these suggestions do not resolve the issue, you may need to provide more context or details about the environment and how the script is being executed.

Please, also check if these links can help you solve your problem:

Build custom Torizon OS with and without containers.

Greetings @jeffbelz,

The only way to get this error is if your action handler returned something other than an exit code of 0. This would mean something failed in your action handler. Could you share your full Aktualizr logs of the attempted update. If your action handler script gave some non-zero exit code then there should be a line like:

(action=install) returned an exit code of ...

Best Regards,
Jeremias

Here ya go.
I have confirmed the copy works. it fails on the tar command

log.txt (1.8 KB)
polardelight-update-handler.sh (1.1 KB)

Ahh okay wait I see what is happening here. The issue is not the exit code it’s the output your script is producing actually.

Notice on your tar command you have the flag for verbose (-v). This produces a lot of non-json output from your script. The only output that is expected from the action handler is json i.e ({"status": "ok", "message": "New file updated"}). Since you have the verbose flag though it produces a lot of unexpected output that the system doesn’t know how to handle/parse.

Either you remove the verbose flag from tar and then the update should work. Or you redirect the output elsewhere so it’s not in the stdout of your action-handler script. You can redirect the output to a log file somewhere if you want to keep the output for review later.

Keep in mind you must make sure no non-json output is being produced from your action handler. This includes errors. For example normally cp is a “silent” command if successful, but if it fails then it produces something like:

cp: cannot stat 'foo': No such file or directory

To not break things you must redirect potential error messages as well. Again either redirect to a log file somewhere or redirect to /dev/null.

Best Regards,
Jeremias

nice catch. I took the -v out and it worked

Glad I was able to help.

As I said before, while designing your action handler script be aware of any command that produces output on stdout or stderr. Otherwise you’ll end up in a similar situation.

Best Regards,
Jeremias