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 @@ -387,6 +389,19 @@ def _delete_startup_entry(self, key_name, service_name):
ignore_stdout=True,
)

def _delete_uninstall_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 }"
% {"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_uninstall_entry(self):
self.morphing_tools._delete_uninstall_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