Skip to content

Adding on_docker and is_local methods #1731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def __init__(
self._cached_routine = None
self._geometry = None
self._kylov = None
self._on_docker = None
self._platform = None

# Setting up loggers
self._log = logger.add_instance_logger(
Expand Down Expand Up @@ -3867,3 +3869,49 @@ def set(
)
else:
return output

def _check_mapdl_os(self):
platform = self.get_value("active", 0, "platform").strip()
if "l" in platform.lower():
self._platform = "linux"
elif "w" in platform.lower(): # pragma: no cover
self._platform = "windows"
else: # pragma: no cover
raise MapdlRuntimeError("Unknown platform: {}".format(platform))

@property
def platform(self):
"""Return the platform where MAPDL is running."""
if self._platform is None:
self._check_mapdl_os()
return self._platform

def _check_on_docker(self):
"""Check if MAPDL is running on docker."""
# self.get_mapdl_envvar("ON_DOCKER") # for later

if self.platform == "linux":
self.sys(
"if grep -sq 'docker\|lxc' /proc/1/cgroup; then echo 'true' > __outputcmd__.txt; else echo 'false' > __outputcmd__.txt;fi;"
)
elif self.platform == "windows": # pragma: no cover
return False # TODO: check if it is running a windows docker container. So far it is not supported.

if self.is_grpc and not self.is_local:
return self._download_as_raw("__outputcmd__.txt").decode().strip() == "true"
else: # pragma: no cover
file_ = os.path.join(self.directory, "__outputcmd__.txt")
with open(file_, "r") as f:
return f.read().strip() == "true"

@property
def on_docker(self):
"""Check if MAPDL is running on docker."""
if self._on_docker is None:
self._on_docker = self._check_on_docker()
return self._on_docker

@property
def is_local(self):
"""Check if the instance is running locally or remotely."""
return self._local
12 changes: 12 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1668,3 +1668,15 @@ def test_remove_lock_file(mapdl, tmpdir):

mapdl._remove_lock_file(tmpdir_)
assert not os.path.exists(lock_file)


def test_is_local(mapdl):
assert mapdl.is_local == mapdl._local


def test_on_docker(mapdl):
assert mapdl.on_docker == mapdl._on_docker
if os.getenv("PYMAPDL_START_INSTANCE", "false") == "true":
assert mapdl.on_docker
else:
assert not mapdl.on_docker