Skip to content

Commit

Permalink
feat: add gh actions runner setup snippet to azure setup script
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
anjannath committed Jul 12, 2024
1 parent ae9a21a commit 0473410
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
36 changes: 25 additions & 11 deletions cmd/mapt/cmd/azure/hosts/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
azureWindows "github.com/redhat-developer/mapt/pkg/provider/azure/action/windows"
spotprice "github.com/redhat-developer/mapt/pkg/provider/azure/module/spot-price"
"github.com/redhat-developer/mapt/pkg/util/ghactions"
"github.com/redhat-developer/mapt/pkg/util/logging"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -74,6 +75,7 @@ func getCreate() *cobra.Command {
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))

// ParseEvictionRate
var spotToleranceValue = spotprice.DefaultEvictionRate
if viper.IsSet(paramSpotTolerance) {
var ok bool
Expand All @@ -83,19 +85,30 @@ func getCreate() *cobra.Command {
return fmt.Errorf("%s is not a valid spot tolerance value", viper.GetString(paramSpotTolerance))
}
}
// ParseEvictionRate

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
viper.GetString(params.GHActionsRunnerName),
viper.GetString(params.GHActionsRunnerRepo))
if err != nil {
logging.Error(err)
}
}

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),
Spot: viper.IsSet(paramSpot),
SpotTolerance: spotToleranceValue}); err != nil {
logging.Error(err)
}
return nil
Expand All @@ -113,6 +126,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
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/provider/azure/action/windows/rhqp-ci-setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ param(
$hostname,
[Parameter(Mandatory,HelpMessage='authorizedkey for ssh private key for the user')]
$authorizedKey,
[Parameter(HelpMessage='token for the github actions runner')]
$ghToken,
[switch]$crcProfile=$false
)
# Create local user
Expand Down Expand Up @@ -124,6 +126,12 @@ if ($crcProfile) {
New-LocalGroup -Name "crc-users"
Add-LocalGroupMember -Group "crc-users" -Member $user
}

# Install github-actions-runner if needed
{{ if .InstallActionsRunner }}
{{- .ActionsRunnerSnippet }}
{{ end }}

# Restart computer to have the ssh connection available with setup from this script
Start-Process powershell -verb runas -ArgumentList "Restart-Computer -Force"

Expand Down
35 changes: 32 additions & 3 deletions pkg/provider/azure/action/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/redhat-developer/mapt/pkg/provider/util/output"
"github.com/redhat-developer/mapt/pkg/provider/util/security"
"github.com/redhat-developer/mapt/pkg/util"
"github.com/redhat-developer/mapt/pkg/util/file"
"github.com/redhat-developer/mapt/pkg/util/ghactions"
"github.com/redhat-developer/mapt/pkg/util/logging"
resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources"
"golang.org/x/exp/slices"
Expand All @@ -40,6 +42,13 @@ type WindowsRequest struct {
Spot bool
SpotTolerance spotprice.EvictionRate
Profiles []string
// setup as github actions runner
SetupGHActionsRunner bool
}

type ghActionsRunnerData struct {
InstallActionsRunner bool
ActionsRunnerSnippet string
}

func Create(r *WindowsRequest) (err error) {
Expand Down Expand Up @@ -315,18 +324,20 @@ func (r *WindowsRequest) postInitSetup(ctx *pulumi.Context, rg *resources.Resour
return nil, nil, err
}
// the post script command will be generated based on generated data as parameters
setupCommand := pulumi.All(userPasswd.Result, privateKey.PublicKeyOpenssh, vm.OsProfile.ComputerName()).ApplyT(
setupCommand := pulumi.All(userPasswd.Result, privateKey.PublicKeyOpenssh, vm.OsProfile.ComputerName(), ghactions.GetToken()).ApplyT(
func(args []interface{}) string {
password := args[0].(string)
authorizedKey := args[1].(string)
hostname := args[2].(*string)
token := args[3].(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 -ghToken \"%s\" -authorizedKey \"%s\"",
scriptName,
r.profilesAsParams(),
password,
r.Username,
*hostname,
token,
authorizedKey,
)
}).(pulumi.StringOutput)
Expand Down Expand Up @@ -386,13 +397,23 @@ func (r *WindowsRequest) uploadScript(ctx *pulumi.Context,
if err != nil {
return nil, err
}

data := ghActionsRunnerData{
r.SetupGHActionsRunner,
ghactions.GetActionRunnerSnippetWin(),
}
ciSetupScript, err := file.Template(data, "ciSetupScript", string(RHQPCISetupScript))
if err != nil {
return nil, err
}

return storage.NewBlob(ctx,
resourcesUtil.GetResourceName(r.Prefix, azureWindowsDesktopID, "bl"),
&storage.BlobArgs{
AccountName: sa.Name,
ContainerName: c.Name,
ResourceGroupName: rg.Name,
Source: pulumi.NewStringAsset(string(RHQPCISetupScript)),
Source: pulumi.NewStringAsset(ciSetupScript),
BlobName: pulumi.String(scriptName),
})
}
Expand All @@ -416,3 +437,11 @@ func (r *WindowsRequest) profilesAsParams() string {
})
return strings.Join(pp, " ")
}

func installActionsRunnerAsParam(installRunner bool) string {
if installRunner {
// the arg name has to match with the one defined in rhqp-ci-setup.ps1
return "-installGHActionsRunner"
}
return ""
}

0 comments on commit 0473410

Please sign in to comment.