Skip to content

Commit

Permalink
feat: Detaching logging from main logic (#3205)
Browse files Browse the repository at this point in the history
* First approach to login

* Adding HPS dependencies

* Adding changelog entry: 3205.miscellaneous.md

* feat: Coupling login code to current implementation.
Allowing login using token which is now the preferred method.

* coupling cli

* fix: command name in CLI

* Adding changelog entry: 3205.added.md

* fix: wrong argument that avoid having the input file as argument

* chore: checking config file in submit function.

* feat: avoid venv creation of ``requirements_file`` is False.

* feat: login CLI finished

* feat: making sure we don't get import errors when PyHPS is not installed.

* feat: Adding docstrings

* chore: renaming 'access' to 'get_token_access'.

* fix: failing piping on the CLI.

---------

Co-authored-by: pyansys-ci-bot <pyansys.github.bot@ansys.com>
  • Loading branch information
germa89 and pyansys-ci-bot authored Jun 26, 2024
1 parent dd0cb4b commit 4145688
Show file tree
Hide file tree
Showing 9 changed files with 765 additions and 43 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3205.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: Detaching logging from main logic
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ doc = [
"vtk==9.3.0",
]

hps =[
"ansys-hps-client==0.8.0",
"keyring==25.2.1",
]

[tool.flit.module]
name = "ansys.mapdl.core"

Expand Down
24 changes: 19 additions & 5 deletions src/ansys/mapdl/core/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
_HAS_CLICK = False


try:
from ansys.hps.client import Client

_HAS_HPS = True
except ModuleNotFoundError:
_HAS_HPS = False

if _HAS_CLICK:
###################################
# PyMAPDL CLI
Expand All @@ -39,7 +46,6 @@ def main(ctx):
pass

from ansys.mapdl.core.cli.convert import convert
from ansys.mapdl.core.cli.hpc import submit
from ansys.mapdl.core.cli.list_instances import list_instances
from ansys.mapdl.core.cli.start import start
from ansys.mapdl.core.cli.stop import stop
Expand All @@ -50,10 +56,18 @@ def main(ctx):
main.add_command(list_instances, name="list")

# HPC commands
# pymapdl hpc submit
# pymapdl hpc list
# pymapdl hpc stop
main.add_command(submit)
# pymapdl (hpc) login
# pymapdl (hpc) submit
# pymapdl (hpc) list #To be implemented
# pymapdl (hpc) stop #To be implemented

if _HAS_HPS:
from ansys.mapdl.core.cli.hpc import submit
from ansys.mapdl.core.cli.login import login, logout

main.add_command(login)
main.add_command(submit)
main.add_command(logout)

def old_pymapdl_convert_script_entry_point():
print(
Expand Down
3 changes: 3 additions & 0 deletions src/ansys/mapdl/core/cli/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ def convert(
)

else:
if not filename_in:
raise ValueError("A file path must be provided.")

convert_script(
filename_in,
filename_out,
Expand Down
54 changes: 42 additions & 12 deletions src/ansys/mapdl/core/cli/hpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@

import click

from ansys.mapdl.core.cli import main

logger = logging.getLogger()


@main.command(
@click.command(
short_help="Submit jobs to an HPC cluster using PyHPS.",
help="""
Submit jobs to an HPC cluster using PyHPS.
Expand All @@ -55,16 +53,35 @@
"--url",
default=None,
type=str,
help="""URL where the HPS cluster is deployed. For example: "https://myserver:3000/hps" """,
help="""URL where the HPS cluster is deployed. For example: "https://myserver:3000/hps".
If it is not input, there is a chain of places where PyMAPDL looks for an URL.
First, it checks if the URL is given in the file specified by the argument ``--config_file``.
If that file does not have an URL or does not exist, then it checks the default user credentials stored with ``pymapdl login --default`` CLI command.
If no URL is found, an exception is raised.""",
)
@click.option(
"--user", default=None, type=str, help="Username for logging into the HPC cluster."
"--user",
default=None,
type=str,
help="""Username for logging into the HPC cluster.
If it is not input, there is a chain of places where PyMAPDL looks for an username.
First, it checks if the username is given in the file specified by the argument ``--config_file``.
If that file does not have an username or does not exist, then it checks the username configured using ``pymapdl login`` CLI command, for the given HPS cluster URL.
If there is no user credential stored for that HPS cluster URL, then it checks the default user credentials stored with ``pymapdl login --default`` CLI command.
If no user is found, an exception is raised.
""",
)
@click.option(
"--password",
default=None,
type=str,
help="Password for logging into the HPC cluster.",
help="""Password for logging into the HPC cluster.
If it is not input, there is a chain of places where PyMAPDL looks for a password.
First, it checks if the password is given in the file specified by the argument ``--config_file``.
If that file does not have a password or does not exist, then it checks the password configured using ``pymapdl login`` CLI command, for the given HPS cluster URL.
If there is no user credential stored for that HPS cluster URL, then it checks the default user credentials stored with ``pymapdl login --default`` CLI command.
If no password is found, an exception is raised.
""",
)
@click.option(
"--python",
Expand Down Expand Up @@ -235,7 +252,7 @@ def submit(
mode: Optional[Union["python", "shell", "apdl"]] = None,
to_json: Optional[bool] = False,
):
import json
from ansys.mapdl.core.hpc.login import get_default_url, get_token_access

if to_json:
import json
Expand All @@ -254,11 +271,25 @@ def submit(

if config_file is None:
config_file = os.path.join(os.getcwd(), "hps_config.json")
if not os.path.exists(config_file):
config_file = None
logger.debug(f"Using default HPS configuration file: {config_file}")

url = get_value_from_json_or_default(url, config_file, "url", None)
user = get_value_from_json_or_default(user, config_file, "user", None)
password = get_value_from_json_or_default(password, config_file, "password", None)
# Getting cluster login configuration from CLI or file
url = get_value_from_json_or_default(
url, config_file, "url", None, raise_if_none=False
)
url = url or get_default_url() # using default URL stored.

# allow retrieving user from the configuration
user = get_value_from_json_or_default(
user, config_file, "user", raise_if_none=False
)

# Getting access token
token = get_token_access(url, user, password)

# Getting other configuration from CLI or file
python = get_value_from_json_or_default(python, config_file, "python", 3)
name = get_value_from_json_or_default(name, config_file, "name", "My PyMAPDL job")

Expand All @@ -276,8 +307,7 @@ def submit(

job = PyMAPDLJobSubmission(
url=url,
user=user,
password=password,
token=token,
main_file=main_file,
mode=mode,
inputs=inputs,
Expand Down
1 change: 1 addition & 0 deletions src/ansys/mapdl/core/cli/list_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@main.command(
short_help="List MAPDL running instances.",
help="""This command list MAPDL instances""",
name="list",
)
@click.option(
"--instances",
Expand Down
Loading

0 comments on commit 4145688

Please sign in to comment.