Skip to content

Commit

Permalink
feat: mac-pool service on AWS
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Riobo <[email protected]>
  • Loading branch information
adrianriobo committed Jan 15, 2025
1 parent 08c1840 commit bc1d016
Show file tree
Hide file tree
Showing 34 changed files with 1,142 additions and 431 deletions.
14 changes: 14 additions & 0 deletions cmd/mapt/cmd/aws/constants/contants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package constants

import "github.com/redhat-developer/mapt/pkg/provider/aws/action/mac"

const (
ParamMACArch string = "arch"
ParamMACArchDesc string = "mac architecture allowed values x86, m1, m2"
ParamMACArchDefault string = mac.DefaultArch
ParamMACOSVersion string = "version"
ParamMACOSVersionDesc string = "macos operating system vestion 11, 12 on x86 and m1/m2; 13, 14 on all archs"
ParamMACOSVersionDefault string = mac.DefaultOSVersion
ParamMACFixedLocation string = "fixed-location"
ParamMACFixedLocationDesc string = "if this flag is set the host will be created only on the region set by the AWS Env (AWS_DEFAULT_REGION)"
)
12 changes: 4 additions & 8 deletions cmd/mapt/cmd/aws/hosts/constans.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package hosts

const (
spot string = "spot"
spotDesc string = "if this flag is set the host will be created only on the region set by the AWS Env (AWS_DEFAULT_REGION)"
airgap string = "airgap"
airgapDesc string = "if this flag is set the host will be created as airgap machine. Access will done through a bastion"
serverless string = "serverless"
serverlessDesc string = "if serverless is set the command will be executed as a serverless action."
timeout string = "timeout"
timeoutDesc string = "if timeout is set a serverless destroy actions will be set on the time according to the timeout. The Timeout value is a duration conforming to Go ParseDuration format."
spot string = "spot"
spotDesc string = "if this flag is set the host will be created only on the region set by the AWS Env (AWS_DEFAULT_REGION)"
airgap string = "airgap"
airgapDesc string = "if this flag is set the host will be created as airgap machine. Access will done through a bastion"

vmTypes string = "vm-types"
vmTypesDescription string = "set an specific set of vm-types and ignore any CPUs, Memory, Arch parameters set. Note vm-type should match requested arch. Also if --spot flag is used set at least 3 types."
Expand Down
14 changes: 8 additions & 6 deletions cmd/mapt/cmd/aws/hosts/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func getFedoraCreate() *cobra.Command {
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
viper.GetUint(params.DebugLevel),
false)

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
Expand Down Expand Up @@ -81,7 +82,7 @@ func getFedoraCreate() *cobra.Command {
VMType: viper.GetStringSlice(vmTypes),
InstanceRequest: instanceRequest,
Spot: viper.IsSet(spot),
Timeout: viper.GetString(timeout),
Timeout: viper.GetString(params.Timeout),
SetupGHActionsRunner: viper.IsSet(params.InstallGHActionsRunner),
Airgap: viper.IsSet(airgap)}); err != nil {
logging.Error(err)
Expand All @@ -97,7 +98,7 @@ func getFedoraCreate() *cobra.Command {
flagSet.StringSliceP(vmTypes, "", []string{}, vmTypesDescription)
flagSet.Bool(airgap, false, airgapDesc)
flagSet.Bool(spot, false, spotDesc)
flagSet.StringP(timeout, "", "", timeout)
flagSet.StringP(params.Timeout, "", "", params.TimeoutDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
flagSet.AddFlagSet(params.GetCpusAndMemoryFlagset())
c.PersistentFlags().AddFlagSet(flagSet)
Expand All @@ -117,18 +118,19 @@ func getFedoraDestroy() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
viper.GetUint(params.DebugLevel),
viper.IsSet(params.Serverless))

logging.Debug("Run fedora destroy")

if err := fedora.Destroy(viper.IsSet(serverless)); err != nil {
if err := fedora.Destroy(); err != nil {
logging.Error(err)
}
return nil
},
}
flagSet := pflag.NewFlagSet(params.DestroyCmdName, pflag.ExitOnError)
flagSet.Bool(serverless, false, serverlessDesc)
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
28 changes: 11 additions & 17 deletions cmd/mapt/cmd/aws/hosts/mac.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hosts

import (
awsParams "github.com/redhat-developer/mapt/cmd/mapt/cmd/aws/constants"
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"
Expand All @@ -19,16 +20,8 @@ const (
releaseCmd = "release"
releaseCmdDesc = "release mac machine"

dhID string = "dedicated-host-id"
dhIDDesc string = "id for the dedicated host"
arch string = "arch"
archDesc string = "mac architecture allowed values x86, m1, m2"
archDefault string = mac.DefaultArch
osVersion string = "version"
osVersionDesc string = "macos operating system vestion 11, 12 on x86 and m1/m2; 13, 14 on all archs"
osDefault string = mac.DefaultOSVersion
fixedLocation string = "fixed-location"
fixedLocationDesc string = "if this flag is set the host will be created only on the region set by the AWS Env (AWS_DEFAULT_REGION)"
dhID string = "dedicated-host-id"
dhIDDesc string = "id for the dedicated host"
)

func GetMacCmd() *cobra.Command {
Expand Down Expand Up @@ -62,7 +55,8 @@ func getMacRequest() *cobra.Command {
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
viper.GetUint(params.DebugLevel),
false)

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
Expand All @@ -79,9 +73,9 @@ func getMacRequest() *cobra.Command {
if err := mac.Request(
&mac.MacRequest{
Prefix: "main",
Architecture: viper.GetString(arch),
Version: viper.GetString(osVersion),
FixedLocation: viper.IsSet(fixedLocation),
Architecture: viper.GetString(awsParams.ParamMACArch),
Version: viper.GetString(awsParams.ParamMACOSVersion),
FixedLocation: viper.IsSet(awsParams.ParamMACFixedLocation),
SetupGHActionsRunner: viper.GetBool(params.InstallGHActionsRunner),
Airgap: viper.IsSet(airgap)}); err != nil {
logging.Error(err)
Expand All @@ -92,9 +86,9 @@ func getMacRequest() *cobra.Command {
flagSet := pflag.NewFlagSet(requestCmd, pflag.ExitOnError)
flagSet.StringP(params.ConnectionDetailsOutput, "", "", params.ConnectionDetailsOutputDesc)
flagSet.StringToStringP(params.Tags, "", nil, params.TagsDesc)
flagSet.StringP(arch, "", archDefault, archDesc)
flagSet.StringP(osVersion, "", osDefault, osVersionDesc)
flagSet.Bool(fixedLocation, false, fixedLocationDesc)
flagSet.StringP(awsParams.ParamMACArch, "", awsParams.ParamMACArchDefault, awsParams.ParamMACArchDesc)
flagSet.StringP(awsParams.ParamMACOSVersion, "", awsParams.ParamMACOSVersion, awsParams.ParamMACOSVersionDefault)
flagSet.Bool(awsParams.ParamMACFixedLocation, false, awsParams.ParamMACFixedLocationDesc)
flagSet.Bool(airgap, false, airgapDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
c.PersistentFlags().AddFlagSet(flagSet)
Expand Down
14 changes: 8 additions & 6 deletions cmd/mapt/cmd/aws/hosts/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func getRHELCreate() *cobra.Command {
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
viper.GetUint(params.DebugLevel),
false)

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
Expand Down Expand Up @@ -81,7 +82,7 @@ func getRHELCreate() *cobra.Command {
SubsUserpass: viper.GetString(params.SubsUserpass),
ProfileSNC: viper.IsSet(params.ProfileSNC),
Spot: viper.IsSet(spot),
Timeout: viper.GetString(timeout),
Timeout: viper.GetString(params.Timeout),
Airgap: viper.IsSet(airgap),
SetupGHActionsRunner: viper.IsSet(params.InstallGHActionsRunner),
}); err != nil {
Expand All @@ -100,7 +101,7 @@ func getRHELCreate() *cobra.Command {
flagSet.StringP(params.SubsUserpass, "", "", params.SubsUserpassDesc)
flagSet.Bool(airgap, false, airgapDesc)
flagSet.Bool(spot, false, spotDesc)
flagSet.StringP(timeout, "", "", timeout)
flagSet.StringP(params.Timeout, "", "", params.TimeoutDesc)
flagSet.Bool(params.ProfileSNC, false, params.ProfileSNCDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
flagSet.AddFlagSet(params.GetCpusAndMemoryFlagset())
Expand All @@ -121,16 +122,17 @@ func getRHELDestroy() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
viper.GetUint(params.DebugLevel),
viper.IsSet(params.Serverless))

if err := rhel.Destroy(viper.IsSet(serverless)); err != nil {
if err := rhel.Destroy(); err != nil {
logging.Error(err)
}
return nil
},
}
flagSet := pflag.NewFlagSet(params.DestroyCmdName, pflag.ExitOnError)
flagSet.Bool(serverless, false, serverlessDesc)
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
14 changes: 8 additions & 6 deletions cmd/mapt/cmd/aws/hosts/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func getWindowsCreate() *cobra.Command {
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
viper.GetUint(params.DebugLevel),
false)

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
Expand All @@ -86,7 +87,7 @@ func getWindowsCreate() *cobra.Command {
AMIKeepCopy: viper.IsSet(amiKeepCopy),
Spot: viper.IsSet(spot),
Airgap: viper.IsSet(airgap),
Timeout: viper.GetString(timeout),
Timeout: viper.GetString(params.Timeout),
SetupGHActionsRunner: viper.IsSet(params.InstallGHActionsRunner),
}); err != nil {
logging.Error(err)
Expand All @@ -103,7 +104,7 @@ func getWindowsCreate() *cobra.Command {
flagSet.StringP(amiLang, "", amiLangDefault, amiLangDesc)
flagSet.Bool(airgap, false, airgapDesc)
flagSet.Bool(spot, false, spotDesc)
flagSet.StringP(timeout, "", "", timeout)
flagSet.StringP(params.Timeout, "", "", params.TimeoutDesc)
flagSet.Bool(amiKeepCopy, false, amiKeepCopyDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
c.PersistentFlags().AddFlagSet(flagSet)
Expand All @@ -123,16 +124,17 @@ func getWindowsDestroy() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
viper.GetUint(params.DebugLevel),
viper.IsSet(params.Serverless))

if err := windows.Destroy(viper.IsSet(serverless)); err != nil {
if err := windows.Destroy(); err != nil {
logging.Error(err)
}
return nil
},
}
flagSet := pflag.NewFlagSet(params.DestroyCmdName, pflag.ExitOnError)
flagSet.Bool(serverless, false, serverlessDesc)
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
139 changes: 139 additions & 0 deletions cmd/mapt/cmd/aws/services/mac-pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package services

import (
awsParams "github.com/redhat-developer/mapt/cmd/mapt/cmd/aws/constants"
params "github.com/redhat-developer/mapt/cmd/mapt/cmd/constants"
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
macpool "github.com/redhat-developer/mapt/pkg/provider/aws/action/mac-pool"
"github.com/redhat-developer/mapt/pkg/util/logging"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
cmdMacPool = "mac-pool"
cmdMacPoolDesc = "mac pool operations"

cmdHousekeep = "house-keep"
cmdHousekeepDesc = "house keeping for mac pool. Detroy old machines on over capacity and create new ones if capacity not meet"

paramName = "name"
paramNameDesc = "pool name it is a unique identifier for the pool. The name should be unique for the whole AWS account"
paramOfferedCapacity = "offered-capacity"
paramOfferedCapacityDesc = "offered capacity to accept new workloads (non locked machines in the pool) at any given time. This is limited by max pool size parameter"
paramOfferedCapacityDefault = 1
paramMaxSize = "max-size"
paramMaxSizeDesc = "max number of machines in the pool"
paramMaxSizeDefault = 2
)

func GetMacPoolCmd() *cobra.Command {
c := &cobra.Command{
Use: cmdMacPool,
Short: cmdMacPoolDesc,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
return nil
},
}
c.AddCommand(
getCreateMacPool(),
getHouseKeepingMacPool())
return c
}

func getCreateMacPool() *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),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel),
false)

if err := macpool.Create(
&macpool.RequestArgs{
Prefix: "main",
PoolName: viper.GetString(paramName),
Architecture: viper.GetString(awsParams.ParamMACArch),
Version: viper.GetString(awsParams.ParamMACOSVersion),
OfferedCapacity: viper.GetInt(paramOfferedCapacity),
MaxSize: viper.GetInt(paramMaxSize),
FixedLocation: viper.IsSet(awsParams.ParamMACFixedLocation)}); 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(paramName, "", "", paramNameDesc)
flagSet.Int(paramOfferedCapacity, paramOfferedCapacityDefault, paramOfferedCapacityDesc)
flagSet.Int(paramMaxSize, paramMaxSizeDefault, paramMaxSizeDesc)
flagSet.StringP(awsParams.ParamMACArch, "", awsParams.ParamMACArchDefault, awsParams.ParamMACArchDesc)
flagSet.StringP(awsParams.ParamMACOSVersion, "", awsParams.ParamMACOSVersion, awsParams.ParamMACOSVersionDefault)
flagSet.Bool(awsParams.ParamMACFixedLocation, false, awsParams.ParamMACFixedLocationDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
c.PersistentFlags().AddFlagSet(flagSet)
return c
}

func getHouseKeepingMacPool() *cobra.Command {
c := &cobra.Command{
Use: cmdHousekeep,
Short: cmdHousekeepDesc,
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),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel),
viper.IsSet(params.Serverless))

if err := macpool.HouseKeeper(
&macpool.RequestArgs{
Prefix: "main",
PoolName: viper.GetString(paramName),
Architecture: viper.GetString(awsParams.ParamMACArch),
Version: viper.GetString(awsParams.ParamMACOSVersion),
OfferedCapacity: viper.GetInt(paramOfferedCapacity),
MaxSize: viper.GetInt(paramMaxSize),
FixedLocation: viper.IsSet(awsParams.ParamMACFixedLocation)}); 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(paramName, "", "", paramNameDesc)
flagSet.Int(paramOfferedCapacity, paramOfferedCapacityDefault, paramOfferedCapacityDesc)
flagSet.Int(paramMaxSize, paramMaxSizeDefault, paramMaxSizeDesc)
flagSet.StringP(awsParams.ParamMACArch, "", awsParams.ParamMACArchDefault, awsParams.ParamMACArchDesc)
flagSet.StringP(awsParams.ParamMACOSVersion, "", awsParams.ParamMACOSVersion, awsParams.ParamMACOSVersionDefault)
flagSet.Bool(awsParams.ParamMACFixedLocation, false, awsParams.ParamMACFixedLocationDesc)
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
flagSet.AddFlagSet(params.GetGHActionsFlagset())
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package replica
package services

import (
params "github.com/redhat-developer/mapt/cmd/mapt/cmd/constants"
Expand Down
Loading

0 comments on commit bc1d016

Please sign in to comment.