Created
February 25, 2025 07:01
-
-
Save mritsurgeon/0870956adf3ff7552f0e19d6e3413dd0 to your computer and use it in GitHub Desktop.
BAT CSR Approval Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @echo off | |
| setlocal enabledelayedexpansion | |
| :: Set initial color to red | |
| color 4F | |
| :: Initialize start time | |
| set "start_time=%time%" | |
| set "elapsed_seconds=0" | |
| :: Function to convert seconds to minutes and seconds display | |
| :SHOW_TIME | |
| set /a "minutes=elapsed_seconds/60" | |
| set /a "seconds=elapsed_seconds%%60" | |
| set "time_display=!minutes! minutes !seconds! seconds" | |
| :: Clear the screen and show persistent header | |
| :SHOW_HEADER | |
| cls | |
| call :SHOW_TIME | |
| echo =============================== | |
| echo PLEASE WAIT - LAB NOT READY | |
| echo DO NOT START LAB YET | |
| echo =============================== | |
| echo Time Elapsed: !time_display! | |
| echo. | |
| :: Set variables | |
| set KUBECONFIG=C:\oc\kubeconfig | |
| set web_url=https://console-openshift-console.apps.handsonlab.openshift/dashboards | |
| set total_timeout=600 | |
| set current_time=0 | |
| :CHECK_CONNECTION | |
| set "mSpinner=." | |
| :CONNECTION_SPINNER | |
| cls | |
| call :SHOW_TIME | |
| echo =============================== | |
| echo PLEASE WAIT - LAB NOT READY | |
| echo DO NOT START LAB YET | |
| echo =============================== | |
| echo Time Elapsed: !time_display! | |
| echo. | |
| echo Testing connection to OpenShift cluster... | |
| echo %mSpinner% | |
| set mSpinner=%mSpinner%. | |
| if "%mSpinner%"==".............................." set "mSpinner=." | |
| timeout /t 1 >nul | |
| set /a "elapsed_seconds+=1" | |
| oc get nodes >nul 2>&1 | |
| if errorlevel 1 ( | |
| timeout /t 2 >nul | |
| set /a current_time+=2 | |
| set /a "elapsed_seconds+=2" | |
| if !current_time! lss !total_timeout! ( | |
| goto CONNECTION_SPINNER | |
| ) else ( | |
| echo Maximum time reached ^(10 minutes^). Please check your cluster status. | |
| pause | |
| exit /b 1 | |
| ) | |
| ) else ( | |
| echo Connection successful. | |
| ) | |
| :CSR_AND_URL_CHECK | |
| set "pending_count=0" | |
| set "csr_names=" | |
| set "mSpinner=." | |
| :CSR_LOOP | |
| cls | |
| call :SHOW_TIME | |
| echo =============================== | |
| echo PLEASE WAIT - LAB NOT READY | |
| echo DO NOT START LAB YET | |
| echo =============================== | |
| echo Time Elapsed: !time_display! | |
| echo. | |
| echo Checking for Pending CSRs... | |
| echo %mSpinner% | |
| set mSpinner=%mSpinner%. | |
| if "%mSpinner%"==".............................." set "mSpinner=." | |
| set /a "elapsed_seconds+=1" | |
| timeout /t 1 >nul | |
| :: Check for pending CSRs | |
| for /f "tokens=1 delims= " %%i in ('oc get csr ^| findstr "Pending"') do ( | |
| set "csr_names=!csr_names! %%i" | |
| set /a pending_count+=1 | |
| ) | |
| if !pending_count! gtr 0 ( | |
| echo Found !pending_count! pending CSRs | |
| for %%j in (!csr_names!) do ( | |
| if not "%%j"=="" ( | |
| echo Approving CSR: %%j | |
| oc adm certificate approve %%j | |
| ) | |
| ) | |
| ) | |
| :: Check URL availability | |
| start /wait cmd /c "C:\oc\check url.bat" | |
| if %errorlevel% equ 0 ( | |
| :: Change color to green when URL is ready | |
| color 2F | |
| cls | |
| call :SHOW_TIME | |
| echo =============================== | |
| echo LAB IS READY - YOU CAN START | |
| echo =============================== | |
| echo Total Time Taken: !time_display! | |
| echo. | |
| echo OpenShift console is available at: %web_url% | |
| pause | |
| exit /b 0 | |
| ) else ( | |
| timeout /t 5 >nul | |
| set /a "elapsed_seconds+=5" | |
| set "pending_count=0" | |
| set "csr_names=" | |
| goto CSR_LOOP | |
| ) | |
| :SHOW_TIME | |
| set /a "minutes=elapsed_seconds/60" | |
| set /a "seconds=elapsed_seconds%%60" | |
| set "time_display=!minutes! minutes !seconds! seconds" | |
| goto :eof | |
| endlocal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment