Skip to content

Commit

Permalink
feat: add gh actions runner setup for rhel on aws
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
anjannath committed Jul 12, 2024
1 parent 60a6772 commit 8876bc6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
32 changes: 23 additions & 9 deletions cmd/mapt/cmd/aws/hosts/rhel.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/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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion pkg/provider/aws/action/rhel/cloud-config-base
Original file line number Diff line number Diff line change
Expand Up @@ -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
- 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 }}
8 changes: 8 additions & 0 deletions pkg/provider/aws/action/rhel/cloud-config-snc
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
9 changes: 8 additions & 1 deletion pkg/provider/aws/action/rhel/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand All @@ -56,6 +59,8 @@ type userDataValues struct {
SubscriptionUsername string
SubscriptionPassword string
Username string
InstallActionsRunner bool
ActionsRunnerSnippet string
}

//go:embed cloud-config-base
Expand Down Expand Up @@ -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)
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 @@ -33,9 +33,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 %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() string {
if (args == &RunnerArgs{}) {
return ""
}
return fmt.Sprintf(WindowsActionsRunnerInstallSnippet, args.Token, args.RepoURL, args.Name)
}

func GetActionRunnerSnippetLinux() string {
if (args == &RunnerArgs{}) {
return ""
}
return fmt.Sprintf(LinuxActionsRunnerInstallSnippet, args.Token, args.RepoURL, args.Name)
}

0 comments on commit 8876bc6

Please sign in to comment.