From de480f1d30d450644f5dad158b7d74d29ee3e9a9 Mon Sep 17 00:00:00 2001 From: dev-0pz Date: Tue, 21 Oct 2025 18:26:24 +0000 Subject: [PATCH 1/2] refactor changes needs to be tested still, added the 3 changes from the task https://stfc.atlassian.net/browse/CLDSCRM-2987 --- usr/local/bin/rename-old-images.py | 71 ++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/usr/local/bin/rename-old-images.py b/usr/local/bin/rename-old-images.py index 4709017..dc96f79 100644 --- a/usr/local/bin/rename-old-images.py +++ b/usr/local/bin/rename-old-images.py @@ -111,7 +111,8 @@ def SendMail(Subject , Body, Recipient): existing_visibility = "private" previous_image = None - if next_image.name in all_image_names: + # Fixed: Check if current_image_name exists, not next_image.name + if current_image_name in all_image_names: previous_image = conn.image.find_image(current_image_name, ignore_missing=False) existing_visibility = previous_image.visibility print(existing_visibility) @@ -133,36 +134,53 @@ def SendMail(Subject , Body, Recipient): print("Existing Member = " + existing_member_string) new_member = conn.image.add_member(next_image, member_id=existing_member_string) conn.image.update_member(new_member, next_image, status="accepted") - print("Current image: " + previous_image.name) - print("Next image: " + next_image.name) + + # Rename current image to warehoused if previous_image: + print("Current image: " + previous_image.name) + print("Next image: " + next_image.name) conn.image.deactivate_image(previous_image) print("Rename " + previous_image.name + " to " + "warehoused-" + previous_image.name + "-" + DATE) conn.image.update_image(previous_image.id, name="warehoused-" + previous_image.name + "-" + DATE, is_hidden=True) - print("Rename " + next_image.name + " to " + current_image_name) - conn.image.update_image(next_image.id, name=current_image_name) + + # Rename new image to production name + print("Rename " + next_image.name + " to " + current_image_name) + conn.image.update_image(next_image.id, name=current_image_name) # print(images) # print(members) - for image_name in all_image_names: - if current_image_name.lower() in image_name.lower(): - print("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/build/*.json") - cl("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/build/*.json") - print("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/source-images.json") - cl("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/source-images.json") + # Update config files with new image ID + if previous_image: + for image_name in all_image_names: + if current_image_name.lower() in image_name.lower(): + print("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/build/*.json") + cl("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/build/*.json") + print("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/source-images.json") + cl("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/source-images.json") with open(mailfilepath, "w") as mailfile: mailfile.write( - "Build of " + image_name + " succeeded on " + DATE + ". New image ID is \`" + next_image.id + "\`") - body="Build of " + image_name + " succeeded on " + DATE + ". New image ID is \`" + next_image.id + "\`" + "Build of " + current_image_name + " succeeded on " + DATE + ". New image ID is \`" + next_image.id + "\`") + body="Build of " + current_image_name + " succeeded on " + DATE + ". New image ID is \`" + next_image.id + "\`" # SendMail(current_image_name + " - Build Succeeded", mailfilepath, success_address) + # Send JSON message to Slack webhook slackheaders = {'Content-Type': 'application/json'} - slackdata = { - 'text': "Build of " + current_image_name + " succeeded on " + DATE + ". New image ID is \`" + next_image.id + "\`"} - slackdebug = requests.post(slackhook, headers=slackheaders, data=json.dumps(slackdata)) + slack_message = { + 'text': f"✅ Image promotion successful!", + 'attachments': [{ + 'color': 'good', + 'fields': [ + {'title': 'Image Name', 'value': current_image_name, 'short': True}, + {'title': 'New Image ID', 'value': next_image.id, 'short': True}, + {'title': 'Previous Image ID', 'value': previous_image.id if previous_image else 'N/A', 'short': True}, + {'title': 'Timestamp', 'value': DATE, 'short': True} + ] + }] + } + slackdebug = requests.post(slackhook, headers=slackheaders, data=json.dumps(slack_message)) print(slackdebug) @@ -171,6 +189,21 @@ def SendMail(Subject , Body, Recipient): mailfile.write(f"Build of {next_image_name} failed on {DATE} due to rally test failing") body=f"Build of {next_image_name} failed on {DATE} due to rally test failing" # SendMail("Build Failed", body, failure_address) - visibility = " --private " - - conn.image.update_image(next_image.id, visibility=visibility, name="Broken-" + current_image_name + DATE) + + # Also send failure to Slack + slackheaders = {'Content-Type': 'application/json'} + slack_message = { + 'text': f"❌ Build Failed", + 'attachments': [{ + 'color': 'danger', + 'fields': [ + {'title': 'Image Name', 'value': next_image_name, 'short': True}, + {'title': 'Reason', 'value': 'Rally test failed', 'short': True}, + {'title': 'Timestamp', 'value': DATE, 'short': True} + ] + }] + } + requests.post(slackhook, headers=slackheaders, data=json.dumps(slack_message)) + + visibility = "private" # Fixed: was " --private " which is wrong for the API + conn.image.update_image(next_image.id, visibility=visibility, name="Broken-" + current_image_name + "-" + DATE) \ No newline at end of file From c33f51d1117770b27661dd023583531e2a11866a Mon Sep 17 00:00:00 2001 From: dev-0pz Date: Tue, 21 Oct 2025 19:00:57 +0000 Subject: [PATCH 2/2] refactor changes I have coded all these changes and refactors for the task https://stfc.atlassian.net/browse/CLDSCRM-2987 Has not been tested yet in a dev environment so will test that tomorrow and re get pr if successful. --- .../__pycache__/rename_images.cpython-312.pyc | Bin 0 -> 5347 bytes usr/local/bin/rename-old-images.py | 111 ++++++------------ 2 files changed, 36 insertions(+), 75 deletions(-) create mode 100644 usr/local/bin/__pycache__/rename_images.cpython-312.pyc diff --git a/usr/local/bin/__pycache__/rename_images.cpython-312.pyc b/usr/local/bin/__pycache__/rename_images.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c1deb81f45ad450954d66a8588be4cabf677b08f GIT binary patch literal 5347 zcmbstTWs6b^^zhbijpn){kGY}vaKd^TsKKqCw;hfoH&W)B6hn~TgRy)W7DxDkEEPf zlLvS3FbC_V-Fp6 z0iH|VbMLw5eeUJgRaHEK_9*w=?8j9I{S$u_8&i1MU4fU|NJ1ebQ4&2*g(&E4^K_IB z(G(tI=50|X#6;N;8?}e*WQ?A7gd8+Fk0f>#N%lLmMJ>dUF$ausr7@@ETty*Xc1irT zK7>@5BlC9(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wf+p(=E;9_(S`sY21PFCF_7I5#br5y}DUU%%syM_x zmRNlj1B}!#LnU9}!;&OgbPkB=GLo$9g_SGlK38RUyEoq0xU`u}`@ZP=yf58qRF7u4 zb9ryu`WuF)*Wh|BT`;hq*zh~ig#a)6bYTOD>2jcuvrAIg7aj#^HHdRmb zG1G2MAd*6rBh&U}u&RP<7X_nBtQy&ZKQX{{Rm0)Cigsv*<#*f^E9~0s zZ2gYc0ic#h@$@uaId1vDz0$F3dL89QWbMCHPYrP z4O$p0=E}KJb}S&ElSf}uy^ch5Y`-EuvhGBfN_4vR_L`%i%+qK z(qg|4trV+XlU#>Vaiu5o#gnq{4c|3^z6ojx};JsvzSj86=oKPOz7oF1MYp9~69BUi>>ofa-n zo*y4c*7&4=FB<)k@>?4UK)*)p8_Q!oFI(BC}C5W zj>9>OB1;oEm4HOQ7RHoVcwT^i2D&_ytQCNWyr=+^CHXu;pLLanjn#zgPc~b;z^Oz^ zM96lePy=nZRitrpHEpr@b(4e3*j4=R!E~td!hAR)t2p11(qPqZQc;L7U8+&QlMP?yP}Q*Ke)hYUqfbtqRBYddR8Z(Gx!r(W}xu>$d26g@1DJR_O~zQTt_ml zBgWCwTdwC;M)QrQRwnZ;eYuu_Ov}LCru31mmKO}B5f;=myl-C_zu(xi)!4T(zB;*W zN40IYyWZ={)f~&z9NV1SsyVxIG2hU6TYOK)@=uJ`W_+1 z$v>(_EN{4dS*923?!v7Q7SE{n8GKJ+Y|NkK~WDGnqqeCh;si8b^IL-WYEB05&xat uPIWy*0KW5fQ)eC`06RgdpYm@1j6#fiE%}YD {warehoused_name}") + + # Rename the current image to warehoused name var + conn.image.update_image( + previous_image.id, + name=warehoused_name, + ) + print(f"✓ Successfully warehoused {previous_image.name}") # Rename new image to production name - print("Rename " + next_image.name + " to " + current_image_name) + print(f"Promoting: {next_image.name} -> {current_image_name}") conn.image.update_image(next_image.id, name=current_image_name) - - # print(images) - - # print(members) + print(f"✓ Successfully promoted to {current_image_name}") # Update config files with new image ID if previous_image: for image_name in all_image_names: if current_image_name.lower() in image_name.lower(): - print("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/build/*.json") - cl("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/build/*.json") - print("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/source-images.json") - cl("sed -i 's/" + previous_image.id + "/" + next_image.id + "/g' /etc/packer-utils/source-images.json") - - with open(mailfilepath, "w") as mailfile: - mailfile.write( - "Build of " + current_image_name + " succeeded on " + DATE + ". New image ID is \`" + next_image.id + "\`") - body="Build of " + current_image_name + " succeeded on " + DATE + ". New image ID is \`" + next_image.id + "\`" -# SendMail(current_image_name + " - Build Succeeded", mailfilepath, success_address) - - # Send JSON message to Slack webhook + print(f"sed -i 's/{previous_image.id}/{next_image.id}/g' /etc/packer-utils/build/*.json") + cl(f"sed -i 's/{previous_image.id}/{next_image.id}/g' /etc/packer-utils/build/*.json") + print(f"sed -i 's/{previous_image.id}/{next_image.id}/g' /etc/packer-utils/source-images.json") + cl(f"sed -i 's/{previous_image.id}/{next_image.id}/g' /etc/packer-utils/source-images.json") + + # Send success notification to Slack slackheaders = {'Content-Type': 'application/json'} slack_message = { 'text': f"✅ Image promotion successful!", @@ -176,6 +139,7 @@ def SendMail(Subject , Body, Recipient): {'title': 'Image Name', 'value': current_image_name, 'short': True}, {'title': 'New Image ID', 'value': next_image.id, 'short': True}, {'title': 'Previous Image ID', 'value': previous_image.id if previous_image else 'N/A', 'short': True}, + {'title': 'Warehoused', 'value': f"warehoused-{previous_image.name}-{DATE}" if previous_image else 'N/A', 'short': False}, {'title': 'Timestamp', 'value': DATE, 'short': True} ] }] @@ -183,14 +147,8 @@ def SendMail(Subject , Body, Recipient): slackdebug = requests.post(slackhook, headers=slackheaders, data=json.dumps(slack_message)) print(slackdebug) - else: - with open(mailfilepath, "w") as mailfile: - mailfile.write(f"Build of {next_image_name} failed on {DATE} due to rally test failing") - body=f"Build of {next_image_name} failed on {DATE} due to rally test failing" -# SendMail("Build Failed", body, failure_address) - - # Also send failure to Slack + # Send failure notification to Slack slackheaders = {'Content-Type': 'application/json'} slack_message = { 'text': f"❌ Build Failed", @@ -205,5 +163,8 @@ def SendMail(Subject , Body, Recipient): } requests.post(slackhook, headers=slackheaders, data=json.dumps(slack_message)) - visibility = "private" # Fixed: was " --private " which is wrong for the API - conn.image.update_image(next_image.id, visibility=visibility, name="Broken-" + current_image_name + "-" + DATE) \ No newline at end of file + # Mark failed image as broken + visibility = "private" + broken_name = f"Broken-{current_image_name}-{DATE}" + print(f"Marking failed image as: {broken_name}") + conn.image.update_image(next_image.id, visibility=visibility, name=broken_name) \ No newline at end of file