| 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 check-versions [OPTIONS] ARGS |
| 20 |
|
| 21 |
Check that Cylc versions match on different platforms. |
| 22 |
|
| 23 |
Check that the remote versions of Cylc invoked on each of the platforms used by |
| 24 |
a workflow matches the version this script is run with. |
| 25 |
|
| 26 |
Note: |
| 27 |
Cylc supports multiple parallel installations at different versions via the |
| 28 |
Cylc wrapper script. For more information see the installation section of the |
| 29 |
documentation. |
| 30 |
|
| 31 |
Use -v/--verbose to see the command invoked to determine the remote version |
| 32 |
(all remote Cylc command invocations will be of the same form, which may be |
| 33 |
site dependent -- see cylc global config documentation. |
| 34 |
""" |
| 35 |
|
| 36 |
import sys |
| 37 |
from typing import TYPE_CHECKING |
| 38 |
|
| 39 |
import cylc.flow.flags |
| 40 |
from cylc.flow.option_parsers import ( |
| 41 |
WORKFLOW_ID_OR_PATH_ARG_DOC, |
| 42 |
CylcOptionParser as COP, |
| 43 |
) |
| 44 |
from cylc.flow.cylc_subproc import procopen, PIPE, DEVNULL |
| 45 |
from cylc.flow import __version__ as CYLC_VERSION |
| 46 |
from cylc.flow.config import WorkflowConfig |
| 47 |
from cylc.flow.exceptions import NoHostsError |
| 48 |
from cylc.flow.id_cli import parse_id |
| 49 |
from cylc.flow.platforms import get_platform, get_host_from_platform |
| 50 |
from cylc.flow.remote import construct_ssh_cmd |
| 51 |
from cylc.flow.templatevars import get_template_vars |
| 52 |
from cylc.flow.terminal import cli_function |
| 53 |
|
| 54 |
if TYPE_CHECKING: |
| 55 |
from optparse import Values |
| 56 |
|
| 57 |
|
| 58 |
def get_option_parser(): |
| 59 |
parser = COP( |
| 60 |
__doc__, |
| 61 |
jset=True, |
| 62 |
argdoc=[WORKFLOW_ID_OR_PATH_ARG_DOC], |
| 63 |
) |
| 64 |
|
| 65 |
parser.add_option( |
| 66 |
"-e", "--error", |
| 67 |
help=( |
| 68 |
f"Exit with error status if {CYLC_VERSION} is not available " |
| 69 |
"on all remote platforms." |
| 70 |
), |
| 71 |
action="store_true", default=False, dest="error" |
| 72 |
) |
| 73 |
|
| 74 |
return parser |
| 75 |
|
| 76 |
|
| 77 |
@cli_function(get_option_parser) |
| 78 |
def main(_, options: 'Values', *ids) -> None: |
| 79 |
workflow_id, _, flow_file = parse_id( |
| 80 |
*ids, |
| 81 |
src=True, |
| 82 |
constraint='workflows', |
| 83 |
) |
| 84 |
|
| 85 |
# extract task host platforms from the workflow_id |
| 86 |
config = WorkflowConfig( |
| 87 |
workflow_id, |
| 88 |
flow_file, |
| 89 |
options, |
| 90 |
get_template_vars(options)) |
| 91 |
|
| 92 |
platforms = { |
| 93 |
config.get_config(['runtime', name, 'platform']) |
| 94 |
for name in config.get_namespace_list('all tasks') |
| 95 |
} - {None, 'localhost'} |
| 96 |
|
| 97 |
# When "workflow run hosts" are formalised as "flow platforms" |
| 98 |
# we can substitute `localhost` for this, in the mean time |
| 99 |
# we will have to assume that flow hosts are configured correctly. |
| 100 |
|
| 101 |
if not platforms: |
| 102 |
sys.exit(0) |
| 103 |
|
| 104 |
verbose = cylc.flow.flags.verbosity > 0 |
| 105 |
versions = check_versions(platforms, verbose) |
| 106 |
report_results(platforms, versions, options.error) |
| 107 |
|
| 108 |
|
| 109 |
def check_versions(platforms, verbose): |
| 110 |
# get the cylc version on each platform |
| 111 |
versions = {} |
| 112 |
for platform_name in sorted(platforms): |
| 113 |
platform = get_platform(platform_name) |
| 114 |
try: |
| 115 |
host = get_host_from_platform( |
| 116 |
platform, |
| 117 |
bad_hosts=None |
| 118 |
) |
| 119 |
except NoHostsError: |
| 120 |
print( |
| 121 |
f'Could not connect to {platform["name"]}', |
| 122 |
file=sys.stderr |
| 123 |
) |
| 124 |
continue |
| 125 |
cmd = construct_ssh_cmd( |
| 126 |
['version'], |
| 127 |
platform, |
| 128 |
host |
| 129 |
) |
| 130 |
if verbose: |
| 131 |
print(cmd) |
| 132 |
proc = procopen(cmd, stdin=DEVNULL, stdout=PIPE, stderr=PIPE) |
| 133 |
out, err = proc.communicate() |
| 134 |
out = out.decode() |
| 135 |
err = err.decode() |
| 136 |
if proc.wait() == 0: |
| 137 |
if verbose: |
| 138 |
print(" %s" % out) |
| 139 |
versions[platform_name] = out.strip() |
| 140 |
else: |
| 141 |
versions[platform_name] = f'ERROR: {err.strip()}' |
| 142 |
return versions |
| 143 |
|
| 144 |
|
| 145 |
def report_results(platforms, versions, exit_error): |
| 146 |
# report results |
| 147 |
max_len = max((len(platform_name) for platform_name in platforms)) |
| 148 |
print(f'{"platform".rjust(max_len)}: cylc version') |
| 149 |
print('-' * (max_len + 14)) |
| 150 |
for platform_name, result in versions.items(): |
| 151 |
print(f'{platform_name.rjust(max_len)}: {result}') |
| 152 |
if all((version == CYLC_VERSION for version in versions.values())): |
| 153 |
ret_code = 0 |
| 154 |
elif exit_error: |
| 155 |
ret_code = 1 |
| 156 |
else: |
| 157 |
ret_code = 0 |
| 158 |
sys.exit(ret_code) |