diff mbox series

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

Message ID 20231023185515.20200-1-jan.feemers@gmx.de
State Accepted
Headers show
Series [1/2] feat: add path option for python swupdate client | expand

Commit Message

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

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. 24, 2023, 11:12 a.m. UTC | #1
On 23.10.23 20:55, Jan Feemers wrote:
> if the update server is behind a reverse proxy a path is needed.
> The path should be optional and also work with websever without an
> additional path.
> 
> 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..7f755e3 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] [path]
>   ```
> 
>   ### pipenv
>   ```
> -pipenv run swupdateclient <path-to-swu> <host_name> [port]
> +pipenv run swupdateclient <path-to-swu> <host_name> [port] [path]
>   ```
> 
> 
> diff --git a/tools/python/swupdateclient/swupdateclient/main.py b/tools/python/swupdateclient/swupdateclient/main.py
> index 26e5c0b..a6ab768 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, path="", logger=None, log_level=logging.DEBUG):
>           self._image = path_image
>           self._host_name = host_name
>           self._port = port
> +        self._path = path
>           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._path)
>               ) 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._path),
>               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(
> +        "path",
> +        help="Name of the webserver-path (e.g. /PATH)",
> +        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
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/tools/python/swupdateclient/README.md b/tools/python/swupdateclient/README.md
index 50bd8cf..7f755e3 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] [path]
 ```

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


diff --git a/tools/python/swupdateclient/swupdateclient/main.py b/tools/python/swupdateclient/swupdateclient/main.py
index 26e5c0b..a6ab768 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, path="", logger=None, log_level=logging.DEBUG):
         self._image = path_image
         self._host_name = host_name
         self._port = port
+        self._path = path
         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._path)
             ) 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._path),
             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(
+        "path",
+        help="Name of the webserver-path (e.g. /PATH)",
+        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)