From f85dc037f0b566035a1097e25d0322dedbc3ac83 Mon Sep 17 00:00:00 2001 From: Subash Kotha <47501783+subashkotha@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:33:50 -0800 Subject: [PATCH] ci: Update Remove Finch VM step to only shutdown if required (#1182) * ci: Update Remove Finch VM step Signed-off-by: Subash Kotha * ci: Shutdown only if there are running distributions Signed-off-by: Subash Kotha * ci: Create and reuse script to clean up WSL Signed-off-by: Subash Kotha --------- Signed-off-by: Subash Kotha Co-authored-by: Subash Kotha --- .github/workflows/build-and-test-msi.yaml | 56 ++++------------------- .github/workflows/e2e-windows.yaml | 20 +------- scripts/cleanup_wsl.ps1 | 28 ++++++++++++ 3 files changed, 40 insertions(+), 64 deletions(-) create mode 100644 scripts/cleanup_wsl.ps1 diff --git a/.github/workflows/build-and-test-msi.yaml b/.github/workflows/build-and-test-msi.yaml index 44876a8aa..230b20cb8 100644 --- a/.github/workflows/build-and-test-msi.yaml +++ b/.github/workflows/build-and-test-msi.yaml @@ -81,14 +81,8 @@ jobs: aws-region: ${{ secrets.WINDOWS_REGION }} - name: Remove Finch VM timeout-minutes: 2 - run: | - $ErrorActionPreference = 'Ignore' - taskkill /f /im wslservice.exe 2> nul || cmd /c "exit /b 0" - wsl --list --verbose - sc query LxssManager | findstr "STATE" | findstr /C:"STOPPED" > nul && net start LxssManager - wsl --shutdown - wsl --unregister lima-finch - wsl --list --verbose + shell: pwsh + run: ./scripts/cleanup_wsl.ps1 - name: Clean up previous files run: | takeown /F C:\actions-runner\_work\finch /R @@ -148,16 +142,9 @@ jobs: - name: Remove Finch VM and Clean Up Previous Environment if: ${{ always() }} timeout-minutes: 2 + shell: pwsh run: | - # We want these cleanup commands to always run, ignore errors so the step completes. - $ErrorActionPreference = 'Ignore' - taskkill /f /im wslservice.exe 2> nul || cmd /c "exit /b 0" - wsl --list --verbose - sc query LxssManager | findstr "STATE" | findstr /C:"STOPPED" > nul && net start LxssManager - wsl --shutdown - wsl --unregister lima-finch - wsl --list --verbose - Remove-Item C:\Users\Administrator\AppData\Local\.finch -Recurse + ./scripts/cleanup_wsl.ps1 make clean cd deps/finch-core && make clean exit 0 # Cleanup may set the exit code e.g. if a file doesn't exist; just ignore @@ -203,15 +190,8 @@ jobs: aws-region: ${{ secrets.REGION }} - name: Remove Finch VM timeout-minutes: 2 - run: | - # We want these cleanup commands to always run, ignore errors so the step completes. - $ErrorActionPreference = 'Ignore' - taskkill /f /im wslservice.exe 2> nul || cmd /c "exit /b 0" - wsl --list --verbose - sc query LxssManager | findstr "STATE" | findstr /C:"STOPPED" > nul && net start LxssManager - wsl --shutdown - wsl --unregister lima-finch - wsl --list --verbose + shell: pwsh + run: ./scripts/cleanup_wsl.ps1 - name: Clean up previous files run: | Remove-Item C:\Users\Administrator\.finch -Recurse -ErrorAction Ignore @@ -250,17 +230,8 @@ jobs: make test-e2e-vm - name: Remove Finch VM timeout-minutes: 2 - run: | - # We want these cleanup commands to always run, ignore errors so the step completes. - $ErrorActionPreference = 'Ignore' - taskkill /f /im wslservice.exe 2> nul || cmd /c "exit /b 0" - wsl --list --verbose - sc query LxssManager | findstr "STATE" | findstr /C:"STOPPED" > nul && net start LxssManager - wsl --shutdown - Start-Sleep -s 10 - wsl --unregister lima-finch - Remove-Item C:\Users\Administrator\AppData\Local\.finch -Recurse - wsl --list --verbose + shell: pwsh + run: ./scripts/cleanup_wsl.ps1 - name: Run container e2e tests uses: nick-fields/retry@7152eba30c6575329ac0576536151aca5a72780e # v3.0.0 with: @@ -287,16 +258,9 @@ jobs: - name: Remove Finch VM and Clean Up Previous Environment if: ${{ always() }} timeout-minutes: 2 + shell: pwsh run: | - # We want these cleanup commands to always run, ignore errors so the step completes. - $ErrorActionPreference = 'Ignore' - taskkill /f /im wslservice.exe 2> nul || cmd /c "exit /b 0" - wsl --list --verbose - sc query LxssManager | findstr "STATE" | findstr /C:"STOPPED" > nul && net start LxssManager - wsl --shutdown - wsl --unregister lima-finch - wsl --list --verbose - Remove-Item C:\Users\Administrator\AppData\Local\.finch -Recurse + ./scripts/cleanup_wsl.ps1 make clean cd deps/finch-core && make clean exit 0 # Cleanup may set the exit code e.g. if a file doesn't exist; just ignore diff --git a/.github/workflows/e2e-windows.yaml b/.github/workflows/e2e-windows.yaml index 46961e448..92c27e18e 100644 --- a/.github/workflows/e2e-windows.yaml +++ b/.github/workflows/e2e-windows.yaml @@ -59,15 +59,7 @@ jobs: aws-region: ${{ secrets.REGION }} - name: Remove Finch VM timeout-minutes: 2 - run: | - # We want these cleanup commands to always run, ignore errors so the step completes. - $ErrorActionPreference = 'Ignore' - taskkill /f /im wslservice.exe 2> nul || cmd /c "exit /b 0" - wsl --list --verbose - sc query LxssManager | findstr "STATE" | findstr /C:"STOPPED" > nul && net start LxssManager - wsl --shutdown - wsl --unregister lima-finch - wsl --list --verbose + run: ./scripts/cleanup_wsl.ps1 - name: Clean up previous files run: | Remove-Item C:\Users\Administrator\.finch -Recurse -ErrorAction Ignore @@ -94,15 +86,7 @@ jobs: if: ${{ always() }} timeout-minutes: 2 run: | - # We want these cleanup commands to always run, ignore errors so the step completes. - $ErrorActionPreference = 'Ignore' - taskkill /f /im wslservice.exe 2> nul || cmd /c "exit /b 0" - wsl --list --verbose - sc query LxssManager | findstr "STATE" | findstr /C:"STOPPED" > nul && net start LxssManager - wsl --shutdown - wsl --unregister lima-finch - wsl --list --verbose - Remove-Item C:\Users\Administrator\AppData\Local\.finch -Recurse + ./scripts/cleanup_wsl.ps1 make clean cd deps/finch-core && make clean exit 0 # Cleanup may set the exit code e.g. if a file doesn't exist; just ignore diff --git a/scripts/cleanup_wsl.ps1 b/scripts/cleanup_wsl.ps1 new file mode 100644 index 000000000..6a1c2c7c5 --- /dev/null +++ b/scripts/cleanup_wsl.ps1 @@ -0,0 +1,28 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +# We want these cleanup commands to always run, ignore errors so the step completes. +$ErrorActionPreference = 'Ignore' + +taskkill /f /im wslservice.exe 2> nul || cmd /c "exit /b 0" +sc query LxssManager | findstr "STATE" | findstr /C:"STOPPED" > nul && net start LxssManager +wsl --list --verbose --all + +# Attempt to shut down WSL if any distribution is running +if (wsl --list --verbose | findstr /C:"Running" > nul) { + timeout 60s wsl --shutdown + # Forcefully kill WSL if still running + if (Get-Process -Name "wsl" -ErrorAction SilentlyContinue) { + Stop-Process -Name "wsl" -Force + } + echo "WSL has been shut down successfully." +} + +# Unregister 'lima-finch' distribution if it exists +if (wsl --list --quiet | findstr /C:"lima-finch" > nul) { + wsl --unregister lima-finch + echo "'lima-finch' has been unregistered successfully." +} + +wsl --list --verbose --all +Remove-Item C:\Users\Administrator\AppData\Local\.finch -Recurse \ No newline at end of file