| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import asyncio |
| 18 |
import json |
| 19 |
import os |
| 20 |
from typing import ( |
| 21 |
Any, |
| 22 |
Dict, |
| 23 |
List, |
| 24 |
Optional, |
| 25 |
Tuple, |
| 26 |
Union, |
| 27 |
) |
| 28 |
|
| 29 |
from cylc.flow.exceptions import ( |
| 30 |
ClientError, |
| 31 |
ClientTimeout, |
| 32 |
) |
| 33 |
from cylc.flow.network.client import WorkflowRuntimeClientBase |
| 34 |
from cylc.flow.network.client_factory import CommsMeth |
| 35 |
from cylc.flow.remote import remote_cylc_cmd |
| 36 |
from cylc.flow.workflow_files import ( |
| 37 |
ContactFileFields, |
| 38 |
load_contact_file, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
class WorkflowRuntimeClient(WorkflowRuntimeClientBase): |
| 43 |
"""Client to scheduler communication using ssh.""" |
| 44 |
|
| 45 |
DEFAULT_TIMEOUT = 300 # seconds |
| 46 |
SLEEP_INTERVAL = 0.1 |
| 47 |
|
| 48 |
async def async_request( |
| 49 |
self, command: str, |
| 50 |
args: Optional[Dict[str, Any]] = None, |
| 51 |
timeout: Optional[float] = None, |
| 52 |
req_meta: Optional[Dict[str, Any]] = None |
| 53 |
): |
| 54 |
"""Send asynchronous request via SSH. |
| 55 |
|
| 56 |
Determines ssh_cmd, cylc_path and login_shell settings from the contact |
| 57 |
file. |
| 58 |
|
| 59 |
Converts message to JSON and sends this to stdin. Executes the Cylc |
| 60 |
command, then deserialises the output. |
| 61 |
|
| 62 |
Args: |
| 63 |
command (str): The name of the endpoint to call. |
| 64 |
args (dict): Arguments to pass to the endpoint function. |
| 65 |
timeout (float): Override the default timeout (seconds). |
| 66 |
Raises: |
| 67 |
ClientError: Coverall, on error from function call |
| 68 |
Returns: |
| 69 |
object: Deserialized output from function called. |
| 70 |
""" |
| 71 |
if timeout is None: |
| 72 |
timeout = self.timeout |
| 73 |
try: |
| 74 |
async with asyncio.timeout(timeout): |
| 75 |
cmd, ssh_cmd, login_sh, cylc_path, msg = self.prepare_command( |
| 76 |
command, args, timeout |
| 77 |
) |
| 78 |
platform: dict = { |
| 79 |
'ssh command': ssh_cmd, |
| 80 |
'cylc path': cylc_path, |
| 81 |
'use login shell': login_sh, |
| 82 |
'ssh forward environment variables': [], |
| 83 |
} |
| 84 |
# NOTE: this can not raise NoHostsError |
| 85 |
# because we have provided the host |
| 86 |
proc = remote_cylc_cmd( |
| 87 |
cmd, |
| 88 |
platform, |
| 89 |
host=self.host, |
| 90 |
stdin_str=msg, |
| 91 |
capture_process=True |
| 92 |
) |
| 93 |
while proc.poll() is None: |
| 94 |
await asyncio.sleep(self.SLEEP_INTERVAL) |
| 95 |
out, err = proc.communicate() |
| 96 |
if proc.returncode: |
| 97 |
raise ClientError(err, f"return-code={proc.returncode}") |
| 98 |
return json.loads(out) |
| 99 |
except asyncio.TimeoutError: |
| 100 |
self.timeout_handler() |
| 101 |
raise ClientTimeout( |
| 102 |
f"Command exceeded the timeout {timeout}s. " |
| 103 |
"This could be due to network problems. " |
| 104 |
"Check the workflow log." |
| 105 |
) from None |
| 106 |
|
| 107 |
def prepare_command( |
| 108 |
self, command: str, args: Optional[dict], timeout: Union[float, str] |
| 109 |
) -> Tuple[List[str], str, str, Optional[str], str]: |
| 110 |
"""Prepare command for submission.""" |
| 111 |
# Set environment variable to determine the communication for use on |
| 112 |
# the scheduler |
| 113 |
os.environ["CLIENT_COMMS_METH"] = CommsMeth.SSH.value |
| 114 |
cmd = ["client"] |
| 115 |
if timeout: |
| 116 |
cmd.append(f'--comms-timeout={timeout}') |
| 117 |
cmd += [self.workflow, command] |
| 118 |
contact = load_contact_file(self.workflow) |
| 119 |
ssh_cmd = contact[ContactFileFields.SCHEDULER_SSH_COMMAND] |
| 120 |
login_shell = contact[ContactFileFields.SCHEDULER_USE_LOGIN_SHELL] |
| 121 |
cylc_path = contact[ContactFileFields.SCHEDULER_CYLC_PATH] |
| 122 |
cylc_path = None if cylc_path == 'None' else cylc_path |
| 123 |
if not args: |
| 124 |
args = {} |
| 125 |
message = json.dumps(args) |
| 126 |
return cmd, ssh_cmd, login_shell, cylc_path, message |