Skip to content

Commit

Permalink
feat(device): wait until virtual device is online on rapyuta.io
Browse files Browse the repository at this point in the history
This commit adds a field in the device manifest for the apply command to
wait until a virtual device is online on rapyuta.io.

Fixes AB#18043
  • Loading branch information
pallabpain committed Oct 15, 2024
1 parent 9e801d2 commit 4bfde38
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions riocli/apply/manifests/device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ spec:
os: "ubuntu" # Options: ["ubuntu" (default), "debian" ]
codename: "focal" # Options: ["bionic", "focal" (default), "jammy", "bullseye"]
highperf: False # Optional [True, False (default)]
wait: True # Optional [True, False (default)] Wait until the device is ready.
docker:
enabled: True # Required
rosbagMountPath: "/opt/rapyuta/volumes/rosbag"
Expand Down
10 changes: 8 additions & 2 deletions riocli/device/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
from riocli.config import new_client
from riocli.constants import ApplyResult
from riocli.device.util import (DeviceNotFound, create_hwil_device, delete_hwil_device, execute_onboard_command,
find_device_by_name, make_device_labels_from_hwil_device)
find_device_by_name, make_device_labels_from_hwil_device, wait_until_online)
from riocli.exceptions import ResourceNotFound
from riocli.model import Model


class Device(Model):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)

def apply(self, *args, **kwargs) -> ApplyResult:
Expand All @@ -36,8 +37,10 @@ def apply(self, *args, **kwargs) -> ApplyResult:
except DeviceNotFound:
pass

virtual = self.spec.get('virtual', {})

# If the device is not virtual, create it and return.
if not self.spec.get('virtual', {}).get('enabled', False):
if not virtual.get('enabled', False):
if device is None:
client.create_device(self.to_v1())
return ApplyResult.CREATED
Expand Down Expand Up @@ -79,6 +82,9 @@ def apply(self, *args, **kwargs) -> ApplyResult:
onboard_command = onboard_script.full_command()
execute_onboard_command(hwil_response.id, onboard_command)

if virtual.get('wait', False):
wait_until_online(device)

return result

def delete(self, *args, **kwargs) -> None:
Expand Down
23 changes: 23 additions & 0 deletions riocli/device/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import functools
import json
import re
import time
import typing
from pathlib import Path

Expand All @@ -22,6 +23,7 @@
from rapyuta_io import Client
from rapyuta_io.clients import LogUploads
from rapyuta_io.clients.device import Device
from rapyuta_io.clients.device import DeviceStatus
from rapyuta_io.utils import RestClient
from rapyuta_io.utils.rest_client import HttpMethod

Expand Down Expand Up @@ -85,6 +87,7 @@ def find_device_guid(client: Client, name: str) -> str:

raise DeviceNotFound()


def find_device_by_name(client: Client, name: str) -> Device:
devices = client.get_all_devices(device_name=name)
if devices:
Expand Down Expand Up @@ -290,3 +293,23 @@ def sanitize_hwil_device_name(name):
r = r + c

return r


def wait_until_online(device: Device, timeout: int = 600) -> None:
"""Wait until the device is online.
This is a helper method that waits until the device is online.
Or, until the timeout is reached. The default timeout is 600 seconds.
"""
counter, interval = 0, 20
failed_states = (DeviceStatus.FAILED, DeviceStatus.REJECTED)

device.refresh()

while not device.is_online() and device.status not in failed_states and counter < timeout:
counter += interval
time.sleep(interval)
device.refresh()

if not device.is_online() and counter >= timeout:
raise Exception('timeout reached while waiting for the device to be online')
3 changes: 3 additions & 0 deletions riocli/jsonschema/schemas/device-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ definitions:
enabled:
enum:
- True
wait:
type: boolean
default: False
product:
type: string
enum:
Expand Down

0 comments on commit 4bfde38

Please sign in to comment.