Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check gh runner args as nil in case not initialized #258

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/mapt/cmd/aws/hosts/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func getRHELCreate() *cobra.Command {
ProfileSNC: viper.IsSet(profileSNC),
Spot: viper.IsSet(spot),
Airgap: viper.IsSet(airgap),
SetupGHActionsRunner: viper.GetBool(params.InstallGHActionsRunner),
SetupGHActionsRunner: viper.IsSet(params.InstallGHActionsRunner),
}); err != nil {
logging.Error(err)
}
Expand Down
77 changes: 35 additions & 42 deletions pkg/util/ghactions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/pkg/errors"
"github.com/redhat-developer/mapt/pkg/util"
)

type RunnerArgs struct {
Expand All @@ -12,22 +13,33 @@ type RunnerArgs struct {
Name string
}

const runnerVersion = "2.317.0"
const (
runnerVersion = "2.317.0"

var (
args *RunnerArgs
runnerBaseURLWin = "https://github.com/actions/runner/releases/download/v%[1]s/actions-runner-win-x64-%[1]s.zip"
runnerBaseURLLinux = "https://github.com/actions/runner/releases/download/v%[1]s/actions-runner-linux-x64-%[1]s.tar.gz"

runnerBaseURLWin = "https://github.com/actions/runner/releases/download/v%s/actions-runner-win-x64-%s.zip"
runnerBaseURLLinux = "https://github.com/actions/runner/releases/download/v%s/actions-runner-linux-x64-%s.tar.gz"
)
// $ghToken needs to be set externally before use; it is defined in the platform specific setup scripts
// for aws this is defined in the script and for azure it is passed as an arg to the setup script
installActionRunnerSnippetWindows string = `New-Item -Path C:\actions-runner -Type Directory ; cd C:\actions-runner
Invoke-WebRequest -Uri %s -OutFile actions-runner-win.zip
Add-Type -AssemblyName System.IO.Compression.FileSystem ;
[System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD\actions-runner-win.zip", "$PWD")
./config.cmd --token $ghToken --url %s --name %s --unattended --runasservice --replace`

func getRunnerDownloadURLWin() string {
return fmt.Sprintf(runnerBaseURLWin, runnerVersion, runnerVersion)
}
// whitespace at the start is required since this is expanded in a cloud-init yaml file
// to start as service need to relable the runsvc.sh file on rhel: https://github.com/actions/runner/issues/3222
installActionRunnerSnippetLinux string = ` mkdir ~/actions-runner && cd ~/actions-runner` + "\n" +
` curl -o actions-runner-linux.tar.gz -L %s` + "\n" +
` tar xzf ./actions-runner-linux.tar.gz` + "\n" +
` sudo ./bin/installdependencies.sh` + "\n" +
` ./config.sh --token %s --url %s --name %s --unattended --replace` + "\n" +
` sudo ./svc.sh install` + "\n" +
` chcon system_u:object_r:usr_t:s0 $(pwd)/runsvc.sh` + "\n" +
` sudo ./svc.sh start`
)

func getRunnerDownloadURLLinux() string {
return fmt.Sprintf(runnerBaseURLLinux, runnerVersion, runnerVersion)
}
var args *RunnerArgs
anjannath marked this conversation as resolved.
Show resolved Hide resolved

func InitGHRunnerArgs(token, name, repoURL string) error {
if token == "" || name == "" || repoURL == "" {
Expand All @@ -42,43 +54,24 @@ func InitGHRunnerArgs(token, name, repoURL string) error {
}

func GetToken() string {
if (args == &RunnerArgs{}) {
return ""
var token = func() string {
return args.Token
}
return args.Token
return util.IfNillable(args != nil, token, "")
}

// $ghToken needs to be set externally before use; it is defined in the platform specific setup scripts
// for aws this is defined in the script and for azure it is passed as an arg to the setup script
const WindowsActionsRunnerInstallSnippet string = `New-Item -Path C:\actions-runner -Type Directory ; cd C:\actions-runner
Invoke-WebRequest -Uri %s -OutFile actions-runner-win.zip
Add-Type -AssemblyName System.IO.Compression.FileSystem ;
[System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD\actions-runner-win.zip", "$PWD")
./config.cmd --token $ghToken --url %s --name %s --unattended --runasservice --replace`

// whitespace at the start is required since this is expanded in a cloud-init yaml file
// to start as service need to relable the runsvc.sh file on rhel: https://github.com/actions/runner/issues/3222
const LinuxActionsRunnerInstallSnippet string = ` mkdir ~/actions-runner && cd ~/actions-runner` + "\n" +
` curl -o actions-runner-linux.tar.gz -L %s` + "\n" +
` tar xzf ./actions-runner-linux.tar.gz` + "\n" +
` sudo ./bin/installdependencies.sh` + "\n" +
` ./config.sh --token %s --url %s --name %s --unattended --replace` + "\n" +
` sudo ./svc.sh install` + "\n" +
` chcon system_u:object_r:usr_t:s0 $(pwd)/runsvc.sh` + "\n" +
` sudo ./svc.sh start`

func GetActionRunnerSnippetWin() string {
if (args == &RunnerArgs{}) {
return ""
var snippetWindows = func() string {
return fmt.Sprintf(installActionRunnerSnippetWindows,
fmt.Sprintf(runnerBaseURLWin, runnerVersion), args.RepoURL, args.Name)
}
return fmt.Sprintf(WindowsActionsRunnerInstallSnippet,
getRunnerDownloadURLWin(), args.RepoURL, args.Name)
return util.IfNillable(args != nil, snippetWindows, "")
}

func GetActionRunnerSnippetLinux() string {
if (args == &RunnerArgs{}) {
return ""
var snippetLinux = func() string {
return fmt.Sprintf(installActionRunnerSnippetLinux,
fmt.Sprintf(runnerBaseURLLinux, runnerVersion), args.Token, args.RepoURL, args.Name)
}
return fmt.Sprintf(LinuxActionsRunnerInstallSnippet,
getRunnerDownloadURLLinux(), args.Token, args.RepoURL, args.Name)
return util.IfNillable(args != nil, snippetLinux, "")
}
16 changes: 16 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ func If[T any](cond bool, vtrue, vfalse T) T {
return vfalse
}

// In case vtrue value depends on a variable checked on condition which could be nil
// as params are evaluated within the If function invokation it will produce a panic error
// in that case we will pass the vtrue as a function which will be evaluated only if condition is met
//
// i.e. If(foo != nil, foo.bar, "") In this case if foo is nill this will error with panic as the evaluation will try access foo which is nil
//
// so, in this case we will use IfNillable:
// bar = func() { return foo.bar}
// IfNillable(foo != nil, bar, "")
func IfNillable[T any](cond bool, vtrueNillable func() T, vfalse T) T {
if cond {
return vtrueNillable()
}
return vfalse
}

func ArrayFilter[T any](source []T, filter func(item T) bool) []T {
var result []T
for _, item := range source {
Expand Down