diff --git a/cmd/mapt/cmd/aws/hosts/rhel.go b/cmd/mapt/cmd/aws/hosts/rhel.go index 82d4b34cb..41abc2dc6 100644 --- a/cmd/mapt/cmd/aws/hosts/rhel.go +++ b/cmd/mapt/cmd/aws/hosts/rhel.go @@ -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/rhel" + "github.com/redhat-developer/mapt/pkg/util/ghactions" "github.com/redhat-developer/mapt/pkg/util/logging" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -61,18 +62,30 @@ func getRHELCreate() *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 := 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), + }); err != nil { logging.Error(err) } return nil @@ -89,6 +102,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..ab05c2446 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" ) @@ -41,6 +42,8 @@ type Request struct { ProfileSNC bool Spot bool Airgap bool + // setup as github actions runner + SetupGHActionsRunner bool // internal management // For airgap scenario there is an orchestation of // a phase with connectivity on the machine (allowing bootstraping) @@ -56,6 +59,8 @@ type userDataValues struct { SubscriptionUsername string SubscriptionPassword string Username string + InstallActionsRunner bool + ActionsRunnerSnippet string } //go:embed cloud-config-base @@ -274,7 +279,9 @@ func (r *Request) getUserdata() (pulumi.StringPtrInput, error) { userDataValues{ r.SubsUsername, r.SubsUserpass, - amiUserDefault}, + amiUserDefault, + r.SetupGHActionsRunner, + ghactions.GetActionRunnerSnippetLinux()}, resourcesUtil.GetResourceName( r.Prefix, awsRHELDedicatedID, "userdata"), templateConfig) diff --git a/pkg/util/ghactions/runner.go b/pkg/util/ghactions/runner.go index fcede6016..bf4459e1b 100644 --- a/pkg/util/ghactions/runner.go +++ b/pkg/util/ghactions/runner.go @@ -42,9 +42,28 @@ 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 $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-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() string { if (args == &RunnerArgs{}) { return "" } return fmt.Sprintf(WindowsActionsRunnerInstallSnippet, args.RepoURL, args.Name) } + +func GetActionRunnerSnippetLinux() string { + if (args == &RunnerArgs{}) { + return "" + } + return fmt.Sprintf(LinuxActionsRunnerInstallSnippet, args.Token, args.RepoURL, args.Name) +}