-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathdynatrace.go
More file actions
161 lines (137 loc) · 5.7 KB
/
dynatrace.go
File metadata and controls
161 lines (137 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package dynatrace
import (
"fmt"
"os"
"github.com/openshift/osdctl/cmd/promote/utils"
"github.com/spf13/cobra"
)
type promoteDynatraceOptions struct {
list bool
appInterfaceProvidedPath string
gitHash string
component string
terraform bool
module string
dynatraceConfigCheckoutDir string
}
// NewCmdDynatrace implements the promote command to promote services/operators
func NewCmdDynatrace() *cobra.Command {
ops := &promoteDynatraceOptions{}
promoteDynatraceCmd := &cobra.Command{
Use: "dynatrace",
Short: "Utilities to promote dynatrace",
Args: cobra.NoArgs,
DisableAutoGenTag: true,
Long: `Promote Dynatrace components or terraform modules.
DYNATRACE COMPONENTS:
Components are defined in app-interface under:
data/services/osd-operators/cicd/saas/saas-dynatrace/
Each component maps to a specific path within the dynatrace-config repository.
When promoting a component, only changes to that component's path are included
in the promotion diff.
Please run 'osdctl promote dynatrace --list' to check available dynatrace components for promotion & their corresponding paths.
TERRAFORM MODULES:
Modules are defined in the dynatrace-config repository under:
terraform/modules/
Promoting a module updates configs in:
terraform/redhat-aws/sd-sre/`,
Example: `
# List all Dynatrace components available for promotion
osdctl promote dynatrace --list
# Promote a dynatrace component
osdctl promote dynatrace --component <component> --gitHash <git-hash>
# List all dynatrace-config modules available for promotion
osdctl promote dynatrace --terraform --list
# Promote a dynatrace module
osdctl promote dynatrace --terraform --module=<module-name>`,
RunE: func(cmd *cobra.Command, args []string) error {
if ops.terraform {
dynatraceConfig := DynatraceConfigPromotion(ops.dynatraceConfigCheckoutDir)
if ops.list {
ops.validateSaasFlow()
if ops.component != "" || ops.gitHash != "" || ops.module != "" {
fmt.Printf("Error: Please provide correct parameters \n\n")
fmt.Printf("Please run 'osdctl promote dynatrace --terraform --list' to check available dynatrace-config modules for promotion.\n\n")
cmd.Help()
os.Exit(1)
}
_ = listDynatraceModuleNames(dynatraceConfig)
os.Exit(0)
} else {
if ops.module == "" {
fmt.Printf("Error: Please provide correct parameters \n\n")
fmt.Printf("Please run 'osdctl promote dynatrace --terraform --module=<module-name>' to check promote dynatrace-config module to latest ref.\n\n")
cmd.Help()
os.Exit(1)
} else if ops.component != "" || ops.gitHash != "" {
fmt.Printf("Error: Please provide correct parameters \n\n")
fmt.Printf("Please run 'osdctl promote dynatrace --terraform --module=<module-name>' to check promote dynatrace-config module to latest ref.\n\n")
cmd.Help()
os.Exit(1)
} else {
err := modulePromotion(dynatraceConfig, ops.module)
if err != nil {
fmt.Printf("Error while promoting module: %v\n", err)
os.Exit(1)
}
}
}
} else {
ops.validateSaasFlow()
appInterfaceClone, err := utils.FindAppInterfaceClone(ops.appInterfaceProvidedPath)
if err != nil {
return err
}
servicesRegistry, err := utils.NewServicesRegistry(
appInterfaceClone,
validateDynatraceServiceFilePath,
saasDynatraceDir,
)
if err != nil {
return err
}
if ops.list {
if ops.component != "" || ops.gitHash != "" {
fmt.Printf("Error: --list cannot be used with any other flags\n\n")
cmd.Help()
os.Exit(1)
}
_ = listServiceIds(servicesRegistry)
os.Exit(0)
}
if ops.component == "" {
fmt.Printf("Error: Please provide dynatrace component to promote.\n\n")
fmt.Printf("Please run 'osdctl promote dynatrace --list' to check available dynatrace components for promotion.\n\n")
cmd.Help()
os.Exit(1)
}
service, err := servicesRegistry.GetService(ops.component)
if err != nil {
return err
}
err = service.Promote(&utils.DefaultPromoteCallbacks{Service: service}, ops.gitHash)
if err != nil {
fmt.Printf("Error while promoting service: %v\n", err)
os.Exit(1)
}
}
return nil
},
}
promoteDynatraceCmd.Flags().BoolVarP(&ops.list, "list", "l", false, "List all SaaS services/operators")
promoteDynatraceCmd.Flags().StringVarP(&ops.component, "component", "c", "", "Dynatrace component getting promoted")
promoteDynatraceCmd.Flags().StringVarP(&ops.gitHash, "gitHash", "g", "", "Git hash of the SaaS service/operator commit getting promoted")
promoteDynatraceCmd.Flags().StringVarP(&ops.appInterfaceProvidedPath, "appInterfaceDir", "", "", "location of app-interface checkout. Falls back to current working directory")
promoteDynatraceCmd.Flags().BoolVarP(&ops.terraform, "terraform", "t", false, "deploy dynatrace-config terraform job")
promoteDynatraceCmd.Flags().StringVarP(&ops.module, "module", "m", "", "module to promote")
promoteDynatraceCmd.Flags().StringVarP(&ops.dynatraceConfigCheckoutDir, "dynatraceConfigDir", "", "", "location of dynatrace-config checkout. Falls back to current working directory")
return promoteDynatraceCmd
}
func (o *promoteDynatraceOptions) validateSaasFlow() {
if o.component == "" && o.gitHash == "" && !o.terraform {
fmt.Printf("Usage: For dynatrace component, please provide --component and (optional) --gitHash\n")
fmt.Printf("--component is the name of the component, i.e. dynatrace-dynakube\n")
fmt.Printf("--gitHash is the target git commit for the dt component, if not specified defaults to HEAD of master\n\n")
return
}
}