Skip to content

Commit 5b273db

Browse files
committed
Refactor cloudbase-init extraction
1 parent b1cbc90 commit 5b273db

2 files changed

Lines changed: 32 additions & 20 deletions

File tree

coriolis/osmorphing/windows.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,36 @@
186186
Invoke-Main $NICS_INFO $IPS_INFO
187187
""" # noqa
188188

189+
EXTRACT_TEMPLATE = """
190+
function Extract-Path {
191+
[CmdletBinding()]
192+
Param(
193+
[Parameter(Mandatory=$true)]
194+
[String]$source,
195+
196+
[Parameter(Mandatory=$true)]
197+
[String]$destination
198+
)
199+
try {
200+
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
201+
$zip = [System.IO.Compression.ZipFile]::OpenRead($source)
202+
foreach ($entry in $zip.Entries) {
203+
$targetPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($destination, $entry.FullName))
204+
if ($entry.FullName -match '[\\/]$') {
205+
if (!(Test-Path $targetPath)) { New-Item -ItemType Directory -Force -Path $targetPath | Out-Null }
206+
} else {
207+
$parentPath = Split-Path $targetPath
208+
if (!(Test-Path $parentPath)) { New-Item -ItemType Directory -Force -Path $parentPath | Out-Null }
209+
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $targetPath, $true)
210+
}
211+
}
212+
} finally {
213+
if ($zip) { $zip.Dispose() }
214+
}
215+
}
216+
Extract-Path '%(source)s' '%(destination)s'
217+
""" # noqa
218+
189219

190220
class BaseWindowsMorphingTools(base.BaseOSMorphingTools):
191221

@@ -303,16 +333,7 @@ def _expand_archive(self, path, destination, overwrite=True):
303333
"rm -recurse -force %s" % destination)
304334

305335
self._conn.exec_ps_command(
306-
"if(([System.Management.Automation.PSTypeName]"
307-
"'System.IO.Compression.ZipFile').Type -or "
308-
"[System.Reflection.Assembly]::LoadWithPartialName("
309-
"'System.IO.Compression.FileSystem')) {"
310-
"[System.IO.Compression.ZipFile]::ExtractToDirectory('%(path)s', "
311-
"'%(destination)s')} else {mkdir -Force '%(destination)s'; "
312-
"$shell = New-Object -ComObject Shell.Application;"
313-
"$shell.Namespace('%(destination)s').copyhere(($shell.NameSpace("
314-
"'%(path)s')).items())}" %
315-
{"path": path, "destination": destination},
336+
EXTRACT_TEMPLATE % {"source": path, "destination": destination},
316337
ignore_stdout=True)
317338

318339
def _set_service_start_mode(self, key_name, service_name, start_mode):

coriolis/tests/osmorphing/test_windows.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,7 @@ def test__expand_archive_remove_destination(self):
231231
self.conn.exec_ps_command.assert_has_calls([
232232
mock.call("rm -recurse -force %s" % destination),
233233
mock.call(
234-
"if(([System.Management.Automation.PSTypeName]"
235-
"'System.IO.Compression.ZipFile').Type -or "
236-
"[System.Reflection.Assembly]::LoadWithPartialName("
237-
"'System.IO.Compression.FileSystem')) {"
238-
"[System.IO.Compression.ZipFile]::ExtractToDirectory("
239-
"'%(path)s', '%(destination)s')} else {mkdir -Force "
240-
"'%(destination)s'; $shell = New-Object -ComObject "
241-
"Shell.Application;$shell.Namespace("
242-
"'%(destination)s').copyhere(($shell.NameSpace("
243-
"'%(path)s')).items())}" %
234+
windows.EXTRACT_TEMPLATE %
244235
{"path": mock.sentinel.archive_path,
245236
"destination": destination},
246237
ignore_stdout=True)

0 commit comments

Comments
 (0)