-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.py
More file actions
198 lines (161 loc) · 6.02 KB
/
Copy pathrepl.py
File metadata and controls
198 lines (161 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import cmd
from contextlib import ExitStack
import errno
import glob
import os
from pathlib import Path
import sys
import traceback
from urllib.parse import quote, urlencode
import invoke
from tqdm import tqdm
from hosts import get_conn, wait_for_port
class StdinWithCR:
def read(self, i):
a = sys.stdin.read(i)
if a == "\n":
return "\r"
return a
def __getattr__(self, name):
return getattr(sys.stdin, name)
def ssh_shell(virtual_machine_spec):
print("-> Waiting for SSH to become available...")
wait_for_port(virtual_machine_spec, 22)
print("-> Opening ssh session...")
conn = get_conn(virtual_machine_spec)
if virtual_machine_spec["os"] == "windows":
conn.run(
"set TERM=xterm-256color && cmd.exe",
pty=True,
in_stream=StdinWithCR(),
warn=True,
)
else:
conn.run("/bin/bash", pty=True, env={"TERM": "xterm-256color"}, warn=True)
def open_rdp(virtual_machine_spec: dict, rdp_port: int, verbose: bool):
if sys.platform == "darwin":
print("-> Waiting for RDP to become available...")
wait_for_port(virtual_machine_spec, 3389)
password = virtual_machine_spec["password"]
user = virtual_machine_spec["user"]
print(f"-> Launching RDP client... (password is '{password}')")
args = {
"full address": f"s:localhost:{rdp_port}",
"username": f"s:{user}",
"use redirection server name": "i:1", # fixes connection to ubuntu rdp server
"screen mode id": "i:1" # windowed mode
}
invoke.run(
f"open 'rdp://{urlencode(args, quote_via=quote)}'",
hide="out",
)
else:
print("-> Auto open RDP is currently only supported on macOS")
class Repl(cmd.Cmd):
intro = "Welcome to the testfarm vm shell. Type help or ? to list commands.\n"
prompt = "(vm) "
def __init__(
self,
*,
virtual_machine_spec: dict,
rdp_port: int,
verbose: bool,
exit_stack: ExitStack,
):
super().__init__()
self.virtual_machine_spec = virtual_machine_spec
self.rdp_port = rdp_port
self.verbose = verbose
self.exit_stack = exit_stack
def do_expose(self, arg: str):
"expose a local port to the vm: expose local_port [remote_port]"
args = arg.split()
if len(args) < 1:
print("missing argument: local_port")
return
try:
local_port = int(args[0])
remote_port = int(args[1]) if len(args) > 1 else local_port
self.exit_stack.enter_context(
get_conn(self.virtual_machine_spec).forward_remote(
local_port=local_port, remote_port=remote_port
)
)
except Exception:
traceback.print_exc()
def do_rdp(self, arg: str):
"open rdp client"
try:
open_rdp(
self.virtual_machine_spec, rdp_port=self.rdp_port, verbose=self.verbose
)
except Exception:
traceback.print_exc()
def do_ssh(self, arg: str):
"run single command via ssh or spawn interactive ssh session: ssh [cmd]"
try:
if len(arg) > 0:
get_conn(self.virtual_machine_spec).run(arg, warn=True)
else:
ssh_shell(self.virtual_machine_spec)
except Exception:
traceback.print_exc()
def do_put(self, arg: str):
"copy file(s) to vm: put [-r] local_path remote_path"
args = arg.split()
recursive = "-r" in args
positional_args = [arg for arg in args if not arg.startswith("-")]
def remote_home():
conn = get_conn(self.virtual_machine_spec)
if self.virtual_machine_spec["os"] == "windows":
return Path(conn.run("echo %HOME%", hide="both").stdout.strip().replace("\\","/"))
else:
return Path(conn.run("echo -n $HOME", hide="both").stdout)
arg_local = positional_args[0]
arg_remote = positional_args[1]
if arg_remote.startswith("~/"):
arg_remote = remote_home() / arg_remote.removeprefix("~/")
try:
sftp = get_conn(self.virtual_machine_spec).sftp()
def dir_exists(path: Path | str):
if isinstance(path, str):
path = Path(path)
old_cwd = sftp.getcwd()
try:
sftp.chdir(str(path))
return True
except IOError as err:
if err.errno == errno.ENOENT:
return False
raise err
finally:
sftp.chdir(old_cwd)
def mkdir(path: Path | str):
if isinstance(path, str):
path = Path(path)
if not dir_exists(path.parent):
mkdir(path.parent)
if not dir_exists(path):
sftp.mkdir(str(path))
if not dir_exists(arg_remote):
try:
mkdir(Path(arg_remote))
except IOError as err:
if err.errno == errno.EACCES:
print(f"Permission denied: {arg_remote}")
return
raise err
if recursive:
files = glob.glob("**", root_dir=arg_local, recursive=recursive, include_hidden=True)
for file in tqdm(files, unit="files"):
local_path = Path(arg_local) / file
target = Path(arg_remote) / file
if os.path.isdir(local_path):
if not dir_exists(target):
mkdir(target)
else:
sftp.put(local_path, str(target), confirm=False)
else:
sftp.put(arg_local, str(arg_remote))
except Exception:
traceback.print_exc()