| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Creates a Docker Compose deployment bundle from a Kestrun service package. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Expands a `.krpack` service package, validates its `Service.psd1` descriptor, |
| | | 6 | | and generates a self-contained Docker deployment bundle that includes: |
| | | 7 | | |
| | | 8 | | - `docker-compose.yml` |
| | | 9 | | - `Dockerfile` |
| | | 10 | | - `entrypoint.sh` |
| | | 11 | | - the input `.krpack` copied as `app.krpack` |
| | | 12 | | - a local copy of the current `Kestrun` PowerShell module |
| | | 13 | | - `.dockerignore` |
| | | 14 | | |
| | | 15 | | The generated container uses the Microsoft ASP.NET Core .NET 10 base image |
| | | 16 | | and installs PowerShell from the Microsoft Linux package repository. |
| | | 17 | | .PARAMETER PackagePath |
| | | 18 | | Path to the `.krpack` service package. |
| | | 19 | | .PARAMETER OutputPath |
| | | 20 | | Output directory for the generated deployment bundle. |
| | | 21 | | Defaults to `<service-name>-docker` in the current directory. |
| | | 22 | | .PARAMETER ImageName |
| | | 23 | | Docker image name written to `docker-compose.yml`. |
| | | 24 | | Defaults to `kestrun-<service-name-normalized>:<version>`. |
| | | 25 | | .PARAMETER ServiceName |
| | | 26 | | Docker Compose service name and container name. |
| | | 27 | | Defaults to the service descriptor name normalized to lowercase kebab-case. |
| | | 28 | | .PARAMETER PublishedPort |
| | | 29 | | Host port mapped by Docker Compose. |
| | | 30 | | Defaults to `8080`. |
| | | 31 | | .PARAMETER ContainerPort |
| | | 32 | | Container port exposed by the generated image and used by `ASPNETCORE_URLS`. |
| | | 33 | | Defaults to `8080`. |
| | | 34 | | .PARAMETER KestrunModulePath |
| | | 35 | | Optional path to the `Kestrun` module root folder to stage into the deployment bundle. |
| | | 36 | | Defaults to the currently loaded module source folder. |
| | | 37 | | .PARAMETER Force |
| | | 38 | | Overwrite an existing generated deployment bundle. |
| | | 39 | | .PARAMETER WhatIf |
| | | 40 | | Shows what would happen if the cmdlet runs. The cmdlet is not executed. |
| | | 41 | | .PARAMETER Confirm |
| | | 42 | | Prompts for confirmation before running the cmdlet. |
| | | 43 | | .EXAMPLE |
| | | 44 | | New-KrDockerDeployment -PackagePath .\my-service.krpack |
| | | 45 | | .EXAMPLE |
| | | 46 | | New-KrDockerDeployment -PackagePath .\my-service.krpack -PublishedPort 5000 -OutputPath .\deploy\docker |
| | | 47 | | .EXAMPLE |
| | | 48 | | New-KrDockerDeployment -PackagePath .\my-service.krpack -ImageName 'my-registry/my-service:1.2.0' -Force |
| | | 49 | | #> |
| | | 50 | | function New-KrDockerDeployment { |
| | | 51 | | [KestrunRuntimeApi('Everywhere')] |
| | | 52 | | [CmdletBinding(SupportsShouldProcess)] |
| | | 53 | | [OutputType([pscustomobject])] |
| | | 54 | | param( |
| | | 55 | | [Parameter(Mandatory)] |
| | | 56 | | [ValidateNotNullOrEmpty()] |
| | | 57 | | [string]$PackagePath, |
| | | 58 | | |
| | | 59 | | [Parameter()] |
| | | 60 | | [string]$OutputPath, |
| | | 61 | | |
| | | 62 | | [Parameter()] |
| | | 63 | | [string]$ImageName, |
| | | 64 | | |
| | | 65 | | [Parameter()] |
| | | 66 | | [string]$ServiceName, |
| | | 67 | | |
| | | 68 | | [Parameter()] |
| | | 69 | | [ValidateRange(1, 65535)] |
| | | 70 | | [int]$PublishedPort = 8080, |
| | | 71 | | |
| | | 72 | | [Parameter()] |
| | | 73 | | [ValidateRange(1, 65535)] |
| | | 74 | | [int]$ContainerPort = 8080, |
| | | 75 | | |
| | | 76 | | [Parameter()] |
| | | 77 | | [string]$KestrunModulePath, |
| | | 78 | | |
| | | 79 | | [Parameter()] |
| | | 80 | | [switch]$Force |
| | | 81 | | ) |
| | | 82 | | |
| | 1 | 83 | | $declaringModuleBase = if ($MyInvocation.MyCommand.Module) { |
| | 1 | 84 | | $MyInvocation.MyCommand.Module.ModuleBase |
| | | 85 | | } else { |
| | 0 | 86 | | $null |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | function Get-KrDefaultModuleRoot { |
| | | 90 | | <# |
| | | 91 | | .SYNOPSIS |
| | | 92 | | Resolves the default Kestrun module root path based on the current script location. |
| | | 93 | | .DESCRIPTION |
| | | 94 | | Resolves the module root from the current module base when available and |
| | | 95 | | falls back to nearby script locations when running from a source checkout. |
| | | 96 | | Validates that the resolved path contains `Kestrun.psd1` to ensure it is correct. |
| | | 97 | | .OUTPUTS |
| | | 98 | | String path to the Kestrun module root. |
| | | 99 | | #> |
| | 1 | 100 | | $candidateRoots = [System.Collections.Generic.List[string]]::new() |
| | 1 | 101 | | foreach ($candidateRoot in @( |
| | 1 | 102 | | $declaringModuleBase |
| | 1 | 103 | | $ExecutionContext.SessionState.Module.ModuleBase |
| | 1 | 104 | | $PSScriptRoot |
| | 2 | 105 | | (Split-Path -Parent $PSScriptRoot) |
| | 3 | 106 | | (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) |
| | | 107 | | )) { |
| | 1 | 108 | | if ([string]::IsNullOrWhiteSpace($candidateRoot)) { |
| | | 109 | | continue |
| | | 110 | | } |
| | | 111 | | |
| | 1 | 112 | | $resolvedCandidateRoot = [System.IO.Path]::GetFullPath($candidateRoot) |
| | 1 | 113 | | if (-not $candidateRoots.Contains($resolvedCandidateRoot)) { |
| | 1 | 114 | | $candidateRoots.Add($resolvedCandidateRoot) |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | 1 | 118 | | foreach ($candidateRoot in $candidateRoots) { |
| | 2 | 119 | | if (Test-Path -LiteralPath (Join-Path -Path $candidateRoot -ChildPath 'Kestrun.psd1') -PathType Leaf) { |
| | 1 | 120 | | return $candidateRoot |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | throw "Unable to resolve the Kestrun module root from '$PSScriptRoot'." |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | function Get-KrNormalizedDockerName { |
| | | 128 | | <# |
| | | 129 | | .SYNOPSIS |
| | | 130 | | Normalizes a string to a valid Docker name. |
| | | 131 | | .DESCRIPTION |
| | | 132 | | Converts the input string to lowercase, replaces invalid characters with hyphens, and trims leading/trailing |
| | | 133 | | If the result is empty, returns a fallback name. |
| | | 134 | | .PARAMETER Name |
| | | 135 | | The input string to normalize. |
| | | 136 | | .PARAMETER Fallback |
| | | 137 | | The fallback name to use if normalization results in an empty string. |
| | | 138 | | .OUTPUTS |
| | | 139 | | String containing the normalized Docker name. |
| | | 140 | | #> |
| | | 141 | | param( |
| | | 142 | | [Parameter(Mandatory)] |
| | | 143 | | [string]$Name, |
| | | 144 | | |
| | | 145 | | [string]$Fallback = 'kestrun-app' |
| | | 146 | | ) |
| | | 147 | | |
| | 1 | 148 | | $normalized = $Name.ToLowerInvariant() |
| | 1 | 149 | | $normalized = [System.Text.RegularExpressions.Regex]::Replace($normalized, '[^a-z0-9]+', '-') |
| | 1 | 150 | | $normalized = $normalized.Trim('-') |
| | | 151 | | |
| | 1 | 152 | | if ([string]::IsNullOrWhiteSpace($normalized)) { |
| | 0 | 153 | | return $Fallback |
| | | 154 | | } |
| | | 155 | | |
| | 1 | 156 | | return $normalized |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | function Get-KrStableDockerSuffix { |
| | | 160 | | <# |
| | | 161 | | .SYNOPSIS |
| | | 162 | | Generates a stable suffix for Docker resource names based on an input string. |
| | | 163 | | .DESCRIPTION |
| | | 164 | | Computes a SHA256 hash of the input string and returns the first 12 characters as a hexadecimal suffix. |
| | | 165 | | This ensures that the same input will always produce the same suffix, which is useful for generating consist |
| | | 166 | | .PARAMETER InputValue |
| | | 167 | | The input string used to generate the hash-based suffix. |
| | | 168 | | .OUTPUTS |
| | | 169 | | String containing the stable Docker suffix derived from the input value. |
| | | 170 | | #> |
| | | 171 | | param( |
| | | 172 | | [Parameter(Mandatory)] |
| | | 173 | | [string]$InputValue |
| | | 174 | | ) |
| | | 175 | | |
| | 1 | 176 | | $bytes = [System.Text.Encoding]::UTF8.GetBytes($InputValue) |
| | 1 | 177 | | $hash = [System.Security.Cryptography.SHA256]::HashData($bytes) |
| | 2 | 178 | | return ([System.Convert]::ToHexString($hash)).Substring(0, 12).ToLowerInvariant() |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | function Get-KrApplicationDataDefinition { |
| | | 182 | | <# |
| | | 183 | | .SYNOPSIS |
| | | 184 | | Generates application data volume definitions for Docker Compose based on service descriptor entries. |
| | | 185 | | .DESCRIPTION |
| | | 186 | | For each relative path specified in the service descriptor's |
| | | 187 | | `ApplicationDataFolders`, this function generates a corresponding Docker |
| | | 188 | | volume name and storage path. Each path must point to a subdirectory |
| | | 189 | | under the service root. |
| | | 190 | | .PARAMETER NormalizedServiceName |
| | | 191 | | The normalized name of the service, used as a prefix for volume names. |
| | | 192 | | .PARAMETER RelativePaths |
| | | 193 | | An array of relative paths from the service descriptor's |
| | | 194 | | `ApplicationDataFolders` entry. Each path is processed to generate a |
| | | 195 | | corresponding Docker volume definition. |
| | | 196 | | .OUTPUTS |
| | | 197 | | An array of custom objects containing `RelativePath`, `VolumeName`, and |
| | | 198 | | `StoragePath` properties for use in Docker Compose volume definitions. |
| | | 199 | | #> |
| | | 200 | | param( |
| | | 201 | | [Parameter(Mandatory)] |
| | | 202 | | [string]$NormalizedServiceName, |
| | | 203 | | |
| | | 204 | | [Parameter()] |
| | | 205 | | [string[]]$RelativePaths |
| | | 206 | | ) |
| | | 207 | | |
| | 1 | 208 | | $definitions = [System.Collections.Generic.List[object]]::new() |
| | 2 | 209 | | foreach ($relativePath in @($RelativePaths)) { |
| | 1 | 210 | | if ([string]::IsNullOrWhiteSpace($relativePath)) { |
| | | 211 | | continue |
| | | 212 | | } |
| | | 213 | | |
| | 1 | 214 | | $normalizedRelativePath = $relativePath.Replace('\\', '/').Trim() |
| | 1 | 215 | | $trimmedRelativePath = $normalizedRelativePath.Trim('/') |
| | 2 | 216 | | $pathSegments = @($trimmedRelativePath -split '/') |
| | | 217 | | |
| | 1 | 218 | | if ($pathSegments -contains '.' -or $pathSegments -contains '..') { |
| | 1 | 219 | | throw ( |
| | 1 | 220 | | "ApplicationDataFolders entry '{0}' must resolve to a subdirectory under the service root." -f |
| | | 221 | | $relativePath |
| | | 222 | | ) |
| | | 223 | | } |
| | | 224 | | |
| | 1 | 225 | | if ([string]::IsNullOrWhiteSpace($trimmedRelativePath)) { |
| | | 226 | | continue |
| | | 227 | | } |
| | | 228 | | |
| | 2 | 229 | | $dockerSegment = Get-KrNormalizedDockerName -Name ($trimmedRelativePath -replace '/', '-') -Fallback 'app-da |
| | 1 | 230 | | $hashSuffix = Get-KrStableDockerSuffix -InputValue $trimmedRelativePath.ToLowerInvariant() |
| | 1 | 231 | | $volumeName = '{0}-appdata-{1}-{2}' -f $NormalizedServiceName, $dockerSegment, $hashSuffix |
| | 1 | 232 | | $storagePath = '/opt/kestrun/application-data/{0}-{1}' -f $dockerSegment, $hashSuffix |
| | | 233 | | |
| | 2 | 234 | | $definitions.Add([pscustomobject]([ordered]@{ |
| | 1 | 235 | | RelativePath = $normalizedRelativePath |
| | 1 | 236 | | VolumeName = $volumeName |
| | 1 | 237 | | StoragePath = $storagePath |
| | | 238 | | })) |
| | | 239 | | } |
| | | 240 | | |
| | 1 | 241 | | return $definitions |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | function Get-KrDeploymentOutputPath { |
| | | 245 | | <# |
| | | 246 | | .SYNOPSIS |
| | | 247 | | Resolves the output path for the Docker deployment bundle. |
| | | 248 | | .DESCRIPTION |
| | | 249 | | If a path is provided, it returns the full path. Otherwise, it combines the current location with a default |
| | | 250 | | .PARAMETER ProvidedOutputPath |
| | | 251 | | The user-provided output path. |
| | | 252 | | .PARAMETER DefaultDirectoryName |
| | | 253 | | The default directory name to use if no path is provided. |
| | | 254 | | .OUTPUTS |
| | | 255 | | String containing the resolved output path. |
| | | 256 | | #> |
| | | 257 | | param( |
| | | 258 | | [string]$ProvidedOutputPath, |
| | | 259 | | [string]$DefaultDirectoryName |
| | | 260 | | ) |
| | | 261 | | |
| | 1 | 262 | | if ([string]::IsNullOrWhiteSpace($ProvidedOutputPath)) { |
| | 0 | 263 | | return [System.IO.Path]::Combine((Get-Location).Path, $DefaultDirectoryName) |
| | | 264 | | } |
| | | 265 | | |
| | 1 | 266 | | return [System.IO.Path]::GetFullPath($ProvidedOutputPath) |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | function Set-KrGeneratedFileContent { |
| | | 270 | | <# |
| | | 271 | | .SYNOPSIS |
| | | 272 | | Writes content to a file, ensuring the directory exists and handling overwrites based on the Force parameter |
| | | 273 | | .DESCRIPTION |
| | | 274 | | Creates the target directory if it does not exist. If the file already |
| | | 275 | | exists and Force is not set, it throws an error. Writes the content |
| | | 276 | | using UTF-8 encoding without a BOM. |
| | | 277 | | .PARAMETER Path |
| | | 278 | | The file path where the content should be written. |
| | | 279 | | .PARAMETER Content |
| | | 280 | | The content to write to the file. |
| | | 281 | | .OUTPUTS |
| | | 282 | | Boolean indicating whether the file was written. |
| | | 283 | | #> |
| | | 284 | | [CmdletBinding(SupportsShouldProcess)] |
| | | 285 | | param( |
| | | 286 | | [Parameter(Mandatory)] |
| | | 287 | | [string]$Path, |
| | | 288 | | |
| | | 289 | | [Parameter(Mandatory)] |
| | | 290 | | [string]$Content |
| | | 291 | | ) |
| | | 292 | | |
| | 1 | 293 | | if (-not $PSCmdlet.ShouldProcess($Path, 'Write generated file content')) { |
| | 0 | 294 | | return $false |
| | | 295 | | } |
| | | 296 | | |
| | 1 | 297 | | $directory = [System.IO.Path]::GetDirectoryName($Path) |
| | 2 | 298 | | if (-not [string]::IsNullOrWhiteSpace($directory) -and -not (Test-Path -LiteralPath $directory -PathType Contain |
| | 0 | 299 | | $null = New-Item -ItemType Directory -Path $directory -Force |
| | | 300 | | } |
| | | 301 | | |
| | 2 | 302 | | if ((Test-Path -LiteralPath $Path -PathType Leaf) -and -not $Force) { |
| | 0 | 303 | | throw "Output file already exists: $Path. Use -Force to overwrite." |
| | | 304 | | } |
| | | 305 | | |
| | 1 | 306 | | $utf8NoBom = [System.Text.UTF8Encoding]::new($false) |
| | 1 | 307 | | [System.IO.File]::WriteAllText($Path, $Content, $utf8NoBom) |
| | | 308 | | |
| | 1 | 309 | | return $true |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | function Copy-KrGeneratedDirectory { |
| | | 313 | | <# |
| | | 314 | | .SYNOPSIS |
| | | 315 | | Copies a directory and its contents, handling overwrites based on the Force parameter. |
| | | 316 | | .DESCRIPTION |
| | | 317 | | If the destination directory already exists and Force is not set, it throws an error. Otherwise, it removes |
| | | 318 | | .PARAMETER SourcePath |
| | | 319 | | The path of the source directory to copy. |
| | | 320 | | .PARAMETER DestinationPath |
| | | 321 | | The path of the destination directory. |
| | | 322 | | .OUTPUTS |
| | | 323 | | Boolean indicating whether the directory was copied. |
| | | 324 | | #> |
| | | 325 | | [CmdletBinding(SupportsShouldProcess)] |
| | | 326 | | param( |
| | | 327 | | [Parameter(Mandatory)] |
| | | 328 | | [string]$SourcePath, |
| | | 329 | | |
| | | 330 | | [Parameter(Mandatory)] |
| | | 331 | | [string]$DestinationPath |
| | | 332 | | ) |
| | | 333 | | |
| | 1 | 334 | | if (-not $PSCmdlet.ShouldProcess($DestinationPath, 'Copy generated directory contents')) { |
| | 0 | 335 | | return $false |
| | | 336 | | } |
| | | 337 | | |
| | 1 | 338 | | if (Test-Path -LiteralPath $DestinationPath) { |
| | 0 | 339 | | if (-not $Force) { |
| | 0 | 340 | | throw "Output directory already exists: $DestinationPath. Use -Force to overwrite." |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | Remove-Item -LiteralPath $DestinationPath -Recurse -Force |
| | | 344 | | } |
| | | 345 | | |
| | 1 | 346 | | Copy-Item -LiteralPath $SourcePath -Destination $DestinationPath -Recurse -Force |
| | | 347 | | |
| | 1 | 348 | | return $true |
| | | 349 | | } |
| | | 350 | | |
| | 1 | 351 | | $temporaryExtractionRoot = $null |
| | | 352 | | |
| | | 353 | | try { |
| | 1 | 354 | | $resolvedPackagePath = [System.IO.Path]::GetFullPath($PackagePath) |
| | 2 | 355 | | if (-not (Test-Path -LiteralPath $resolvedPackagePath -PathType Leaf)) { |
| | 0 | 356 | | throw "Package file not found: $resolvedPackagePath" |
| | | 357 | | } |
| | | 358 | | |
| | 1 | 359 | | if (-not $resolvedPackagePath.EndsWith('.krpack', [System.StringComparison]::OrdinalIgnoreCase)) { |
| | 0 | 360 | | throw "PackagePath must point to a .krpack file: $resolvedPackagePath" |
| | | 361 | | } |
| | | 362 | | |
| | 1 | 363 | | $resolvedModuleRoot = if ([string]::IsNullOrWhiteSpace($KestrunModulePath)) { |
| | 1 | 364 | | Get-KrDefaultModuleRoot |
| | | 365 | | } else { |
| | 0 | 366 | | [System.IO.Path]::GetFullPath($KestrunModulePath) |
| | | 367 | | } |
| | | 368 | | |
| | 2 | 369 | | if (-not (Test-Path -LiteralPath $resolvedModuleRoot -PathType Container)) { |
| | 0 | 370 | | throw "Kestrun module path not found: $resolvedModuleRoot" |
| | | 371 | | } |
| | | 372 | | |
| | 3 | 373 | | if (-not (Test-Path -LiteralPath (Join-Path -Path $resolvedModuleRoot -ChildPath 'Kestrun.psd1') -PathType Leaf) |
| | 0 | 374 | | throw "Kestrun module manifest not found under: $resolvedModuleRoot" |
| | | 375 | | } |
| | | 376 | | |
| | 3 | 377 | | $temporaryExtractionRoot = Join-Path ([System.IO.Path]::GetTempPath()) ('kestrun-docker-{0}' -f [Guid]::NewGuid( |
| | 1 | 378 | | $null = New-Item -ItemType Directory -Path $temporaryExtractionRoot -Force |
| | 1 | 379 | | Expand-Archive -LiteralPath $resolvedPackagePath -DestinationPath $temporaryExtractionRoot -Force |
| | | 380 | | |
| | 1 | 381 | | $descriptor = Get-KrServiceDescriptor -Path $temporaryExtractionRoot |
| | 1 | 382 | | $normalizedServiceName = if ([string]::IsNullOrWhiteSpace($ServiceName)) { |
| | 1 | 383 | | Get-KrNormalizedDockerName -Name $descriptor.Name -Fallback 'kestrun-app' |
| | | 384 | | } else { |
| | 1 | 385 | | Get-KrNormalizedDockerName -Name $ServiceName -Fallback 'kestrun-app' |
| | | 386 | | } |
| | | 387 | | |
| | 1 | 388 | | $resolvedImageName = if ([string]::IsNullOrWhiteSpace($ImageName)) { |
| | 1 | 389 | | 'kestrun-{0}:{1}' -f $normalizedServiceName, $descriptor.Version |
| | | 390 | | } else { |
| | 1 | 391 | | $ImageName |
| | | 392 | | } |
| | | 393 | | |
| | 2 | 394 | | $resolvedOutputPath = Get-KrDeploymentOutputPath -ProvidedOutputPath $OutputPath -DefaultDirectoryName ('{0}-doc |
| | 1 | 395 | | $resolvedPowerShellPackageVersion = '7.6.0-1.deb' |
| | 1 | 396 | | $composePath = Join-Path -Path $resolvedOutputPath -ChildPath 'docker-compose.yml' |
| | 1 | 397 | | $dockerfilePath = Join-Path -Path $resolvedOutputPath -ChildPath 'Dockerfile' |
| | 1 | 398 | | $entrypointPath = Join-Path -Path $resolvedOutputPath -ChildPath 'entrypoint.sh' |
| | 1 | 399 | | $dockerignorePath = Join-Path -Path $resolvedOutputPath -ChildPath '.dockerignore' |
| | 1 | 400 | | $packageDestinationPath = Join-Path -Path $resolvedOutputPath -ChildPath 'app.krpack' |
| | 1 | 401 | | $moduleDestinationPath = Join-Path -Path $resolvedOutputPath -ChildPath 'Kestrun' |
| | 2 | 402 | | $applicationDataDefinitions = @(Get-KrApplicationDataDefinition -NormalizedServiceName $normalizedServiceName -R |
| | | 403 | | |
| | 1 | 404 | | $composeLines = [System.Collections.Generic.List[string]]::new() |
| | 1 | 405 | | $composeLines.Add('services:') |
| | 1 | 406 | | $composeLines.Add(" ${normalizedServiceName}:") |
| | 1 | 407 | | $composeLines.Add(" image: $resolvedImageName") |
| | 1 | 408 | | $composeLines.Add(' build:') |
| | 1 | 409 | | $composeLines.Add(' context: .') |
| | 1 | 410 | | $composeLines.Add(' dockerfile: Dockerfile') |
| | 1 | 411 | | $composeLines.Add(' ports:') |
| | 2 | 412 | | $composeLines.Add((' - "{0}:{1}"' -f $PublishedPort, $ContainerPort)) |
| | 1 | 413 | | $composeLines.Add(' environment:') |
| | 2 | 414 | | $composeLines.Add((' PORT: "{0}"' -f $ContainerPort)) |
| | 2 | 415 | | $composeLines.Add((' ASPNETCORE_URLS: "http://+:{0}"' -f $ContainerPort)) |
| | 1 | 416 | | if ($applicationDataDefinitions.Count -gt 0) { |
| | 1 | 417 | | $composeLines.Add(' volumes:') |
| | 1 | 418 | | foreach ($applicationDataDefinition in $applicationDataDefinitions) { |
| | 3 | 419 | | $composeLines.Add(" - $($applicationDataDefinition.VolumeName):$($applicationDataDefinition.Storage |
| | | 420 | | } |
| | | 421 | | } |
| | | 422 | | |
| | 1 | 423 | | $composeLines.Add(' restart: unless-stopped') |
| | | 424 | | |
| | 1 | 425 | | if ($applicationDataDefinitions.Count -gt 0) { |
| | 1 | 426 | | $composeLines.Add('volumes:') |
| | 1 | 427 | | foreach ($applicationDataDefinition in $applicationDataDefinitions) { |
| | 2 | 428 | | $composeLines.Add(" $($applicationDataDefinition.VolumeName):") |
| | | 429 | | } |
| | | 430 | | } |
| | | 431 | | |
| | 1 | 432 | | $composeContent = $composeLines -join "`n" |
| | | 433 | | |
| | 1 | 434 | | $dockerfileContent = @" |
| | | 435 | | FROM mcr.microsoft.com/dotnet/aspnet:10.0 |
| | | 436 | | |
| | | 437 | | ARG POWERSHELL_PACKAGE_VERSION=$resolvedPowerShellPackageVersion |
| | | 438 | | |
| | | 439 | | RUN apt-get update \ |
| | | 440 | | && apt-get install -y --no-install-recommends wget ca-certificates \ |
| | | 441 | | && . /etc/os-release \ |
| | | 442 | | && wget -q "https://packages.microsoft.com/config/`${ID}/`${VERSION_ID}/packages-microsoft-prod.deb" \ |
| | | 443 | | && dpkg -i packages-microsoft-prod.deb \ |
| | | 444 | | && rm packages-microsoft-prod.deb \ |
| | | 445 | | && apt-get update \ |
| | | 446 | | && apt-get install -y --no-install-recommends powershell=`${POWERSHELL_PACKAGE_VERSION} \ |
| | | 447 | | && apt-get clean \ |
| | | 448 | | && rm -rf /var/lib/apt/lists/* |
| | | 449 | | |
| | | 450 | | ENV PORT=$ContainerPort |
| | | 451 | | ENV ASPNETCORE_URLS=http://+:$ContainerPort |
| | | 452 | | WORKDIR /opt/kestrun |
| | | 453 | | |
| | | 454 | | COPY Kestrun/ /opt/kestrun/Kestrun/ |
| | | 455 | | COPY app.krpack /opt/kestrun/app/app.krpack |
| | | 456 | | COPY entrypoint.sh /opt/kestrun/entrypoint.sh |
| | | 457 | | |
| | | 458 | | RUN module_root="`$(pwsh -NoLogo -NoProfile -Command '(`$env:PSModulePath -split [System.IO.Path]::PathSeparator)[0]')" |
| | | 459 | | && module_version="`$(pwsh -NoLogo -NoProfile -Command '(Import-PowerShellDataFile -LiteralPath "/opt/kestrun/Kestru |
| | | 460 | | && mkdir -p "`$module_root/Kestrun/`$module_version" \ |
| | | 461 | | && cp -R /opt/kestrun/Kestrun/. "`$module_root/Kestrun/`$module_version/" \ |
| | | 462 | | && rm -rf /opt/kestrun/Kestrun \ |
| | | 463 | | && printf '%s\n' 'if (Get-Module -ListAvailable Kestrun) {' ' Import-Module Kestrun' '}' > /opt/microsoft/powersh |
| | | 464 | | && chmod +x /opt/kestrun/entrypoint.sh |
| | | 465 | | |
| | | 466 | | EXPOSE $ContainerPort |
| | | 467 | | |
| | | 468 | | ENTRYPOINT ["/opt/kestrun/entrypoint.sh"] |
| | | 469 | | "@ |
| | | 470 | | |
| | 1 | 471 | | $entrypointLines = [System.Collections.Generic.List[string]]::new() |
| | 1 | 472 | | @( |
| | 1 | 473 | | '#!/bin/sh' |
| | 1 | 474 | | 'set -eu' |
| | 1 | 475 | | '' |
| | 1 | 476 | | 'PACKAGE_PATH="/opt/kestrun/app/app.krpack"' |
| | 1 | 477 | | 'SERVICE_ROOT="/opt/kestrun/service"' |
| | 1 | 478 | | 'PERSISTENT_ROOT="/opt/kestrun/application-data"' |
| | 1 | 479 | | 'ENTRYPOINT_FILE="/opt/kestrun/app/entrypoint-path.txt"' |
| | 1 | 480 | | '' |
| | 1 | 481 | | 'mkdir -p "$PERSISTENT_ROOT"' |
| | 1 | 482 | | '' |
| | 1 | 483 | | "pwsh -NoLogo -NoProfile -File - <<'POWERSHELL'" |
| | 1 | 484 | | '$ErrorActionPreference = ''Stop''' |
| | 1 | 485 | | '$packagePath = ''/opt/kestrun/app/app.krpack''' |
| | 1 | 486 | | '$serviceRoot = ''/opt/kestrun/service''' |
| | 1 | 487 | | '$entrypointFile = ''/opt/kestrun/app/entrypoint-path.txt''' |
| | 1 | 488 | | '$serviceRootWithSeparator = ([System.IO.Path]::GetFullPath($serviceRoot)).TrimEnd(' |
| | 1 | 489 | | ' [System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar) +' |
| | 1 | 490 | | ' [System.IO.Path]::DirectorySeparatorChar' |
| | 1 | 491 | | '$applicationDataDefinitions = @(' |
| | 1 | 492 | | ).ForEach({ $entrypointLines.Add($_) }) |
| | | 493 | | |
| | 1 | 494 | | foreach ($applicationDataDefinition in $applicationDataDefinitions) { |
| | 3 | 495 | | $entrypointLines.Add(" [pscustomobject]@{ RelativePath = '$($applicationDataDefinition.RelativePath.Repla |
| | | 496 | | } |
| | | 497 | | |
| | 1 | 498 | | @( |
| | 1 | 499 | | ')' |
| | 1 | 500 | | 'if (Test-Path -LiteralPath $serviceRoot) {' |
| | 1 | 501 | | ' Remove-Item -LiteralPath $serviceRoot -Recurse -Force' |
| | 1 | 502 | | '}' |
| | 1 | 503 | | '$null = New-Item -ItemType Directory -Path $serviceRoot -Force' |
| | 1 | 504 | | 'Expand-Archive -LiteralPath $packagePath -DestinationPath $serviceRoot -Force' |
| | 1 | 505 | | '$descriptorPath = [System.IO.Path]::Combine($serviceRoot, ''Service.psd1'')' |
| | 1 | 506 | | '$descriptor = Import-PowerShellDataFile -LiteralPath $descriptorPath' |
| | 1 | 507 | | 'if (-not $descriptor.ContainsKey(''EntryPoint'') -or [string]::IsNullOrWhiteSpace([string]$descriptor[''Ent |
| | 1 | 508 | | ' throw (''Descriptor {0} is missing required key EntryPoint.'' -f $descriptorPath)' |
| | 1 | 509 | | '}' |
| | 1 | 510 | | 'foreach ($applicationDataDefinition in $applicationDataDefinitions) {' |
| | 1 | 511 | | ' $relativePath = [string]$applicationDataDefinition.RelativePath' |
| | 1 | 512 | | ' $storagePath = [string]$applicationDataDefinition.StoragePath' |
| | 1 | 513 | | ' $servicePath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($serviceRoot, $relativePath))' |
| | 1 | 514 | | ' if ($servicePath -eq $serviceRoot -or -not $servicePath.StartsWith($serviceRootWithSeparator, [System.S |
| | 1 | 515 | | ' throw (''ApplicationDataFolders entry ''''{0}'''' must resolve to a subdirectory under the service |
| | 1 | 516 | | ' }' |
| | 1 | 517 | | ' $storageDirectory = [System.IO.Path]::GetDirectoryName($storagePath)' |
| | 1 | 518 | | ' if (-not [string]::IsNullOrWhiteSpace($storageDirectory)) {' |
| | 1 | 519 | | ' $null = New-Item -ItemType Directory -Path $storageDirectory -Force' |
| | 1 | 520 | | ' }' |
| | 1 | 521 | | ' if (-not (Test-Path -LiteralPath $storagePath -PathType Container)) {' |
| | 1 | 522 | | ' $null = New-Item -ItemType Directory -Path $storagePath -Force' |
| | 1 | 523 | | ' }' |
| | 1 | 524 | | ' $storageChildren = @(Get-ChildItem -LiteralPath $storagePath -Force -ErrorAction SilentlyContinue)' |
| | 1 | 525 | | ' if ((Test-Path -LiteralPath $servicePath -PathType Container) -and $storageChildren.Count -eq 0) {' |
| | 1 | 526 | | ' foreach ($child in Get-ChildItem -LiteralPath $servicePath -Force -ErrorAction SilentlyContinue) {' |
| | 1 | 527 | | ' Copy-Item -LiteralPath $child.FullName -Destination $storagePath -Recurse -Force' |
| | 1 | 528 | | ' }' |
| | 1 | 529 | | ' }' |
| | 1 | 530 | | ' if (Test-Path -LiteralPath $servicePath) {' |
| | 1 | 531 | | ' Remove-Item -LiteralPath $servicePath -Recurse -Force' |
| | 1 | 532 | | ' }' |
| | 1 | 533 | | ' $serviceDirectory = [System.IO.Path]::GetDirectoryName($servicePath)' |
| | 1 | 534 | | ' if (-not [string]::IsNullOrWhiteSpace($serviceDirectory)) {' |
| | 1 | 535 | | ' $null = New-Item -ItemType Directory -Path $serviceDirectory -Force' |
| | 1 | 536 | | ' }' |
| | 1 | 537 | | ' $null = New-Item -ItemType SymbolicLink -Path $servicePath -Target $storagePath' |
| | 1 | 538 | | '}' |
| | 1 | 539 | | '$entryPointPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($serviceRoot, [string]$descriptor |
| | 1 | 540 | | '[System.IO.File]::WriteAllText($entrypointFile, $entryPointPath, [System.Text.UTF8Encoding]::new($false))' |
| | 1 | 541 | | 'POWERSHELL' |
| | 1 | 542 | | '' |
| | 1 | 543 | | 'ENTRYPOINT_PATH=$(cat "$ENTRYPOINT_FILE")' |
| | 1 | 544 | | '' |
| | 2 | 545 | | ('export ASPNETCORE_URLS="${{ASPNETCORE_URLS:-http://+:{0}}}"' -f $ContainerPort) |
| | 2 | 546 | | ('export PORT="${{PORT:-{0}}}"' -f $ContainerPort) |
| | 1 | 547 | | '' |
| | 1 | 548 | | 'cd "$SERVICE_ROOT"' |
| | 1 | 549 | | 'exec pwsh -NoLogo -File "$ENTRYPOINT_PATH" "$@"' |
| | 1 | 550 | | ).ForEach({ $entrypointLines.Add($_) }) |
| | 1 | 551 | | $entrypointContent = $entrypointLines -join "`n" |
| | | 552 | | |
| | 1 | 553 | | $dockerignoreContent = @' |
| | | 554 | | * |
| | | 555 | | !Dockerfile |
| | | 556 | | !docker-compose.yml |
| | | 557 | | !entrypoint.sh |
| | | 558 | | !app.krpack |
| | | 559 | | !Kestrun/ |
| | | 560 | | !Kestrun/** |
| | | 561 | | '@ |
| | | 562 | | |
| | 1 | 563 | | if (-not $PSCmdlet.ShouldProcess($resolvedOutputPath, 'Create Docker deployment bundle')) { |
| | | 564 | | return |
| | | 565 | | } |
| | | 566 | | |
| | 2 | 567 | | if (-not (Test-Path -LiteralPath $resolvedOutputPath -PathType Container)) { |
| | 1 | 568 | | $null = New-Item -ItemType Directory -Path $resolvedOutputPath -Force |
| | | 569 | | } |
| | | 570 | | |
| | 2 | 571 | | Set-KrGeneratedFileContent -Path $composePath -Content $composeContent -Confirm:$false -WhatIf:$false | Out-Null |
| | 2 | 572 | | Set-KrGeneratedFileContent -Path $dockerfilePath -Content $dockerfileContent -Confirm:$false -WhatIf:$false | Ou |
| | 2 | 573 | | Set-KrGeneratedFileContent -Path $entrypointPath -Content $entrypointContent -Confirm:$false -WhatIf:$false | Ou |
| | 2 | 574 | | Set-KrGeneratedFileContent -Path $dockerignorePath -Content $dockerignoreContent -Confirm:$false -WhatIf:$false |
| | | 575 | | |
| | 2 | 576 | | if ((Test-Path -LiteralPath $packageDestinationPath -PathType Leaf) -and -not $Force) { |
| | 0 | 577 | | throw "Output file already exists: $packageDestinationPath. Use -Force to overwrite." |
| | | 578 | | } |
| | | 579 | | |
| | 1 | 580 | | Copy-Item -LiteralPath $resolvedPackagePath -Destination $packageDestinationPath -Force |
| | 2 | 581 | | Copy-KrGeneratedDirectory -SourcePath $resolvedModuleRoot -DestinationPath $moduleDestinationPath -Confirm:$fals |
| | | 582 | | |
| | 2 | 583 | | [pscustomobject]([ordered]@{ |
| | 1 | 584 | | PackagePath = $resolvedPackagePath |
| | 1 | 585 | | DeploymentPath = $resolvedOutputPath |
| | 1 | 586 | | ComposePath = $composePath |
| | 1 | 587 | | DockerfilePath = $dockerfilePath |
| | 1 | 588 | | EntrypointPath = $entrypointPath |
| | 1 | 589 | | DockerignorePath = $dockerignorePath |
| | 1 | 590 | | ServiceName = $normalizedServiceName |
| | 1 | 591 | | ImageName = $resolvedImageName |
| | 1 | 592 | | DescriptorName = $descriptor.Name |
| | 1 | 593 | | Version = $descriptor.Version |
| | 1 | 594 | | EntryPoint = $descriptor.EntryPoint |
| | 1 | 595 | | PublishedPort = $PublishedPort |
| | 1 | 596 | | ContainerPort = $ContainerPort |
| | | 597 | | }) |
| | | 598 | | } finally { |
| | 2 | 599 | | if (-not [string]::IsNullOrWhiteSpace($temporaryExtractionRoot) -and (Test-Path -LiteralPath $temporaryExtractio |
| | 1 | 600 | | Remove-Item -LiteralPath $temporaryExtractionRoot -Recurse -Force -ErrorAction SilentlyContinue |
| | | 601 | | } |
| | | 602 | | } |
| | | 603 | | } |