Skip to content

Commit

Permalink
feat: sync profiles for RHEL machines offered in Azure
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Riobo <[email protected]>
  • Loading branch information
adrianriobo authored and anjannath committed Nov 11, 2024
1 parent 445f1ce commit d37ee99
Show file tree
Hide file tree
Showing 22 changed files with 366 additions and 242 deletions.
4 changes: 2 additions & 2 deletions cmd/mapt/cmd/aws/hosts/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func getFedoraCreate() *cobra.Command {
CPUs: viper.GetInt32(params.CPUs),
MemoryGib: viper.GetInt32(params.Memory),
Arch: util.If(viper.GetString(params.LinuxArch) == "arm64", instancetypes.Arm64, instancetypes.Amd64),
NestedVirt: viper.GetBool(profileSNC) || viper.GetBool(params.NestedVirt),
NestedVirt: viper.GetBool(params.ProfileSNC) || viper.GetBool(params.NestedVirt),
}

// Run create
if err := fedora.Create(
&fedora.Request{
Prefix: "main",
Version: viper.GetString(rhelVersion),
Version: viper.GetString(fedoraVersion),
Arch: viper.GetString(params.LinuxArch),
VMType: viper.GetStringSlice(vmTypes),
InstanceRequest: instanceRequest,
Expand Down
28 changes: 9 additions & 19 deletions cmd/mapt/cmd/aws/hosts/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ import (
const (
cmdRHEL = "rhel"
cmdRHELDesc = "manage rhel dedicated host"

rhelVersion string = "version"
rhelVersionDesc string = "version for the RHEL OS"
rhelVersionDefault string = "9.4"
subsUsername string = "rh-subscription-username"
subsUsernameDesc string = "username to register the subscription"
subsUserpass string = "rh-subscription-password"
subsUserpassDesc string = "password to register the subscription"
profileSNC string = "snc"
profileSNCDesc string = "if this flag is set the RHEL will be setup with SNC profile. Setting up all requirements to run https://github.com/crc-org/snc"
)

func GetRHELCmd() *cobra.Command {
Expand Down Expand Up @@ -74,20 +64,20 @@ func getRHELCreate() *cobra.Command {
CPUs: viper.GetInt32(params.CPUs),
MemoryGib: viper.GetInt32(params.Memory),
Arch: util.If(viper.GetString(params.LinuxArch) == "arm64", instancetypes.Arm64, instancetypes.Amd64),
NestedVirt: viper.GetBool(profileSNC) || viper.GetBool(params.NestedVirt),
NestedVirt: viper.GetBool(params.ProfileSNC) || viper.GetBool(params.NestedVirt),
}

// Run create
if err := rhel.Create(
&rhel.Request{
Prefix: "main",
Version: viper.GetString(rhelVersion),
Version: viper.GetString(params.RhelVersion),
Arch: viper.GetString(params.LinuxArch),
InstanceRequest: instanceRequest,
VMType: viper.GetStringSlice(vmTypes),
SubsUsername: viper.GetString(subsUsername),
SubsUserpass: viper.GetString(subsUserpass),
ProfileSNC: viper.IsSet(profileSNC),
SubsUsername: viper.GetString(params.SubsUsername),
SubsUserpass: viper.GetString(params.SubsUserpass),
ProfileSNC: viper.IsSet(params.ProfileSNC),
Spot: viper.IsSet(spot),
Airgap: viper.IsSet(airgap),
SetupGHActionsRunner: viper.IsSet(params.InstallGHActionsRunner),
Expand All @@ -100,14 +90,14 @@ func getRHELCreate() *cobra.Command {
flagSet := pflag.NewFlagSet(params.CreateCmdName, pflag.ExitOnError)
flagSet.StringP(params.ConnectionDetailsOutput, "", "", params.ConnectionDetailsOutputDesc)
flagSet.StringToStringP(params.Tags, "", nil, params.TagsDesc)
flagSet.StringP(rhelVersion, "", rhelVersionDefault, rhelVersionDesc)
flagSet.StringP(params.RhelVersion, "", params.RhelVersionDefault, params.RhelVersionDesc)
flagSet.StringP(params.LinuxArch, "", params.LinuxArchDefault, params.LinuxArchDesc)
flagSet.StringSliceP(vmTypes, "", []string{}, vmTypesDescription)
flagSet.StringP(subsUsername, "", "", subsUsernameDesc)
flagSet.StringP(subsUserpass, "", "", subsUserpassDesc)
flagSet.StringP(params.SubsUsername, "", "", params.SubsUsernameDesc)
flagSet.StringP(params.SubsUserpass, "", "", params.SubsUserpassDesc)
flagSet.Bool(airgap, false, airgapDesc)
flagSet.Bool(spot, false, spotDesc)
flagSet.Bool(profileSNC, false, profileSNCDesc)
flagSet.Bool(params.ProfileSNC, false, params.ProfileSNCDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
flagSet.AddFlagSet(params.GetCpusAndMemoryFlagset())
c.PersistentFlags().AddFlagSet(flagSet)
Expand Down
5 changes: 5 additions & 0 deletions cmd/mapt/cmd/azure/hosts/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ const (
paramSpotTolerance = "spot-eviction-tolerance"
paramSpotToleranceDesc = "if spot is enable we can define the minimum tolerance level of eviction. Allowed value are: lowest, low, medium, high or highest"
defaultSpotTolerance = "lowest"
paramLinuxVersion = "version"
paramLinuxVersionDesc = "linux version. Version should be formated as X.Y (Major.minor)"
defaultUbuntuVersion = "24.04"
defaultRHELVersion = "9.4"
defaultFedoraVersion = "40.0"
)
13 changes: 0 additions & 13 deletions cmd/mapt/cmd/azure/hosts/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,14 @@ import (
const (
cmdUbuntu = "ubuntu"
cmdUbuntuDesc = "ubuntu operations"
cmdRHEL = "rhel"
cmdRHELDesc = "RHEL operations"
cmdFedora = "fedora"
cmdFedoraDesc = "fedora operations"
defaultVMSize = "Standard_D8as_v5"

paramLinuxVersion = "version"
paramLinuxVersionDesc = "linux version. Version should be formated as X.Y (Major.minor)"
defaultUbuntuVersion = "24.04"
defaultRHELVersion = "9.4"
defaultFedoraVersion = "40.0"
)

func GetUbuntuCmd() *cobra.Command {
return getLinuxCmd(cmdUbuntu, cmdUbuntuDesc, azureLinux.Ubuntu, defaultUbuntuVersion)
}

func GetRHELCmd() *cobra.Command {
return getLinuxCmd(cmdRHEL, cmdRHELDesc, azureLinux.RHEL, defaultRHELVersion)
}

func GetFedoraCmd() *cobra.Command {
return getLinuxCmd(cmdFedora, cmdFedoraDesc, azureLinux.Fedora, defaultFedoraVersion)
}
Expand Down
143 changes: 143 additions & 0 deletions cmd/mapt/cmd/azure/hosts/rhel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package hosts

import (
"fmt"

azparams "github.com/redhat-developer/mapt/cmd/mapt/cmd/azure/constants"
params "github.com/redhat-developer/mapt/cmd/mapt/cmd/constants"
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
azureRHEL "github.com/redhat-developer/mapt/pkg/provider/azure/action/rhel"
"github.com/redhat-developer/mapt/pkg/provider/util/instancetypes"
"github.com/redhat-developer/mapt/pkg/util"

spotAzure "github.com/redhat-developer/mapt/pkg/spot/azure"
"github.com/redhat-developer/mapt/pkg/util/ghactions"
"github.com/redhat-developer/mapt/pkg/util/logging"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
cmdRHEL = "rhel"
cmdRHELDesc = "RHEL operations"
)

func GetRHELCmd() *cobra.Command {
c := &cobra.Command{
Use: cmdRHEL,
Short: cmdRHELDesc,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
return nil
},
}
c.AddCommand(getCreateRHEL(), getDestroyRHEL())
return c
}

func getCreateRHEL() *cobra.Command {
c := &cobra.Command{
Use: params.CreateCmdName,
Short: params.CreateCmdName,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
// Initialize context
maptContext.Init(
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
if viper.IsSet(paramSpotTolerance) {
var ok bool
spotToleranceValue, ok = spotAzure.ParseEvictionRate(
viper.GetString(paramSpotTolerance))
if !ok {
return fmt.Errorf("%s is not a valid spot tolerance value", viper.GetString(azparams.ParamSpotTolerance))
}
}
instanceRequest := &instancetypes.AzureInstanceRequest{
CPUs: viper.GetInt32(params.CPUs),
MemoryGib: viper.GetInt32(params.Memory),
Arch: util.If(viper.GetString(params.LinuxArch) == "arm64", instancetypes.Arm64, instancetypes.Amd64),
NestedVirt: viper.GetBool(params.ProfileSNC) || viper.GetBool(params.NestedVirt),
}

// 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),
viper.GetStringSlice(params.GHActionsRunnerLabels))
if err != nil {
logging.Error(err)
}
}

if err := azureRHEL.Create(
&azureRHEL.Request{
Prefix: viper.GetString(params.ProjectName),
Location: viper.GetString(paramLocation),
VMSizes: viper.GetStringSlice(paramVMSize),
InstanceRequest: instanceRequest,
Version: viper.GetString(paramLinuxVersion),
Arch: viper.GetString(params.LinuxArch),
SubsUsername: viper.GetString(params.SubsUsername),
SubsUserpass: viper.GetString(params.SubsUserpass),
ProfileSNC: viper.IsSet(params.ProfileSNC),
Username: viper.GetString(paramUsername),
Spot: viper.IsSet(paramSpot),
SpotTolerance: spotToleranceValue,
SetupGHActionsRunner: viper.IsSet(params.InstallGHActionsRunner)}); err != nil {
logging.Error(err)
}
return nil
},
}
flagSet := pflag.NewFlagSet(params.CreateCmdName, pflag.ExitOnError)
flagSet.StringP(params.ConnectionDetailsOutput, "", "", params.ConnectionDetailsOutputDesc)
flagSet.StringToStringP(params.Tags, "", nil, params.TagsDesc)
flagSet.StringP(paramLocation, "", defaultLocation, paramLocationDesc)
flagSet.StringP(params.LinuxArch, "", params.LinuxArchDefault, params.LinuxArchDesc)
flagSet.StringSliceP(paramVMSize, "", []string{}, paramVMSizeDesc)
flagSet.StringP(paramLinuxVersion, "", defaultRHELVersion, paramLinuxVersionDesc)
flagSet.StringP(paramUsername, "", defaultUsername, paramUsernameDesc)
flagSet.StringP(params.SubsUsername, "", "", params.SubsUsernameDesc)
flagSet.StringP(params.SubsUserpass, "", "", params.SubsUserpassDesc)
flagSet.Bool(params.ProfileSNC, false, params.ProfileSNCDesc)
flagSet.Bool(paramSpot, false, paramSpotDesc)
flagSet.StringP(paramSpotTolerance, "", defaultSpotTolerance, paramSpotToleranceDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
flagSet.AddFlagSet(params.GetCpusAndMemoryFlagset())
c.PersistentFlags().AddFlagSet(flagSet)
return c
}

func getDestroyRHEL() *cobra.Command {
return &cobra.Command{
Use: params.DestroyCmdName,
Short: params.DestroyCmdName,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
// Initialize context
maptContext.Init(
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
if err := azureRHEL.Destroy(); err != nil {
logging.Error(err)
}
return nil
},
}
}
11 changes: 11 additions & 0 deletions cmd/mapt/cmd/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ const (
GHActionsRunnerName string = "ghactions-runner-name"
GHActionsRunnerRepo string = "ghactions-runner-repo"
GHActionsRunnerLabels string = "ghactions-runner-labels"

//RHEL
SubsUsername string = "rh-subscription-username"
SubsUsernameDesc string = "username to register the subscription"
SubsUserpass string = "rh-subscription-password"
SubsUserpassDesc string = "password to register the subscription"
ProfileSNC string = "snc"
ProfileSNCDesc string = "if this flag is set the RHEL will be setup with SNC profile. Setting up all requirements to run https://github.com/crc-org/snc"
RhelVersion string = "version"
RhelVersionDesc string = "version for the RHEL OS"
RhelVersionDefault string = "9.4"
)

func GetGHActionsFlagset() *pflag.FlagSet {
Expand Down
2 changes: 0 additions & 2 deletions pkg/provider/aws/action/fedora/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ func (r *Request) getUserdata() (pulumi.StringPtrInput, error) {
amiUserDefault,
r.SetupGHActionsRunner,
ghactions.GetActionRunnerSnippetLinux()},
resourcesUtil.GetResourceName(
r.Prefix, awsFedoraDedicatedID, "userdata"),
templateConfig)
return pulumi.String(base64.StdEncoding.EncodeToString([]byte(userdata))), err
}
1 change: 0 additions & 1 deletion pkg/provider/aws/action/mac/mac-machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ func (r *MacRequest) getBootstrapScript(ctx *pulumi.Context) (
authorizedKey,
r.SetupGHActionsRunner,
ghactions.GetActionRunnerSnippetMacos()},
resourcesUtil.GetResourceName(r.Prefix, awsMacMachineID, "mac-bootstrap"),
string(BootstrapScript[:]))
}).(pulumi.StringOutput)
return postscript, password, ukp, nil
Expand Down
43 changes: 5 additions & 38 deletions pkg/provider/aws/action/rhel/rhel.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package rhel

import (
_ "embed"
"encoding/base64"
"fmt"
"os"

Expand All @@ -24,9 +22,8 @@ import (
"github.com/redhat-developer/mapt/pkg/provider/util/command"
"github.com/redhat-developer/mapt/pkg/provider/util/instancetypes"
"github.com/redhat-developer/mapt/pkg/provider/util/output"
targetRHEL "github.com/redhat-developer/mapt/pkg/targets/rhel"
"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 Down Expand Up @@ -60,20 +57,6 @@ type Request struct {
spotPrice float64
}

type userDataValues struct {
SubscriptionUsername string
SubscriptionPassword string
Username string
InstallActionsRunner bool
ActionsRunnerSnippet string
}

//go:embed cloud-config-base
var CloudConfigBase []byte

//go:embed cloud-config-snc
var CloudConfigSNC []byte

// Create orchestrate 2 stacks:
// If spot is enable it will run best spot option to get the best option to spin the machine
// Then it will run the stack for windows dedicated host
Expand Down Expand Up @@ -214,7 +197,9 @@ func (r *Request) deploy(ctx *pulumi.Context) error {
return err
}
// Compute
userDataB64, err := r.getUserdata()
userDataB64, err := targetRHEL.GetUserdata(r.ProfileSNC,
r.SubsUsername, r.SubsUserpass, amiUserDefault,
r.SetupGHActionsRunner)
if err != nil {
return err
}
Expand All @@ -224,7 +209,7 @@ func (r *Request) deploy(ctx *pulumi.Context) error {
VPC: vpc,
Subnet: targetSubnet,
AMI: ami,
UserDataAsBase64: userDataB64,
UserDataAsBase64: pulumi.String(userDataB64),
KeyResources: keyResources,
SecurityGroups: securityGroups,
InstaceTypes: util.If(len(r.VMType) > 0,
Expand Down Expand Up @@ -287,21 +272,3 @@ func (r *Request) securityGroups(ctx *pulumi.Context,
})
return pulumi.StringArray(sgs[:]), nil
}

func (r *Request) getUserdata() (pulumi.StringPtrInput, error) {
templateConfig := string(CloudConfigBase[:])
if r.ProfileSNC {
templateConfig = string(CloudConfigSNC[:])
}
userdata, err := file.Template(
userDataValues{
r.SubsUsername,
r.SubsUserpass,
amiUserDefault,
r.SetupGHActionsRunner,
ghactions.GetActionRunnerSnippetLinux()},
resourcesUtil.GetResourceName(
r.Prefix, awsRHELDedicatedID, "userdata"),
templateConfig)
return pulumi.String(base64.StdEncoding.EncodeToString([]byte(userdata))), err
}
3 changes: 1 addition & 2 deletions pkg/provider/aws/action/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ func (r *Request) getUserdata(ctx *pulumi.Context,
func(args []interface{}) (string, error) {
password := args[0].(string)
authorizedKey := args[1].(string)
tmplName := resourcesUtil.GetResourceName(r.Prefix, awsWindowsDedicatedID, "userdatas")
udv := userDataValues{
r.AMIUser,
password,
Expand All @@ -333,7 +332,7 @@ func (r *Request) getUserdata(ctx *pulumi.Context,
ghactions.GetActionRunnerSnippetWin(),
ghactions.GetToken(),
}
userdata, err := file.Template(udv, tmplName, string(BootstrapScript[:]))
userdata, err := file.Template(udv, string(BootstrapScript[:]))
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit d37ee99

Please sign in to comment.