Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions coriolis/osmorphing/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
SERVICES_PATH_FORMAT = "HKLM:\\%s\\ControlSet001\\Services"
SERVICE_PATH_FORMAT = "HKLM:\\%s\\ControlSet001\\Services\\%s"
RUN_PATH_FORMAT = "HKLM:\\%s\\\Microsoft\\Windows\\CurrentVersion\\Run"
UNINSTALL_PATH_FORMAT = \
"HKLM:\\%s\\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*"
CLOUDBASEINIT_SERVICE_NAME = "cloudbase-init"
CLOUDBASE_INIT_DEFAULT_PLUGINS = [
'cloudbaseinit.plugins.common.mtu.MTUPlugin',
Expand Down Expand Up @@ -350,6 +352,19 @@ def _delete_startup_entry(self, key_name, service_name):
ignore_stdout=True,
)

def _delete_unistall_entry(self, key_name, service_name):
registry_path = UNINSTALL_PATH_FORMAT % key_name
LOG.info("Deleting uninstall entry: %s", service_name)

self._conn.exec_ps_command(
"$ErrorActionPreference = 'Stop';"
"Get-ItemProperty '%(path)s' | "
"Where-Object { $_.DisplayName -like '%(entry)s' } | "
"ForEach-Object { Remove-Item -Path $_.PSPath -Force }"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the entry's not found? Won't this error out and cancel the whole process?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no entry found by "Where-Object", it doesn't return anything and what's in the "ForEach-Object" block doesn't execute. It does not throw any errors or stop, at least in powershell 5.1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double-checked this, yep, looks good. Thank you!

% {"path": registry_path, "entry": service_name},
ignore_stdout=True,
)

def run_user_script(self, user_script):
if len(user_script) == 0:
return
Expand Down
34 changes: 34 additions & 0 deletions coriolis/tests/osmorphing/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,40 @@ def test__create_service(self):
self.conn.exec_ps_command.assert_called_once_with(
expected_commands, ignore_stdout=True)

def test__delete_startup_entry(self):
self.morphing_tools._delete_startup_entry(
"mock_key_name", "mock_service_name")

registry_path = ("HKLM:\\mock_key_name\\\Microsoft\\"
"Windows\\CurrentVersion\\Run")

expected_commands = (
"$ErrorActionPreference = 'Stop';"
"Remove-ItemProperty -Path "
f"'{registry_path}' "
"-Name 'mock_service_name' -Force"
)

self.conn.exec_ps_command.assert_called_once_with(
expected_commands, ignore_stdout=True)

def test__delete_unistall_entry(self):
self.morphing_tools._delete_unistall_entry(
"mock_key_name", "mock_service_name")

registry_path = ("HKLM:\\mock_key_name\\\Microsoft\\"
"Windows\\CurrentVersion\\Uninstall\\*")

expected_commands = (
"$ErrorActionPreference = 'Stop';"
f"Get-ItemProperty '{registry_path}' | "
"Where-Object { $_.DisplayName -like 'mock_service_name' } | "
"ForEach-Object { Remove-Item -Path $_.PSPath -Force }"
)

self.conn.exec_ps_command.assert_called_once_with(
expected_commands, ignore_stdout=True)

@mock.patch.object(windows.utils, 'write_winrm_file')
def test_run_user_script(self, mock_write_winrm_file):
user_script = 'echo "Hello, World!"'
Expand Down