From 8960ff7dc9d8763ba95241a089214d32f9c5245c Mon Sep 17 00:00:00 2001 From: Mikhail Goncharov Date: Tue, 1 Jun 2021 15:26:00 +0200 Subject: [PATCH] Pre-checkout for windows Kill all running processes in BUILDKITE_BUILD_PATH and force unlock git folder. --- containers/agent-windows-buildkite/Dockerfile | 1 - .../agent-windows-buildkite/pre-checkout.bat | 7 +- .../agent-windows-buildkite/start_agent.ps1 | 2 +- containers/build_run.ps1 | 89 ++++++++++--------- scripts/steps.py | 2 +- scripts/windows/pre-checkout.ps1 | 26 ++++++ scripts/windows/start_container.ps1 | 53 +++++++++++ .../windows}/unlock_path.ps1 | 17 +++- scripts/windows_agent_start_buildkite.ps1 | 40 ++------- 9 files changed, 154 insertions(+), 83 deletions(-) create mode 100644 scripts/windows/pre-checkout.ps1 create mode 100644 scripts/windows/start_container.ps1 rename {containers/agent-windows-buildkite => scripts/windows}/unlock_path.ps1 (63%) diff --git a/containers/agent-windows-buildkite/Dockerfile b/containers/agent-windows-buildkite/Dockerfile index 09faf0c..e4c7517 100644 --- a/containers/agent-windows-buildkite/Dockerfile +++ b/containers/agent-windows-buildkite/Dockerfile @@ -7,5 +7,4 @@ FROM gcr.io/llvm-premerge-checks/agent-windows-vs2019:${agent_windows_version} RUN choco install -y handle --checksum 524E61547C8E26608CDA1B11B6E9471616CCCC48530F6E7EC9131EABF839357E COPY start_agent.ps1 C:\scripts\ COPY pre-checkout.bat c:\buildkite-agent\hooks\ -COPY unlock_path.ps1 c:\scripts CMD "powershell C:\scripts\start_agent.ps1" \ No newline at end of file diff --git a/containers/agent-windows-buildkite/pre-checkout.bat b/containers/agent-windows-buildkite/pre-checkout.bat index a0d12aa..ff6f06e 100644 --- a/containers/agent-windows-buildkite/pre-checkout.bat +++ b/containers/agent-windows-buildkite/pre-checkout.bat @@ -1,2 +1,5 @@ -handle -nobanner %BUILDKITE_BUILD_CHECKOUT_PATH% -powershell c:\scripts\unlock_path.ps1 %BUILDKITE_BUILD_CHECKOUT_PATH% \ No newline at end of file +echo "pre-checkout: update scripts" +cd c:\llvm-premerge-checks +git pull +git rev-parse HEAD +powershell c:\llvm-premerge-checks\scripts\windows\pre-checkout.ps1 \ No newline at end of file diff --git a/containers/agent-windows-buildkite/start_agent.ps1 b/containers/agent-windows-buildkite/start_agent.ps1 index 5b95b0c..9b77fa5 100644 --- a/containers/agent-windows-buildkite/start_agent.ps1 +++ b/containers/agent-windows-buildkite/start_agent.ps1 @@ -1,7 +1,7 @@ c:\credentials\buildkite-env.ps1 +git clone https://github.com/google/llvm-premerge-checks.git c:\llvm-premerge-checks # Install Buildkite agent. iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/buildkite/agent/master/install.ps1')) - $env:SCCACHE_DIR="C:\ws\sccache" $env:SCCACHE_IDLE_TIMEOUT="0" Remove-Item -Recurse -Force -ErrorAction Ignore $env:SCCACHE_DIR diff --git a/containers/build_run.ps1 b/containers/build_run.ps1 index 3717bd2..2d2940d 100644 --- a/containers/build_run.ps1 +++ b/containers/build_run.ps1 @@ -1,43 +1,46 @@ -# Copyright 2019 Google LLC -# -# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://llvm.org/LICENSE.txt -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# define command line arguments -param( - [Parameter(Mandatory=$true)][string]$IMAGE_NAME, - [Parameter(Mandatory=$false)][string]$CMD="", - [string]$token -) - -# set script to stop on first error -$ErrorActionPreference = "Stop" - -# some docs recommend setting 2GB memory limit -docker build ` - --memory 2GB ` - -t $IMAGE_NAME ` - --build-arg token=$token ` - "$PSScriptRoot\$IMAGE_NAME" -If ($LastExitCode -ne 0) { - exit -} - -# mount a persistent workspace for experiments -docker run -it ` - -v C:/ws:C:/ws ` - -v C:/credentials:C:/credentials ` - -e PARENT_HOSTNAME=$env:computername ` - $IMAGE_NAME $CMD -If ($LastExitCode -ne 0) { - exit -} \ No newline at end of file +# Copyright 2019 Google LLC +# +# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://llvm.org/LICENSE.txt +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# define command line arguments +param( + [Parameter(Mandatory=$true)][string]$IMAGE_NAME, + [Parameter(Mandatory=$false)][string]$CMD="", + [string]$token +) + +# set script to stop on first error +$ErrorActionPreference = "Stop" + +# some docs recommend setting 2GB memory limit +docker build ` + --memory 2GB ` + -t $IMAGE_NAME ` + --build-arg token=$token ` + "$PSScriptRoot\$IMAGE_NAME" +If ($LastExitCode -ne 0) { + exit +} + +$DIGEST=$(docker image inspect --format "{{range .RepoDigests}}{{.}}{{end}}" $IMAGE_NAME) -replace ".*@sha256:(.{6})(.*)$","`$1" +# mount a persistent workspace for experiments +docker run -it ` + -v C:/ws:C:/ws ` + -v C:/credentials:C:/credentials ` + -e BUILDKITE_BUILD_PATH=C:\ws ` + -e IMAGE_DIGEST=${DIGEST} ` + -e PARENT_HOSTNAME=$env:computername ` + $IMAGE_NAME $CMD +If ($LastExitCode -ne 0) { + exit +} diff --git a/scripts/steps.py b/scripts/steps.py index a709a87..e22a088 100644 --- a/scripts/steps.py +++ b/scripts/steps.py @@ -110,7 +110,7 @@ def generic_windows(projects: str) -> List: ], 'artifact_paths': ['artifacts/**/*', '*_result.json', 'build/test-results.xml'], 'agents': win_agents, - 'timeout_in_minutes': 120, + 'timeout_in_minutes': 150, 'retry': {'automatic': [ {'exit_status': -1, 'limit': 2}, # Agent lost {'exit_status': 255, 'limit': 2}, # Forced agent shutdown diff --git a/scripts/windows/pre-checkout.ps1 b/scripts/windows/pre-checkout.ps1 new file mode 100644 index 0000000..221681d --- /dev/null +++ b/scripts/windows/pre-checkout.ps1 @@ -0,0 +1,26 @@ +# Copyright 2021 Google LLC + +# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# https://llvm.org/LICENSE.txt + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +echo "BUILDKITE_BUILD_CHECKOUT_PATH: $env:BUILDKITE_BUILD_CHECKOUT_PATH" +echo "unlocking git" +taskkill /F /IM git.exe +rm -Force "$env:BUILDKITE_BUILD_CHECKOUT_PATH/.git/index.lock" +echo "BUILDKITE_BUILD_PATH: $env:BUILDKITE_BUILD_PATH" +echo 'running processes (before)' +Get-Process | Where-Object {$_.Path -like "$env:BUILDKITE_BUILD_PATH*"} | Select-Object -ExpandProperty Path +echo "unlocking $env:BUILDKITE_BUILD_PATH" +handle -nobanner $env:BUILDKITE_BUILD_CHECKOUT_PATH +c:\llvm-premerge-checks\scripts\windows\unlock_path.ps1 $env:BUILDKITE_BUILD_CHECKOUT_PATH +echo 'running processes (after)' +Get-Process | Where-Object {$_.Path -like "$env:BUILDKITE_BUILD_PATH*"} | Select-Object -ExpandProperty Path diff --git a/scripts/windows/start_container.ps1 b/scripts/windows/start_container.ps1 new file mode 100644 index 0000000..181cf79 --- /dev/null +++ b/scripts/windows/start_container.ps1 @@ -0,0 +1,53 @@ +# Copyright 2021 Google LLC + +# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# https://llvm.org/LICENSE.txt + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Pull and start the Docker container for a Windows agent. +# To setup a Windows agent see docs/playbooks.md + +param( + [string]$version = "stable", + [switch]$testing = $false, + [string]$workdir = "c:\ws" +) + +$NAME="agent-windows-buildkite" +$IMAGE="gcr.io/llvm-premerge-checks/${NAME}:${version}" + +Write-Output "Authenticating docker..." +Write-Output "y`n" | gcloud auth configure-docker + +Write-Output "Pulling new image '${IMAGE}'..." +docker pull ${IMAGE} +$DIGEST=$(docker image inspect --format "{{range .RepoDigests}}{{.}}{{end}}" $IMAGE) -replace ".*@sha256:(.{6})(.*)$","`$1" +Write-Output "Image digest ${DIGEST}" +Write-Output "Stopping old container..." +docker stop ${NAME} +docker rm ${NAME} +Write-Output "Starting container..." +if (${testing}) { + docker run -it ` + -v ${workdir}:C:\ws ` + -v C:\credentials:C:\credentials ` + -e BUILDKITE_BUILD_PATH=C:\ws ` + -e IMAGE_DIGEST=${DIGEST} ` + ${IMAGE} powershell +} else { + docker run -d ` + -v ${workdir}:C:\ws ` + -v C:\credentials:C:\credentials ` + -e BUILDKITE_BUILD_PATH=C:\ws ` + -e IMAGE_DIGEST=${DIGEST} ` + --name ${NAME} ` + ${IMAGE} +} diff --git a/containers/agent-windows-buildkite/unlock_path.ps1 b/scripts/windows/unlock_path.ps1 similarity index 63% rename from containers/agent-windows-buildkite/unlock_path.ps1 rename to scripts/windows/unlock_path.ps1 index 8572c61..d643b7f 100644 --- a/containers/agent-windows-buildkite/unlock_path.ps1 +++ b/scripts/windows/unlock_path.ps1 @@ -1,12 +1,25 @@ +# Copyright 2021 Google LLC + +# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# https://llvm.org/LICENSE.txt + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # Based ob https://dandraka.com/2019/08/13/find-and-kill-processes-that-lock-a-file-or-directory/ $path = $args[0] $handleOutput = & handle -a $path - +Write-Host "Unlocking path" $path if ($handleOutput -match "no matching handles found") { Write-Host "Nothing to kill, exiting" exit } - $pidList = New-Object System.Collections.ArrayList $lines = $handleOutput -split "`n" foreach($line in $lines) { diff --git a/scripts/windows_agent_start_buildkite.ps1 b/scripts/windows_agent_start_buildkite.ps1 index 0f5b77f..2364b56 100644 --- a/scripts/windows_agent_start_buildkite.ps1 +++ b/scripts/windows_agent_start_buildkite.ps1 @@ -12,42 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Pull and start the Docker container for a Windows agent. -# To setup a Windows agent see docs/playbooks.md +# Invoked at startup of windows machine. This indirection allows to simply +# restart a machine and it will pick up all script changes and will use the +# latest stable image. param( - [string]$version = "latest", + [string]$version = "stable", [switch]$testing = $false, [string]$workdir = "c:\ws" ) -$NAME="agent-windows-buildkite" -$IMAGE="gcr.io/llvm-premerge-checks/${NAME}:${version}" - -Write-Output "Authenticating docker..." -Write-Output "y`n" | gcloud auth configure-docker - -Write-Output "Pulling new image '${IMAGE}'..." -docker pull ${IMAGE} -$DIGEST=$(docker image inspect --format "{{range .RepoDigests}}{{.}}{{end}}" $IMAGE) -replace ".*@sha256:(.{6})(.*)$","`$1" -Write-Output "Image digest ${DIGEST}" -Write-Output "Stopping old container..." -docker stop ${NAME} -docker rm ${NAME} -Write-Output "Starting container..." -if (${testing}) { - docker run -it ` - -v ${workdir}:C:\ws ` - -v C:\credentials:C:\credentials ` - -e BUILDKITE_BUILD_PATH=C:\ws ` - -e IMAGE_DIGEST=${DIGEST} ` - ${IMAGE} powershell -} else { - docker run -d ` - -v ${workdir}:C:\ws ` - -v C:\credentials:C:\credentials ` - -e BUILDKITE_BUILD_PATH=C:\ws ` - -e IMAGE_DIGEST=${DIGEST} ` - --name ${NAME} ` - ${IMAGE} -} +cd c:\llvm-premerge-checks +git pull +c:\llvm-premerge-checks\scripts\windows\start_container.ps1 -version $version -testing $testing -workdir $workdir