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

feat: add gh actions runner setup for mac on aws #264

Merged
merged 2 commits into from
Jul 22, 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
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ SOURCES := $(shell find . -name "*.go" -not -path "./vendor/*")
# LDFLAGS := $(VERSION_VARIABLES) -extldflags='-static' ${GO_EXTRA_LDFLAGS}
LDFLAGS := $(VERSION_VARIABLES) ${GO_EXTRA_LDFLAGS}
GCFLAGS := all=-N -l
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)

TOOLS_DIR := tools
include tools/tools.mk
Expand All @@ -37,7 +39,7 @@ install: $(SOURCES)
go install -ldflags="$(LDFLAGS)" $(GO_EXTRA_BUILDFLAGS) ./cmd/mapt

$(BUILD_DIR)/mapt: $(SOURCES)
GOOS=linux GOARCH=amd64 go build -gcflags="$(GCFLAGS)" -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/mapt $(GO_EXTRA_BUILDFLAGS) ./cmd/mapt
GOOS="$(GOOS)" GOARCH=$(GOARCH) go build -gcflags="$(GCFLAGS)" -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/mapt $(GO_EXTRA_BUILDFLAGS) ./cmd/mapt

.PHONY: build
build: $(BUILD_DIR)/mapt
Expand Down
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, "")
}