From af934c5ff2e8f4e4e5ccab1ffe90a38b37cbd24b Mon Sep 17 00:00:00 2001 From: Anjan Nath Date: Wed, 10 Jul 2024 18:56:50 +0530 Subject: [PATCH] feat: add gh actions runner setup for rhel on aws this adds new flags to the command `mapt aws rhel create` 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 --- cmd/mapt/cmd/aws/hosts/rhel.go | 24 ++++++++++++------- .../aws/action/rhel/cloud-config-base | 10 +++++++- pkg/provider/aws/action/rhel/cloud-config-snc | 8 +++++++ pkg/provider/aws/action/rhel/rhel.go | 17 +++++++++---- pkg/util/ghactions/runner.go | 16 +++++++++++++ 5 files changed, 61 insertions(+), 14 deletions(-) diff --git a/cmd/mapt/cmd/aws/hosts/rhel.go b/cmd/mapt/cmd/aws/hosts/rhel.go index 82d4b34cb..d2e164ab9 100644 --- a/cmd/mapt/cmd/aws/hosts/rhel.go +++ b/cmd/mapt/cmd/aws/hosts/rhel.go @@ -64,15 +64,20 @@ func getRHELCreate() *cobra.Command { // Run create if err := rhel.Create( &rhel.Request{ - Prefix: "main", - Version: viper.GetString(rhelVersion), - Arch: viper.GetString(rhelArch), - VMType: viper.GetStringSlice(vmTypes), - SubsUsername: viper.GetString(subsUsername), - SubsUserpass: viper.GetString(subsUserpass), - ProfileSNC: viper.IsSet(profileSNC), - Spot: viper.IsSet(spot), - Airgap: viper.IsSet(airgap)}); err != nil { + Prefix: "main", + Version: viper.GetString(rhelVersion), + Arch: viper.GetString(rhelArch), + VMType: viper.GetStringSlice(vmTypes), + SubsUsername: viper.GetString(subsUsername), + SubsUserpass: viper.GetString(subsUserpass), + ProfileSNC: viper.IsSet(profileSNC), + Spot: viper.IsSet(spot), + Airgap: viper.IsSet(airgap), + SetupGHActionsRunner: viper.GetBool(params.InstallGHActionsRunner), + GHActionsRunnerToken: viper.GetString(params.GHActionsRunnerToken), + GHActionsRunnerName: viper.GetString(params.GHActionsRunnerName), + GHActionsRunnerRepo: viper.GetString(params.GHActionsRunnerRepo), + }); err != nil { logging.Error(err) } return nil @@ -89,6 +94,7 @@ func getRHELCreate() *cobra.Command { flagSet.Bool(airgap, false, airgapDesc) flagSet.Bool(spot, false, spotDesc) flagSet.Bool(profileSNC, false, profileSNCDesc) + flagSet.AddFlagSet(params.GetGHActionsFlagset()) c.PersistentFlags().AddFlagSet(flagSet) // if err := c.MarkFlagRequired(subsUsername); err != nil { // logging.Error(err) diff --git a/pkg/provider/aws/action/rhel/cloud-config-base b/pkg/provider/aws/action/rhel/cloud-config-base index 6809e8dc2..817d04265 100644 --- a/pkg/provider/aws/action/rhel/cloud-config-base +++ b/pkg/provider/aws/action/rhel/cloud-config-base @@ -5,4 +5,12 @@ rh_subscription: auto-attach: true runcmd: - while fuser /var/lib/rpm/.rpm.lock > /dev/null 2>&1 ; do sleep 1 ; done - - dnf install -y podman \ No newline at end of file + - dnf install -y podman +{{ if .InstallActionsRunner }} - sudo -u {{ .Username }} bash -c /opt/install-ghrunner.sh{{ end }} +{{ if .InstallActionsRunner }}write_files: + # Github actions runner installation + - content: | + {{ .ActionsRunnerSnippet }} + path: /opt/install-ghrunner.sh + permissions: '0755' +{{ end }} diff --git a/pkg/provider/aws/action/rhel/cloud-config-snc b/pkg/provider/aws/action/rhel/cloud-config-snc index 9f4a6a0f2..6dcc22d94 100644 --- a/pkg/provider/aws/action/rhel/cloud-config-snc +++ b/pkg/provider/aws/action/rhel/cloud-config-snc @@ -25,3 +25,11 @@ runcmd: - echo "user.max_user_namespaces=28633" | tee -a /etc/sysctl.d/userns.conf - sysctl -p /etc/sysctl.d/userns.conf - dnf upgrade -y curl openssl +{{ if .InstallActionsRunner }} - sudo -u {{ .Username }} bash -c /opt/install-ghrunner.sh {{ end }} +{{ if .InstallActionsRunner }}write_files: + # Github actions runner installation + - content: | + {{ .ActionsRunnerSnippet }} + path: /opt/install-ghrunner.sh + permissions: '0755' +{{ end }} diff --git a/pkg/provider/aws/action/rhel/rhel.go b/pkg/provider/aws/action/rhel/rhel.go index 0b43e128e..2a5c3684f 100644 --- a/pkg/provider/aws/action/rhel/rhel.go +++ b/pkg/provider/aws/action/rhel/rhel.go @@ -25,6 +25,7 @@ import ( "github.com/redhat-developer/mapt/pkg/provider/util/output" "github.com/redhat-developer/mapt/pkg/util" "github.com/redhat-developer/mapt/pkg/util/file" + "github.com/redhat-developer/mapt/pkg/util/ghactions" resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources" ) @@ -38,9 +39,13 @@ type Request struct { SubsUserpass string // if profile SNC is enabled machine is setup to // be used as SNC runner - ProfileSNC bool - Spot bool - Airgap bool + ProfileSNC bool + Spot bool + Airgap bool + SetupGHActionsRunner bool // setup as github actions runner + GHActionsRunnerToken string + GHActionsRunnerName string + GHActionsRunnerRepo string // internal management // For airgap scenario there is an orchestation of // a phase with connectivity on the machine (allowing bootstraping) @@ -56,6 +61,8 @@ type userDataValues struct { SubscriptionUsername string SubscriptionPassword string Username string + InstallActionsRunner bool + ActionsRunnerSnippet string } //go:embed cloud-config-base @@ -274,7 +281,9 @@ func (r *Request) getUserdata() (pulumi.StringPtrInput, error) { userDataValues{ r.SubsUsername, r.SubsUserpass, - amiUserDefault}, + amiUserDefault, + r.SetupGHActionsRunner, + ghactions.GetActionRunnerSnippetLinux(r.GHActionsRunnerToken, r.GHActionsRunnerName, r.GHActionsRunnerRepo)}, resourcesUtil.GetResourceName( r.Prefix, awsRHELDedicatedID, "userdata"), templateConfig) diff --git a/pkg/util/ghactions/runner.go b/pkg/util/ghactions/runner.go index 403f39e43..7e57fc964 100644 --- a/pkg/util/ghactions/runner.go +++ b/pkg/util/ghactions/runner.go @@ -9,6 +9,22 @@ if((Get-FileHash -Path actions-runner-win-x64-2.317.0.zip -Algorithm SHA256).Has [System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD\actions-runner-win-x64-2.317.0.zip", "$PWD") ./config.cmd --token %s --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-x64-2.317.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.317.0/actions-runner-linux-x64-2.317.0.tar.gz` + "\n" + + ` echo "9e883d210df8c6028aff475475a457d380353f9d01877d51cc01a17b2a91161d actions-runner-linux-x64-2.317.0.tar.gz" | sha256sum -c` + "\n" + + ` tar xzf ./actions-runner-linux-x64-2.317.0.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(token, name, repoURL string) string { return fmt.Sprintf(WindowsActionsRunnerInstallSnippet, token, repoURL, name) } + +func GetActionRunnerSnippetLinux(token, name, repoURL string) string { + return fmt.Sprintf(LinuxActionsRunnerInstallSnippet, token, repoURL, name) +}