| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 4 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
"""cylc validate-reinstall [OPTIONS] ARGS |
| 20 |
|
| 21 |
Validate, reinstall, and reload or restart a workflow. |
| 22 |
|
| 23 |
This command updates a workflow to reflect any new changes made in the workflow |
| 24 |
source directory since it was installed. |
| 25 |
|
| 26 |
The workflow will be reinstalled, then either: |
| 27 |
* Reloaded (if the workflow is running), |
| 28 |
* or restarted (if it is stopped). |
| 29 |
|
| 30 |
Cylc will show you the list of files changed and prompt for confirmation before |
| 31 |
reinstalling. Use the '--yes' option to bypass this. |
| 32 |
|
| 33 |
This command is equivalent to: |
| 34 |
$ cylc validate myworkflow --against-source |
| 35 |
$ cylc reinstall myworkflow |
| 36 |
# if myworkflow is running: |
| 37 |
$ cylc reload myworkflow |
| 38 |
# else: |
| 39 |
$ cylc play myworkflow |
| 40 |
|
| 41 |
Note: |
| 42 |
"cylc validate --against-source" checks the code in the workflow source |
| 43 |
directory against any options (e.g. template variables) which have been set |
| 44 |
in the installed workflow to ensure the change can be safely applied. |
| 45 |
""" |
| 46 |
|
| 47 |
from ansimarkup import parse as cparse |
| 48 |
import asyncio |
| 49 |
from pathlib import Path |
| 50 |
import sys |
| 51 |
from typing import TYPE_CHECKING, Union |
| 52 |
|
| 53 |
if TYPE_CHECKING: |
| 54 |
from optparse import Values |
| 55 |
|
| 56 |
from cylc.flow import LOG |
| 57 |
from cylc.flow.exceptions import ( |
| 58 |
WorkflowStopped, |
| 59 |
) |
| 60 |
from cylc.flow.id_cli import parse_id_async |
| 61 |
from cylc.flow.loggingutil import set_timestamps |
| 62 |
from cylc.flow.option_parsers import ( |
| 63 |
WORKFLOW_ID_ARG_DOC, |
| 64 |
CylcOptionParser as COP, |
| 65 |
combine_options, |
| 66 |
log_subcommand, |
| 67 |
cleanup_sysargv |
| 68 |
) |
| 69 |
from cylc.flow.scheduler_cli import PLAY_OPTIONS, cylc_play |
| 70 |
from cylc.flow.scripts.ping import run as cylc_ping |
| 71 |
from cylc.flow.scripts.validate import ( |
| 72 |
VALIDATE_OPTIONS, |
| 73 |
VALIDATE_AGAINST_SOURCE_OPTION, |
| 74 |
run as cylc_validate, |
| 75 |
) |
| 76 |
from cylc.flow.scripts.reinstall import ( |
| 77 |
REINSTALL_CYLC_ROSE_OPTIONS, |
| 78 |
REINSTALL_OPTIONS, |
| 79 |
reinstall_cli as cylc_reinstall, |
| 80 |
) |
| 81 |
from cylc.flow.scripts.reload import ( |
| 82 |
RELOAD_OPTIONS, |
| 83 |
run as cylc_reload |
| 84 |
) |
| 85 |
from cylc.flow.terminal import cli_function, is_terminal |
| 86 |
from cylc.flow.workflow_files import get_workflow_run_dir |
| 87 |
|
| 88 |
|
| 89 |
CYLC_ROSE_OPTIONS = COP.get_cylc_rose_options() |
| 90 |
VR_OPTIONS = combine_options( |
| 91 |
VALIDATE_OPTIONS, |
| 92 |
REINSTALL_OPTIONS, |
| 93 |
REINSTALL_CYLC_ROSE_OPTIONS, |
| 94 |
RELOAD_OPTIONS, |
| 95 |
PLAY_OPTIONS, |
| 96 |
CYLC_ROSE_OPTIONS, |
| 97 |
modify={'cylc-rose': 'validate, install'} |
| 98 |
) |
| 99 |
|
| 100 |
_input = input # to enable testing |
| 101 |
|
| 102 |
|
| 103 |
def get_option_parser() -> COP: |
| 104 |
parser = COP( |
| 105 |
__doc__, |
| 106 |
comms=True, |
| 107 |
jset=True, |
| 108 |
argdoc=[WORKFLOW_ID_ARG_DOC], |
| 109 |
) |
| 110 |
for option in VR_OPTIONS: |
| 111 |
parser.add_option(*option.args, **option.kwargs) |
| 112 |
parser.set_defaults(is_validate=True) |
| 113 |
|
| 114 |
return parser |
| 115 |
|
| 116 |
|
| 117 |
def check_tvars_and_workflow_stopped( |
| 118 |
is_running: bool, tvars: list, tvars_file: list |
| 119 |
) -> bool: |
| 120 |
"""are template variables set and workflow stopped? |
| 121 |
|
| 122 |
Template vars set by --set (options.templatevars) or --set-file |
| 123 |
(optiions.templatevars_file) are only valid if the workflow is stopped |
| 124 |
and vr will play it. |
| 125 |
|
| 126 |
args: |
| 127 |
is_running: Is workflow running? |
| 128 |
tvars: options.tvars, from `--set` |
| 129 |
tvars_file: options.tvars_file, from `--set-file` |
| 130 |
""" |
| 131 |
if is_running and (tvars or tvars_file): |
| 132 |
LOG.warning( |
| 133 |
'Template variables (from --set/--set-file) can ' |
| 134 |
'only be changed if the workflow is stopped.' |
| 135 |
) |
| 136 |
return False |
| 137 |
return True |
| 138 |
|
| 139 |
|
| 140 |
@cli_function(get_option_parser) |
| 141 |
def main(parser: COP, options: 'Values', workflow_id: str): |
| 142 |
ret = asyncio.run(vr_cli(parser, options, workflow_id)) |
| 143 |
if isinstance(ret, str): |
| 144 |
# NOTE: cylc_play must be called from sync code (not async code) |
| 145 |
cylc_play(options, ret, parse_workflow_id=False) |
| 146 |
elif ret is False: |
| 147 |
sys.exit(1) |
| 148 |
|
| 149 |
|
| 150 |
async def vr_cli( |
| 151 |
parser: COP, options: 'Values', workflow_id: str |
| 152 |
) -> Union[bool, str]: |
| 153 |
"""Validate and reinstall and optionally reload workflow. |
| 154 |
|
| 155 |
Runs: |
| 156 |
* Validate |
| 157 |
* Reinstall |
| 158 |
* Reload (if the workflow is already running) |
| 159 |
|
| 160 |
Returns: |
| 161 |
The workflow_id or a True/False outcome. |
| 162 |
|
| 163 |
workflow_id: If the workflow is stopped and requires restarting. |
| 164 |
True: If workflow is running and does not require restarting. |
| 165 |
False: If this command should "exit 1". |
| 166 |
|
| 167 |
""" |
| 168 |
unparsed_wid = workflow_id |
| 169 |
workflow_id, *_ = await parse_id_async( |
| 170 |
workflow_id, |
| 171 |
constraint='workflows', |
| 172 |
) |
| 173 |
|
| 174 |
# First attempt to work out whether the workflow is running. |
| 175 |
# We are trying to avoid reinstalling then subsequently being |
| 176 |
# unable to play or reload because we cannot identify workflow state. |
| 177 |
log_subcommand('ping', workflow_id) |
| 178 |
try: |
| 179 |
result = await cylc_ping(options, workflow_id) |
| 180 |
# (don't catch CylcError: unable to determine if running or not) |
| 181 |
except WorkflowStopped: |
| 182 |
print(cparse(f"<green>{workflow_id} is not running</green>")) |
| 183 |
workflow_running = False |
| 184 |
else: |
| 185 |
print(cparse(f"<green>{workflow_id}: {result['stdout'][0]}</green>")) |
| 186 |
workflow_running = True |
| 187 |
|
| 188 |
# options.tvars and tvars_file are _only_ valid when playing a stopped |
| 189 |
# workflow: Fail if they are set and workflow running: |
| 190 |
if not check_tvars_and_workflow_stopped( |
| 191 |
workflow_running, options.templatevars, options.templatevars_file |
| 192 |
): |
| 193 |
return False |
| 194 |
|
| 195 |
# Save the location of the existing workflow run dir in the |
| 196 |
# against source option: |
| 197 |
options.against_source = Path(get_workflow_run_dir(workflow_id)) |
| 198 |
|
| 199 |
# Run "cylc validate" |
| 200 |
log_subcommand('validate --against-source', workflow_id) |
| 201 |
await cylc_validate(parser, options, workflow_id) |
| 202 |
|
| 203 |
# Unset options that do not apply after validation: |
| 204 |
del options.against_source |
| 205 |
del options.is_validate |
| 206 |
|
| 207 |
# Run "cylc reinstall" |
| 208 |
log_subcommand('reinstall', workflow_id) |
| 209 |
reinstall_ok = await cylc_reinstall( |
| 210 |
options, |
| 211 |
workflow_id, |
| 212 |
[], |
| 213 |
print_reload_tip=False |
| 214 |
) |
| 215 |
|
| 216 |
if not reinstall_ok: |
| 217 |
# No changes OR user said No to the reinstall prompt. |
| 218 |
|
| 219 |
# If not a terminal and not --yes do nothing. |
| 220 |
if not is_terminal() and not options.skip_interactive: |
| 221 |
return False |
| 222 |
|
| 223 |
# Else if not --yes, prompt user to continue or not. |
| 224 |
if not options.skip_interactive: |
| 225 |
usr = None |
| 226 |
if not workflow_running: |
| 227 |
# No changes to install, and the workflow is not running. |
| 228 |
# Can still restart with no changes. |
| 229 |
action = "Restart" |
| 230 |
else: |
| 231 |
# No changes to install, and the workflow is running. |
| 232 |
# Reload is probably pointless, but not necessarily (the user |
| 233 |
# could modify the run dir and do 'vr' to restart or reload). |
| 234 |
action = "Reload" |
| 235 |
while usr not in ['y', 'n']: |
| 236 |
usr = _input( |
| 237 |
cparse(f'<bold>{action} anyway?</bold> [y/n]: ') |
| 238 |
).lower() |
| 239 |
if usr == 'n': |
| 240 |
return False |
| 241 |
|
| 242 |
# Run "cylc reload" (if workflow is running or paused) |
| 243 |
if workflow_running: |
| 244 |
log_subcommand('reload', workflow_id) |
| 245 |
await cylc_reload(options, workflow_id) |
| 246 |
return True |
| 247 |
|
| 248 |
# Run "cylc play" (if workflow is stopped) |
| 249 |
else: |
| 250 |
set_timestamps(LOG, options.log_timestamp) |
| 251 |
cleanup_sysargv( |
| 252 |
'play', |
| 253 |
unparsed_wid, |
| 254 |
options, |
| 255 |
compound_script_opts=[*VR_OPTIONS, VALIDATE_AGAINST_SOURCE_OPTION], |
| 256 |
script_opts=(*PLAY_OPTIONS, *parser.get_std_options()), |
| 257 |
source='', # Intentionally blank |
| 258 |
) |
| 259 |
|
| 260 |
log_subcommand(*sys.argv[1:]) |
| 261 |
return workflow_id |