golang.org/x/build@v0.0.0-20240506185731-218518f32b70/env/windows/startup.ps1 (about)

     1  # Copyright 2017 The Go Authors. All rights reserved.
     2  # Use of this source code is governed by a BSD-style
     3  # license that can be found in the LICENSE file.
     4  
     5  Set-StrictMode -Version Latest
     6  
     7  # Helpers
     8  function Test-RegistryKeyExists($path, $name)
     9  {
    10      $key = Get-Item -LiteralPath $path -ErrorAction SilentlyContinue
    11      ($key -and $null -ne $key.GetValue($name, $null)) -ne $false
    12  }
    13  
    14  function Get-FileFromUrl(
    15  	[string] $URL,
    16  	[string] $Output)
    17  {
    18      Add-Type -AssemblyName "System.Net.Http"
    19  
    20      $client = New-Object System.Net.Http.HttpClient
    21      $request = New-Object System.Net.Http.HttpRequestMessage -ArgumentList @([System.Net.Http.HttpMethod]::Get, $URL)
    22      $responseMsg = $client.SendAsync($request)
    23      $responseMsg.Wait()
    24  
    25      if (!$responseMsg.IsCanceled)
    26      {
    27  			$response = $responseMsg.Result
    28  			if ($response.IsSuccessStatusCode)
    29  			{
    30  			    $downloadedFileStream = [System.IO.File]::Create($Output)
    31  			    $copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream)
    32  			    $copyStreamOp.Wait()
    33  			    $downloadedFileStream.Close()
    34  			    if ($copyStreamOp.Exception -ne $null)
    35  			    {
    36  				throw $copyStreamOp.Exception
    37  			    }
    38  			}
    39      }
    40  }
    41  
    42  # https://social.technet.microsoft.com/Forums/ie/en-US/29508e4e-a2b5-42eb-9729-6eca473716ae/disabling-password-complexity-via-command?forum=ITCG
    43  function Disable-PasswordComplexity
    44  {
    45      param()
    46  
    47      $secEditPath = [System.Environment]::ExpandEnvironmentVariables("%SystemRoot%\system32\secedit.exe")
    48      $tempFile = [System.IO.Path]::GetTempFileName()
    49  
    50      $exportArguments = '/export /cfg "{0}" /quiet' -f $tempFile
    51      $importArguments = '/configure /db secedit.sdb /cfg "{0}" /quiet' -f $tempFile
    52  
    53      Start-Process -FilePath $secEditPath -ArgumentList $exportArguments -Wait
    54  
    55      $currentConfig = Get-Content -Path $tempFile
    56  
    57      $currentConfig = $currentConfig -replace 'PasswordComplexity = .', 'PasswordComplexity = 0'
    58      $currentConfig = $currentConfig -replace 'MinimumPasswordLength = .', 'MinimumPasswordLength = 0'
    59      $currentConfig | Out-File -FilePath $tempFile
    60  
    61      Start-Process -FilePath $secEditPath -ArgumentList $importArguments -Wait
    62     
    63      Remove-Item -Path .\secedit.sdb
    64      Remove-Item -Path $tempFile
    65  }
    66  
    67  # Wait till network comes up
    68  while(-Not (Test-NetConnection 169.254.169.254 -Port 53 | ? { $_.TcpTestSucceeded })) {
    69    Write-Host "waiting for network (metadata service) to come up"
    70    sleep 3
    71  }
    72  while(-Not (Test-NetConnection 8.8.8.8 -Port 53 | ? { $_.TcpTestSucceeded })) {
    73    Write-Host "waiting for network (external network) to come up"
    74    sleep 3
    75  }
    76  
    77  # Disable password complexity, automatic updates, windows defender, windows firewall, error reporting, and UAC
    78  # 
    79  # - Update can interrupt the builds
    80  # - We don't care about security since this isn't going to be Internet-facing
    81  # - No ports will ever be accessible externally
    82  # - We can be trusted to run as a real Administrator
    83  Write-Host "disabling security features"
    84  Disable-PasswordComplexity
    85  New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name NoAutoUpdate -Value 1 -Force | Out-Null
    86  new-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\Windows Error Reporting" -Name Disabled -Value 1 -Force | Out-Null
    87  new-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\Windows Error Reporting" -Name DontShowUI -Value 1 -Force | Out-Null
    88  New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" -Name EnableLUA -PropertyType DWord -Value 0 -Force | Out-Null
    89  netsh advfirewall set allprofiles state off
    90  netsh firewall set opmode mode=disable profile=ALL
    91  Uninstall-WindowsFeature -Name Windows-Defender
    92  
    93  # Disable unwanted services
    94  Write-Host "disabling unused services"
    95  Set-Service -Name 'NlaSvc' -StartupType 'Disabled'
    96  Set-Service -Name 'LanmanServer' -StartupType 'Disabled'
    97  Set-Service -Name 'MpsSvc' -StartupType 'Disabled'
    98  Set-Service -Name 'BITS' -StartupType 'Disabled'
    99  Set-Service -Name 'DPS' -StartupType 'Disabled'
   100  Set-Service -Name 'MSDTC' -StartupType 'Disabled'
   101  Set-Service -Name 'IKEEXT' -StartupType 'Disabled'
   102  Set-Service -Name 'RemoteRegistry' -StartupType 'Disabled'
   103  Set-Service -Name 'lmhosts' -StartupType 'Disabled'
   104  
   105  # Download buildlet
   106  Write-Host "downloading stage0"
   107  $builder_dir = "C:\golang"
   108  $bootstrap_exe_path = "$builder_dir\bootstrap.exe"
   109  mkdir $builder_dir
   110  Get-FileFromUrl -URL 'https://storage.googleapis.com/go-builder-data/buildlet-stage0.windows-amd64' -Output $bootstrap_exe_path
   111  
   112  # OpenSSH (from https://github.com/PowerShell/Win32-OpenSSH/releases)
   113  Write-Host "downloading OpenSSH"
   114  $openssh_tar = "$builder_dir\openssh.tar.gz"
   115  Get-FileFromUrl -URL 'https://storage.googleapis.com/go-builder-data/win32-openssh-0.0.18.0.tar.gz' -Output "$openssh_tar"
   116  Write-Host "extracting OpenSSH"
   117  $extract_args=@("--untar-file=$openssh_tar", "--untar-dest-dir=$builder_dir")
   118  & $bootstrap_exe_path $extract_args 
   119  Write-Host "Installing OpenSSH"
   120  $openssh_dir = "$builder_dir\OpenSSH-Win32"
   121  cd $openssh_dir
   122  & "$openssh_dir\install-sshd.ps1"
   123  & "$openssh_dir\ssh-keygen.exe" "-A"
   124  & "$openssh_dir\FixHostFilePermissions.ps1" -Confirm:$false
   125  
   126  Set-Service -Name 'sshd' -StartupType 'Automatic'
   127  Set-Service -Name 'ssh-agent' -StartupType 'Automatic'
   128  
   129  # Download and unpack GCC
   130  Write-Host "downloading GCC"
   131  $dep_dir = "C:\godep"
   132  $gcc32_tar = "$dep_dir\gcc32.tar.gz"
   133  $gcc64_tar = "$dep_dir\gcc64.tar.gz"
   134  mkdir $dep_dir
   135  Get-FileFromUrl -URL "https://storage.googleapis.com/go-builder-data/llvm-mingw-20220323-msvcrt-i686.tar.gz" -Output "$gcc32_tar"
   136  Get-FileFromUrl -URL "https://storage.googleapis.com/go-builder-data/llvm-mingw-20220323-msvcrt-x86_64.tar.gz" -Output "$gcc64_tar"
   137  
   138  Write-Host "extracting GCC"
   139  $extract32_args=@("--untar-file=$gcc32_tar", "--untar-dest-dir=$dep_dir")
   140  & $bootstrap_exe_path $extract32_args 
   141  $extract64_args=@("--untar-file=$gcc64_tar", "--untar-dest-dir=$dep_dir")
   142  & $bootstrap_exe_path $extract64_args 
   143  
   144  $builder_dir = "C:\golang"
   145  $bootstrap_exe_path = "$builder_dir\bootstrap.exe"
   146  
   147  # Download and install Visual Studio Build Tools (MSVC)
   148  # https://docs.microsoft.com/en-us/visualstudio/install/build-tools-container
   149  Write-Host "downloading Visual Studio Build Tools"
   150  $vs_buildtools = "$builder_dir\vs_buildtools.exe"
   151  Get-FileFromUrl -URL "https://storage.googleapis.com/go-builder-data/vs_buildtools.exe" -Output "$vs_buildtools"
   152  
   153  Write-Host "installing Visual Studio Build Tools"
   154  & $vs_buildtools --quiet --wait --norestart --nocache --installPath "$dep_dir\vs" --all
   155  
   156  # Download and install the root certificate used for crypto/x509 testing
   157  Write-Host "downloading crypto/x509 test root"
   158  $test_root = "$builder_dir\test_root.pem"
   159  Get-FileFromUrl -URL "https://storage.googleapis.com/go-builder-data/platform_root_cert.pem" -Output "$test_root"
   160  
   161  Write-Host "installing crypto/x509 test root"
   162  Import-Certificate -FilePath "$test_root" -CertStoreLocation "Cert:\LocalMachine\Root"
   163  
   164  # Create a buildlet user
   165  Write-Host "creating buildlet user"
   166  $buildlet_user = "gopher"
   167  $buildlet_password = "gopher"
   168  net user $buildlet_user $buildlet_password /ADD
   169  net localgroup administrators $buildlet_user /ADD
   170  
   171  # Run the bootstrap program on login
   172  Write-Host "setting stage0 to run on start"
   173  $bootstrap_cmd = "cmd /k ""cd $builder_dir && $bootstrap_exe_path"""
   174  New-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "Buildlet" -PropertyType ExpandString -Value $bootstrap_cmd -Force
   175  
   176  # Setup autologon and reboot
   177  $RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
   178  if ((Test-RegistryKeyExists $RegPath "DefaultUsername") -eq $false) {
   179    Write-Host "configuring auto login"
   180    Remove-ItemProperty -Path 'HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Name 'AutoLogonCount' -Force | Out-Null
   181    Set-ItemProperty $RegPath "AutoAdminLogon" -Value "1" -type String 
   182    Set-ItemProperty $RegPath "DefaultUsername" -Value "$buildlet_user" -type String 
   183    Set-ItemProperty $RegPath "DefaultPassword" -Value "$buildlet_password" -type String
   184    Set-ItemProperty $RegPath "LogonCount" -Value "99999999" -type String
   185    Write-Host "rebooting"
   186    shutdown /r /t 0
   187  }