Skip to content

Commit b088aed

Browse files
authored
Make remote_run Windows compatible (#803)
1 parent 5b8af43 commit b088aed

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

automation/script/remote_run.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import copy
1010
from datetime import datetime
1111
from script.script_utils import *
12+
import platform
1213

1314

1415
def remote_run(self_module, i):
@@ -86,6 +87,13 @@ def remote_run(self_module, i):
8687

8788
run_cmds = []
8889
remote_mlc_python_venv = i.get('remote_python_venv', 'mlcflow')
90+
91+
# Determine if the local system is Windows to adjust command formatting
92+
is_windows = platform.system() == 'Windows'
93+
94+
# Note: The remote activation command uses Unix syntax because we're SSHing into a (likely) Unix server
95+
# Even if we're running from Windows locally, the remote commands execute
96+
# on the remote server
8997
run_cmds.append(f". {remote_mlc_python_venv}/bin/activate")
9098
if i.get('remote_pull_mlc_repos', False):
9199
run_cmds.append("mlc pull repo")
@@ -96,10 +104,9 @@ def remote_run(self_module, i):
96104
for key in env_keys_to_copy:
97105
if key in env and os.path.exists(env[key]):
98106
files_to_copy.append(env[key])
99-
remote_env[key] = os.path.join(
100-
"mlc-remote-artifacts",
101-
os.path.basename(
102-
env[key]))
107+
# Use forward slashes for remote path (Unix/Linux servers)
108+
remote_env[key] = "mlc-remote-artifacts/" + \
109+
os.path.basename(env[key])
103110

104111
for k, value in input_mapping.items():
105112
if value == key and k in run_input:
@@ -203,9 +210,14 @@ def regenerate_script_cmd(i):
203210
value = env[key]
204211

205212
# Check if the value is a string containing the specified paths
213+
# Use both forward and backward slashes for Windows compatibility
206214
if isinstance(value, str) and (
207215
os.path.join("local", "cache", "") in value or
216+
"local/cache/" in value or
217+
"local\\cache\\" in value or
208218
os.path.join("MLC", "repos", "") in value or
219+
"MLC/repos/" in value or
220+
"MLC\\repos\\" in value or
209221
"<<<" in value
210222
):
211223
del env[key]
@@ -217,8 +229,12 @@ def regenerate_script_cmd(i):
217229
val for val in value
218230
if isinstance(val, str) and (
219231
os.path.join("local", "cache", "") in val or
232+
"local/cache/" in val or
233+
"local\\cache\\" in val or
220234
os.path.join("MLC", "repos", "") in val or
221-
"<<<" in value
235+
"MLC/repos/" in val or
236+
"MLC\\repos\\" in val or
237+
"<<<" in val
222238
)
223239
]
224240

script/remote-run-commands/customize.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from mlc import utils
22
import os
33
import subprocess
4+
import platform
45

56

67
def preprocess(i):
@@ -11,11 +12,17 @@ def preprocess(i):
1112

1213
cmd_string = ''
1314

14-
# pre_run_cmds = env.get('MLC_SSH_PRE_RUN_CMDS', ['source $HOME/cm/bin/activate'])
15+
is_windows = os_info['platform'] == 'windows'
16+
17+
# pre_run_cmds = env.get('MLC_SSH_PRE_RUN_CMDS', ['source $HOME/mlcflow/bin/activate'])
1518
pre_run_cmds = env.get('MLC_SSH_PRE_RUN_CMDS', [])
1619

1720
files_to_copy = env.get('MLC_SSH_FILES_TO_COPY', [])
1821

22+
if "<<<HOME>>>" in env.get('MLC_SSH_KEY_FILE', ''):
23+
env['MLC_SSH_KEY_FILE'] = env['MLC_SSH_KEY_FILE'].replace(
24+
"<<<HOME>>>", os.path.expanduser("~"))
25+
1926
run_cmds = env.get('MLC_SSH_RUN_COMMANDS', [])
2027

2128
run_cmds = pre_run_cmds + run_cmds
@@ -26,8 +33,12 @@ def preprocess(i):
2633
cmd = cmd.replace(";;", ",")
2734
run_cmds[i] = cmd
2835

36+
# Use semicolon for Unix-like systems, the remote server will handle it
2937
cmd_string += " ; ".join(run_cmds)
30-
user = env.get('MLC_SSH_USER', os.environ.get('USER'))
38+
39+
# Get username - on Windows, USERNAME is the env var, not USER
40+
user = env.get('MLC_SSH_USER', os.environ.get(
41+
'USER') or os.environ.get('USERNAME'))
3142
password = env.get('MLC_SSH_PASSWORD', None)
3243
host = env.get('MLC_SSH_HOST')
3344
port = env.get('MLC_SSH_PORT', '22')
@@ -40,29 +51,47 @@ def preprocess(i):
4051
ssh_cmd = ["ssh", "-p", port]
4152

4253
if env.get("MLC_SSH_SKIP_HOST_VERIFY"):
54+
# Use NUL on Windows, /dev/null on Unix
55+
null_device = "NUL" if is_windows else "/dev/null"
4356
ssh_cmd += ["-o", "StrictHostKeyChecking=no",
44-
"-o", "UserKnownHostsFile=/dev/null"]
57+
"-o", f"UserKnownHostsFile={null_device}"]
4558

4659
key_file = env.get("MLC_SSH_KEY_FILE")
4760
if key_file:
4861
ssh_cmd += ["-i", key_file]
4962

5063
ssh_cmd_str = " ".join(ssh_cmd)
5164

65+
# Use double quotes on Windows, single quotes on Unix for better
66+
# compatibility
67+
quote_char = '"' if is_windows else "'"
5268
ssh_run_command = ssh_cmd_str + " " + user + "@" + host + \
53-
password_string + " '" + cmd_string + "'"
69+
password_string + " " + quote_char + cmd_string + quote_char
5470
env['MLC_SSH_CMD'] = ssh_run_command
5571

56-
# ---- Use sshpass if password is provided ----
72+
# ---- Use sshpass if password is provided (only on Unix-like systems) ----
5773
rsync_base = ["rsync", "-avz"]
5874

59-
if password:
75+
if password and not is_windows:
6076
rsync_base = ["sshpass", "-p", password] + rsync_base
6177

62-
target_directory = env['MLC_SSH_TARGET_COPY_DIRECTORY']
78+
target_directory = env.get('MLC_SSH_TARGET_COPY_DIRECTORY', '')
6379

6480
# ---- Execute copy commands ----
6581
for file in files_to_copy:
82+
# Check if rsync is available
83+
rsync_available = True
84+
try:
85+
subprocess.run(["rsync", "--version"],
86+
capture_output=True, check=True)
87+
except (subprocess.CalledProcessError, FileNotFoundError):
88+
rsync_available = False
89+
90+
if not rsync_available:
91+
print(f"⚠️ rsync not found. Skipping file copy for {file}")
92+
print(" On Windows, install rsync via WSL, Cygwin, or use Git Bash")
93+
continue
94+
6695
cmd = [
6796
"rsync",
6897
"-avz",

script/remote-run-commands/run.bat

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
rem native script
1+
@echo off
2+
set cmd=%MLC_SSH_CMD%
3+
echo %cmd%
4+
%cmd%

0 commit comments

Comments
 (0)