Skip to content

Commit

Permalink
Improve deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 24, 2025
1 parent e0698b2 commit 7b501ed
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 62 deletions.
65 changes: 23 additions & 42 deletions src/nanosaur/ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@
ros2_distro = 'humble'
ros2_sources = f'/opt/ros/{ros2_distro}/setup.bash'

ISAAC_ROS_COMMON_FOLDER = 'isaac_ros_common'
ISAAC_ROS_COMMON_REPO = 'https://github.com/NVIDIA-ISAAC-ROS/isaac_ros_common'

NANOSAUR_DOCKER_PACKAGE_ROBOT = "nanosaur"
NANOSAUR_DOCKER_PACKAGE_SIMULATION = "simulation"


def run_docker_isaac_ros(workspace_path, auto_commands=[]):
nanosaur_home_path = get_nanosaur_home()
# Path to the Isaac ROS common package
isaac_ros_common_path = os.path.join(nanosaur_home_path, 'isaac_ros_common')
isaac_ros_common_path = os.path.join(nanosaur_home_path, ISAAC_ROS_COMMON_FOLDER)

# Path to the script you want to run
command = "./scripts/run_dev.sh"
Expand Down Expand Up @@ -248,42 +246,19 @@ def run_colcon_build(folder_path) -> bool:
print(f"An error occurred while running the colcon build command: {e}")
return False


def deploy_docker_simulation(docker_user: str, simulation_ws_path: str, image_name: str = None) -> bool:
# Get the path to the nanosaur_simulations package
nanosaur_simulations_path = os.path.join(simulation_ws_path, 'src', 'nanosaur_simulations')
# Build Gazebo sim docker
if image_name == 'gazebo' or image_name is None:
tag_image = f"{docker_user}/{NANOSAUR_DOCKER_PACKAGE_SIMULATION}:gazebo"
try:
print(TerminalFormatter.color_text(f"Building Gazebo simulation docker image {tag_image}", color='magenta', bold=True))
docker.build(
get_nanosaur_home(),
file=f"{nanosaur_simulations_path}/Dockerfile.gazebo",
tags=tag_image
)
except DockerException as e:
print(TerminalFormatter.color_text(f"Error building Gazebo Docker image: {e}", color='red'))
return False

# Build the Docker image for nanosaur bridge
if image_name == 'robot' or image_name is None:
tag_image = f"{docker_user}/{NANOSAUR_DOCKER_PACKAGE_ROBOT}:simulation"
try:
print(TerminalFormatter.color_text(f"Building Nanosaur robot docker image {tag_image}", color='magenta', bold=True))
docker.build(
get_nanosaur_home(),
file=f"{nanosaur_simulations_path}/Dockerfile.nanosaur",
tags=tag_image
)
except DockerException as e:
print(TerminalFormatter.color_text(f"Error building Nanosaur Docker image: {e}", color='red'))
return False

# Print success message
print(TerminalFormatter.color_text("Docker image built successfully", color='green'))
return True

def deploy_docker_image(dockerfile_path, tag_image):
try:
print(TerminalFormatter.color_text(f"Building Docker image {tag_image}", color='magenta', bold=True))
docker.build(
get_nanosaur_home(),
file=dockerfile_path,
tags=tag_image
)
print(TerminalFormatter.color_text("Docker image built successfully", color='green'))
return True
except DockerException as e:
print(TerminalFormatter.color_text(f"Error building Docker image: {e}", color='red'))
return False

def deploy_docker_isaac_ros(isaac_ros_ws_path, tags, release_tag_name, debug=False) -> bool:
"""
Expand All @@ -307,7 +282,7 @@ def deploy_docker_isaac_ros(isaac_ros_ws_path, tags, release_tag_name, debug=Fal
]
ws_dir_list = '--ws-src ' + ' --ws-src '.join(src_folders)
# Path to the Isaac ROS common package
isaac_ros_common_path = os.path.join(get_nanosaur_home(), 'isaac_ros_common')
isaac_ros_common_path = os.path.join(get_nanosaur_home(), ISAAC_ROS_COMMON_FOLDER)
# Path to the Nanosaur Docker scripts
nanosaur_docker_path = os.path.join(shared_path, 'nanosaur', 'nanosaur', 'docker')
# Build the command to run the Docker build script
Expand All @@ -316,6 +291,7 @@ def deploy_docker_isaac_ros(isaac_ros_ws_path, tags, release_tag_name, debug=Fal
command = f"{nanosaur_docker_path}/docker_build_isaac_ros.sh {debug_flag} -d {tags_name} -c {isaac_ros_common_path} -i {release_tag_name} {ws_dir_list}"

try:
print(TerminalFormatter.color_text(f"Deploying {release_tag_name}", color='magenta', bold=True))
# Run the command and stream the output live
process = subprocess.Popen(
command,
Expand All @@ -336,14 +312,19 @@ def deploy_docker_isaac_ros(isaac_ros_ws_path, tags, release_tag_name, debug=Fal
else:
print(TerminalFormatter.color_text("Command completed successfully", color='green'))
return True
except KeyboardInterrupt:
print(TerminalFormatter.color_text("Process interrupted by user", color='red'))
process.terminate()
process.wait()
return False
except Exception as e:
print(f"An error occurred while running the command: {e}")
return False


def manage_isaac_ros_common_repo(nanosaur_home_path: str, isaac_ros_branch: str, force) -> bool:
# Path to the Isaac ROS common package
isaac_ros_common_path = os.path.join(nanosaur_home_path, 'isaac_ros_common')
isaac_ros_common_path = os.path.join(nanosaur_home_path, ISAAC_ROS_COMMON_FOLDER)

def update_existing_repo():
try:
Expand Down
71 changes: 51 additions & 20 deletions src/nanosaur/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@

ISAAC_ROS_RELEASE = "release-3.2"
ISAAC_ROS_DISTRO_SUFFIX = "ros2_humble"
ISAAC_ROS_DOCKER_CAMERA_LIST = ["realsense", "zed"]
NANOSAUR_DOCKERFILE_SUFFIX = "nanosaur"

DEFAULT_WORKSPACE_PERCEPTION = 'perception_ws'
DEFAULT_WORKSPACE_SIMULATION = 'simulation_ws'
DEFAULT_WORKSPACE_ROBOT = 'robot_ws'
DEFAULT_WORKSPACE_DEVELOPER = 'ros_ws'

NANOSAUR_DOCKER_PACKAGE_ROBOT = "nanosaur"
NANOSAUR_DOCKER_PACKAGE_SIMULATION = "simulation"
NANOSAUR_DOCKER_PACKAGE_PERCEPTION = "perception"


Expand Down Expand Up @@ -122,8 +125,7 @@ def clean(platform, params: utilities.Params, args):
}
if args.all:
print(TerminalFormatter.color_text("Cleaning all workspaces", bold=True))
results = [action() for action in workspace_actions.values()]
return all(results)
return all(action() for action in workspace_actions.values())
# Get the workspace
workspace = get_selected_workspace(params, workspace_actions, args)
if workspace is None:
Expand Down Expand Up @@ -193,8 +195,7 @@ def update_workspace(params, workspace_type, workspace_name_key, force, skip_ros
ros.manage_isaac_ros_common_repo(nanosaur_home_path, isaac_ros_branch, args.force)
print(TerminalFormatter.color_text("Updating all workspaces", bold=True))
update_shared_workspace(args.force)
results = [action() for action in workspace_actions.values()]
return all(results)
return all(action() for action in workspace_actions.values())
# Get the workspace
workspace = get_selected_workspace(params, workspace_actions, args)
if workspace is None:
Expand Down Expand Up @@ -235,8 +236,7 @@ def get_build_action(workspace_name_key):
}
if args.all:
print(TerminalFormatter.color_text("Building all workspaces", bold=True))
results = [action() for action in workspace_actions.values()]
return all(results)
return all(action() for action in workspace_actions.values())
# Get the workspace
workspace = get_selected_workspace(params, workspace_actions, args)
if workspace is None:
Expand Down Expand Up @@ -271,37 +271,68 @@ def deploy(platform, params: utilities.Params, args):
""" Deploy the workspace """
# Get the Nanosaur docker user
nanosaur_docker_user = utilities.get_nanosaur_docker_user(params)
# Get the Isaac ROS distro name
isaac_ros_distro_name = params.get('isaac_ros_distro', ISAAC_ROS_DISTRO_SUFFIX)

def deploy_simulation(image_name):
""" Deploy the simulation workspace """
simulation_ws_path = get_workspace_path(params, 'ws_simulation_name')
# Get the path to the nanosaur_simulations package
nanosaur_simulations_path = os.path.join(simulation_ws_path, 'src', 'nanosaur_simulations')

# Build Gazebo sim docker
if image_name == 'gazebo' or image_name is None:
tag_image = f"{nanosaur_docker_user}/{NANOSAUR_DOCKER_PACKAGE_SIMULATION}:gazebo"
dockerfile_path = f"{nanosaur_simulations_path}/Dockerfile.gazebo"
if not ros.deploy_docker_image(dockerfile_path, tag_image):
return False

# Build the Docker image for nanosaur bridge
if image_name == 'robot' or image_name is None:
tag_image = f"{nanosaur_docker_user}/{NANOSAUR_DOCKER_PACKAGE_ROBOT}:simulation"
dockerfile_path = f"{nanosaur_simulations_path}/Dockerfile.nanosaur"
if not ros.deploy_docker_image(dockerfile_path, tag_image):
return False
return True

def deploy_perception():
def deploy_perception(image_name):
""" Deploy the perception workspace """
# determine the device type
status = []
device_type = "robot" if platform['Machine'] == 'jetson' else "desktop"

# Get the path to the perception workspace
perception_ws_path = get_workspace_path(params, 'ws_perception_name')
isaac_ros_distro_name = params.get('isaac_ros_distro', ISAAC_ROS_DISTRO_SUFFIX)
# Get the release tag name
release_tag_name = f"{nanosaur_docker_user}/{NANOSAUR_DOCKER_PACKAGE_PERCEPTION}"
# Deploy the perception workspace
if device_type == "robot" or args.all:
for camera in ["realsense", "zed"]:
# Deploy the perception workspace for each camera
cameras = ISAAC_ROS_DOCKER_CAMERA_LIST if args.all or image_name is None else [image_name]
for camera in cameras:
# Create the tags for the docker image
tags = [isaac_ros_distro_name, camera, NANOSAUR_DOCKERFILE_SUFFIX]
print(TerminalFormatter.color_text(f"Deploying {release_tag_name}:{camera}", bold=True))
status += [ros.deploy_docker_isaac_ros(perception_ws_path, tags, f"{release_tag_name}:{camera}")]
# Deploy the perception workspace for each camera
if not ros.deploy_docker_isaac_ros(perception_ws_path, tags, f"{release_tag_name}:{camera}"):
return False
if device_type == "desktop" or args.all:
# Deploy the perception workspace for simulation
tags = [isaac_ros_distro_name, NANOSAUR_DOCKERFILE_SUFFIX]
status += [ros.deploy_docker_isaac_ros(perception_ws_path, tags, f"{release_tag_name}:simulation")]
return all(status)
if not ros.deploy_docker_isaac_ros(perception_ws_path, tags, f"{release_tag_name}:simulation"):
return False
return True

# Get the deploy action
workspace_actions = {
'developer': lambda: ros.deploy_docker_isaac_ros(get_workspace_path(params, 'ws_developer_name'), f'{nanosaur_docker_user}/developer'),
'simulation': lambda: ros.deploy_docker_simulation(nanosaur_docker_user, get_workspace_path(params, 'ws_simulation_name'), args.image_name),
'perception': lambda: deploy_perception(),
'developer': lambda: ros.deploy_docker_isaac_ros(
get_workspace_path(params, 'ws_developer_name'),
[isaac_ros_distro_name, NANOSAUR_DOCKERFILE_SUFFIX],
f'{nanosaur_docker_user}/developer'
),
'simulation': lambda: deploy_simulation(args.image_name),
'perception': lambda: deploy_perception(args.image_name),
}
if args.all:
print(TerminalFormatter.color_text("Deploying all workspaces", bold=True))
results = [action() for action in workspace_actions.values()]
return all(results)
return all(action() for action in workspace_actions.values())
# Get the workspace
workspace = get_selected_workspace(params, workspace_actions, args)
if workspace is None:
Expand Down

0 comments on commit 7b501ed

Please sign in to comment.