Skip to content

Commit be1a2b0

Browse files
nvazquezdhslove
authored andcommitted
[DB] Add force recreate parameter to cloudstack-setup-databases script (apache#11239)
1 parent ead22f9 commit be1a2b0

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

setup/bindir/cloud-setup-databases.in

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ class DBDeployer(object):
129129
(value, index) = self.dbDotProperties[key]
130130
return value
131131

132+
def areCloudDatabasesCreated(self):
133+
cmd = "SELECT CASE WHEN COUNT(DISTINCT SCHEMA_NAME) >= 1 THEN 1 ELSE 0 END AS schema_exists \
134+
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME IN ('cloud', 'cloud_usage');"
135+
databases = self.runMysql(cmd, "databases", self.rootuser != None)
136+
return databases.replace("schema_exists", "").strip() == "1"
137+
132138
def runMysql(self, text, table, isRoot=False):
133139
kwargs = {}
134140
if not isRoot:
@@ -151,7 +157,7 @@ class DBDeployer(object):
151157
open(self.tmpMysqlFile, 'w').write(text)
152158
mysqlCmds.append('<')
153159
mysqlCmds.append(self.tmpMysqlFile)
154-
runCmd(mysqlCmds)
160+
return runCmd(mysqlCmds)
155161

156162
except Exception as e:
157163
err = '''Encountering an error when executing mysql script
@@ -233,6 +239,10 @@ for full help
233239
("DROP USER 'cloud'@'%' ;", "DO NULL;")
234240
)
235241

242+
if self.areCloudDatabasesCreated() and not self.options.forcerecreate:
243+
self.errorAndExit("Aborting script as the databases (cloud, cloud_usage) already exist.\n" \
244+
"Please use the --force-recreate parameter if you want to recreate the schemas.")
245+
236246
scriptsToRun = ["create-database","create-schema", "create-database-premium","create-schema-premium"]
237247
if self.options.schemaonly:
238248
scriptsToRun = ["create-schema", "create-schema-premium"]
@@ -610,6 +620,9 @@ for example:
610620
help="Creates the db schema without having to pass root credentials - " \
611621
"Please note: The databases (cloud, cloud_usage) and user (cloud) has to be configured " \
612622
"manually prior to running this script when using this flag.")
623+
self.parser.add_option("--force-recreate", action="store_true", dest="forcerecreate", default=False,
624+
help="Force recreation of the existing DB schemas. This option is disabled by default." \
625+
"Please note: The databases (cloud, cloud_usage) and its tables data will be lost and recreated.")
613626

614627
self.parser.add_option("-a", "--auto", action="store", type="string", dest="serversetup", default="",
615628
help="Path to an XML file describing an automated unattended cloud setup")

0 commit comments

Comments
 (0)