diff mbox series

[isar-cip-core,v7] swupdate.bbclass: add scripts section to the swu file

Message ID 20240222141526.1431859-1-Quirin.Gylstorff@siemens.com (mailing list archive)
State Accepted
Headers show
Series [isar-cip-core,v7] swupdate.bbclass: add scripts section to the swu file | expand

Commit Message

Gylstorff Quirin Feb. 22, 2024, 2:14 p.m. UTC
From: Quirin Gylstorff <quirin.gylstorff@siemens.com>

This allows the user to add scripts[1] to the swu file by
setting the variable `SWU_SCRIPTS`. Scripts can be used to
prepare the system for an update.

```
SWU_SCRIPTS = "postinstall"
SWU_SCRIPT_postinstall[file] = "postinstall.sh"
SWU_SCRIPT_postinstall[type] = "postinstall"
```
An optional data element is possible with

```
SWU_SCRIPT_postinstall[data]= "some_data"
```

This will add  `file://<script_file_name>` to the variable `SRC_URI` and
`<script_file_name>` to `SWU_ADDTIONAL_FILES`. The sw-description will contain
the following section:
```
    scripts: (
        {
          filename = "<script_file_name>";
          type = "<script_type>";
          data = "<script_data>";
          sha256 = "<automatically added sha256 of script_file_name>";
        },):
```

[1]: https://sbabic.github.io/swupdate/sw-description.html?#scripts

Signed-off-by: Quirin Gylstorff <quirin.gylstorff@siemens.com>
---
Changes v7:
 - remove message in case of missing type
 - change documentation for flag type
Changes v6:
 - rebase onto next
 - if type is not set still generate the script section
 - print info if type is not set

Changes v5:
 - use variable flags

Changes v4:
 - rebase onto next
Changes v3:
 - fix message
 - add luascript example to README

Changes v2:
 - change seperator from `:` to `@` as it already used for the
   configfilecheck
 - add optional data entry
 - add warning if less then 2 parameters are given for entry




 classes/swupdate.bbclass                    | 43 ++++++++++
 doc/README.swupdate.md                      | 90 +++++++++++++++++++++
 recipes-core/images/swu/sw-description.tmpl |  1 +
 3 files changed, 134 insertions(+)

Comments

Jan Kiszka Feb. 22, 2024, 4:31 p.m. UTC | #1
On 22.02.24 15:14, Quirin Gylstorff wrote:
> From: Quirin Gylstorff <quirin.gylstorff@siemens.com>
> 
> This allows the user to add scripts[1] to the swu file by
> setting the variable `SWU_SCRIPTS`. Scripts can be used to
> prepare the system for an update.
> 
> ```
> SWU_SCRIPTS = "postinstall"
> SWU_SCRIPT_postinstall[file] = "postinstall.sh"
> SWU_SCRIPT_postinstall[type] = "postinstall"
> ```
> An optional data element is possible with
> 
> ```
> SWU_SCRIPT_postinstall[data]= "some_data"
> ```
> 
> This will add  `file://<script_file_name>` to the variable `SRC_URI` and
> `<script_file_name>` to `SWU_ADDTIONAL_FILES`. The sw-description will contain
> the following section:
> ```
>     scripts: (
>         {
>           filename = "<script_file_name>";
>           type = "<script_type>";
>           data = "<script_data>";
>           sha256 = "<automatically added sha256 of script_file_name>";
>         },):
> ```
> 
> [1]: https://sbabic.github.io/swupdate/sw-description.html?#scripts
> 
> Signed-off-by: Quirin Gylstorff <quirin.gylstorff@siemens.com>
> ---
> Changes v7:
>  - remove message in case of missing type
>  - change documentation for flag type
> Changes v6:
>  - rebase onto next
>  - if type is not set still generate the script section
>  - print info if type is not set
> 
> Changes v5:
>  - use variable flags
> 
> Changes v4:
>  - rebase onto next
> Changes v3:
>  - fix message
>  - add luascript example to README
> 
> Changes v2:
>  - change seperator from `:` to `@` as it already used for the
>    configfilecheck
>  - add optional data entry
>  - add warning if less then 2 parameters are given for entry
> 
> 
> 
> 
>  classes/swupdate.bbclass                    | 43 ++++++++++
>  doc/README.swupdate.md                      | 90 +++++++++++++++++++++
>  recipes-core/images/swu/sw-description.tmpl |  1 +
>  3 files changed, 134 insertions(+)
> 
> diff --git a/classes/swupdate.bbclass b/classes/swupdate.bbclass
> index 403bdef..2c69892 100644
> --- a/classes/swupdate.bbclass
> +++ b/classes/swupdate.bbclass
> @@ -57,6 +57,7 @@ IMAGE_TEMPLATE_VARS:swu = " \
>      SWU_NAME \
>      SWU_FILE_NODES \
>      SWU_BOOTLOADER_FILE_NODE \
> +    SWU_SCRIPTS_NODE \
>      "
>  
>  # TARGET_IMAGE_UUID needs to be generated before completing the template
> @@ -93,6 +94,48 @@ python add_swu_compression(){
>          d.setVar('SWU_COMPRESSION_NODE', '')
>  }
>  
> +
> +SWU_EXTEND_SW_DESCRIPTION += "add_scripts"
> +python add_scripts(){
> +    swu_scripts = d.getVar('SWU_SCRIPTS')
> +    if not swu_scripts:
> +        return
> +    swu_script_entries = swu_scripts.split()
> +    script_node_list = []
> +    for entry in swu_script_entries:
> +        script_entry = f"SWU_SCRIPT_{entry}"
> +
> +        script_file = d.getVarFlag(script_entry, "file")
> +        if not script_file:
> +            bb.warn(f"flag 'file' is empty for {script_entry} ")
> +            continue
> +
> +        script_type = d.getVarFlag(script_entry, "type") or None
> +        allowed_script_types = [None, "lua", "shellscript", "preinstall", "postinstall"]
> +        if script_type not in allowed_script_types:
> +            bb.warn(f"flag 'type' is not of value {allowed_script_types} ")
> +            continue
> +
> +        script_data = d.getVarFlag(script_entry, "data")
> +        node = f"""
> +        {{
> +          filename = "{script_file}";
> +        """
> +        if script_type:
> +            node += f"""  type = "{script_type}";"""
> +        if script_data:
> +            node += f"""  data = "{script_data}";"""
> +        node += f"""
> +          sha256 = "{script_file}-sha256";
> +        }}"""
> +        script_node_list.append(node)
> +        d.appendVar('SWU_ADDITIONAL_FILES', " " + script_file)
> +        d.appendVar('SRC_URI', f" file://{script_file}")
> +
> +    swu_scripts_node = "scripts: (" + ','.join([n for n in script_node_list]) + ");"
> +    d.appendVar('SWU_SCRIPTS_NODE', swu_scripts_node)
> +}
> +
>  # convert between swupdate compressor name and imagetype extension
>  def get_swu_compression_type(d):
>      swu_ct = d.getVar('SWU_COMPRESSION_TYPE')
> diff --git a/doc/README.swupdate.md b/doc/README.swupdate.md
> index 1c94699..cf1bcfb 100644
> --- a/doc/README.swupdate.md
> +++ b/doc/README.swupdate.md
> @@ -21,6 +21,96 @@ window is still possible.
>  If the variable `SWU_EBG_UPDATE` is set to `"1"` the update is also stored in
>  the `*.swu` file.
>  
> +## SWUpdate scripts
> +
> +It is possible to add [scripts](https://sbabic.github.io/swupdate/sw-description.html?#scripts) to a swu file.
> +
> +To add a script entry in isar-cip-core set the variable `SWU_SCRIPTS`.
> +The content of the variable has the following pattern:
> +`script_name`
> +
> +For each `script_name` the following flags need to be set:
> +
> +```
> +SWU_SCRIPT_script_name[file] = "<script_file_name>"
> +```
> +
> +The optional flag `type` can be used to set one of the following script types:
> + - [lua](https://sbabic.github.io/swupdate/sw-description.html#lua)
> + - [shellscript](https://sbabic.github.io/swupdate/sw-description.html#shellscript)
> + - [preinstall](https://sbabic.github.io/swupdate/sw-description.html#preinstall)
> + - [postinstall](https://sbabic.github.io/swupdate/sw-description.html#postinstall)
> +
> +If no type is given SWUpdate defaults to "lua".
> +```
> +SWU_SCRIPT_script_name[type] = "<script_type>"
> +```
> +
> +The optional flag `data` can be used as an script argument:
> +
> +```
> +SWU_SCRIPT_script_name[data] = "<script argument>"
> +```
> +
> +The file referenced by `<script_file_name>` is added to the variables `SRC_URI`
> +and `SWU_ADDITIONAL_FILES`. Therefore, it needs to be saved in a `FILESPATH`
> +location.
> +
> +### Example: postinstall.sh
> +
> +```
> +SWU_SCRIPTS = "postinstall"
> +SWU_SCRIPT_postinstall[file] = "postinstall.sh"
> +SWU_SCRIPT_postinstall[type] = "postinstall"
> +SWU_SCRIPT_postinstall[data] = "some_data"
> +```
> +
> +This will add  `file://postinstall.sh` to the variable `SRC_URI` and
> +`postinstall.sh` to `SWU_ADDTIONAL_FILES`. The sw-description will contain
> +the following section:
> +```
> +    scripts: (
> +        {
> +          filename = "postinstall.sh";
> +          type = "postinstall";
> +          data = "some_data"
> +          sha256 = "<sha256 of postinstall.sh>";
> +        }):
> +```
> +### Example: Luascript
> +The simplest lua script has the following content:
> +```lua
> +function preinst()
> +	local message = "preinst called\n"
> +	local success = true
> +	return success, message
> +end
> +function postinst()
> +	local message = "postinst called\n"
> +	local success = true
> +	return success, message
> +end
> +```
> +and is added:
> +
> +```
> +SWU_SCRIPTS = "luascript"
> +SWU_SCRIPT_luascript[file] = "luascript.lua"
> +SWU_SCRIPT_luascript[type] = "luascript"
> +SWU_SCRIPT_luascript[data] = "some_data"
> +```
> +
> +The sw-description will contain the following section:
> +```
> +    scripts: (
> +        {
> +          filename = "luascript.lua";
> +          type = "lua";
> +          data = "some_data"
> +          sha256 = "<sha256 of luascript.lua>";
> +        }):
> +```
> +
>  # Building and testing the CIP Core image
>  
>  Set up `kas-container` as described in the [top-level README](../README.md).
> diff --git a/recipes-core/images/swu/sw-description.tmpl b/recipes-core/images/swu/sw-description.tmpl
> index c52372c..88cb475 100644
> --- a/recipes-core/images/swu/sw-description.tmpl
> +++ b/recipes-core/images/swu/sw-description.tmpl
> @@ -35,4 +35,5 @@ software =
>              };
>              sha256 = "linux.efi-sha256";
>      }${SWU_FILE_NODES});
> +    ${SWU_SCRIPTS_NODE}
>  }

Thanks, applied.

Jan
diff mbox series

Patch

diff --git a/classes/swupdate.bbclass b/classes/swupdate.bbclass
index 403bdef..2c69892 100644
--- a/classes/swupdate.bbclass
+++ b/classes/swupdate.bbclass
@@ -57,6 +57,7 @@  IMAGE_TEMPLATE_VARS:swu = " \
     SWU_NAME \
     SWU_FILE_NODES \
     SWU_BOOTLOADER_FILE_NODE \
+    SWU_SCRIPTS_NODE \
     "
 
 # TARGET_IMAGE_UUID needs to be generated before completing the template
@@ -93,6 +94,48 @@  python add_swu_compression(){
         d.setVar('SWU_COMPRESSION_NODE', '')
 }
 
+
+SWU_EXTEND_SW_DESCRIPTION += "add_scripts"
+python add_scripts(){
+    swu_scripts = d.getVar('SWU_SCRIPTS')
+    if not swu_scripts:
+        return
+    swu_script_entries = swu_scripts.split()
+    script_node_list = []
+    for entry in swu_script_entries:
+        script_entry = f"SWU_SCRIPT_{entry}"
+
+        script_file = d.getVarFlag(script_entry, "file")
+        if not script_file:
+            bb.warn(f"flag 'file' is empty for {script_entry} ")
+            continue
+
+        script_type = d.getVarFlag(script_entry, "type") or None
+        allowed_script_types = [None, "lua", "shellscript", "preinstall", "postinstall"]
+        if script_type not in allowed_script_types:
+            bb.warn(f"flag 'type' is not of value {allowed_script_types} ")
+            continue
+
+        script_data = d.getVarFlag(script_entry, "data")
+        node = f"""
+        {{
+          filename = "{script_file}";
+        """
+        if script_type:
+            node += f"""  type = "{script_type}";"""
+        if script_data:
+            node += f"""  data = "{script_data}";"""
+        node += f"""
+          sha256 = "{script_file}-sha256";
+        }}"""
+        script_node_list.append(node)
+        d.appendVar('SWU_ADDITIONAL_FILES', " " + script_file)
+        d.appendVar('SRC_URI', f" file://{script_file}")
+
+    swu_scripts_node = "scripts: (" + ','.join([n for n in script_node_list]) + ");"
+    d.appendVar('SWU_SCRIPTS_NODE', swu_scripts_node)
+}
+
 # convert between swupdate compressor name and imagetype extension
 def get_swu_compression_type(d):
     swu_ct = d.getVar('SWU_COMPRESSION_TYPE')
diff --git a/doc/README.swupdate.md b/doc/README.swupdate.md
index 1c94699..cf1bcfb 100644
--- a/doc/README.swupdate.md
+++ b/doc/README.swupdate.md
@@ -21,6 +21,96 @@  window is still possible.
 If the variable `SWU_EBG_UPDATE` is set to `"1"` the update is also stored in
 the `*.swu` file.
 
+## SWUpdate scripts
+
+It is possible to add [scripts](https://sbabic.github.io/swupdate/sw-description.html?#scripts) to a swu file.
+
+To add a script entry in isar-cip-core set the variable `SWU_SCRIPTS`.
+The content of the variable has the following pattern:
+`script_name`
+
+For each `script_name` the following flags need to be set:
+
+```
+SWU_SCRIPT_script_name[file] = "<script_file_name>"
+```
+
+The optional flag `type` can be used to set one of the following script types:
+ - [lua](https://sbabic.github.io/swupdate/sw-description.html#lua)
+ - [shellscript](https://sbabic.github.io/swupdate/sw-description.html#shellscript)
+ - [preinstall](https://sbabic.github.io/swupdate/sw-description.html#preinstall)
+ - [postinstall](https://sbabic.github.io/swupdate/sw-description.html#postinstall)
+
+If no type is given SWUpdate defaults to "lua".
+```
+SWU_SCRIPT_script_name[type] = "<script_type>"
+```
+
+The optional flag `data` can be used as an script argument:
+
+```
+SWU_SCRIPT_script_name[data] = "<script argument>"
+```
+
+The file referenced by `<script_file_name>` is added to the variables `SRC_URI`
+and `SWU_ADDITIONAL_FILES`. Therefore, it needs to be saved in a `FILESPATH`
+location.
+
+### Example: postinstall.sh
+
+```
+SWU_SCRIPTS = "postinstall"
+SWU_SCRIPT_postinstall[file] = "postinstall.sh"
+SWU_SCRIPT_postinstall[type] = "postinstall"
+SWU_SCRIPT_postinstall[data] = "some_data"
+```
+
+This will add  `file://postinstall.sh` to the variable `SRC_URI` and
+`postinstall.sh` to `SWU_ADDTIONAL_FILES`. The sw-description will contain
+the following section:
+```
+    scripts: (
+        {
+          filename = "postinstall.sh";
+          type = "postinstall";
+          data = "some_data"
+          sha256 = "<sha256 of postinstall.sh>";
+        }):
+```
+### Example: Luascript
+The simplest lua script has the following content:
+```lua
+function preinst()
+	local message = "preinst called\n"
+	local success = true
+	return success, message
+end
+function postinst()
+	local message = "postinst called\n"
+	local success = true
+	return success, message
+end
+```
+and is added:
+
+```
+SWU_SCRIPTS = "luascript"
+SWU_SCRIPT_luascript[file] = "luascript.lua"
+SWU_SCRIPT_luascript[type] = "luascript"
+SWU_SCRIPT_luascript[data] = "some_data"
+```
+
+The sw-description will contain the following section:
+```
+    scripts: (
+        {
+          filename = "luascript.lua";
+          type = "lua";
+          data = "some_data"
+          sha256 = "<sha256 of luascript.lua>";
+        }):
+```
+
 # Building and testing the CIP Core image
 
 Set up `kas-container` as described in the [top-level README](../README.md).
diff --git a/recipes-core/images/swu/sw-description.tmpl b/recipes-core/images/swu/sw-description.tmpl
index c52372c..88cb475 100644
--- a/recipes-core/images/swu/sw-description.tmpl
+++ b/recipes-core/images/swu/sw-description.tmpl
@@ -35,4 +35,5 @@  software =
             };
             sha256 = "linux.efi-sha256";
     }${SWU_FILE_NODES});
+    ${SWU_SCRIPTS_NODE}
 }