Skip to content

Commit

Permalink
Improve deploy for Isaac Sim
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 25, 2025
1 parent 48afb37 commit c68cc98
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
45 changes: 23 additions & 22 deletions src/nanosaur/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# https://gabrieldemarmiesse.github.io/python-on-whales/
from python_on_whales import docker, DockerClient, DockerException
from nanosaur.utilities import Params, RobotList, get_nanosaur_home, build_env_file, is_env_file
from nanosaur.utilities import Params, RobotList, get_nanosaur_home, build_env_file
from nanosaur.prompt_colors import TerminalFormatter
import os

Expand All @@ -41,25 +41,23 @@ def docker_robot_run_command(platform, params: Params, command, name=None):
docker_compose = f"docker-compose.{workspace_type}.yml"
nanosaur_home_path = get_nanosaur_home()
# Create the full file path
docker_compose_path = os.path.join(nanosaur_home_path, docker_compose)
robot = RobotList.get_robot(params)
docker_compose_path = os.path.join(nanosaur_home_path, docker_compose)
env_file_path = os.path.join(nanosaur_home_path, f'{robot.name}.env')

# Check which simulation tool is selected only if robot.simulation is true
if robot.simulation and 'simulation_tool' not in params:
print(TerminalFormatter.color_text("No simulation tool selected. Please run simulation set first.", color='red'))
return False
# Start the container in detached mode
simulation_tool = params['simulation_tool'].lower().replace(' ', '_')

# Build env file
if not is_env_file():
print(TerminalFormatter.color_text("Creating the environment file...", color='green'))
build_env_file(params)
build_env_file(params)

# Create a DockerClient object with the docker-compose file
nanosaur_compose = DockerClient(compose_files=[docker_compose_path])
nanosaur_compose = DockerClient(compose_files=[docker_compose_path], compose_env_files=[env_file_path])
try:
nanosaur_compose.compose.run(service=simulation_tool, command=command, remove=True, tty=True, name=name)
# TODO make a new docker only for this command
nanosaur_compose.compose.run(service='gazebo', command=command, remove=True, tty=True, name=name)
except DockerException as e:
print(TerminalFormatter.color_text(f"Error running the command: {e}", color='red'))
return False
Expand All @@ -76,26 +74,26 @@ def docker_robot_start(platform, params: Params, args):
docker_compose = f"docker-compose.{workspace_type}.yml"
nanosaur_home_path = get_nanosaur_home()
# Create the full file path
docker_compose_path = os.path.join(nanosaur_home_path, docker_compose)
robot = RobotList.get_robot(params)

docker_compose_path = os.path.join(nanosaur_home_path, docker_compose)
env_file_path = os.path.join(nanosaur_home_path, f'{robot.name}.env')

# Check which simulation tool is selected only if robot.simulation is true
if robot.simulation and 'simulation_tool' not in params:
print(TerminalFormatter.color_text("No simulation tool selected. Please run simulation set first.", color='red'))
return False

# Build env file
if not is_env_file():
print(TerminalFormatter.color_text("Creating the environment file...", color='green'))
build_env_file(params)
build_env_file(params)

print(TerminalFormatter.color_text(f"robot {robot.name} starting", color='green'))

compose_profiles = []
if args.profile:
print(TerminalFormatter.color_text(f"Starting with profile: {args.profile}", color='green'))
compose_profiles = [args.profile]
# Create a DockerClient object with the docker-compose file
nanosaur_compose = DockerClient(compose_files=[docker_compose_path], compose_profiles=compose_profiles)
nanosaur_compose = DockerClient(compose_files=[docker_compose_path], compose_env_files=[env_file_path], compose_profiles=compose_profiles)
# Start the container in detached mode
try:
nanosaur_compose.compose.up(detach=args.detach)
Expand All @@ -117,24 +115,26 @@ def docker_simulator_start(platform, params: Params, args):
docker_compose = f"docker-compose.{workspace_type}.yml"
nanosaur_home_path = get_nanosaur_home()
# Create the full file path
robot = RobotList.get_robot(params)
docker_compose_path = os.path.join(nanosaur_home_path, docker_compose)
env_file_path = os.path.join(nanosaur_home_path, f'{robot.name}.env')

# Check which simulation tool is selected
if 'simulation_tool' not in params:
print(TerminalFormatter.color_text("No simulation tool selected. Please run simulation set first.", color='red'))
return False
# Start the container in detached mode
simulation_tool = params['simulation_tool'].lower().replace(' ', '_')
simulation_tool = params['simulation_tool'].lower().replace(' ', '-')
# Create a DockerClient object with the docker-compose file
nanosaur_compose = DockerClient(compose_files=[docker_compose_path])
nanosaur_compose = DockerClient(compose_files=[docker_compose_path], compose_env_files=[env_file_path])

# if len(nanosaur_compose.compose.ps()) > 0:
# print(TerminalFormatter.color_text(f"The robot {robot.name} is already running.", color='red'))
# return False

# Build env file
if not is_env_file():
print(TerminalFormatter.color_text("Creating the environment file...", color='green'))
build_env_file(params)
build_env_file(params)

print(TerminalFormatter.color_text(f"Simulator {simulation_tool} starting", color='green'))
try:
nanosaur_compose.compose.up(services=[f'{simulation_tool}'], recreate=False)
Expand All @@ -155,11 +155,12 @@ def docker_robot_stop(platform, params: Params, args):
docker_compose = f"docker-compose.{workspace_type}.yml"
nanosaur_home_path = get_nanosaur_home()
# Create the full file path
robot = RobotList.get_robot(params)
docker_compose_path = os.path.join(nanosaur_home_path, docker_compose)
env_file_path = os.path.join(nanosaur_home_path, f'{robot.name}.env')

robot = RobotList.get_robot(params)
# Create a DockerClient object with the docker-compose file
nanosaur_compose = DockerClient(compose_files=[docker_compose_path])
nanosaur_compose = DockerClient(compose_files=[docker_compose_path], compose_env_files=[env_file_path])
if len(nanosaur_compose.compose.ps()) > 0:
nanosaur_compose.compose.down(volumes=True)
print(TerminalFormatter.color_text(f"robot {robot.name} stopped", color='green'))
Expand Down
2 changes: 1 addition & 1 deletion src/nanosaur/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def install(platform, params: Params, args):
def nanosaur_wake_up(platform, params: Params, args):
args.detach = False
# Start the container in detached mode
simulation_tool = params.get('simulation_tool', '').lower().replace(' ', '_')
simulation_tool = params.get('simulation_tool', '').lower().replace(' ', '-')
args.profile = simulation_tool
return docker_robot_start(platform, params, args)

Expand Down
10 changes: 5 additions & 5 deletions src/nanosaur/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,9 @@ def save(self):
# Get the current nanosaur's home directory
create_nanosaur_home()
# Save the parameters to the file
print(TerminalFormatter.color_text(f"Saving parameters to {params_file} and update .env file", color='yellow'))
print(TerminalFormatter.color_text(f"Saving parameters to {params_file}", color='yellow'))
with open(params_file, 'w') as file:
yaml.dump(self._params_dict, file)
# Make a env file
build_env_file(self._params_dict)

@staticmethod
def get_params_file() -> str:
Expand Down Expand Up @@ -309,15 +307,17 @@ def is_env_file():

def build_env_file(params):
nanosaur_home_path = get_nanosaur_home()
env_path = os.path.join(nanosaur_home_path, '.env')
# Get current robot running
robot = RobotList.get_robot(params)
uid = os.getuid()
gid = os.getgid()
env_path = os.path.join(nanosaur_home_path, f'{robot.name}.env')
# Create a .env file and save UID and GID
with open(env_path, 'w') as env_file:
env_file.write(f"USER_UID={uid}\n")
env_file.write(f"USER_GID={gid}\n")
# Robot home folder
env_file.write(f"ROBOT_HOME={nanosaur_home_path}\n")
# Pass robot name
env_file.write(f"ROBOT_NAME={robot.name}\n")
# Pass robot simulation type
Expand All @@ -335,7 +335,7 @@ def build_env_file(params):
env_file.write(f"PERCEPTION_TAG={perception_tag}\n")
# Check which simulation tool is selected and save it in the .env file
if 'simulation_tool' in params:
simulation_tool = params['simulation_tool'].lower().replace(' ', '_')
simulation_tool = params['simulation_tool'].lower().replace(' ', '-')
env_file.write(f"SIMULATION={simulation_tool}\n")
# Pass robot ros commands
env_file.write(f"COMMANDS={robot.config_to_ros()}\n")
Expand Down
7 changes: 6 additions & 1 deletion src/nanosaur/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,12 @@ def deploy_simulation(image_name):
dockerfile_path = f"{nanosaur_simulations_path}/Dockerfile.gazebo"
if not ros.deploy_docker_image(dockerfile_path, tag_image):
return False

# Build Isaac Sim docker
if image_name == 'isaac-sim' or image_name is None:
tag_image = f"{nanosaur_docker_user}/{NANOSAUR_DOCKER_PACKAGE_SIMULATION}:isaac-sim"
dockerfile_path = f"{nanosaur_simulations_path}/Dockerfile.isaac-sim"
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"
Expand Down

0 comments on commit c68cc98

Please sign in to comment.