Update MySQL upgrade pipeline to support MySQL 5.7 update procedure

PROD-36779

Change-Id: I7af952aa73348751de83f1b7e1e8a62236c980bd
(cherry picked from commit fb10a4db540544ab86f322aaca054f12b0afacf1)
diff --git a/openstack-galera-upgrade.groovy b/openstack-galera-upgrade.groovy
index f124051..56c0af9 100644
--- a/openstack-galera-upgrade.groovy
+++ b/openstack-galera-upgrade.groovy
@@ -10,6 +10,7 @@
  *   OS_UPGRADE                         Upgrade all installed applications (apt-get upgrade)
  *   TARGET_SERVERS                     Comma separated list of salt compound definitions to upgrade.
  *   INTERACTIVE                        Ask interactive questions during pipeline run (bool).
+ *   UPDATE_TO_MYSQL57                  Set this flag if you are updating MySQL from 5.6 to 5.7
  *
 **/
 
@@ -22,6 +23,7 @@
 def shutdownCluster = SHUTDOWN_CLUSTER.toBoolean()
 def interactive = INTERACTIVE.toBoolean()
 def LinkedHashMap upgradeStageMap = [:]
+def updateToMysql57 = UPDATE_TO_MYSQL57.toBoolean()
 
 upgradeStageMap.put('Pre upgrade',
   [
@@ -118,6 +120,20 @@
 
     for (target in upgradeTargets) {
       salt.runSaltProcessStep(env, target, 'saltutil.refresh_pillar', [], null, true)
+      mysqlPillarVersion = salt.getPillar(env, target, "galera:version:mysql").get("return")[0].values()[0].toString().toLowerCase()
+      mysql56InstalledVersion = salt.getReturnValues(salt.runSaltProcessStep(env, target, 'pkg.version', 'mysql-wsrep-server-5.6', null, true)).toString().toLowerCase().take(3)
+      mysql57InstalledVersion = salt.getReturnValues(salt.runSaltProcessStep(env, target, 'pkg.version', 'mysql-wsrep-server-5.7', null, true)).toString().toLowerCase().take(3)
+
+      if (mysqlPillarVersion == '5.6' && mysql57InstalledVersion == '5.7') {
+        error("""Pre upgrade check failed. You are trying to downgrade MySQL package from 5.7 version to 5.6.
+                 Check value for galera_mysql_version variable in your model.""")
+      }
+      if (mysqlPillarVersion == '5.7' && mysql56InstalledVersion == '5.6' && updateToMysql57 != true) {
+        error("""Pre upgrade check failed. You are trying to update MySQL package from version 5.6 to version 5.7 the wrong way.
+                  If you want to update from 5.6 version to 5.7 set flag UPDATE_TO_MYSQL57 in the current job.
+                  If you don't want to update from 5.6 version to 5.7 you need to change the value for galera_mysql_version to 5.6 in your model.""")
+      }
+
       salt.enforceState(env, target, ['linux.system.repo'])
       common.stageWrapper(upgradeStageMap, "Pre upgrade", target, interactive) {
         openstack.runOpenStackUpgradePhase(env, target, 'pre')
@@ -125,6 +141,10 @@
       }
     }
 
+    if (updateToMysql57 == true) {
+      shutdownCluster = true
+    }
+
     if (shutdownCluster){
       for (target in stopTargets) {
         common.stageWrapper(upgradeStageMap, "Stop MySQL service", target, interactive) {
@@ -169,8 +189,18 @@
         }
 
         common.stageWrapper(upgradeStageMap, "Upgrade MySQL server", target, interactive) {
-          openstack.runOpenStackUpgradePhase(env, target, 'pkgs_latest')
-          openstack.runOpenStackUpgradePhase(env, target, 'render_config')
+          if (updateToMysql57 == true) {
+            if (target == masterNode) {
+              openstack.runOpenStackUpgradePhase(env, target, 'update_master')
+            }
+            else {
+              openstack.runOpenStackUpgradePhase(env, target, 'update_slave')
+            }
+          }
+          else {
+            openstack.runOpenStackUpgradePhase(env, target, 'pkgs_latest')
+            openstack.runOpenStackUpgradePhase(env, target, 'render_config')
+          }
         }
 
         if (shutdownCluster && target == masterNode){