Skip to content
This repository was archived by the owner on Mar 11, 2020. It is now read-only.

Commit 81b9ab2

Browse files
committed
Add support for running on local machine
Signed-off-by: Elton Stoneman <elton@sixeyed.com>
1 parent 7dff54b commit 81b9ab2

File tree

8 files changed

+92
-60
lines changed

8 files changed

+92
-60
lines changed

Functions/Private/Artifacts/IIS/Discover_IIS.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ $ManifestResult = @{
4343
# Windows 5.2 -> Server 2003; use MetaBase discovery for IIS 6:
4444
if ($ImageWindowsVersion -eq '5.2') {
4545
Write-Verbose -Message "Checking IIS MetaBase config for Windows Version: $ImageWindowsVersion"
46-
$ManifestResult = GetManifestFromMetabase -OutputPath $OutputPath -MountPath $Mount.Path -ImageWindowsVersion $ImageWindowsVersion -ArtifactParam $ArtifactParam
46+
$ManifestResult = GetManifestFromMetabase -OutputPath $OutputPath -MountPath $MountPath -ImageWindowsVersion $ImageWindowsVersion -ArtifactParam $ArtifactParam
4747
}
4848
else {
4949
# Use Application Host discovery for IIS 7 onwards:
5050
Write-Verbose -Message "Checking IIS ApplicationHost config for Windows Version: $ImageWindowsVersion"
51-
$ManifestResult = GetManifestFromApplicationHost -OutputPath $OutputPath -MountPath $Mount.Path -ImageWindowsVersion $ImageWindowsVersion -ArtifactParam $ArtifactParam
51+
$ManifestResult = GetManifestFromApplicationHost -OutputPath $OutputPath -MountPath $MountPath -ImageWindowsVersion $ImageWindowsVersion -ArtifactParam $ArtifactParam
5252
}
5353

5454
if ($ManifestResult.Status -eq 'Present'){

Functions/Private/Artifacts/IIS/Generate_IIS.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ Optional - one or more Website names to include in the output.
1313
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments",'')]
1414
[CmdletBinding()]
1515
param (
16+
[Parameter(Mandatory = $true)]
17+
[string] $MountPath,
18+
1619
[Parameter(Mandatory = $true)]
1720
[string] $ManifestPath,
1821

1922
[Parameter(Mandatory = $false)]
20-
[string[]] $ArtifactParam
23+
[string[]] $ArtifactParam
2124
)
2225

2326
function IncludePath([string[]] $pathParts) {
@@ -40,19 +43,23 @@ function ProcessDirectory([System.Text.StringBuilder] $DirectoryBuilder,
4043
[System.Text.StringBuilder] $CopyBuilder,
4144
[string] $SourcePath,
4245
[bool] $FirstDirectory) {
43-
Write-Verbose "Processing source directory: $SourcePath"
46+
Write-Verbose "Processing source directory: $SourcePath"
47+
$targetPath = $SourcePath.Substring(2) # skip the local drive letter
4448
if ($FirstDirectory -eq $true) {
45-
$newPath = "RUN New-Item -Path 'C:$SourcePath' -Type Directory -Force; ``"
49+
$newPath = "RUN New-Item -Path 'C:$targetPath' -Type Directory -Force; ``"
4650
}
4751
else {
48-
$newPath = " New-Item -Path 'C:$SourcePath' -Type Directory -Force; ``"
52+
$newPath = " New-Item -Path 'C:$targetPath' -Type Directory -Force; ``"
4953
}
5054
$null = $DirectoryBuilder.AppendLine($newPath)
5155

52-
$copy = 'COPY ["{0}", "{1}"]' -f (Split-Path $SourcePath -Leaf),($sourcePath -Replace "\\","/")
56+
$copy = 'COPY ["{0}", "{1}"]' -f (Split-Path $SourcePath -Leaf),($targetPath -Replace "\\","/")
5357
$null = $CopyBuilder.AppendLine($copy)
5458

55-
$fullSourcePath = $Mount.Path + $SourcePath
59+
$fullSourcePath = $SourcePath
60+
if ($Mount) {
61+
$fullSourcePath = $MountPath + $targetPath
62+
}
5663
Copy-Item $fullSourcePath $ManifestPath -Recurse -Force
5764
}
5865

@@ -66,7 +73,7 @@ $Artifact = Get-Content -Path $Manifest -Raw | ConvertFrom-Json
6673

6774
if ($Artifact.Status -eq 'Present') {
6875
Write-Verbose ('Copying {0} configuration files' -f $ArtifactName)
69-
$ConfigPath = $Mount.Path + "\" + "Windows\System32\inetsrv\config"
76+
$ConfigPath = $MountPath + "\" + "Windows\System32\inetsrv\config"
7077
if (Test-Path -Path $ConfigPath) {
7178
Copy-Item $ConfigPath $ManifestPath -Recurse
7279
}

Functions/Private/Artifacts/IIS/GetManifestFromApplicationHost.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ if (Test-Path -Path $ApplicationHostPath) {
5656
ForEach ($virtualDirectory in $application.virtualDirectory){
5757
$virtualDirectories.add([PSCustomObject]@{
5858
Path = $virtualDirectory.path;
59-
PhysicalPath = $virtualDirectory.physicalPath.replace('%SystemDrive%\','\').replace('C:\','\').Replace('c:\','\');
59+
PhysicalPath = $virtualDirectory.physicalPath.replace('%SystemDrive%','C:'); # TODO - resolve for mount & local
6060
}) | Out-Null
6161
}
6262
$applications.add([PSCustomObject]@{
@@ -113,7 +113,12 @@ if (Test-Path -Path $ApplicationHostPath) {
113113

114114
#feature selection not valid for 2008 and below:
115115
if ([decimal]$ImageWindowsVersion -gt 6.1) {
116-
$WindowsFeatures = Get-WindowsOptionalFeature -Path $Mount.Path
116+
if ($Mount) {
117+
$WindowsFeatures = Get-WindowsOptionalFeature -Path $MountPath
118+
}
119+
else {
120+
$WindowsFeatures = Get-WindowsOptionalFeature -Online
121+
}
117122
$IIS = $WindowsFeatures.Where{$_.FeatureName -eq 'IIS-WebServer'}
118123
$EnabledFeatures = $WindowsFeatures.Where{$_.State -eq 'Enabled'}
119124
$FeaturesToExport = $EnabledFeatures.Where{$_.FeatureName -match 'IIS'-or

Functions/Private/Artifacts/IIS/GetManifestFromMetabase.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ if (Test-Path -Path $MetabasePath) {
7171
Name = $app.AppFriendlyName;
7272
ID = $app.Location;
7373
ApplicationPool = $app.AppPoolId;
74-
PhysicalPath = $app.Path.replace('%SystemDrive%\','\').replace('C:\','\').Replace('c:\','\');
74+
PhysicalPath = $app.Path.replace('%SystemDrive%','C:'); # TODO - resolve for mount & local
7575
Binding = [PSCustomObject]@{ Protocol = 'http'; #TODO - discover protocol from metabase
7676
BindingInformation = "*" + $site.Bindings }
7777
}) | Out-Null
@@ -89,7 +89,7 @@ if (Test-Path -Path $MetabasePath) {
8989
$virtualDirectories = New-Object System.Collections.ArrayList
9090
$mainVirtualDir = [PSCustomObject]@{
9191
Path = '/';
92-
PhysicalPath = $mainApp.Path.replace('%SystemDrive%\','\').replace('C:\','\').Replace('c:\','\');
92+
PhysicalPath = $mainApp.Path.replace('%SystemDrive%','C:'); # TODO - resolve for mount & local
9393
}
9494
$virtualDirectories.add($mainVirtualDir) | Out-Null
9595

@@ -99,7 +99,7 @@ if (Test-Path -Path $MetabasePath) {
9999
$_.AppFriendlyName -eq $null}){
100100
$virtualDirectories.add([PSCustomObject]@{
101101
Path = '/' + $virtualDirectory.Location.Substring("$SiteId/root/".Length);
102-
PhysicalPath = $virtualDirectory.Path.replace('%SystemDrive%\','\').replace('C:\','\').Replace('c:\','\');
102+
PhysicalPath = $virtualDirectory.Path.replace('%SystemDrive%','C:'); # TODO - resolve for mount & local
103103
}) | Out-Null
104104
}
105105
$applications.add([PSCustomObject]@{

Functions/Private/DiscoverArtifacts.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#>
1616
[CmdletBinding()]
1717
param (
18+
[Parameter(Mandatory = $true)]
19+
[string] $MountPath,
20+
1821
[Parameter(Mandatory = $true)]
1922
[string[]] $Artifact,
2023

@@ -32,12 +35,12 @@
3235

3336
if (!$ArtifactParam) {
3437
foreach ($item in $Artifact) {
35-
& "Discover_$item" -OutputPath $OutputPath -MountPath $Mount.Path -ImageWindowsVersion $ImageWindowsVersion
38+
& "Discover_$item" -OutputPath $OutputPath -MountPath $MountPath -ImageWindowsVersion $ImageWindowsVersion
3639
}
3740
}
3841
else {
3942
foreach ($item in $Artifact) {
40-
& "Discover_$item" -OutputPath $OutputPath -MountPath $Mount.Path -ImageWindowsVersion $ImageWindowsVersion -ArtifactParam $ArtifactParam
43+
& "Discover_$item" -OutputPath $OutputPath -MountPath $MountPath -ImageWindowsVersion $ImageWindowsVersion -ArtifactParam $ArtifactParam
4144
}
4245
}
4346

Functions/Private/GenerateDockerfile.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess",'')]
1414
[CmdletBinding()]
1515
param (
16+
[Parameter(Mandatory = $true)]
17+
[string] $MountPath,
18+
1619
[Parameter(Mandatory = $true)]
1720
[string] $ArtifactPath,
1821

@@ -24,14 +27,14 @@
2427

2528
)
2629

27-
Write-Verbose -Message ('Generating Dockerfile based on discovered artifacts in :{0}' -f $Mount.Path)
30+
Write-Verbose -Message ('Generating Dockerfile based on discovered artifacts in :{0}' -f $MountPath)
2831

2932
$Dockerfile = ''
3033
if (! $ArtifactParam) {
31-
$Dockerfile = & "Generate_$Artifact" -ManifestPath $ArtifactPath
34+
$Dockerfile = & "Generate_$Artifact" -MountPath $MountPath -ManifestPath $ArtifactPath
3235
}
3336
else {
34-
$Dockerfile = & "Generate_$Artifact" -ManifestPath $ArtifactPath -ArtifactParam $ArtifactParam
37+
$Dockerfile = & "Generate_$Artifact" -MountPath $MountPath -ManifestPath $ArtifactPath -ArtifactParam $ArtifactParam
3538
}
3639

3740
$DockerfilePath = '{0}\Dockerfile' -f $ArtifactPath

Functions/Private/GetImageType.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,4 @@
4747
Write-Error -Message ('Error occurred while attempting to inspect the image file. {0}' -f $PSItem.Exception.Message)
4848
throw $PSItem
4949
}
50-
51-
}
52-
50+
}

Functions/Public/ConvertTo/ConvertTo-Dockerfile.ps1

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
[CmdletBinding()]
7373
param (
74-
[Parameter(Mandatory = $true)]
74+
[Parameter(Mandatory = $false)]
7575
[ValidateScript({if (!(Test-Path -Path $PSItem)) { return $false } else { return $true } })]
7676
[string] $ImagePath,
7777

@@ -82,71 +82,87 @@
8282
[string] $MountPath,
8383

8484
[Parameter(Mandatory = $true)]
85-
[string[]] $Artifact,
85+
[string] $Artifact,
8686

8787
[Parameter(Mandatory = $false)]
8888
[string[]] $ArtifactParam,
8989

9090
[Parameter(Mandatory = $false)]
91-
[Switch] $Force
91+
[Switch] $Force,
9292

93+
[Parameter(Mandatory = $false)]
94+
[Switch] $Local
9395
)
9496

97+
# TODO - validat eLOcal & ImagePath
98+
9599
### If the user doesn't specify an output path, then generate one
96100
if (!$PSBoundParameters.Keys.Contains('OutputPath')) {
97101
$OutputPath = GenerateOutputFolder
98102
}
99-
elseif(($PSBoundParameters.Keys.Contains('OutputPath')) -and ($PSBoundParameters.Keys.Contains('Force')))
100-
{
103+
elseif(($PSBoundParameters.Keys.Contains('OutputPath')) -and ($PSBoundParameters.Keys.Contains('Force'))) {
101104
$OutputPath = GenerateOutputFolder -Path $OutputPath -Force
102105
}
103106
else {
104107
$OutputPath = GenerateOutputFolder -Path $OutputPath
105108
}
106-
Write-Verbose -Message ('Starting conversion process')
107-
108-
### Verify the image type before proceeding
109-
$ImageType = GetImageType -Path $ImagePath
110-
Write-Verbose -Message ('Image type is: {0}' -f $ImageType)
111109

112-
try {
113-
### Mount the image to a directory
110+
# load the source - local drive, or VHD
111+
if ($Local) {
112+
$MountPath = $env:SystemDrive
113+
$version = [Environment]::OSVersion.Version
114+
$ImageWindowsVersion = "$($version.Major).$($version.Minor)"
115+
Write-Verbose -Message "Using local drive: $MountPath"
116+
}
117+
else {
118+
# Verify the image type before proceeding
119+
$ImageType = GetImageType -Path $ImagePath
120+
Write-Verbose -Message ('Image type is: {0}' -f $ImageType)
114121

122+
try {
123+
# Mount the image to a directory
115124
$Mount = MountImage -ImagePath $ImagePath -MountPath $MountPath
116-
Write-Verbose -Message ('Finished mounting image to: {0}' -f $Mount.Path)
125+
$MountPath = $Mount.Path
126+
Write-Verbose -Message ('Finished mounting image to: {0}' -f $MountPath)
117127
}
118128
catch {
119129
throw 'Fatal error: couldn''t mount image file: {0}' -f $PSItem
120130
}
121131

122-
### Get the Windows version in the image, returns Major.Minor - e.g. 6.2 is Server 2012
123-
### https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
124-
$info = Get-WindowsImage -Index 1 -ImagePath $ImagePath
125-
$ImageWindowsVersion = "$($info.MajorVersion).$($info.MinorVersion)"
126-
127-
### Perform artifact discovery
128-
if (!$PSBoundParameters.Keys.Contains('Artifact')) {
129-
$Artifact = Get-WindowsArtifact
130-
}
131-
if (!$PSBoundParameters.Keys.Contains('ArtifactParam')) {
132-
DiscoverArtifacts -Artifact $Artifact -OutputPath $OutputPath -ImageWindowsVersion $ImageWindowsVersion
133-
}
134-
else {
135-
DiscoverArtifacts -Artifact $Artifact -OutputPath $OutputPath -ImageWindowsVersion $ImageWindowsVersion -ArtifactParam $ArtifactParam
132+
# Get the Windows version in the image, returns Major.Minor - e.g. 6.2 is Server 2012
133+
# https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
134+
$info = Get-WindowsImage -Index 1 -ImagePath $ImagePath
135+
$ImageWindowsVersion = "$($info.MajorVersion).$($info.MinorVersion)"
136136
}
137137

138-
### Generate Dockerfile
139-
if (!$PSBoundParameters.Keys.Contains('ArtifactParam')) {
140-
GenerateDockerfile -ArtifactPath $OutputPath -Artifact $Artifact
138+
Write-Verbose -Message ('Starting conversion process')
139+
try {
140+
### Perform artifact discovery
141+
if (!$PSBoundParameters.Keys.Contains('Artifact')) {
142+
$Artifact = Get-WindowsArtifact
143+
}
144+
if (!$PSBoundParameters.Keys.Contains('ArtifactParam')) {
145+
DiscoverArtifacts -MountPath $MountPath -Artifact $Artifact -OutputPath $OutputPath -ImageWindowsVersion $ImageWindowsVersion
146+
}
147+
else {
148+
DiscoverArtifacts -MountPath $MountPath -Artifact $Artifact -OutputPath $OutputPath -ImageWindowsVersion $ImageWindowsVersion -ArtifactParam $ArtifactParam
149+
}
150+
151+
### Generate Dockerfile
152+
if (!$PSBoundParameters.Keys.Contains('ArtifactParam')) {
153+
GenerateDockerfile -MountPath $MountPath -ArtifactPath $OutputPath -Artifact $Artifact
154+
}
155+
else {
156+
GenerateDockerfile -MountPath $MountPath -ArtifactPath $OutputPath -Artifact $Artifact -ArtifactParam $ArtifactParam
157+
}
158+
Write-Verbose -Message 'Finished generating the Dockerfile'
141159
}
142-
else {
143-
GenerateDockerfile -ArtifactPath $OutputPath -Artifact $Artifact -ArtifactParam $ArtifactParam
160+
finally {
161+
if ($Mount) {
162+
### Dismount the image when inspection is completed
163+
$null = Dismount-WindowsImage -Path $MountPath -Discard
164+
Write-Verbose -Message ('Finished dismounting the Windows image from {0}' -f $MountPath)
165+
}
144166
}
145-
Write-Verbose -Message 'Finished generating the Dockerfile'
146-
147-
### Dismount the image when inspection is completed
148-
$null = Dismount-WindowsImage -Path $Mount.Path -Discard
149-
Write-Verbose -Message ('Finished dismounting the Windows image from {0}' -f $Mount.Path)
150-
151167
}
152168

0 commit comments

Comments
 (0)