diff --git a/cmd/mapt/cmd/aws/hosts/fedora.go b/cmd/mapt/cmd/aws/hosts/fedora.go index d44570632..99b51e578 100644 --- a/cmd/mapt/cmd/aws/hosts/fedora.go +++ b/cmd/mapt/cmd/aws/hosts/fedora.go @@ -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) { @@ -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) diff --git a/cmd/mapt/cmd/aws/hosts/mac.go b/cmd/mapt/cmd/aws/hosts/mac.go index abe07b6a0..ee473437a 100644 --- a/cmd/mapt/cmd/aws/hosts/mac.go +++ b/cmd/mapt/cmd/aws/hosts/mac.go @@ -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) { @@ -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 @@ -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 diff --git a/cmd/mapt/cmd/aws/hosts/rhel.go b/cmd/mapt/cmd/aws/hosts/rhel.go index dc0278569..793517a41 100644 --- a/cmd/mapt/cmd/aws/hosts/rhel.go +++ b/cmd/mapt/cmd/aws/hosts/rhel.go @@ -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) { @@ -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) diff --git a/cmd/mapt/cmd/aws/hosts/windows.go b/cmd/mapt/cmd/aws/hosts/windows.go index cc9e4266e..1fe7540ce 100644 --- a/cmd/mapt/cmd/aws/hosts/windows.go +++ b/cmd/mapt/cmd/aws/hosts/windows.go @@ -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) { @@ -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) diff --git a/cmd/mapt/cmd/azure/hosts/linux.go b/cmd/mapt/cmd/azure/hosts/linux.go index 692f14db5..6fe21f249 100644 --- a/cmd/mapt/cmd/azure/hosts/linux.go +++ b/cmd/mapt/cmd/azure/hosts/linux.go @@ -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 @@ -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) } diff --git a/cmd/mapt/cmd/azure/hosts/rhel.go b/cmd/mapt/cmd/azure/hosts/rhel.go index ed2d2d57e..2012ca222 100644 --- a/cmd/mapt/cmd/azure/hosts/rhel.go +++ b/cmd/mapt/cmd/azure/hosts/rhel.go @@ -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 @@ -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) } diff --git a/cmd/mapt/cmd/azure/hosts/windows.go b/cmd/mapt/cmd/azure/hosts/windows.go index 577bd2b94..8d71f86c8 100644 --- a/cmd/mapt/cmd/azure/hosts/windows.go +++ b/cmd/mapt/cmd/azure/hosts/windows.go @@ -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 @@ -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) } diff --git a/cmd/mapt/cmd/azure/services/aks.go b/cmd/mapt/cmd/azure/services/aks.go index d4d7d3412..95512f89a 100644 --- a/cmd/mapt/cmd/azure/services/aks.go +++ b/cmd/mapt/cmd/azure/services/aks.go @@ -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 @@ -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) } diff --git a/cmd/mapt/cmd/constants/constants.go b/cmd/mapt/cmd/constants/constants.go index a88410e19..71c52371a 100644 --- a/cmd/mapt/cmd/constants/constants.go +++ b/cmd/mapt/cmd/constants/constants.go @@ -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" diff --git a/cmd/mapt/cmd/root.go b/cmd/mapt/cmd/root.go index 6b1c04703..f822a3217 100644 --- a/cmd/mapt/cmd/root.go +++ b/cmd/mapt/cmd/root.go @@ -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( diff --git a/pkg/manager/context/context.go b/pkg/manager/context/context.go index ce7a85a3e..4c501a7f3 100644 --- a/pkg/manager/context/context.go +++ b/pkg/manager/context/context.go @@ -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, } } @@ -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 diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index c7b55540e..7aef52fc2 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -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" ) @@ -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)) @@ -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) } diff --git a/pkg/provider/aws/action/mac/mac.go b/pkg/provider/aws/action/mac/mac.go index d22bf8384..1dce5b64b 100644 --- a/pkg/provider/aws/action/mac/mac.go +++ b/pkg/provider/aws/action/mac/mac.go @@ -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 @@ -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{ @@ -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 @@ -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)