@@ -3,18 +3,25 @@ package dynatrace
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "os/exec"
7
+ "path"
6
8
"path/filepath"
7
9
"sort"
8
10
"strings"
9
11
)
10
12
11
13
const (
12
14
saasDynatraceDir = "data/services/osd-operators/cicd/saas/saas-dynatrace"
15
+ moduleDir = "terraform/modules"
16
+ ProductionDir = "terraform/redhat-aws/sd-sre/production"
17
+ pattern = "git::https://gitlab.cee.redhat.com/service/dynatrace-config.git//terraform/modules/"
13
18
)
14
19
15
20
var (
16
21
ServicesSlice []string
17
22
ServicesFilesMap = map [string ]string {}
23
+ ModulesSlice []string
24
+ ModulesFilesMap = map [string ]string {}
18
25
)
19
26
20
27
func listServiceNames (appInterface AppInterface ) error {
@@ -139,3 +146,179 @@ func GetSaasDir(component string) (string, error) {
139
146
}
140
147
return "" , fmt .Errorf ("saas directory for service %s not found" , component )
141
148
}
149
+
150
+ func listDynatraceModuleNames (dynatraceConfig DynatraceConfig ) error {
151
+
152
+ baseDir := dynatraceConfig .GitDirectory
153
+ _ , err := GeModulesNames (baseDir , moduleDir )
154
+ if err != nil {
155
+ return err
156
+ }
157
+
158
+ sort .Strings (ModulesSlice )
159
+ fmt .Println ("### Available terraform modules in dynatrace-config ###" )
160
+ for _ , module := range ModulesSlice {
161
+ fmt .Println (module )
162
+ }
163
+
164
+ return nil
165
+ }
166
+
167
+ func GeModulesNames (baseDir , dir string ) ([]string , error ) {
168
+
169
+ dirGlob := filepath .Join (baseDir , dir )
170
+ filepaths , err := os .ReadDir (dirGlob )
171
+
172
+ if err != nil {
173
+ return nil , err
174
+ }
175
+
176
+ for _ , filepath := range filepaths {
177
+ if filepath .IsDir () {
178
+ filename := filepath .Name ()
179
+ ModulesSlice = append (ModulesSlice , filename )
180
+ ModulesFilesMap [filename ] = filepath .Name ()
181
+ }
182
+ }
183
+
184
+ return ModulesSlice , nil
185
+ }
186
+
187
+ func ValidateModuleName (moduleName string ) (string , error ) {
188
+ fmt .Printf ("### Checking if service %s exists ###\n " , moduleName )
189
+ for _ , service := range ModulesSlice {
190
+ if service == moduleName {
191
+ fmt .Printf ("Module %s found in dynatrace-config\n " , moduleName )
192
+ return moduleName , nil
193
+ }
194
+ }
195
+
196
+ return moduleName , fmt .Errorf ("service %s not found" , moduleName )
197
+ }
198
+
199
+ func updatePromotionGitHash (module string , dir string , promotionGitHash string ) (string , error ) {
200
+
201
+ fmt .Printf ("Iterating over directory : %s" , dir )
202
+ items , _ := os .ReadDir (dir )
203
+ for _ , item := range items {
204
+ fmt .Println ("Production tenant: " , item .Name ())
205
+ if item .IsDir () {
206
+ subDir := filepath .Join (dir , item .Name ())
207
+ subitems , _ := os .ReadDir (subDir )
208
+ for _ , subitem := range subitems {
209
+ if subitem .IsDir () {
210
+ fmt .Println ("Folder : " , subitem .Name ())
211
+ subDir2 := filepath .Join (subDir , subitem .Name ())
212
+ subitems2 , _ := os .ReadDir (subDir2 )
213
+ for _ , subitem2 := range subitems2 {
214
+ if ! subitem2 .IsDir () {
215
+ filePath := filepath .Join (subDir2 , subitem2 .Name ())
216
+ extension := path .Ext (filePath )
217
+ if extension == ".tf" {
218
+ err := updateFileContent (filePath , module , promotionGitHash )
219
+ if err != nil {
220
+ return "" , fmt .Errorf ("error while writing files %s" , err )
221
+ }
222
+ }
223
+ }
224
+ }
225
+ }
226
+ }
227
+ }
228
+ }
229
+
230
+ return "" , nil
231
+ }
232
+
233
+ func updateFileContent (filePath string , module , promotionGitHash string ) error {
234
+ var filename = filePath
235
+ file , err := Open (filename )
236
+ if err != nil {
237
+ fmt .Println (err )
238
+ }
239
+
240
+ ok := UpdateDefaultValue (file , module , promotionGitHash )
241
+ if ok {
242
+ err := Save (filename , file )
243
+ if err != nil {
244
+ return fmt .Errorf ("Error while updating file %s: %s\n " , filename , err )
245
+ }
246
+ fmt .Printf ("File Updated :%s \n " , filePath )
247
+ return nil
248
+ }
249
+ return nil
250
+ }
251
+
252
+ func GetProductionDir (baseDir string ) string {
253
+
254
+ dirGlob := filepath .Join (baseDir , ProductionDir )
255
+ return dirGlob
256
+ }
257
+
258
+ func getLatestGitHash (basedir , module string ) (string , error ) {
259
+
260
+ moduleFilePath := filepath .Join (basedir , moduleDir , module )
261
+ cmd := exec .Command ("git" , "rev-list" , "-n" , "1" , "HEAD" , "--" , moduleFilePath )
262
+ output , err := cmd .Output ()
263
+ if err != nil {
264
+ return "" , fmt .Errorf ("failed to get git hash: %v" , err )
265
+ }
266
+ gitHash := strings .TrimSpace (string (output ))
267
+ fmt .Printf ("The head githash for module %s is %s\n " , module , gitHash )
268
+
269
+ return gitHash , nil
270
+ }
271
+
272
+ func modulePromotion (dynatraceConfig DynatraceConfig , module string ) error {
273
+
274
+ baseDir := dynatraceConfig .GitDirectory
275
+
276
+ _ , err := GeModulesNames (baseDir , moduleDir )
277
+ if err != nil {
278
+ return err
279
+ }
280
+
281
+ module , err = ValidateModuleName (module )
282
+ if err != nil {
283
+ return fmt .Errorf ("Module Name : %s is not valid" , module )
284
+ }
285
+ fmt .Printf ("Module Name : %s is valid" , module )
286
+
287
+ prodtenantDir := GetProductionDir (baseDir )
288
+
289
+ promotionGitHash , err := getLatestGitHash (baseDir , module )
290
+
291
+ if err != nil {
292
+ return fmt .Errorf ("failed to checkout and compare git hash: %v" , err )
293
+ }
294
+
295
+ fmt .Printf ("Module: %s will be promoted to %s\n " , module , promotionGitHash )
296
+
297
+ branchName := fmt .Sprintf ("promote-%s-%s" , module , promotionGitHash )
298
+
299
+ err = dynatraceConfig .UpdateDynatraceConfig (module , promotionGitHash , branchName )
300
+ if err != nil {
301
+ return fmt .Errorf ("FAILURE: %v\n " , err )
302
+ }
303
+
304
+ promotePattern := pattern + module + "?ref=" + promotionGitHash
305
+
306
+ _ , err = updatePromotionGitHash (module , prodtenantDir , promotePattern )
307
+
308
+ if err != nil {
309
+ return err
310
+ }
311
+ commitLog := "Promote Module " + module + " to GitHash %s" + promotionGitHash
312
+ fmt .Printf ("commitLog: %v\n " , commitLog )
313
+
314
+ err = dynatraceConfig .commitFiles (commitLog )
315
+ if err != nil {
316
+ return fmt .Errorf ("failed to commit changes to app-interface: %w" , err )
317
+ }
318
+
319
+ fmt .Printf ("The branch %s is ready to be pushed\n " , branchName )
320
+ fmt .Println ("DT service:" , module )
321
+ fmt .Println ("to:" , promotionGitHash )
322
+ fmt .Println ("READY TO PUSH," , module , "promotion commit is ready locally" )
323
+ return nil
324
+ }
0 commit comments