Skip to content

Commit

Permalink
feat: add route option for python swupdate client
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
janfeemers committed Sep 8, 2023
1 parent e768a4e commit ec4fb46
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions tools/python/swupdateclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```


Expand Down
19 changes: 14 additions & 5 deletions tools/python/swupdateclient/swupdateclient/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ def format(self, record):
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:
Expand All @@ -76,7 +77,7 @@ async def wait_update_finished(self):
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:
Expand Down Expand Up @@ -112,7 +113,7 @@ async def wait_update_finished(self):

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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit ec4fb46

Please sign in to comment.