Skip to content

Commit b099d32

Browse files
authored
Merge pull request #160 from victormlg/ssh_user_errors
CFE-4601: Remove errors when connection tries usernames
2 parents e775dab + 44b469a commit b099d32

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

cf_remote/ssh.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from cf_remote import paths
1212
from cf_remote.utils import whoami, read_json
1313
from cf_remote.aramid import ExecutionResult
14-
from cf_remote.paths import SSH_CONFIG_FPATH, SSH_CONFIGS_JSON_FPATH
14+
from cf_remote.paths import SSH_CONFIG_FPATH, SSH_CONFIGS_JSON_FPATH, CLOUD_STATE_FPATH
1515

1616

1717
class LocalConnection:
@@ -77,8 +77,8 @@ def __init__(self, host, user, connect_kwargs=None, port=aramid._DEFAULT_SSH_POR
7777
% " ".join(control_master_args)
7878
)
7979
self._ssh_control_master = subprocess.Popen(
80-
control_master_args,
81-
) # stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
80+
control_master_args, stderr=subprocess.DEVNULL # stdout=subprocess.DEVNULL,
81+
)
8282

8383
self.needs_sudo = self.run("echo $UID", hide=True).stdout.strip() != "0"
8484
log.debug("Connection initialized")
@@ -129,17 +129,34 @@ def _build_ssh_config():
129129
f.write(config)
130130

131131

132+
def host_is_vagrant(host):
133+
134+
config = read_json(CLOUD_STATE_FPATH)
135+
136+
if config is None:
137+
return False
138+
139+
for group in config.values():
140+
for curr_host in group.keys():
141+
if curr_host == host and group["meta"]["provider"] == "vagrant":
142+
return True
143+
144+
return False
145+
146+
132147
def connect(host, users=None):
133148
log.debug("Connecting to '%s'" % host)
134149
log.debug("users= '%s'" % users)
135150

136-
log.debug("Building config file")
137-
138151
parts = urlparse("ssh://%s" % host)
139152
host = parts.hostname
140153
if not users and parts.username:
141154
users = [parts.username]
142155
port = parts.port or aramid._DEFAULT_SSH_PORT
156+
157+
if host_is_vagrant(host):
158+
users = ["vagrant"]
159+
143160
if not users:
144161
users = [
145162
"Administrator",
@@ -179,6 +196,7 @@ def connect(host, users=None):
179196
# and connection should be a keyword argument with default None
180197
# Uses a context manager (with) to ensure connections are closed
181198
def auto_connect(func):
199+
log.debug("Building config file")
182200
_build_ssh_config()
183201

184202
def connect_wrapper(host, *args, **kwargs):

0 commit comments

Comments
 (0)