From d612d0ef09250fd2eb1c228c78a6e1ef2217baea Mon Sep 17 00:00:00 2001
From: Sriram Narasimhan <sriram.narasimhan@windriver.com>
Date: Tue, 25 Jul 2023 09:23:17 -0400
Subject: [PATCH] Update script to refresh deploy plug-in post update

Script will execute the ansible playbook for static image update
conditionally only for deploy plug-in post update.

Test Plan:
PASS: Verify script does execute static image update playbook
      for deploy plug-in post update
PASS: Verify script does not execute static image update playbook
      for deploy plug-in post upgrade

Story: 2010718
Task: 48458

Change-Id: I0d9ca4b4df31fac8fcd2eaa6a470208b63e1c8ee
Signed-off-by: Sriram Narasimhan <sriram.narasimhan@windriver.com>
---
 utilities/platform-util/scripts/update-dm.sh | 37 ++++++++++----------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/utilities/platform-util/scripts/update-dm.sh b/utilities/platform-util/scripts/update-dm.sh
index f877176d..40142ed2 100755
--- a/utilities/platform-util/scripts/update-dm.sh
+++ b/utilities/platform-util/scripts/update-dm.sh
@@ -6,45 +6,46 @@
 #
 
 # This script automates deploy plug-in update to
-# 1. run ansible playbook to upgrade static images
+# 1. run ansible playbook to refresh dm images if specified
 #    - pull image from configured source
 #    - push image to local registry
 # 2. run ansible playbook to refresh deploy plug-in
-#
 
+NAME=$(basename $0)
 export KUBECONFIG=/etc/kubernetes/admin.conf
 
 UPGRADE_STATIC_IMAGE_PLAYBOOK=/usr/share/ansible/stx-ansible/playbooks/upgrade-static-images.yml
 
-DEPLOY_OVERRIDES=$1
+DEPLOY_PLAYBOOK=$1
 DEPLOY_CHART=$2
-DEPLOY_PLAYBOOK=$3
+DEPLOY_OVERRIDES=$3
+REFRESH_DM_IMAGES=$4
 
 # This will log to /var/log/platform.log
 function log {
     logger -p local1.info $1
 }
 
-# Step-1
-log "Run upgrade-static-images playbook to pull image from remote registry & push to local registry"
-K8S_VERSION=$(kubectl get nodes|tail -1|awk '{print $5}')
-ansible-playbook -e "kubernetes_version=${K8S_VERSION}" $UPGRADE_STATIC_IMAGE_PLAYBOOK
-RC=$?
-if [ $RC -eq 0 ]; then
-    log "The upgrade static image playbook was executed successfully"
-else
-    log "The upgrade static image playbook failed with error: $RC"
-    exit $RC
+if [[ "${REFRESH_DM_IMAGES}" == "true" ]]; then
+    log "Run upgrade-static-images playbook to add the latest version of platform images to the local registry."
+    K8S_VERSION=$(kubectl get nodes|tail -1|awk '{print $5}')
+    ansible-playbook -e "kubernetes_version=${K8S_VERSION}" ${UPGRADE_STATIC_IMAGE_PLAYBOOK}
+    RC=$?
+    if [ $RC -eq 0 ]; then
+        log "$NAME: The upgrade-static-images playbook was executed successfully"
+    else
+        log "$NAME: The upgrade-static-images playbook failed with error: $RC"
+        exit $RC
+    fi
 fi
 
-# Step-2
 log "Run ansible playbook to refresh deploy plug-in"
-ansible-playbook -e "deployment_manager_overrides=$DEPLOY_OVERRIDES deployment_manager_chart=$DEPLOY_CHART" $DEPLOY_PLAYBOOK
+ansible-playbook -e "deployment_manager_overrides=${DEPLOY_OVERRIDES} deployment_manager_chart=${DEPLOY_CHART}" ${DEPLOY_PLAYBOOK}
 RC=$?
 if [ $RC -eq 0 ]; then
-    log "The deployment playbook was executed successfully"
+    log "$NAME: The deploy plug-in was refreshed successfully."
 else
-    log "The deployment playbook failed with error: $RC"
+    log "$NAME: The deploy plug-in refresh failed with error: $RC"
     exit $RC
 fi