Skip to content

Commit

Permalink
feat: create launcher for core services (#1587)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
Co-authored-by: Maxime Rey <87315832+MaxJPRey@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 19, 2024
1 parent 9bca5e1 commit dc6f03e
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker_cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
with:
package-name: 'geometry'
token: ${{ secrets.GITHUB_TOKEN }}
tags-kept: 'windows-latest, windows-latest-unstable, core-windows-latest, core-windows-latest-unstable, core-linux-latest, core-linux-latest-unstable, 24.1, 24.2, 25.1, windows-24.1, windows-24.2, windows-25.1, windows-25.2, core-windows-25.2, linux-24.1, linux-24.2, linux-25.1, core-linux-25.2'
tags-kept: 'windows-latest, windows-latest-unstable, core-windows-latest, core-windows-latest-unstable, core-linux-latest, core-linux-latest-unstable, 24.1, 24.2, 25.1, windows-24.1, windows-24.2, windows-25.1, windows-25.2, core-windows-25.2, core-linux-25.2'
1 change: 1 addition & 0 deletions doc/changelog.d/1587.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create launcher for core services
1 change: 1 addition & 0 deletions src/ansys/geometry/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from ansys.geometry.core.connection.launcher import (
launch_docker_modeler,
launch_modeler,
launch_modeler_with_core_service,
launch_modeler_with_discovery,
launch_modeler_with_discovery_and_pimlight,
launch_modeler_with_geometry_service,
Expand Down
1 change: 1 addition & 0 deletions src/ansys/geometry/core/connection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from ansys.geometry.core.connection.launcher import (
launch_docker_modeler,
launch_modeler,
launch_modeler_with_core_service,
launch_modeler_with_discovery,
launch_modeler_with_discovery_and_pimlight,
launch_modeler_with_geometry_service,
Expand Down
18 changes: 7 additions & 11 deletions src/ansys/geometry/core/connection/docker_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,14 @@ class GeometryContainers(Enum):
CORE_LINUX_LATEST_UNSTABLE = 3, "linux", "core-linux-latest-unstable"
# TO BE REMOVED at some point... - START
WINDOWS_LATEST = 4, "windows", "windows-latest"
LINUX_LATEST = 5, "linux", "linux-latest"
WINDOWS_LATEST_UNSTABLE = 6, "windows", "windows-latest-unstable"
LINUX_LATEST_UNSTABLE = 7, "linux", "linux-latest-unstable"
WINDOWS_LATEST_UNSTABLE = 5, "windows", "windows-latest-unstable"
# TO BE REMOVED at some point... - END
WINDOWS_24_1 = 8, "windows", "windows-24.1"
LINUX_24_1 = 9, "linux", "linux-24.1"
WINDOWS_24_2 = 10, "windows", "windows-24.2"
LINUX_24_2 = 11, "linux", "linux-24.2"
WINDOWS_25_1 = 12, "windows", "windows-25.1"
LINUX_25_1 = 13, "linux", "linux-25.1"
WINDOWS_25_2 = 14, "windows", "core-windows-25.2"
LINUX_25_2 = 15, "linux", "core-linux-25.2"
WINDOWS_24_1 = 6, "windows", "windows-24.1"
WINDOWS_24_2 = 7, "windows", "windows-24.2"
WINDOWS_25_1 = 8, "windows", "windows-25.1"
WINDOWS_25_2 = 9, "windows", "windows-25.2"
CORE_WINDOWS_25_2 = 10, "windows", "core-windows-25.2"
CORE_LINUX_25_2 = 11, "linux", "core-linux-25.2"


class LocalDockerInstance:
Expand Down
147 changes: 143 additions & 4 deletions src/ansys/geometry/core/connection/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def launch_modeler(mode: str = None, **kwargs: dict | None) -> "Modeler":
* For ``"docker"`` mode, see the :func:`launch_docker_modeler` method.
* For ``"geometry_service"`` mode, see the
:func:`launch_modeler_with_geometry_service` method.
* For ``"core_service"`` mode, see the :func:`launch_modeler_with_core_service` method.
* For ``"spaceclaim"`` mode, see the :func:`launch_modeler_with_spaceclaim` method.
* For ``"discovery"`` mode, see the :func:`launch_modeler_with_discovery` method.
Expand Down Expand Up @@ -108,6 +109,8 @@ def _launch_with_launchmode(mode: str, **kwargs: dict | None) -> "Modeler":
* ``"docker"``: Launches the ``Modeler`` service locally using Docker.
* ``"geometry_service"``: Launches the ``Modeler`` service locally using the
Ansys Geometry Service.
* ``"core_service"``: Launches the ``Modeler`` service locally using the
Ansys Geometry Core Service.
* ``"spaceclaim"``: Launches the ``Modeler`` service locally using Ansys SpaceClaim.
* ``"discovery"``: Launches the ``Modeler`` service locally using Ansys Discovery.
Expand All @@ -132,6 +135,8 @@ def _launch_with_launchmode(mode: str, **kwargs: dict | None) -> "Modeler":
return launch_remote_modeler(**kwargs)
elif mode == "docker":
return launch_docker_modeler(**kwargs)
elif mode == "core_service":
return launch_modeler_with_core_service(**kwargs)
elif mode == "geometry_service":
return launch_modeler_with_geometry_service(**kwargs)
elif mode == "spaceclaim":
Expand Down Expand Up @@ -167,8 +172,9 @@ def _launch_with_automatic_detection(**kwargs: dict | None) -> "Modeler":
#
# 1. Check if PyPIM is configured and if the environment is configured for it.
# 2. Check if Docker is installed and if the environment is configured for it.
# 3. If you are on a Windows machine:
# - check if the Ansys Geometry service is installed.
# 3. Check if the Ansys Geometry Core service is installed. (OS agnostic)
# 4. If you are on a Windows machine:
# - check if the Ansys Geometry DMS service is installed.
# - check if Ansys SpaceClaim is installed.
# - check if Ansys Discovery is installed.

Expand All @@ -189,15 +195,23 @@ def _launch_with_automatic_detection(**kwargs: dict | None) -> "Modeler":
" Trying to start the Geometry service locally."
)

try:
LOG.info("Starting Geometry Core service locally.")
return launch_modeler_with_core_service(**kwargs)
except Exception:
wrn_msg = "The Geometry Core service could not be started locally."
wrn_msg += " Trying to start the Geometry DMS service locally." if os.name == "nt" else ""
LOG.warning(wrn_msg)

# If we are on a Windows machine, we can try to start the Geometry service locally,
# through various methods: Geometry service, SpaceClaim, Discovery.
if os.name == "nt":
try:
LOG.info("Starting Geometry service locally.")
LOG.info("Starting Geometry DMS service locally.")
return launch_modeler_with_geometry_service(**kwargs)
except Exception:
LOG.warning(
"The Geometry service could not be started locally."
"The Geometry DMS service could not be started locally."
" Trying to start Ansys SpaceClaim locally."
)

Expand Down Expand Up @@ -962,3 +976,128 @@ def _launch_pim_instance(
logging_level=client_log_level,
logging_file=client_log_file,
)


def launch_modeler_with_core_service(
product_version: int = None,
host: str = "localhost",
port: int = None,
enable_trace: bool = False,
timeout: int = 60,
server_log_level: int = 2,
client_log_level: int = logging.INFO,
server_logs_folder: str = None,
client_log_file: str = None,
**kwargs: dict | None,
) -> "Modeler":
"""Start the Geometry Core service locally using the ``ProductInstance`` class.
When calling this method, a standalone Geometry Core service is started.
By default, if an endpoint is specified (by defining `host` and `port` parameters)
but the endpoint is not available, the startup will fail. Otherwise, it will try to
launch its own service.
Parameters
----------
product_version: int, optional
The product version to be started. Goes from v25.2 to
the latest. Default is ``None``.
If a specific product version is requested but not installed locally,
a SystemError will be raised.
**Ansys products versions and their corresponding int values:**
* ``252`` : Ansys 25R2
* ``261`` : Ansys 26R1
host: str, optional
IP address at which the service will be deployed. By default,
its value will be ``localhost``.
port : int, optional
Port at which the service will be deployed. By default, its
value will be ``None``.
enable_trace : bool, optional
Boolean enabling the logs trace on the service console window.
By default its value is ``False``.
timeout : int, optional
Timeout for starting the backend startup process. The default is 60.
server_log_level : int, optional
Backend's log level from 0 to 3:
0: Chatterbox
1: Debug
2: Warning
3: Error
The default is ``2`` (Warning).
client_log_level : int, optional
Logging level to apply to the client. By default, INFO level is used.
Use the logging module's levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.
server_logs_folder : str, optional
Sets the backend's logs folder path. If nothing is defined,
the backend will use its default path.
client_log_file : str, optional
Sets the client's log file path. If nothing is defined,
the client will log to the console.
**kwargs : dict, default: None
Placeholder to prevent errors when passing additional arguments that
are not compatible with this method.
Returns
-------
Modeler
Instance of the Geometry Core service.
Raises
------
ConnectionError
If the specified endpoint is already in use, a connection
error will be raised.
SystemError
If there is not an Ansys product 25.2 version or later installed
a SystemError will be raised.
Examples
--------
Starting a geometry core service with the default parameters and getting back a ``Modeler``
object:
>>> from ansys.geometry.core import launch_modeler_with_core_service
>>> modeler = launch_modeler_with_core_service()
Starting a geometry service, on address ``10.171.22.44``, port ``5001``, with chatty
logs, traces enabled and a ``300`` seconds timeout:
>>> from ansys.geometry.core import launch_modeler_with_core_service
>>> modeler = launch_modeler_with_core_service(host="10.171.22.44",
port=5001,
enable_trace= True,
timeout=300,
server_log_level=0)
"""
# if api_version is passed, throw a warning saying that it is not used
if "api_version" in kwargs:
LOG.warning(
"The 'api_version' parameter is not used in 'launch_modeler_with_core_service'. "
"Please remove it from the arguments."
)

# If we are in a Windows environment, we are going to write down the server
# logs in the %PUBLIC%/Documents/Ansys/GeometryService folder.
if os.name == "nt" and server_logs_folder is None:
# Writing to the "Public" folder by default - no write permissions specifically required.
server_logs_folder = Path(os.getenv("PUBLIC"), "Documents", "Ansys", "GeometryService")
LOG.info(f"Writing server logs to the default folder at {server_logs_folder}.")

return prepare_and_start_backend(
BackendType.LINUX_SERVICE,
product_version=product_version,
host=host,
port=port,
enable_trace=enable_trace,
api_version=ApiVersions.LATEST,
timeout=timeout,
server_log_level=server_log_level,
client_log_level=client_log_level,
server_logs_folder=server_logs_folder,
client_log_file=client_log_file,
specific_minimum_version=252,
)
Loading

0 comments on commit dc6f03e

Please sign in to comment.