In this section we’ll provide a few handy examples of common script operations. The intention is to grow this into a good source to copy paste common code from. All of the examples will be available in the DevGuide Examples repository under the script_driver_recipes folder.

Executing commands on sandbox resources

The following script will try to execute a command only on resources that support it. If a resource does not support the command it will simply ignore it and move to the next resource.

orchestration_scripts_examples/try_execute_commands/try_execute_commands.py view raw
def try_execute_command_on_resources(session, reservation_id, command_name, command_inputs=[]):
    """
    This function will try to execute a command on all app resources that support it
    :param CloudShellAPISession session: CloudShell API Session
    :param str reservation_id: The reservation Id to run the commands on
    :param str command_name: The command to try and execute
    :param list[InputNameValue] command_inputs: Inputs parameters for the command
    :return: The aggregated results of the successful calls
    :rtype: dict[str,str]
    """
    results = {}
    for resource in session.GetReservationDetails(reservation_id).ReservationDescription.Resources:
        try:
            result = session.ExecuteCommand(reservation_id, resource.Name, "Resource", command_name, command_inputs)
            results[resource.Name] = result.Output

        except CloudShellAPIError as exc:
            # Ignore the error if the command doesn't exist on the resource or its not assigned a driver
            if exc.code not in (NO_DRIVER_ERR,DRIVER_FUNCTION_ERROR):
                raise
    return results