From 7a371e65a2ca4951c56e0be609d9f760ebecca6f Mon Sep 17 00:00:00 2001
From: Susendra Selvaraj <Susendra.Selvaraj@windriver.com>
Date: Wed, 28 Jun 2023 06:43:18 -0400
Subject: [PATCH] Add script to refresh deploy plug-in post patching

Utility script is created to update deploy plug-in automatically
post patching. This involves:
1. run ansible playbook to upgrade static images
   - pull deploy plug-in image from configured source
   - push its image to local registry
2. run ansible playbook to refresh deploy plug-in

Test Plan:
PASS: Verify deploy plug-in is refreshed after script execution.
 - Create restart script to invoke the utility script
 - Copy the new debian files onto the controller
 - Create sneaky patch with debian files and restart script
 - Check deploy plug-in image version before applying sneaky patch
 - Apply the sneaky patch
 - Check deploy plug-in image version after applying sneaky patch
PASS: Verify utility script is in debian package
PASS: Verify error handling is done for playbook
PASS: Verify sneaky patch remove is successful
PASS: Verify deploy pod is running new image after the patch
      containing deploy plug-in update is applied
PASS: Verify script logs generated in /var/log/platform.log

Story: 2010718
Task: 48412

Change-Id: Ifd348f005117aca5cf18e16719123410d86d027d
Signed-off-by: Susendra Selvaraj <Susendra.Selvaraj@windriver.com>
---
 .../debian/deb_folder/controller.install      |  1 +
 .../platform-util-controller.install          |  1 +
 .../platform-util/debian/deb_folder/rules     |  3 +-
 utilities/platform-util/scripts/update-dm.sh  | 51 +++++++++++++++++++
 4 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100755 utilities/platform-util/scripts/update-dm.sh

diff --git a/utilities/platform-util/debian/deb_folder/controller.install b/utilities/platform-util/debian/deb_folder/controller.install
index 07eae200..94edeaba 100644
--- a/utilities/platform-util/debian/deb_folder/controller.install
+++ b/utilities/platform-util/debian/deb_folder/controller.install
@@ -4,5 +4,6 @@ scripts/show-certs.sh usr/local/bin
 scripts/stx-iso-utils.sh usr/local/bin
 scripts/stx-iso-utils-centos.sh usr/local/bin
 scripts/update-iso.sh usr/local/bin
+scripts/update-dm.sh usr/local/bin
 scripts/update_docker_registry_auth.sh usr/local/bin
 scripts/change_system_private_registry.sh usr/local/bin
diff --git a/utilities/platform-util/debian/deb_folder/platform-util-controller.install b/utilities/platform-util/debian/deb_folder/platform-util-controller.install
index 68631423..2046fb8b 100644
--- a/utilities/platform-util/debian/deb_folder/platform-util-controller.install
+++ b/utilities/platform-util/debian/deb_folder/platform-util-controller.install
@@ -4,5 +4,6 @@
 /usr/local/bin/stx-iso-utils.sh
 /usr/local/bin/stx-iso-utils-centos.sh
 /usr/local/bin/update-iso.sh
+/usr/local/bin/update-dm.sh
 /usr/local/bin/update_docker_registry_auth.sh
 /usr/local/bin/change_system_private_registry.sh
diff --git a/utilities/platform-util/debian/deb_folder/rules b/utilities/platform-util/debian/deb_folder/rules
index e2341909..90eaa605 100755
--- a/utilities/platform-util/debian/deb_folder/rules
+++ b/utilities/platform-util/debian/deb_folder/rules
@@ -30,6 +30,7 @@ override_dh_auto_install:
 
 	install -d $(DEBIAN_BUILDDIR)/usr/local/bin/
 	install -m 555 scripts/update-iso.sh $(DEBIAN_BUILDDIR)/usr/local/bin/
+	install -m 555 scripts/update-dm.sh $(DEBIAN_BUILDDIR)/usr/local/bin/
 	install -m 555 scripts/gen-bootloader-iso.sh $(DEBIAN_BUILDDIR)/usr/local/bin/
 	install -m 555 scripts/gen-bootloader-iso-centos.sh $(DEBIAN_BUILDDIR)/usr/local/bin/
 	install -m 555 scripts/stx-iso-utils.sh $(DEBIAN_BUILDDIR)/usr/local/bin/
@@ -49,6 +50,6 @@ override_dh_auto_install:
 	dh_install
 
 override_dh_fixperms:
-	dh_fixperms -Xupdate-iso.sh -Xgen-bootloader-iso.sh -Xstx-iso-utils.sh \
+	dh_fixperms -Xupdate-iso.sh -Xpatch-dm.sh -Xgen-bootloader-iso.sh -Xstx-iso-utils.sh \
 	-Xshow-certs.sh -Xupdate_docker_registry_auth.sh -Xchange_system_private_registry.sh \
         -Xis-rootdisk-device.sh -Xpatch-restart-* -Xconnectivity_test -Xset_keystone_user_option.sh
diff --git a/utilities/platform-util/scripts/update-dm.sh b/utilities/platform-util/scripts/update-dm.sh
new file mode 100755
index 00000000..f877176d
--- /dev/null
+++ b/utilities/platform-util/scripts/update-dm.sh
@@ -0,0 +1,51 @@
+#!/bin/bash
+#
+# Copyright (c) 2023 Wind River Systems, Inc.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+
+# This script automates deploy plug-in update to
+# 1. run ansible playbook to upgrade static images
+#    - pull image from configured source
+#    - push image to local registry
+# 2. run ansible playbook to refresh deploy plug-in
+#
+
+export KUBECONFIG=/etc/kubernetes/admin.conf
+
+UPGRADE_STATIC_IMAGE_PLAYBOOK=/usr/share/ansible/stx-ansible/playbooks/upgrade-static-images.yml
+
+DEPLOY_OVERRIDES=$1
+DEPLOY_CHART=$2
+DEPLOY_PLAYBOOK=$3
+
+# 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
+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
+RC=$?
+if [ $RC -eq 0 ]; then
+    log "The deployment playbook was executed successfully"
+else
+    log "The deployment playbook failed with error: $RC"
+    exit $RC
+fi
+
+exit 0