Skip to content

Commit

Permalink
feat: added debug flag to enable increase verbosity on the output whe…
Browse files Browse the repository at this point in the history
…n creating resources on cloud providers.

Signed-off-by: Adrian Riobo <[email protected]>
  • Loading branch information
adrianriobo committed Dec 17, 2024
1 parent 121045e commit 607fe5b
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 29 deletions.
6 changes: 4 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,8 @@ 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))

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

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

if err := fedora.Destroy(); err != nil {
logging.Error(err)
Expand Down
9 changes: 6 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,8 @@ 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))

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

if err := mac.Destroy(
"main",
viper.GetString(dhID)); err != nil {
viper.GetString(dhID),
viper.IsSet(params.Debug)); err != nil {
logging.Error(err)
}
return nil
Expand Down
6 changes: 4 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,8 @@ 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))

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

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

if err := rhel.Destroy(); err != nil {
logging.Error(err)
Expand Down
6 changes: 4 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,8 @@ 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))

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

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

if err := windows.Destroy(); err != nil {
logging.Error(err)
Expand Down
6 changes: 4 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,8 @@ 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))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
Expand Down Expand Up @@ -124,7 +125,8 @@ 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))
if err := azureLinux.Destroy(); err != nil {
logging.Error(err)
}
Expand Down
6 changes: 4 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,8 @@ 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))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
Expand Down Expand Up @@ -133,7 +134,8 @@ 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))
if err := azureRHEL.Destroy(); err != nil {
logging.Error(err)
}
Expand Down
6 changes: 4 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,8 @@ 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))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
Expand Down Expand Up @@ -143,7 +144,8 @@ 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))
if err := azureWindows.Destroy(); err != nil {
logging.Error(err)
}
Expand Down
6 changes: 4 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,8 @@ 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))

// ParseEvictionRate
var spotToleranceValue = spotAzure.DefaultEvictionRate
Expand Down Expand Up @@ -111,7 +112,8 @@ 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))
if err := azureAKS.Destroy(); err != nil {
logging.Error(err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/mapt/cmd/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ 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."
LinuxArch string = "arch"
LinuxArchDesc string = "architecture for the machine. Allowed x86_64 or arm64"
LinuxArchDefault string = "x86_64"
Expand Down
1 change: 1 addition & 0 deletions cmd/mapt/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ 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)
rootCmd.PersistentFlags().AddFlagSet(flagSet)
// Subcommands
rootCmd.AddCommand(
Expand Down
11 changes: 9 additions & 2 deletions pkg/manager/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,31 @@ type context struct {
projectName string
backedURL string
resultsOutput string
debug bool
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) {
c = &context{
runID: CreateRunID(),
projectName: projectName,
backedURL: backedURL,
resultsOutput: resultsOutput,
debug: debug,
tags: tags,
}
addCommonTags()
logging.Debugf("context initialized for %s", c.runID)
}

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

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

func Debug() bool {
return c.debug
}

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 All @@ -27,6 +28,8 @@ type ManagerOptions struct {
Baground bool
}

var logLevel uint = 9

func UpStack(targetStack Stack, opts ...ManagerOptions) (auto.UpResult, error) {
return UpStackTargets(targetStack, nil, opts...)
}
Expand All @@ -38,11 +41,16 @@ 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() {
mOpts = append(mOpts, optup.DebugLogging(
debug.LoggingOptions{
LogLevel: &logLevel,
Debug: true,
FlowToPlugins: true,
LogToStdErr: true}))
}
if len(targetURNs) > 0 {
mOpts = append(mOpts, optup.Target(targetURNs))
Expand All @@ -64,8 +72,18 @@ 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() {
mOpts = append(mOpts, optdestroy.DebugLogging(
debug.LoggingOptions{
LogLevel: &logLevel,
FlowToPlugins: true,
LogToStdErr: true}))
}
if _, err := objectStack.Destroy(ctx, mOpts...); err != nil {
logging.Error(err)
os.Exit(1)
}
Expand Down
8 changes: 4 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) error {
host, err := data.GetDedicatedHost(hostID)
if err != nil {
return err
Expand All @@ -75,7 +75,7 @@ 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)

// Set a default request
r := &MacRequest{
Expand All @@ -89,7 +89,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) error {
host, err := data.GetDedicatedHost(hostID)
if err != nil {
return err
Expand All @@ -98,7 +98,7 @@ 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)
// 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 607fe5b

Please sign in to comment.