From c76dabb256c3aefac8b4fa2aa278e23cfbacc91d Mon Sep 17 00:00:00 2001 From: Anjan Nath Date: Tue, 2 Jul 2024 12:22:20 +0530 Subject: [PATCH] feat: add gh actions runner setup snippet to azure setup script this adds new flags to the command `mapt azure windows create` to setup github actions runner on the provisioned instance it also adds the additional flags to get the various values needed to setup the github actions runner Signed-off-by: Anjan Nath --- cmd/mapt/cmd/azure/hosts/windows.go | 25 +++++++++------ .../azure/action/windows/rhqp-ci-setup.ps1 | 20 +++++++++++- pkg/provider/azure/action/windows/windows.go | 31 ++++++++++++------- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/cmd/mapt/cmd/azure/hosts/windows.go b/cmd/mapt/cmd/azure/hosts/windows.go index 966fa718..9cb73e1d 100644 --- a/cmd/mapt/cmd/azure/hosts/windows.go +++ b/cmd/mapt/cmd/azure/hosts/windows.go @@ -86,16 +86,20 @@ func getCreate() *cobra.Command { // ParseEvictionRate if err := azureWindows.Create( &azureWindows.WindowsRequest{ - Prefix: "", - Location: viper.GetString(paramLocation), - VMSize: viper.GetString(paramVMSize), - Version: viper.GetString(paramVersion), - Feature: viper.GetString(paramFeature), - Username: viper.GetString(paramUsername), - AdminUsername: viper.GetString(paramAdminUsername), - Profiles: viper.GetStringSlice(paramProfile), - Spot: viper.IsSet(paramSpot), - SpotTolerance: spotToleranceValue}); err != nil { + Prefix: "", + Location: viper.GetString(paramLocation), + VMSize: viper.GetString(paramVMSize), + Version: viper.GetString(paramVersion), + Feature: viper.GetString(paramFeature), + Username: viper.GetString(paramUsername), + AdminUsername: viper.GetString(paramAdminUsername), + Profiles: viper.GetStringSlice(paramProfile), + SetupGHActionsRunner: viper.IsSet(params.InstallGHActionsRunner), + GHActionsRunnerToken: viper.GetString(params.GHActionsRunnerToken), + GHActionsRunnerName: viper.GetString(params.GHActionsRunnerName), + GHActionsRunnerRepo: viper.GetString(params.GHActionsRunnerRepo), + Spot: viper.IsSet(paramSpot), + SpotTolerance: spotToleranceValue}); err != nil { logging.Error(err) } return nil @@ -113,6 +117,7 @@ func getCreate() *cobra.Command { flagSet.StringSliceP(paramProfile, "", []string{}, paramProfileDesc) flagSet.Bool(paramSpot, false, paramSpotDesc) flagSet.StringP(paramSpotTolerance, "", defaultSpotTolerance, paramSpotToleranceDesc) + flagSet.AddFlagSet(params.GetGHActionsFlagset()) c.PersistentFlags().AddFlagSet(flagSet) return c } diff --git a/pkg/provider/azure/action/windows/rhqp-ci-setup.ps1 b/pkg/provider/azure/action/windows/rhqp-ci-setup.ps1 index 4ab7736a..14adf3e2 100644 --- a/pkg/provider/azure/action/windows/rhqp-ci-setup.ps1 +++ b/pkg/provider/azure/action/windows/rhqp-ci-setup.ps1 @@ -7,7 +7,14 @@ param( $hostname, [Parameter(Mandatory,HelpMessage='authorizedkey for ssh private key for the user')] $authorizedKey, - [switch]$crcProfile=$false + [Parameter(HelpMessage='token to register the github actions runner')] + $ghRunnerToken, + [Parameter(HelpMessage='name to be used for the github actions runner')] + $ghRunnerName, + [Parameter(HelpMessage='full url of the repo for the github actions runner')] + $ghRunnerRepo, + [switch]$crcProfile=$false, + [switch]$installGHActionsRunner=$false ) # Create local user $Password = ConvertTo-SecureString $userPass -AsPlainText -Force @@ -124,6 +131,17 @@ if ($crcProfile) { New-LocalGroup -Name "crc-users" Add-LocalGroupMember -Group "crc-users" -Member $user } + +# Install github actions runner if requested +if ($installGHActionsRunner) { + New-Item -Path C:\actions-runner -Type Directory ; cd C:\actions-runner + Invoke-WebRequest -Uri https://github.com/actions/runner/releases/download/v2.317.0/actions-runner-win-x64-2.317.0.zip -OutFile actions-runner-win-x64-2.317.0.zip + Add-Type -AssemblyName System.IO.Compression.FileSystem ; + if((Get-FileHash -Path actions-runner-win-x64-2.317.0.zip -Algorithm SHA256).Hash.ToUpper() -ne 'a74dcd1612476eaf4b11c15b3db5a43a4f459c1d3c1807f8148aeb9530d69826'.ToUpper()){ throw 'Computed checksum did not match' } + [System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD\actions-runner-win-x64-2.317.0.zip", "$PWD") + ./config.cmd --token $ghRunnerToken --url $ghRunnerRepo --name $ghRunnerName --unattended --runasservice --replace --ephemeral +} + # Restart computer to have the ssh connection available with setup from this script Start-Process powershell -verb runas -ArgumentList "Restart-Computer -Force" diff --git a/pkg/provider/azure/action/windows/windows.go b/pkg/provider/azure/action/windows/windows.go index 3dabdaa9..99fb04ab 100644 --- a/pkg/provider/azure/action/windows/windows.go +++ b/pkg/provider/azure/action/windows/windows.go @@ -3,6 +3,7 @@ package windows import ( _ "embed" "fmt" + "strconv" "strings" "github.com/pulumi/pulumi-azure-native-sdk/compute/v2" @@ -30,16 +31,20 @@ import ( var RHQPCISetupScript []byte type WindowsRequest struct { - Prefix string - Location string - VMSize string - Version string - Feature string - Username string - AdminUsername string - Spot bool - SpotTolerance spotprice.EvictionRate - Profiles []string + Prefix string + Location string + VMSize string + Version string + Feature string + Username string + AdminUsername string + Spot bool + SpotTolerance spotprice.EvictionRate + Profiles []string + SetupGHActionsRunner bool // setup as github actions runner + GHActionsRunnerToken string + GHActionsRunnerName string + GHActionsRunnerRepo string } func Create(r *WindowsRequest) (err error) { @@ -321,13 +326,17 @@ func (r *WindowsRequest) postInitSetup(ctx *pulumi.Context, rg *resources.Resour authorizedKey := args[1].(string) hostname := args[2].(*string) return fmt.Sprintf( - "powershell -ExecutionPolicy Unrestricted -File %s %s -userPass \"%s\" -user %s -hostname %s -authorizedKey \"%s\"", + "powershell -ExecutionPolicy Unrestricted -File %s %s -userPass \"%s\" -user %s -hostname %s -authorizedKey \"%s\" -installGHActionsRunner %s -ghRunnerToken \"%s\" -ghRunnerRepo \"%s\" -ghRunnerName \"%s\"", scriptName, r.profilesAsParams(), password, r.Username, *hostname, authorizedKey, + strconv.FormatBool(r.SetupGHActionsRunner), + r.GHActionsRunnerToken, + r.GHActionsRunnerRepo, + r.GHActionsRunnerName, ) }).(pulumi.StringOutput) // the post script will be executed as a extension,