@@ -75,6 +75,8 @@ def create_devops_resources(config,signer):
75
75
name = topic_name , compartment_id = compartment_ocid , description = "Created by Automation ToolKit" )).data
76
76
except Exception as e :
77
77
print (e .message )
78
+ if ('Topic with same name already exists in the same tenant or compartment' in str (e .message )):
79
+ print ("If it is in Deleting State, wait for few minutes for it to get terminated and re-try." )
78
80
print ("\n Exiting!!!" )
79
81
exit ()
80
82
toolkit_topic_id = create_topic_response .topic_id
@@ -170,7 +172,7 @@ def update_devops_config(prefix, repo_ssh_url,files_in_repo,dir_values,devops_us
170
172
os .mkdir (jenkins_home )
171
173
git_config_file = jenkins_home + '/git_config'
172
174
173
- # if /cd3user/tenancies/jenkins_home/git_config file exists
175
+ #if git_config_file exists
174
176
if os .path .exists (git_config_file ):
175
177
f = open (git_config_file ,"r" )
176
178
config_file_data = f .read ()
@@ -241,15 +243,18 @@ def update_devops_config(prefix, repo_ssh_url,files_in_repo,dir_values,devops_us
241
243
except Exception as e :
242
244
print (e )
243
245
246
+ if os .path .exists (devops_dir + "../.terraform_files" ):
247
+ shutil .rmtree (devops_dir + "../.terraform_files" , ignore_errors = True )
248
+ os .rename (devops_dir ,devops_dir + "../.terraform_files" )
244
249
245
- # Clean repo config if exists and initiate git repo
246
- subprocess . run ([ 'git' , 'init' ], cwd = devops_dir , stdout = DEVNULL )
250
+ if not os . path . exists ( devops_dir ):
251
+ os . makedirs ( devops_dir )
247
252
subprocess .run (['git' , 'config' , '--global' , 'init.defaultBranch' , "main" ], cwd = devops_dir )
253
+ subprocess .run (['git' , 'init' ], cwd = devops_dir ,stdout = DEVNULL )
248
254
subprocess .run (['git' , 'config' , '--global' , 'safe.directory' , devops_dir ], cwd = devops_dir )
249
- f = open (devops_dir + ".gitignore" , "w" )
250
- git_ignore_file_data = ".DS_Store\n *tfstate*\n *terraform*\n tfplan.out\n tfplan.json\n *backup*\n import_commands*\n *cis_report*\n *showoci_report*\n *.safe\n *stacks.zip\n *cd3Validator*"
251
- f .write (git_ignore_file_data )
252
- f .close ()
255
+ subprocess .run (['git' , 'config' ,'--global' ,'user.email' ,devops_user ], cwd = devops_dir )
256
+ subprocess .run (['git' , 'config' , '--global' , 'user.name' , devops_user ], cwd = devops_dir )
257
+
253
258
# Cleanup existing "origin" remote and create required one
254
259
existing_remote = subprocess .run (['git' , 'remote' ], cwd = devops_dir ,capture_output = True ).stdout
255
260
existing_remote = str (existing_remote ).split ('\' ' )[1 ][:- 2 ]
@@ -266,42 +271,56 @@ def update_devops_config(prefix, repo_ssh_url,files_in_repo,dir_values,devops_us
266
271
f .close ()
267
272
exit (1 )
268
273
269
- for f in glob .glob (jenkins_install + "/*.groovy" ):
270
- shutil .copy2 (f , devops_dir )
274
+
271
275
# Create local branch "main" from remote "main"
272
- subprocess .run (['git' , 'checkout' , '-B' , 'main' ,'-q' ], cwd = devops_dir ,stdout = DEVNULL )
273
- subprocess .run (['git' , 'pull' , 'origin' , 'main' ,'-q' ], cwd = devops_dir ,stdout = DEVNULL ,stderr = DEVNULL )
274
- subprocess .run (['git' , 'add' , '-A' ], cwd = devops_dir ,stdout = DEVNULL )
276
+ subprocess .run (['git' , 'checkout' , '-B' , 'main' ], cwd = devops_dir ,stdout = DEVNULL )
275
277
278
+ subprocess .run (['git' , 'pull' , 'origin' , 'main' ,'--rebase' ], cwd = devops_dir ,stdout = DEVNULL )
279
+ f = open (devops_dir + ".gitignore" , "w" )
280
+ git_ignore_file_data = ".DS_Store\n *tfstate*\n *terraform*\n tfplan.out\n tfplan.json\n *backup*\n import_commands*\n *cis_report*\n *showoci_report*\n *.safe\n *stacks.zip\n *cd3Validator*"
281
+ f .write (git_ignore_file_data )
282
+ f .close ()
283
+
284
+ all_items = glob .glob (devops_dir + "../.terraform_files/*" )+ [devops_dir + "/../.terraform_files/.safe" ]
285
+ for f in all_items :
286
+ actual_file = f .split ("/" )[- 1 ]
287
+ path = devops_dir + actual_file
288
+ if os .path .exists (path ) and os .path .isfile (path ):
289
+ os .remove (devops_dir + actual_file )
290
+ if os .path .exists (path ) and os .path .isdir (path ):
291
+ shutil .rmtree (path , ignore_errors = True )
292
+ if actual_file .endswith (".tf_backup" ) or actual_file .endswith (".tfstate" ):
293
+ continue
294
+ shutil .move (f ,devops_dir )
295
+ for f in glob .glob (jenkins_install + "/*.groovy" ):
296
+ actual_file = f .split ("/" )[- 1 ]
297
+ path = devops_dir + actual_file
298
+ if os .path .exists (path ) and os .path .isfile (path ):
299
+ os .remove (devops_dir + actual_file )
300
+ shutil .copy (f ,devops_dir )
301
+
302
+ last_commit_id = subprocess .run (['git' , 'rev-parse' , '--short' , 'HEAD' ], cwd = devops_dir ,capture_output = True ).stdout
276
303
current_status = subprocess .run (['git' , 'status' ,'--porcelain' ], cwd = devops_dir ,capture_output = True ).stdout
277
304
current_status = str (current_status ).split ('\' ' )[1 ]
305
+ subprocess .run (['git' , 'add' , '-A' ], cwd = devops_dir ,stdout = DEVNULL )
306
+ subprocess .run (['git' , 'commit' , '-m' ,'Initial commit from createTenancyConfig' ], cwd = devops_dir ,stdout = DEVNULL )
278
307
if current_status and files_in_repo > 0 :
279
- subprocess .run (['git' , 'stash' ,'-q' ], cwd = devops_dir ,stdout = DEVNULL )
280
- #subprocess.run(['git', 'rebase','origin/main','-q'], cwd=devops_dir,stdout=DEVNULL)
281
308
repo_changes = input ("\n Data in local terraform_files and repo is not same, which changes you want to retain? Enter local or repo, default is local : " )
282
309
if ("repo" in repo_changes .lower ()):
283
310
print ("Ignoring local changes......" )
284
- # subprocess.run(['git', 'stash '], cwd=devops_dir,stdout=DEVNULL)
285
- # subprocess.run(['git', 'pull ','origin ','main '], cwd=devops_dir,stdout=DEVNULL)
286
- # subprocess.run(['git', 'stash', 'drop', f'stash@{{{0}}} '], cwd=devops_dir,stdout=DEVNULL)
311
+ subprocess .run (['git' , 'branch' , 'main' , '-u' , 'origin/main ' ], cwd = devops_dir ,stdout = DEVNULL )
312
+ subprocess .run (['git' ,'reset ' ,'--hard ' ,'@{u} ' ], cwd = devops_dir ,stdout = DEVNULL )
313
+ subprocess .run (['git' , 'pull' , 'origin' , 'main ' ], cwd = devops_dir ,stdout = DEVNULL )
287
314
else :
288
315
print ("Updating remote with local changes....." )
289
- #subprocess.run(['git', 'stash'], cwd=devops_dir,stdout=DEVNULL)
290
- #subprocess.run(['git', 'pull','origin','main'], cwd=devops_dir,stdout=DEVNULL)
291
- #subprocess.run(['git', 'stash', 'apply', f'stash@{{{0}}}'], cwd=devops_dir,stdout=DEVNULL)
292
- subprocess .run (['git' , 'stash' , 'pop' ], cwd = devops_dir ,stdout = DEVNULL )
293
- subprocess .run (['git' , 'add' , '-A' ], cwd = devops_dir ,stdout = DEVNULL )
294
- #subprocess.run(['git', 'stash', 'drop', f'stash@{{{0}}}'], cwd=devops_dir,stdout=DEVNULL)
295
316
296
- subprocess .run (['git' , 'config' ,'--global' ,'user.email' ,devops_user ], cwd = devops_dir )
297
- subprocess .run (['git' , 'config' , '--global' , 'user.name' , devops_user ], cwd = devops_dir )
298
317
commit_id = 'None'
299
318
try :
300
- subprocess .run (['git' , 'commit' , '-m' ,'Initial commit from createTenancyConfig.py' ], cwd = devops_dir ,stdout = DEVNULL )
319
+ # subprocess.run(['git', 'commit', '-m','Initial commit from createTenancyConfig.py'], cwd=devops_dir,stdout=DEVNULL)
301
320
commit_id = subprocess .run (['git' , 'rev-parse' , '--short' , 'HEAD' ], cwd = devops_dir ,capture_output = True ).stdout
302
321
commit_id = str (commit_id ).split ('\' ' )[1 ][:- 2 ]
303
322
subprocess .run (['git' , 'push' ,'origin' ,'main' ], cwd = devops_dir , stdout = DEVNULL )
304
- print ("Initial Commit to DevOps Repository done with commit id: " + commit_id )
323
+ print ("Latest Commit to DevOps Repository done with commit id: " + commit_id )
305
324
except git .exc .GitCommandError as e :
306
325
if ("nothing to commit, working directory clean" in str (e )):
307
326
print ("Nothing to commit to DevOps Repository." )
@@ -310,8 +329,9 @@ def update_devops_config(prefix, repo_ssh_url,files_in_repo,dir_values,devops_us
310
329
print ("Exiting..." )
311
330
exit (1 )
312
331
# Create develop branch from main
313
- subprocess .run (['git' , 'checkout' , '-B' , 'develop' ,'main' , '-q' ], cwd = devops_dir , stdout = DEVNULL )
314
- subprocess .run (['git' , 'push' , 'origin' , 'develop' ], cwd = devops_dir , stdout = DEVNULL )
332
+ subprocess .run (['git' , 'checkout' , '-B' , 'develop' ,'main' ], cwd = devops_dir , stdout = DEVNULL )
333
+ subprocess .run (['git' , 'pull' , 'origin' , 'develop' ,'--rebase' ], cwd = devops_dir ,stdout = DEVNULL ,stderr = DEVNULL )
334
+ subprocess .run (['git' , 'push' , 'origin' , 'develop' ,'-f' ], cwd = devops_dir , stdout = DEVNULL )
315
335
return commit_id
316
336
317
337
def create_bucket (config , signer ):
0 commit comments