Skip to content

Commit

Permalink
feat: added debug flag and debugLevel flag to enable increase verbosi…
Browse files Browse the repository at this point in the history
…ty on the output when creating resources on cloud providers.

Signed-off-by: Adrian Riobo <[email protected]>
  • Loading branch information
adrianriobo committed Dec 19, 2024
1 parent b75784d commit 3b4233c
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 29 deletions.
8 changes: 6 additions & 2 deletions cmd/mapt/cmd/aws/hosts/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func getFedoraCreate() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
Expand Down Expand Up @@ -111,7 +113,9 @@ func getFedoraDestroy() *cobra.Command {

maptContext.InitBase(
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL))
viper.GetString(params.BackedURL),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

if err := fedora.Destroy(); err != nil {
logging.Error(err)
Expand Down
12 changes: 9 additions & 3 deletions cmd/mapt/cmd/aws/hosts/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func getMacRequest() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
Expand Down Expand Up @@ -112,7 +114,9 @@ func getMacRelease() *cobra.Command {
// Run create
if err := mac.Release(
"main",
viper.GetString(dhID)); err != nil {
viper.GetString(dhID),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel)); err != nil {
logging.Error(err)
}
return nil
Expand Down Expand Up @@ -140,7 +144,9 @@ func getMacDestroy() *cobra.Command {

if err := mac.Destroy(
"main",
viper.GetString(dhID)); err != nil {
viper.GetString(dhID),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel)); err != nil {
logging.Error(err)
}
return nil
Expand Down
8 changes: 6 additions & 2 deletions cmd/mapt/cmd/aws/hosts/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func getRHELCreate() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
Expand Down Expand Up @@ -123,7 +125,9 @@ func getRHELDestroy() *cobra.Command {

maptContext.InitBase(
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL))
viper.GetString(params.BackedURL),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

if err := rhel.Destroy(); err != nil {
logging.Error(err)
Expand Down
8 changes: 6 additions & 2 deletions cmd/mapt/cmd/aws/hosts/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func getWindowsCreate() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

// Initialize gh actions runner if needed
if viper.IsSet(params.InstallGHActionsRunner) {
Expand Down Expand Up @@ -117,7 +119,9 @@ func getWindowsDestroy() *cobra.Command {

maptContext.InitBase(
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL))
viper.GetString(params.BackedURL),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

if err := windows.Destroy(); err != nil {
logging.Error(err)
Expand Down
8 changes: 6 additions & 2 deletions cmd/mapt/cmd/azure/hosts/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func getCreateLinux(ostype azureLinux.OSType, defaultOSVersion string) *cobra.Co
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
Expand Down Expand Up @@ -124,7 +126,9 @@ func getDestroyLinux() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
if err := azureLinux.Destroy(); err != nil {
logging.Error(err)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/mapt/cmd/azure/hosts/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func getCreateRHEL() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
Expand Down Expand Up @@ -133,7 +135,9 @@ func getDestroyRHEL() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
if err := azureRHEL.Destroy(); err != nil {
logging.Error(err)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/mapt/cmd/azure/hosts/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func getCreateWindowsDesktop() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
Expand Down Expand Up @@ -143,7 +145,9 @@ func getDestroyWindowsDesktop() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
if err := azureWindows.Destroy(); err != nil {
logging.Error(err)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/mapt/cmd/azure/services/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func getCreateAKS() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
Expand Down Expand Up @@ -111,7 +113,9 @@ func getDestroyAKS() *cobra.Command {
viper.GetString(params.ProjectName),
viper.GetString(params.BackedURL),
viper.GetString(params.ConnectionDetailsOutput),
viper.GetStringMapString(params.Tags))
viper.GetStringMapString(params.Tags),
viper.IsSet(params.Debug),
viper.GetUint(params.DebugLevel))
if err := azureAKS.Destroy(); err != nil {
logging.Error(err)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/mapt/cmd/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ const (
ProjectName string = "project-name"
ProjectNameDesc string = "project name to identify the instance of the stack"
BackedURL string = "backed-url"
BackedURLDesc string = "backed for stack state. Can be a local path with format file:///path/subpath or s3 s3://existing-bucket"
BackedURLDesc string = "backed for stack state. (local) file:///path/subpath (s3) s3://existing-bucket, (azure) azblob://existing-blobcontainer. See more https://www.pulumi.com/docs/iac/concepts/state-and-backends/#using-a-self-managed-backend"
ConnectionDetailsOutput string = "conn-details-output"
ConnectionDetailsOutputDesc string = "path to export host connection information (host, username and privateKey)"
Debug string = "debug"
DebugDesc string = "Enable debug traces and set verbosity to max. Typically to get information to troubleshooting an issue."
DebugLevel string = "debug-level"
DebugLevelDefault uint = 3
DebugLevelDesc string = "Set the level of verbosity on debug. You can set from minimum 1 to max 9."
LinuxArch string = "arch"
LinuxArchDesc string = "architecture for the machine. Allowed x86_64 or arm64"
LinuxArchDefault string = "x86_64"
Expand Down
2 changes: 2 additions & 0 deletions cmd/mapt/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func init() {
flagSet := pflag.NewFlagSet(commandName, pflag.ExitOnError)
flagSet.StringP(params.ProjectName, "", "", params.ProjectNameDesc)
flagSet.StringP(params.BackedURL, "", "", params.BackedURLDesc)
flagSet.Bool(params.Debug, false, params.DebugDesc)
flagSet.Uint(params.DebugLevel, params.DebugLevelDefault, params.DebugLevelDesc)
rootCmd.PersistentFlags().AddFlagSet(flagSet)
// Subcommands
rootCmd.AddCommand(
Expand Down
18 changes: 16 additions & 2 deletions pkg/manager/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,34 @@ type context struct {
projectName string
backedURL string
resultsOutput string
debug bool
debugLevel uint
tags map[string]string
tagsAsPulumiStringMap pulumi.StringMap
}

var c *context

func Init(projectName, backedURL, resultsOutput string, tags map[string]string) {
func Init(projectName, backedURL, resultsOutput string, tags map[string]string, debug bool, debugLevel uint) {
c = &context{
runID: CreateRunID(),
projectName: projectName,
backedURL: backedURL,
resultsOutput: resultsOutput,
debug: debug,
debugLevel: debugLevel,
tags: tags,
}
addCommonTags()
logging.Debugf("context initialized for %s", c.runID)
}

func InitBase(projectName, backedURL string) {
func InitBase(projectName, backedURL string, debug bool, debugLevel uint) {
c = &context{
projectName: projectName,
backedURL: backedURL,
debug: debug,
debugLevel: debugLevel,
}
}

Expand Down Expand Up @@ -104,6 +110,14 @@ func StackNameByProject(stackName string) string {
return fmt.Sprintf("%s-%s", stackName, c.projectName)
}

func Debug() bool {
return c.debug
}

func DebugLevel() uint {
return c.debugLevel
}

func addCommonTags() {
c.tags[tagKeyOrigin] = origin
c.tags[TagKeyProjectName] = c.projectName
Expand Down
28 changes: 23 additions & 5 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/auto/optdestroy"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
"github.com/redhat-developer/mapt/pkg/manager/credentials"
"github.com/redhat-developer/mapt/pkg/util/logging"
)
Expand Down Expand Up @@ -38,11 +39,17 @@ func UpStackTargets(targetStack Stack, targetURNs []string, opts ...ManagerOptio
// TODO add when loglevel debug control in place
w := logging.GetWritter()
defer w.Close()
var logLevel uint = 10
mOpts := []optup.Option{
optup.ProgressStreams(w),
optup.DebugLogging(
debug.LoggingOptions{LogLevel: &logLevel}),
}
if maptContext.Debug() {
dl := maptContext.DebugLevel()
mOpts = append(mOpts, optup.DebugLogging(
debug.LoggingOptions{
LogLevel: &dl,
Debug: true,
FlowToPlugins: true,
LogToStdErr: true}))
}
if len(targetURNs) > 0 {
mOpts = append(mOpts, optup.Target(targetURNs))
Expand All @@ -64,8 +71,19 @@ func DestroyStack(targetStack Stack, opts ...ManagerOptions) (err error) {
objectStack := getStack(ctx, targetStack)
w := logging.GetWritter()
defer w.Close()
stdoutStreamer := optdestroy.ProgressStreams(w)
if _, err := objectStack.Destroy(ctx, stdoutStreamer); err != nil {
// stdoutStreamer := optdestroy.ProgressStreams(w)
mOpts := []optdestroy.Option{
optdestroy.ProgressStreams(w),
}
if maptContext.Debug() {
dl := maptContext.DebugLevel()
mOpts = append(mOpts, optdestroy.DebugLogging(
debug.LoggingOptions{
LogLevel: &dl,
FlowToPlugins: true,
LogToStdErr: true}))
}
if _, err := objectStack.Destroy(ctx, mOpts...); err != nil {
logging.Error(err)
os.Exit(1)
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/provider/aws/action/mac/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Request(r *MacRequest) error {
// get projectName (tag on the dh)
// load machine stack based on those params
// run release update on it
func Release(prefix string, hostID string) error {
func Release(prefix string, hostID string, debug bool, debugLevel uint) error {
host, err := data.GetDedicatedHost(hostID)
if err != nil {
return err
Expand All @@ -75,7 +75,8 @@ func Release(prefix string, hostID string) error {
// Set context based on info from dedicated host to be released
maptContext.InitBase(
*hi.ProjectName,
*hi.BackedURL)
*hi.BackedURL,
debug, debugLevel)

// Set a default request
r := &MacRequest{
Expand All @@ -89,7 +90,7 @@ func Release(prefix string, hostID string) error {
// Initial scenario consider 1 machine
// If we request destroy mac machine it will look for any machine
// and check if it is locked if not locked it will destroy it
func Destroy(prefix, hostID string) error {
func Destroy(prefix, hostID string, debug bool, debugLevel uint) error {
host, err := data.GetDedicatedHost(hostID)
if err != nil {
return err
Expand All @@ -98,7 +99,8 @@ func Destroy(prefix, hostID string) error {
// Set context based on info from dedicated host to be released
maptContext.InitBase(
*hi.ProjectName,
*hi.BackedURL)
*hi.BackedURL,
debug, debugLevel)
// Dedicated host is not on a valid state to be deleted
// With same backedURL check if machine is locked
machineLocked, err := isMachineLocked(prefix, hi)
Expand Down

0 comments on commit 3b4233c

Please sign in to comment.