Skip to content

Commit

Permalink
feat: add gh actions runner setup for mac on aws
Browse files Browse the repository at this point in the history
this adds new flags to the command `mapt aws mac request` to
to install 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 <kaludios@gmail.com>
  • Loading branch information
anjannath authored and adrianriobo committed Jul 22, 2024
1 parent 9e2d794 commit 87a511e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
23 changes: 18 additions & 5 deletions cmd/mapt/cmd/aws/hosts/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
params "github.com/redhat-developer/mapt/cmd/mapt/cmd/constants"
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
"github.com/redhat-developer/mapt/pkg/provider/aws/action/mac"
"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 @@ -61,14 +62,25 @@ func getMacRequest() *cobra.Command {
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))

// 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)
}
}

// Run create
if err := mac.Request(
&mac.MacRequest{
Prefix: "main",
Architecture: viper.GetString(arch),
Version: viper.GetString(osVersion),
FixedLocation: viper.IsSet(fixedLocation),
Airgap: viper.IsSet(airgap)}); err != nil {
Prefix: "main",
Architecture: viper.GetString(arch),
Version: viper.GetString(osVersion),
FixedLocation: viper.IsSet(fixedLocation),
SetupGHActionsRunner: viper.GetBool(params.InstallGHActionsRunner),
Airgap: viper.IsSet(airgap)}); err != nil {
logging.Error(err)
}
return nil
Expand All @@ -81,6 +93,7 @@ func getMacRequest() *cobra.Command {
flagSet.StringP(osVersion, "", osDefault, osVersionDesc)
flagSet.Bool(fixedLocation, false, fixedLocationDesc)
flagSet.Bool(airgap, false, airgapDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
c.PersistentFlags().AddFlagSet(flagSet)
err := c.MarkPersistentFlagRequired(arch)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion pkg/provider/aws/action/mac/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ sudo sysadminctl -screenLock off -password "{{.Password}}"
mkdir /Users/{{.Username}}/.ssh
echo "{{.AuthorizedKey}}" | tee /Users/{{.Username}}/.ssh/authorized_keys

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

# autologin to take effect
# run reboot on background to successfully finish the remote exec of the script
(sleep 2 && sudo reboot)&
(sleep 2 && sudo reboot)&
13 changes: 9 additions & 4 deletions pkg/provider/aws/action/mac/mac-machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"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"

Expand All @@ -35,9 +36,11 @@ var BootstrapScript []byte

// Need to extend this to also pass the key to be set up on each / create or replace
type userDataValues struct {
Username string
Password string
AuthorizedKey string
Username string
Password string
AuthorizedKey string
InstallActionsRunner bool
ActionsRunnerSnippet string
}

type locked struct {
Expand Down Expand Up @@ -385,7 +388,9 @@ func (r *MacRequest) getBootstrapScript(ctx *pulumi.Context) (
userDataValues{
defaultUsername,
password,
authorizedKey},
authorizedKey,
r.SetupGHActionsRunner,
ghactions.GetActionRunnerSnippetMacos()},
resourcesUtil.GetResourceName(r.Prefix, awsMacMachineID, "mac-bootstrap"),
string(BootstrapScript[:]))
}).(pulumi.StringOutput)
Expand Down
3 changes: 3 additions & 0 deletions pkg/provider/aws/action/mac/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type MacRequest struct {
replace bool
lock bool

// setup as github actions runner
SetupGHActionsRunner bool

// This value wil be used to dynamically expand the pool size
// MaxPoolSize *int

Expand Down
19 changes: 19 additions & 0 deletions pkg/util/ghactions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (

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"
runnerBaseURLMacos = "https://github.com/actions/runner/releases/download/v%[1]s/actions-runner-osx-x64-%[1]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
Expand All @@ -37,6 +38,16 @@ Add-Type -AssemblyName System.IO.Compression.FileSystem ;
` sudo ./svc.sh install` + "\n" +
` chcon system_u:object_r:usr_t:s0 $(pwd)/runsvc.sh` + "\n" +
` sudo ./svc.sh start`

installActionRunnerSnippetMacos string = `mkdir ~/actions-runner && cd ~/actions-runner
curl -o actions-runner-osx.tar.gz -L %s
tar xzf ./actions-runner-osx.tar.gz
./config.sh --token %s --url %s --name %s --unattended --replace
./svc.sh install
plistName=$(basename $(./svc.sh status | grep "plist$"))
mkdir -p ~/Library/LaunchDaemons
mv ~/Library/LaunchAgents/"${plistName}" ~/Library/LaunchDaemons/"${plistName}"
launchctl load ~/Library/LaunchDaemons/"${plistName}"`
)

var args *RunnerArgs
Expand Down Expand Up @@ -75,3 +86,11 @@ func GetActionRunnerSnippetLinux() string {
}
return util.IfNillable(args != nil, snippetLinux, "")
}

func GetActionRunnerSnippetMacos() string {
var snippetMacos = func() string {
return fmt.Sprintf(installActionRunnerSnippetMacos,
fmt.Sprintf(runnerBaseURLMacos, runnerVersion), args.Token, args.RepoURL, args.Name)
}
return util.IfNillable(args != nil, snippetMacos, "")
}

0 comments on commit 87a511e

Please sign in to comment.