Skip to content

Commit 99e3106

Browse files
authored
Merge pull request #8 from rod7760/bugfix/winrm-connector-not-recieving-cli-args
Fixed cli args not being passed to winrm connector
2 parents 48b0019 + 14425f5 commit 99e3106

2 files changed

Lines changed: 67 additions & 53 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
__pycache__/
22
*.py[cod]
33
.venv
4+
*.log

pyinfra_windows/connectors/winrm.py

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This connector is in alpha and may change in future releases.
44
55
Some Windows facts and Windows operations work but this is to be considered
6-
experimental. For now, only ``winrm_username`` and ``winrm_password`` is
6+
experimental. For now, only ``username`` and ``password`` is
77
being used. There are other methods for authentication, but they have not yet
88
been added/experimented with.
99
@@ -14,63 +14,61 @@
1414
.. code:: python
1515
1616
# Get the windows_home fact
17-
pyinfra @winrm/192.168.3.232 --winrm-username vagrant \\
18-
--winrm-password vagrant --winrm-port 5985 -vv --debug fact windows_home
17+
pyinfra @winrm/192.168.3.232 --user vagrant \\
18+
--password vagrant --port 5985 -vv --debug fact windows_home
1919
2020
# Create a directory
21-
pyinfra @winrm/192.168.3.232 --winrm-username vagrant \\
22-
--winrm-password vagrant --winrm-port 5985 windows_files.windows_directory 'c:\temp'
21+
pyinfra @winrm/192.168.3.232 --user vagrant \\
22+
--password vagrant --port 5985 windows_files.windows_directory 'c:\temp'
2323
2424
# Run a powershell command ('ps' is the default shell-executable for the winrm connector)
25-
pyinfra @winrm/192.168.3.232 --winrm-username vagrant \\
26-
--winrm-password vagrant --winrm-port 5985 exec -- write-host hello
25+
pyinfra @winrm/192.168.3.232 --user vagrant \\
26+
--password vagrant --port 5985 exec -- write-host hello
2727
2828
# Run a command using the command prompt:
29-
pyinfra @winrm/192.168.3.232 --winrm-username vagrant \\
30-
--winrm-password vagrant --winrm-port 5985 --shell-executable cmd exec -- date /T
29+
pyinfra @winrm/192.168.3.232 --user vagrant \\
30+
--password vagrant --port 5985 --shell-executable cmd exec -- date /T
3131
3232
# Run a command using the winrm ntlm transport
33-
pyinfra @winrm/192.168.3.232 --winrm-username vagrant \\
34-
--winrm-password vagrant --winrm-port 5985 --winrm-transport ntlm exec -- hostname
33+
pyinfra @winrm/192.168.3.232 --user vagrant \\
34+
--password vagrant --port 5985 --winrm-transport ntlm exec -- hostname
3535
"""
3636

3737
import base64
3838
import ntpath
3939

4040
import click
41+
from typing_extensions import TypedDict
4142

4243
from pyinfra import logger
4344
from pyinfra.api.exceptions import ConnectError, PyinfraError
4445
from pyinfra.api.util import get_file_io, memoize, sha1_hash
4546

46-
from pyinfra.connectors.base import BaseConnector
47+
from pyinfra.connectors.base import BaseConnector, DataMeta
4748
from pyinfra.connectors.util import read_output_buffers
4849
from .pyinfrawinrmsession import PyinfraWinrmSession
4950
from .util import make_win_command
5051

5152

52-
class DataKeys:
53-
hostname = "WinRM hostname to connect to"
54-
port = "WinRM port to connect to"
55-
user = "WinRM username"
56-
password = "WinRM password"
57-
transport = "WinRM transport (default: ``plaintext``)"
58-
read_timeout_sec = "Read timeout in seconds (default: ``30``)"
59-
operation_timeout_sec = "Operation timeout in seconds (default: ``20``)"
53+
class ConnectorData(TypedDict):
54+
winrm_hostname: str
55+
port: int
56+
winrm_user: str
57+
password: str
58+
transport: str
59+
read_timeout_sec: int
60+
operation_timeout_sec: int
6061

6162

62-
# TODO not sure where this logic should live
63-
def _make_keys(keys_prefix, cls):
64-
class Keys:
65-
pass
66-
67-
for key in cls.__dict__:
68-
if not key.startswith("_"):
69-
setattr(Keys, key, f"{keys_prefix}_{key}")
70-
71-
return Keys
72-
73-
DATA_KEYS = _make_keys("winrm", DataKeys)
63+
connector_data_meta: dict[str, DataMeta] = {
64+
"winrm_hostname": DataMeta("WinRM hostname to connect to"),
65+
"port": DataMeta("WinRM port to connect to"),
66+
"winrm_user": DataMeta("WinrRM username"),
67+
"password": DataMeta("WinRM password"),
68+
"transport": DataMeta("WinRM transport"),
69+
"read_timeout_sec": DataMeta("Read timeout in seconds"),
70+
"operation_timeout_sec": DataMeta("Operation timeout in seconds"),
71+
}
7472

7573

7674
def _raise_connect_error(host, message, data):
@@ -85,19 +83,19 @@ def show_warning():
8583

8684
def _make_winrm_kwargs(state, host):
8785
kwargs = {}
88-
8986
for key, value in (
90-
("username", host.data.get(DATA_KEYS.user)),
91-
("password", host.data.get(DATA_KEYS.password)),
92-
("winrm_port", int(host.data.get(DATA_KEYS.port, 0))),
93-
("winrm_transport", host.data.get(DATA_KEYS.transport, "plaintext")),
87+
("hostname", host.data.get("winrm_hostname")),
88+
("username", host.data.get("ssh_user")),
89+
("password", host.data.get("ssh_password")),
90+
("port", int(host.data.get("port", 0))),
91+
("winrm_transport", host.data.get("transport", "plaintext")),
9492
(
9593
"winrm_read_timeout_sec",
96-
host.data.get(DATA_KEYS.read_timeout_sec, 30),
94+
host.data.get("read_timeout_sec", 30),
9795
),
9896
(
9997
"winrm_operation_timeout_sec",
100-
host.data.get(DATA_KEYS.operation_timeout_sec, 20),
98+
host.data.get("operation_timeout_sec", 20),
10199
),
102100
):
103101
if value:
@@ -116,6 +114,10 @@ class WinRMConnector(BaseConnector):
116114

117115
session = None
118116

117+
data: ConnectorData
118+
data_cls = ConnectorData
119+
data_meta = connector_data_meta
120+
119121
@staticmethod
120122
def make_names_data(hostname):
121123
show_warning()
@@ -126,23 +128,23 @@ def connect(self):
126128
"""
127129
Connect to a single host. Returns the winrm Session if successful.
128130
"""
129-
130131
kwargs = _make_winrm_kwargs(self.state, self.host)
131132
logger.debug("Connecting to: %s (%s)", self.host.name, kwargs)
132133

133134
# Hostname can be provided via winrm config (alias), data, or the hosts name
134135
hostname = kwargs.pop(
135136
"hostname",
136-
self.host.data.get(DATA_KEYS.hostname, self.host.name),
137137
)
138138

139139
try:
140140
# Create new session
141-
host_and_port = "{}:{}".format(hostname, self.host.data.get(DATA_KEYS.port))
142-
logger.debug("host_and_port: %s", host_and_port)
141+
port = self.host.data.get("port", 5986)
142+
winrm_target = "{}".format(hostname)
143+
logger.debug("winrm_target: %s", winrm_target)
144+
logger.debug("winrm_user: %s", kwargs["username"])
143145

144146
session = PyinfraWinrmSession(
145-
host_and_port,
147+
winrm_target,
146148
auth=(
147149
kwargs["username"],
148150
kwargs["password"],
@@ -175,7 +177,7 @@ def run_shell_command(
175177
success_exit_codes=None,
176178
print_output=False,
177179
print_input=False,
178-
#return_combined_output=False, # TODO now always return combined
180+
# return_combined_output=False, # TODO now always return combined
179181
shell_executable=None,
180182
**ignored_command_kwargs,
181183
):
@@ -251,16 +253,22 @@ def run_shell_command(
251253
logger.debug("Command exit status: %s", status)
252254

253255
combined_output = read_output_buffers(
254-
std_out, std_err,
255-
timeout=None, # TODO need to take timeout in
256-
print_output=print_output,
257-
print_prefix=self.host.print_prefix,)
256+
std_out,
257+
std_err,
258+
timeout=None, # TODO need to take timeout in
259+
print_output=print_output,
260+
print_prefix=self.host.print_prefix,
261+
)
258262

259263
return status, combined_output
260264

261-
262265
def get_file(
263-
state, host, remote_filename, filename_or_io, remote_temp_filename=None, **command_kwargs
266+
state,
267+
host,
268+
remote_filename,
269+
filename_or_io,
270+
remote_temp_filename=None,
271+
**command_kwargs,
264272
):
265273
raise PyinfraError("Not implemented")
266274

@@ -313,9 +321,14 @@ def put_file(
313321
return False
314322

315323
# Execute run_shell_command w/sudo and/or su_user
316-
command = "Move-Item -Path {0} -Destination {1} -Force".format(temp_file, remote_filename)
324+
command = "Move-Item -Path {0} -Destination {1} -Force".format(
325+
temp_file, remote_filename
326+
)
317327
status, _, stderr = self.run_shell_command(
318-
command, print_output=print_output, print_input=print_input, **command_kwargs
328+
command,
329+
print_output=print_output,
330+
print_input=print_input,
331+
**command_kwargs,
319332
)
320333

321334
if status is False:

0 commit comments

Comments
 (0)