Functions/Private/Artifacts/IIS/Generate_IIS.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Function Generate_IIS {
<#
.SYNOPSIS
Generates Dockerfile contents for Internet Information Services (IIS) feature
 
.PARAMETER ManifestPath
The filesystem path where the JSON manifests are stored.
 
.PARAMETER ArtifactParam
Optional - one or more Website names to include in the output.
#>

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess",'')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments",'')]
[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    [string] $ManifestPath,

    [Parameter(Mandatory = $false)]
    [string[]] $ArtifactParam
)

$ArtifactName = Split-Path -Path $PSScriptRoot -Leaf

Write-Verbose -Message ('Generating result for {0} component' -f (Split-Path -Path $PSScriptRoot -Leaf))
$Manifest = '{0}\{1}.json' -f $ManifestPath, $ArtifactName 

$Artifact = Get-Content -Path $Manifest -Raw | ConvertFrom-Json

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

    $ResultBuilder = New-Object System.Text.StringBuilder

    Write-Verbose -Message ('Writing instruction to install IIS')
    $null = $ResultBuilder.AppendLine('# Install Windows features for IIS')
    $null = $ResultBuilder.Append('RUN Add-WindowsFeature Web-server')
    if ($Artifact.AspNetStatus -eq 'Present') {
        Write-Verbose -Message ('Writing instruction to install ASP.NET')
        $null = $ResultBuilder.Append(', NET-Framework-45-ASPNET, Web-Asp-Net45')
    }
    $null = $ResultBuilder.AppendLine('')
    $null = $ResultBuilder.AppendLine("RUN Enable-WindowsOptionalFeature -Online -FeatureName $($Artifact.FeatureName.Replace(';',','))")

    if ($Artifact.HttpHandlers.Count > 0) {
        Write-Verbose -Message ('Writing instruction to add HTTP handlers')
        $null = $ResultBuilder.Append('RUN ')
        foreach ($HttpHandler in $Artifact.HttpHandlers) {
             $null = $ResultBuilder.AppendLine('New-WebHandler -Name "{0}" -Path "{1}" -Verb "{2}" `' -f $HttpHandler.Name, $HttpHandler.Path, $HttpHandler.Verb)
        }
    }
    $null = $ResultBuilder.AppendLine('')

    for ($i=0;$i -lt $Artifact.Websites.Count;$i++) {
        $Site = $Artifact.Websites[$i]
        $SitePath = $Mount.Path + $Site.PhysicalPath
        Write-Verbose -Message ('Copying website files from {0} to {1}' -f $SitePath, $ManifestPath)
        Copy-Item $SitePath $ManifestPath -Recurse

        Write-Verbose -Message ('Writing instruction to copy files for {0} site' -f  $Site.Name)
        $null = $ResultBuilder.AppendLine("# Set up website: $($Site.Name)")        
        $copy = "COPY {0} {1}" -f (Split-Path $Site.PhysicalPath -Leaf),($Site.PhysicalPath -Replace "\\","/")
        $null = $ResultBuilder.AppendLine($copy)

        Write-Verbose -Message ('Writing instruction to create site {0}' -f  $Site.Name)
        $newSite = 'RUN New-Website -Name ''{0}'' -PhysicalPath "C:{1}" -Port {2} -Force' -f ($Site.Name -replace "'","''"), $Site.PhysicalPath, $Site.binding.bindingInformation.split(':')[-2]
        $null = $ResultBuilder.AppendLine($newSite)

        Write-Verbose -Message ('Writing instruction to expose port for site {0}' -f  $Site.Name)
        $null = $ResultBuilder.AppendLine("EXPOSE $($Site.binding.bindingInformation.split(':')[-2])")
        $null = $ResultBuilder.AppendLine('')
    }
        
    $null = $ResultBuilder.AppendLine('CMD /Wait-Service.ps1 -ServiceName W3SVC -AllowServiceRestart')
}


Write-Output $ResultBuilder.ToString() -NoEnumerate

}