From f93413492e47983bafbc29ab84cb697aeeb41f7b Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Tue, 3 Aug 2021 20:17:31 +0300 Subject: [PATCH] [Windows] Move Windows features to toolset (#3838) * move windows features to toolset * Update WindowsFeatures.Tests.ps1 * revert templates --- .../scripts/ImageHelpers/InstallHelpers.ps1 | 1 + .../scripts/Installers/Install-DotnetSDK.ps1 | 18 +++++--- .../scripts/Installers/Install-PostgreSQL.ps1 | 2 +- .../Installers/Install-WindowsFeatures.ps1 | 42 ++++++++++--------- .../scripts/Tests/WindowsFeatures.Tests.ps1 | 34 ++++----------- images/win/toolsets/toolset-2016.json | 8 ++++ images/win/toolsets/toolset-2019.json | 6 +++ 7 files changed, 60 insertions(+), 51 deletions(-) diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 index 25a785858..14a164832 100644 --- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 @@ -417,6 +417,7 @@ function Install-AndroidSDKPackages { [Parameter(Mandatory=$true)] [string]$AndroidSDKRootPath, [Parameter(Mandatory=$true)] + [AllowEmptyCollection()] [string[]]$AndroidPackages, [string] $PrefixPackageName ) diff --git a/images/win/scripts/Installers/Install-DotnetSDK.ps1 b/images/win/scripts/Installers/Install-DotnetSDK.ps1 index 27cd6d39c..4a9af0f07 100644 --- a/images/win/scripts/Installers/Install-DotnetSDK.ps1 +++ b/images/win/scripts/Installers/Install-DotnetSDK.ps1 @@ -29,6 +29,18 @@ function Invoke-Warmup ( } } +function Fix-ImportPublishProfile ( + $SdkVersion +) { + if (Test-IsWin16 -or Test-IsWin19) { + # Fix for issue https://github.com/dotnet/sdk/issues/1276. This will be fixed in 3.1. + $sdkTargetsName = "Microsoft.NET.Sdk.ImportPublishProfile.targets" + $sdkTargetsUrl = "https://raw.githubusercontent.com/dotnet/sdk/82bc30c99f1325dfaa7ad450be96857a4fca2845/src/Tasks/Microsoft.NET.Build.Tasks/targets/${sdkTargetsName}" + $sdkTargetsPath = "C:\Program Files\dotnet\sdk\$sdkVersion\Sdks\Microsoft.NET.Sdk\targets" + Start-DownloadWithRetry -Url $sdkTargetsUrl -DownloadPath $sdkTargetsPath -Name $sdkTargetsName + } +} + function InstallSDKVersion ( $SdkVersion, $Warmup @@ -44,11 +56,7 @@ function InstallSDKVersion ( Write-Host "Sdk version $sdkVersion already installed" } - # Fix for issue 1276. This will be fixed in 3.1. - $sdkTargetsName = "Microsoft.NET.Sdk.ImportPublishProfile.targets" - $sdkTargetsUrl = "https://raw.githubusercontent.com/dotnet/sdk/82bc30c99f1325dfaa7ad450be96857a4fca2845/src/Tasks/Microsoft.NET.Build.Tasks/targets/${sdkTargetsName}" - $sdkTargetsPath = "C:\Program Files\dotnet\sdk\$sdkVersion\Sdks\Microsoft.NET.Sdk\targets" - Start-DownloadWithRetry -Url $sdkTargetsUrl -DownloadPath $sdkTargetsPath -Name $sdkTargetsName + Fix-ImportPublishProfile -SdkVersion $SdkVersion if ($Warmup) { Invoke-Warmup -SdkVersion $SdkVersion diff --git a/images/win/scripts/Installers/Install-PostgreSQL.ps1 b/images/win/scripts/Installers/Install-PostgreSQL.ps1 index 0f88bb7f4..8fe78c7fb 100644 --- a/images/win/scripts/Installers/Install-PostgreSQL.ps1 +++ b/images/win/scripts/Installers/Install-PostgreSQL.ps1 @@ -7,7 +7,7 @@ Set-SystemVariable -SystemVariable PGUSER -Value $pgUser Set-SystemVariable -SystemVariable PGPASSWORD -Value $pgPwd #Install latest PostgreSQL -Choco-Install -PackageName postgresql -ArgumentList "--params", "'/Password:$pgPwd /NoPath'", "--params-global", "--debug", "--verbose" +Choco-Install -PackageName postgresql -ArgumentList "--params", "'/Password:$pgPwd /NoPath'", "--params-global" #Get Path to pg_ctl.exe $pgPath = (Get-CimInstance Win32_Service -Filter "Name LIKE 'postgresql-%'").PathName diff --git a/images/win/scripts/Installers/Install-WindowsFeatures.ps1 b/images/win/scripts/Installers/Install-WindowsFeatures.ps1 index bfee3730b..8435eb258 100644 --- a/images/win/scripts/Installers/Install-WindowsFeatures.ps1 +++ b/images/win/scripts/Installers/Install-WindowsFeatures.ps1 @@ -1,24 +1,26 @@ -# Install .NET Framework 3.5 (required by Chocolatey) -# Explicitly install all 4.7 sub features to include ASP.Net. -# As of 1/16/2019, WinServer 19 lists .Net 4.7 as NET-Framework-45-Features -Install-WindowsFeature -Name NET-Framework-Features -IncludeAllSubFeature -Install-WindowsFeature -Name NET-Framework-45-Features -IncludeAllSubFeature +$windowsFeatures = (Get-ToolsetContent).windowsFeatures -if (Test-IsWin16) { - Install-WindowsFeature -Name BITS -IncludeAllSubFeature - Install-WindowsFeature -Name DSC-Service -} +foreach ($feature in $windowsFeatures) { + if ($feature.optionalFeature) { + Write-Host "Activating Windows Optional Feature '$($feature.name)'..." + Enable-WindowsOptionalFeature -Online -FeatureName $feature.name -NoRestart -# Install FS-iSCSITarget-Server -$fsResult = Install-WindowsFeature -Name FS-iSCSITarget-Server -IncludeAllSubFeature -IncludeManagementTools -if ( $fsResult.Success ) { - Write-Host "FS-iSCSITarget-Server has been successfully installed" -} else { - Write-Host "Failed to install FS-iSCSITarget-Server" - exit 1 -} + $resultSuccess = $? + } else { + Write-Host "Activating Windows Feature '$($feature.name)'..." + $Arguments = @{ + Name = $feature.name + IncludeAllSubFeature = [System.Convert]::ToBoolean($feature.includeAllSubFeatures) + IncludeManagementTools = [System.Convert]::ToBoolean($feature.includeManagementTools) + } + $result = Install-WindowsFeature @Arguments -Write-Host "Install Containers feature" -Install-WindowsFeature -Name Containers + $resultSuccess = $result.Success + } -Invoke-PesterTests -TestFile "WindowsFeatures" -TestName "ContainersFeature" \ No newline at end of file + if ($resultSuccess) { + Write-Host "Windows Feature '$($feature.name)' was activated successfully" + } else { + throw "Failed to activate Windows Feature '$($feature.name)'" + } +} \ No newline at end of file diff --git a/images/win/scripts/Tests/WindowsFeatures.Tests.ps1 b/images/win/scripts/Tests/WindowsFeatures.Tests.ps1 index 96e453018..d4a811d76 100644 --- a/images/win/scripts/Tests/WindowsFeatures.Tests.ps1 +++ b/images/win/scripts/Tests/WindowsFeatures.Tests.ps1 @@ -1,33 +1,17 @@ Describe "WindowsFeatures" { + $windowsFeatures = (Get-ToolsetContent).windowsFeatures + $testCases = $windowsFeatures | ForEach-Object { @{ Name = $_.name; OptionalFeature = $_.optionalFeature } } - $testCases = @( - @{ FeatureName = "NET-Framework-Features" } - @{ FeatureName = "NET-Framework-45-Features" } - @{ FeatureName = "FS-iSCSITarget-Server" } - ) - - if (Test-isWin16) { - $testCases += @{ FeatureName = "BITS" } - $testCases += @{ FeatureName = "DSC-Service" } - } - if (Test-isWin19) { - $testCases += @{ FeatureName = "Microsoft-Windows-Subsystem-Linux" } - } - - It "Windows Feature is installed" -TestCases $testCases { - (Get-WindowsFeature -Name $FeatureName).InstallState | Should -Be "Installed" - } - - if (Test-isWin19) { - it "Check WSL is on path" { - (Get-Command -Name 'wsl') | Should -BeTrue + It "Windows Feature is installed" -TestCases $testCases { + if ($OptionalFeature) { + (Get-WindowsOptionalFeature -Online -FeatureName $Name).State | Should -Be "Enabled" + } else { + (Get-WindowsFeature -Name $Name).InstallState | Should -Be "Installed" } } -} -Describe "ContainersFeature" { - It "Windows containers feature is installed" { - (Get-WindowsFeature -Name "Containers").InstallState | Should -Be "Installed" + it "Check WSL is on path" -Skip:(-not (Test-IsWin19)) { + (Get-Command -Name 'wsl') | Should -BeTrue } } diff --git a/images/win/toolsets/toolset-2016.json b/images/win/toolsets/toolset-2016.json index f9a4b28f7..09a073f7d 100644 --- a/images/win/toolsets/toolset-2016.json +++ b/images/win/toolsets/toolset-2016.json @@ -248,6 +248,14 @@ } ] }, + "windowsFeatures": [ + { "name": "NET-Framework-Features", "includeAllSubFeatures": true }, + { "name": "NET-Framework-45-Features", "includeAllSubFeatures": true }, + { "name": "BITS", "includeAllSubFeatures": true }, + { "name": "DSC-Service" }, + { "name": "FS-iSCSITarget-Server", "includeAllSubFeatures": true, "includeManagementTools": true }, + { "name": "Containers" } + ], "visualStudio": { "version" : "2017", "subversion" : "15", diff --git a/images/win/toolsets/toolset-2019.json b/images/win/toolsets/toolset-2019.json index 40e8d6b8a..e5416a269 100644 --- a/images/win/toolsets/toolset-2019.json +++ b/images/win/toolsets/toolset-2019.json @@ -248,6 +248,12 @@ } ] }, + "windowsFeatures": [ + { "name": "NET-Framework-Features", "includeAllSubFeatures": true }, + { "name": "NET-Framework-45-Features", "includeAllSubFeatures": true }, + { "name": "FS-iSCSITarget-Server", "includeAllSubFeatures": true, "includeManagementTools": true }, + { "name": "Containers" } + ], "visualStudio": { "version" : "2019", "subversion" : "16",