diff mbox series

[1/2] feat: add route option for python swupdate client

Message ID 20231013141330.45969-1-jan.feemers@gmx.de
State Changes Requested
Delegated to: Stefano Babic
Headers show
Series [1/2] feat: add route option for python swupdate client | expand

Commit Message

Jan Feemers Oct. 13, 2023, 2:13 p.m. UTC
if the update server is behind a reverse proxy a route is needed.
The route should be optional and also work with websever without an
additional route.

Usage:
```shell
python ./main.py ./swu.swu 192.168.0.100 80 /update
```

Signed-off-by: Jan Feemers <jan.feemers@gmx.de>
---
 tools/python/swupdateclient/README.md         |  4 ++--
 .../swupdateclient/swupdateclient/main.py     | 19 ++++++++++++++-----
 2 files changed, 16 insertions(+), 7 deletions(-)

--
2.25.1

Comments

Stefano Babic Oct. 18, 2023, 8:22 p.m. UTC | #1
Hi Jan,

On 13.10.23 16:13, Jan Feemers wrote:
> if the update server is behind a reverse proxy a route is needed.

I understand the reason, but is it "route" a good name ? Route is mainly 
used to ass a network path through a gateway. In our case here, the URL 
is changed adding a path. Should we use something like "prefix" instead ?

Best regards,
Stefano Babic

> The route should be optional and also work with websever without an
> additional route.
> 
> Usage:
> ```shell
> python ./main.py ./swu.swu 192.168.0.100 80 /update
> ```
> 
> Signed-off-by: Jan Feemers <jan.feemers@gmx.de>
> ---
>   tools/python/swupdateclient/README.md         |  4 ++--
>   .../swupdateclient/swupdateclient/main.py     | 19 ++++++++++++++-----
>   2 files changed, 16 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/python/swupdateclient/README.md b/tools/python/swupdateclient/README.md
> index 50bd8cf..5d90cd6 100644
> --- a/tools/python/swupdateclient/README.md
> +++ b/tools/python/swupdateclient/README.md
> @@ -21,12 +21,12 @@ pipenv install
> 
>   ### pip
>   ```
> -swupdateclient <path-to-swu> <host_name> [port]
> +swupdateclient <path-to-swu> <host_name> [port] [route]
>   ```
> 
>   ### pipenv
>   ```
> -pipenv run swupdateclient <path-to-swu> <host_name> [port]
> +pipenv run swupdateclient <path-to-swu> <host_name> [port] [route]
>   ```
> 
> 
> diff --git a/tools/python/swupdateclient/swupdateclient/main.py b/tools/python/swupdateclient/swupdateclient/main.py
> index 26e5c0b..28334a4 100644
> --- a/tools/python/swupdateclient/swupdateclient/main.py
> +++ b/tools/python/swupdateclient/swupdateclient/main.py
> @@ -56,13 +56,14 @@ class ColorFormatter(logging.Formatter):
>   class SWUpdater:
>       """Python helper class for SWUpdate"""
> 
> -    url_upload = "http://{}:{}/upload"
> -    url_status = "ws://{}:{}/ws"
> +    url_upload = "http://{}:{}{}/upload"
> +    url_status = "ws://{}:{}{}/ws"
> 
> -    def __init__(self, path_image, host_name, port=8080, logger=None, log_level=logging.DEBUG):
> +    def __init__(self, path_image, host_name, port=8080, route="", logger=None, log_level=logging.DEBUG):
>           self._image = path_image
>           self._host_name = host_name
>           self._port = port
> +        self._route = route
>           if logger is not None:
>               self._logger = logger
>           else:
> @@ -76,7 +77,7 @@ class SWUpdater:
>           self._logger.info("Waiting for messages on websocket connection")
>           try:
>               async with websockets.connect(
> -                self.url_status.format(self._host_name, self._port)
> +                self.url_status.format(self._host_name, self._port, self._route)
>               ) as websocket:
>                   while True:
>                       try:
> @@ -112,7 +113,7 @@ class SWUpdater:
> 
>       def sync_upload(self, swu_file, timeout):
>           return requests.post(
> -            self.url_upload.format(self._host_name, self._port),
> +            self.url_upload.format(self._host_name, self._port, self._route),
>               files={"file": swu_file},
>               headers={"Cache-Control": "no-cache"},
>               timeout=timeout,
> @@ -172,6 +173,13 @@ def client (args: List[str]) -> None:
>       parser.add_argument("swu_file", help="Path to swu image")
>       parser.add_argument("host_name", help="Host name")
>       parser.add_argument("port", help="Port", type=int, default=8080, nargs="?")
> +    parser.add_argument(
> +        "route",
> +        help="Name of the API route (e.g. /ROUTE)",
> +        type=str,
> +        default="",
> +        nargs="?"
> +    )
>       parser.add_argument(
>           "--timeout",
>           help="Timeout for the whole swupdate process",
> @@ -201,6 +209,7 @@ def client (args: List[str]) -> None:
>           args.swu_file,
>           args.host_name,
>           args.port,
> +        args.route,
>           log_level=args.log_level.upper())
>       updater.update(timeout=args.timeout)
> 
> --
> 2.25.1
>
diff mbox series

Patch

diff --git a/tools/python/swupdateclient/README.md b/tools/python/swupdateclient/README.md
index 50bd8cf..5d90cd6 100644
--- a/tools/python/swupdateclient/README.md
+++ b/tools/python/swupdateclient/README.md
@@ -21,12 +21,12 @@  pipenv install

 ### pip
 ```
-swupdateclient <path-to-swu> <host_name> [port]
+swupdateclient <path-to-swu> <host_name> [port] [route]
 ```

 ### pipenv
 ```
-pipenv run swupdateclient <path-to-swu> <host_name> [port]
+pipenv run swupdateclient <path-to-swu> <host_name> [port] [route]
 ```


diff --git a/tools/python/swupdateclient/swupdateclient/main.py b/tools/python/swupdateclient/swupdateclient/main.py
index 26e5c0b..28334a4 100644
--- a/tools/python/swupdateclient/swupdateclient/main.py
+++ b/tools/python/swupdateclient/swupdateclient/main.py
@@ -56,13 +56,14 @@  class ColorFormatter(logging.Formatter):
 class SWUpdater:
     """Python helper class for SWUpdate"""

-    url_upload = "http://{}:{}/upload"
-    url_status = "ws://{}:{}/ws"
+    url_upload = "http://{}:{}{}/upload"
+    url_status = "ws://{}:{}{}/ws"

-    def __init__(self, path_image, host_name, port=8080, logger=None, log_level=logging.DEBUG):
+    def __init__(self, path_image, host_name, port=8080, route="", logger=None, log_level=logging.DEBUG):
         self._image = path_image
         self._host_name = host_name
         self._port = port
+        self._route = route
         if logger is not None:
             self._logger = logger
         else:
@@ -76,7 +77,7 @@  class SWUpdater:
         self._logger.info("Waiting for messages on websocket connection")
         try:
             async with websockets.connect(
-                self.url_status.format(self._host_name, self._port)
+                self.url_status.format(self._host_name, self._port, self._route)
             ) as websocket:
                 while True:
                     try:
@@ -112,7 +113,7 @@  class SWUpdater:

     def sync_upload(self, swu_file, timeout):
         return requests.post(
-            self.url_upload.format(self._host_name, self._port),
+            self.url_upload.format(self._host_name, self._port, self._route),
             files={"file": swu_file},
             headers={"Cache-Control": "no-cache"},
             timeout=timeout,
@@ -172,6 +173,13 @@  def client (args: List[str]) -> None:
     parser.add_argument("swu_file", help="Path to swu image")
     parser.add_argument("host_name", help="Host name")
     parser.add_argument("port", help="Port", type=int, default=8080, nargs="?")
+    parser.add_argument(
+        "route",
+        help="Name of the API route (e.g. /ROUTE)",
+        type=str,
+        default="",
+        nargs="?"
+    )
     parser.add_argument(
         "--timeout",
         help="Timeout for the whole swupdate process",
@@ -201,6 +209,7 @@  def client (args: List[str]) -> None:
         args.swu_file,
         args.host_name,
         args.port,
+        args.route,
         log_level=args.log_level.upper())
     updater.update(timeout=args.timeout)