From 8671cb8f5a8340bdd5f623343bde39f76e9dc3fc Mon Sep 17 00:00:00 2001 From: John Kung Date: Thu, 21 Jan 2021 09:14:18 -0600 Subject: [PATCH] Upgrades: data migration for dcorch scaling The dcorch scaling feature introduced new tables, of which the subcloud_sync table needs to be populated. The subcloud_sync data is populated with initial values similar to what would have been done on subcloud add. Change-Id: Ib880ea19cb7f69cb004d58557ad07535c1e45774 Story: 2007267 Task: 41648 Signed-off-by: John Kung --- .../53-dcorch-subcloud-sync-migration.py | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 controllerconfig/controllerconfig/upgrade-scripts/53-dcorch-subcloud-sync-migration.py diff --git a/controllerconfig/controllerconfig/upgrade-scripts/53-dcorch-subcloud-sync-migration.py b/controllerconfig/controllerconfig/upgrade-scripts/53-dcorch-subcloud-sync-migration.py new file mode 100755 index 0000000000..e90fd8da5f --- /dev/null +++ b/controllerconfig/controllerconfig/upgrade-scripts/53-dcorch-subcloud-sync-migration.py @@ -0,0 +1,98 @@ +#!/usr/bin/python +# Copyright (c) 2021 Wind River Systems, Inc. +# +# SPDX-License-Identifier: Apache-2.0 +# +# This script updates the subcloud_sync table in dcorch database +# in preparation for upgrade from release 20.06. +# +# This script can be removed in the release that follows. +# + +import json +import psycopg2 +import sys +from controllerconfig.common import log +from psycopg2.extras import RealDictCursor + + +LOG = log.get_logger(__name__) + + +def main(): + action = None + from_release = None + to_release = None + + arg = 1 + while arg < len(sys.argv): + if arg == 1: + from_release = sys.argv[arg] + elif arg == 2: + to_release = sys.argv[arg] + elif arg == 3: + action = sys.argv[arg] + else: + print("Invalid option %s." % sys.argv[arg]) + return 1 + arg += 1 + + log.configure() + + LOG.debug("%s invoked with from_release = %s to_release = %s action = %s" + % (sys.argv[0], from_release, to_release, action)) + if from_release == "20.06" and action == "migrate": + try: + if is_system_controller(): + LOG.info("Performing dcorch subcloud sync data migration...") + update_subcloud_sync() + except Exception as ex: + LOG.exception(ex) + print(ex) + return 1 + + +def is_system_controller(): + conn = psycopg2.connect("dbname='sysinv' user='postgres'") + + with conn: + with conn.cursor(cursor_factory=RealDictCursor) as cur: + cur.execute("SELECT * from i_system") + system = cur.fetchone() + return system['distributed_cloud_role'] == 'systemcontroller' + + +def update_subcloud_sync(): + conn = psycopg2.connect("dbname='dcorch' user='postgres'") + + with conn: + with conn.cursor(cursor_factory=RealDictCursor) as cur: + # Check if there are any subclouds + cur.execute("SELECT * from subcloud") + subcloud_records = cur.fetchall() + if not subcloud_records: + LOG.info("dcorch subcloud_sync data migration not required") + return + + for record in subcloud_records: + capabilities = json.loads(record['capabilities']) + endpoint_types = capabilities.get('endpoint_types') + + for ept in endpoint_types: + # Insert a record into subcloud sync for each of the + # endpoint types supported for each subcloud + cur.execute("INSERT into subcloud_sync (subcloud_id, " + "subcloud_name, endpoint_type, " + "created_at, " + "deleted) values (%d, '%s', " + "'%s', '%s', 0)" + % (record['id'], + record['region_name'], + ept, + record['created_at'])) + + LOG.info("dcorch subcloud_sync data migration completed.") + + +if __name__ == "__main__": + sys.exit(main())