mirror of https://github.com/apache/cloudstack.git
DatabaseCreator: Fast forward merge due to recent pull in merge of master
This commit is contained in:
commit
64f13e1cd8
|
|
@ -65,6 +65,10 @@ db.usage.maxWait=10000
|
|||
db.usage.autoReconnect=true
|
||||
|
||||
# awsapi database settings
|
||||
db.awsapi.username=@DBUSER@
|
||||
db.awsapi.password=@DBPW@
|
||||
db.awsapi.host=@DBHOST@
|
||||
db.awsapi.port=3306
|
||||
db.awsapi.name=cloudbridge
|
||||
|
||||
# Simulator database settings
|
||||
|
|
|
|||
|
|
@ -90,200 +90,6 @@
|
|||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- Database recreator -->
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>sql-maven-plugin</artifactId>
|
||||
<version>1.5</version>
|
||||
<dependencies>
|
||||
<!-- specify the dependent jdbc driver here -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${cs.mysql.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<configuration>
|
||||
<driver>org.gjt.mm.mysql.Driver</driver>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}/cloud</url>
|
||||
<username>${db.cloud.username}</username>
|
||||
<password>${db.cloud.password}</password>
|
||||
<!--all executions are ignored if -Dmaven.test.skip=true -->
|
||||
<skip>${maven.test.skip}</skip>
|
||||
<forceMojoExecution>true</forceMojoExecution>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>drop-database</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>drop database if exists `cloud`</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>create-database</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>create database `cloud`</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>grant-user-cloud</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>GRANT ALL ON cloud.* to
|
||||
${db.cloud.username}@`localhost` identified by
|
||||
'${db.cloud.password}';</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>grant-user-cloud-all</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>GRANT ALL ON cloud.* to
|
||||
${db.cloud.username}@`%` identified by
|
||||
'${db.cloud.password}';</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>drop-database-usage</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>drop database if exists `cloud_usage`</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>create-database-usage</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>create database `cloud_usage`</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>grant-user-cloud-usage</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>GRANT ALL ON cloud_usage.* to
|
||||
${db.cloud.username}@`localhost` identified by
|
||||
'${db.cloud.password}';</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>grant-user-cloud-usage-all</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>GRANT ALL ON cloud_usage.* to
|
||||
${db.cloud.username}@`%` identified by
|
||||
'${db.cloud.password}';</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>drop-database-cloudbridge</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>drop database if exists `cloudbridge`</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>create-database-cloudbridge</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>create database `cloudbridge`</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>grant-user-cloudbridge</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>GRANT ALL ON cloudbridge.* to
|
||||
${db.cloud.username}@`localhost` identified by
|
||||
'${db.cloud.password}';</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>grant-user-cloudbridge-all</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.cloud.host}:${db.cloud.port}</url>
|
||||
<sqlCommand>GRANT ALL ON cloudbridge.* to
|
||||
${db.cloud.username}@`%` identified by
|
||||
'${db.cloud.password}';</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- DatabaseCreator driver here -->
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
|
|
@ -365,6 +171,11 @@
|
|||
<argument>${basedir}/developer-prefill.sql</argument>
|
||||
<!-- Do database upgrade-->
|
||||
<argument>com.cloud.upgrade.DatabaseUpgradeChecker</argument>
|
||||
<argument>--database=cloud,usage,awsapi</argument>
|
||||
<argument>--rootpassword=${db.root.password}</argument>
|
||||
<!-- Print help using -h or dash-dash-help -->
|
||||
<!-- Do dry run using -d or dash-dash-dry -->
|
||||
<!-- Enable verbosity by -v or dash-dash-verbose -->
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
|
@ -402,96 +213,75 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- DatabaseCreator driver here -->
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>sql-maven-plugin</artifactId>
|
||||
<version>1.5</version>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.2.1</version>
|
||||
<dependencies>
|
||||
<!-- specify the dependent jdbc driver here -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${cs.mysql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-dbcp</groupId>
|
||||
<artifactId>commons-dbcp</artifactId>
|
||||
<version>${cs.dbcp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-pool</groupId>
|
||||
<artifactId>commons-pool</artifactId>
|
||||
<version>${cs.pool.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jasypt</groupId>
|
||||
<artifactId>jasypt</artifactId>
|
||||
<version>${cs.jasypt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cloudstack</groupId>
|
||||
<artifactId>cloud-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cloudstack</groupId>
|
||||
<artifactId>cloud-server</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<configuration>
|
||||
<driver>org.gjt.mm.mysql.Driver</driver>
|
||||
<url>jdbc:mysql://${db.simulator.host}:3306/simulator</url>
|
||||
<username>${db.simulator.username}</username>
|
||||
<password>${db.simulator.password}</password>
|
||||
<skip>${maven.test.skip}</skip>
|
||||
<forceMojoExecution>true</forceMojoExecution>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>drop-database</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.simulator.host}:3306</url>
|
||||
<sqlCommand>drop database if exists `simulator`</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>create-database</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.simulator.host}:3306</url>
|
||||
<sqlCommand>create database `simulator`</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>grant-user-cloud</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.simulator.host}:3306</url>
|
||||
<sqlCommand>GRANT ALL ON simulator.* to
|
||||
${db.simulator.username}@`localhost` identified by
|
||||
'${db.simulator.password}';</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>grant-user-cloud-all</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<username>root</username>
|
||||
<password>${db.root.password}</password>
|
||||
<url>jdbc:mysql://${db.simulator.host}:3306</url>
|
||||
<sqlCommand>GRANT ALL ON simulator.* to
|
||||
${db.simulator.username}@`%` identified by
|
||||
'${db.simulator.password}';</sqlCommand>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>create-schema</id>
|
||||
<phase>process-test-resources</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<srcFiles>
|
||||
<srcFile>${basedir}/target/db/create-schema-simulator.sql</srcFile>
|
||||
<srcFile>${basedir}/target/db/templates.simulator.sql</srcFile>
|
||||
</srcFiles>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<includeProjectDependencies>false</includeProjectDependencies>
|
||||
<includePluginDependencies>true</includePluginDependencies>
|
||||
<executableDependency>
|
||||
<groupId>org.apache.cloudstack</groupId>
|
||||
<artifactId>cloud-server</artifactId>
|
||||
</executableDependency>
|
||||
<mainClass>com.cloud.upgrade.DatabaseCreator</mainClass>
|
||||
<arguments>
|
||||
<!-- db properties file -->
|
||||
<argument>${project.parent.basedir}/utils/conf/db.properties</argument>
|
||||
<argument>${project.parent.basedir}/utils/conf/db.properties.override</argument>
|
||||
<!-- simulator sql files -->
|
||||
<argument>${basedir}/target/db/create-schema-simulator.sql</argument>
|
||||
<argument>${basedir}/target/db/templates.simulator.sql</argument>
|
||||
<!-- database upgrade-->
|
||||
<argument>com.cloud.upgrade.DatabaseUpgradeChecker</argument>
|
||||
<argument>--database=simulator</argument>
|
||||
<argument>--rootpassword=${db.root.password}</argument>
|
||||
<!-- enable verbosity by -v or dash-dash-verbose -->
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
|
|||
|
|
@ -18,14 +18,14 @@
|
|||
*/
|
||||
package com.cloud.upgrade;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.cloud.utils.PropertiesUtil;
|
||||
|
||||
|
|
@ -36,39 +36,128 @@ import com.cloud.utils.db.Transaction;
|
|||
// Creates the CloudStack Database by using the 4.0 schema and apply
|
||||
// upgrade steps to it.
|
||||
public class DatabaseCreator {
|
||||
|
||||
protected static void printHelp(String cmd) {
|
||||
System.out.println(
|
||||
"DatabaseCreator creates the database schema by removing the \n" +
|
||||
"\nDatabaseCreator creates the database schema by removing the \n" +
|
||||
"previous schema, creating the schema, and running \n" +
|
||||
"through the database updaters.");
|
||||
System.out.println("Usage: " + cmd + " [db.properties files] [schema.sql files] [database upgrade class]");
|
||||
System.out.println("Usage: " + cmd + " [options] [db.properties file] [schema.sql files] [database upgrade class]\nOptions:"
|
||||
+ "\n --database=a,b comma separate databases to initialize, use the db name in db.properties defined as db.xyz.host, xyz should be passed"
|
||||
+ "\n --rootpassword=password, by default it will try with an empty password"
|
||||
+ "\n --dry or -d, this would not run any process, just does a dry run"
|
||||
+ "\n --verbose or -v to print running sql commands, by default it won't print them"
|
||||
+ "\n --help or -h for help");
|
||||
}
|
||||
|
||||
private static boolean fileExists(String file) {
|
||||
File f = new File(file);
|
||||
if (!f.exists())
|
||||
System.out.println("========> WARNING: Provided file does not exist: " + file);
|
||||
return f.exists();
|
||||
}
|
||||
|
||||
private static void runScript(Connection conn, Reader reader, String filename, boolean verbosity) {
|
||||
ScriptRunner runner = new ScriptRunner(conn, false, true, verbosity);
|
||||
try {
|
||||
runner.runScript(reader);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Unable to read " + filename + ": " + e.getMessage());
|
||||
System.exit(1);
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Unable to execute " + filename + ": " + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void runQuery(String host, String port, String rootPassword, String query, boolean dryRun) {
|
||||
System.out.println("============> Running query: " + query);
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/", host, port),
|
||||
"root", rootPassword);
|
||||
Statement stmt = conn.createStatement();
|
||||
if (!dryRun)
|
||||
stmt.executeUpdate(query);
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
System.out.println("SQL exception in trying initDB: " + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void initDB(String dbPropsFile, String rootPassword, String[] databases, boolean dryRun) {
|
||||
Properties dbProperties = new Properties();
|
||||
try {
|
||||
dbProperties.load(new FileInputStream(new File(dbPropsFile)));
|
||||
} catch (IOException e) {
|
||||
System.out.println("IOError: unable to load/read db properties file: " + e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
for (String database: databases) {
|
||||
String host = dbProperties.getProperty(String.format("db.%s.host", database));
|
||||
String port = dbProperties.getProperty(String.format("db.%s.port", database));
|
||||
String username = dbProperties.getProperty(String.format("db.%s.username", database));
|
||||
String password = dbProperties.getProperty(String.format("db.%s.password", database));
|
||||
String dbName = dbProperties.getProperty(String.format("db.%s.name", database));
|
||||
System.out.println(String.format("========> Initializing database=%s with host=%s port=%s username=%s password=%s", dbName, host, port, username, password));
|
||||
|
||||
List<String> queries = new ArrayList<String>();
|
||||
queries.add(String.format("drop database if exists `%s`", dbName));
|
||||
queries.add(String.format("create database `%s`", dbName));
|
||||
queries.add(String.format("GRANT ALL ON %s.* to '%s'@`localhost` identified by '%s'", dbName, username, password));
|
||||
queries.add(String.format("GRANT ALL ON %s.* to '%s'@`%%` identified by '%s'", dbName, username, password));
|
||||
|
||||
for (String query: queries) {
|
||||
runQuery(host, port, rootPassword, query, dryRun);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> dbPropFiles = new ArrayList<String>();
|
||||
String dbPropsFile = "";
|
||||
List<String> sqlFiles = new ArrayList<String>();
|
||||
List<String> upgradeClasses = new ArrayList<String>();
|
||||
String[] databases = new String[] {};
|
||||
String rootPassword = "";
|
||||
boolean verbosity = false;
|
||||
boolean dryRun = false;
|
||||
|
||||
// Process opts
|
||||
for (String arg: args) {
|
||||
if (arg.endsWith(".sql")) {
|
||||
if (arg.equals("--help") || arg.equals("-h")) {
|
||||
printHelp("DatabaseCreator");
|
||||
System.exit(0);
|
||||
} else if (arg.equals("--verbose") || arg.equals("-v")) {
|
||||
verbosity = true;
|
||||
} else if (arg.equals("--dry") || arg.equals("-d")) {
|
||||
dryRun = true;
|
||||
} else if (arg.startsWith("--rootpassword=")) {
|
||||
rootPassword = arg.substring(arg.lastIndexOf("=") + 1, arg.length());
|
||||
} else if (arg.startsWith("--database=")) {
|
||||
databases = arg.substring(arg.lastIndexOf("=") + 1, arg.length()).split(",");
|
||||
} else if (arg.endsWith(".sql")) {
|
||||
sqlFiles.add(arg);
|
||||
} else if (arg.endsWith(".properties") || arg.endsWith("properties.override")) {
|
||||
dbPropFiles.add(arg);
|
||||
} else if (arg.endsWith(".properties")) {
|
||||
if (!dbPropsFile.endsWith("properties.override") && fileExists(arg))
|
||||
dbPropsFile = arg;
|
||||
} else if (arg.endsWith("properties.override")) {
|
||||
if (fileExists(arg))
|
||||
dbPropsFile = arg;
|
||||
} else {
|
||||
upgradeClasses.add(arg);
|
||||
}
|
||||
}
|
||||
|
||||
if ((dbPropFiles.size() == 0)
|
||||
if ((dbPropsFile.isEmpty())
|
||||
|| (sqlFiles.size() == 0) && upgradeClasses.size() == 0) {
|
||||
printHelp("DatabaseCreator");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
// Process db.properties files
|
||||
for (String dbPropFile: dbPropFiles) {
|
||||
|
||||
}
|
||||
Transaction.initDataSource(dbPropsFile);
|
||||
initDB(dbPropsFile, rootPassword, databases, dryRun);
|
||||
|
||||
// Process sql files
|
||||
for (String sqlFile: sqlFiles) {
|
||||
|
|
@ -79,12 +168,9 @@ public class DatabaseCreator {
|
|||
System.exit(1);
|
||||
}
|
||||
|
||||
System.out.println("=============> Processing SQL file at " + sqlScript.getAbsolutePath());
|
||||
|
||||
System.out.println("========> Processing SQL file at " + sqlScript.getAbsolutePath());
|
||||
Connection conn = Transaction.getStandaloneConnection();
|
||||
try {
|
||||
|
||||
ScriptRunner runner = new ScriptRunner(conn, false, true);
|
||||
FileReader reader = null;
|
||||
try {
|
||||
reader = new FileReader(sqlScript);
|
||||
|
|
@ -92,15 +178,8 @@ public class DatabaseCreator {
|
|||
System.err.println("Unable to read " + sqlFile + ": " + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
try {
|
||||
runner.runScript(reader);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Unable to read " + sqlFile + ": " + e.getMessage());
|
||||
System.exit(1);
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Unable to execute " + sqlFile + ": " + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
if (!dryRun)
|
||||
runScript(conn, reader, sqlFile, verbosity);
|
||||
} finally {
|
||||
try {
|
||||
conn.close();
|
||||
|
|
@ -112,7 +191,7 @@ public class DatabaseCreator {
|
|||
|
||||
// Process db upgrade classes
|
||||
for (String upgradeClass: upgradeClasses) {
|
||||
System.out.println("=============> Processing upgrade: " + upgradeClass);
|
||||
System.out.println("========> Processing upgrade: " + upgradeClass);
|
||||
Class<?> clazz = null;
|
||||
try {
|
||||
clazz = Class.forName(upgradeClass);
|
||||
|
|
|
|||
|
|
@ -56,6 +56,10 @@ db.usage.maxWait=10000
|
|||
db.usage.autoReconnect=true
|
||||
|
||||
# awsapi database settings
|
||||
db.awsapi.username=cloud
|
||||
db.awsapi.password=cloud
|
||||
db.awsapi.host=localhost
|
||||
db.awsapi.port=3306
|
||||
db.awsapi.name=cloudbridge
|
||||
|
||||
# Simulator database settings
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ public class ScriptRunner {
|
|||
|
||||
private boolean stopOnError;
|
||||
private boolean autoCommit;
|
||||
private boolean verbosity = true;
|
||||
|
||||
private String delimiter = DEFAULT_DELIMITER;
|
||||
private boolean fullLineDelimiter = false;
|
||||
|
|
@ -58,6 +59,13 @@ public class ScriptRunner {
|
|||
this.stopOnError = stopOnError;
|
||||
}
|
||||
|
||||
public ScriptRunner(Connection connection, boolean autoCommit, boolean stopOnError, boolean verbosity) {
|
||||
this.connection = connection;
|
||||
this.autoCommit = autoCommit;
|
||||
this.stopOnError = stopOnError;
|
||||
this.verbosity = verbosity;
|
||||
}
|
||||
|
||||
public void setDelimiter(String delimiter, boolean fullLineDelimiter) {
|
||||
this.delimiter = delimiter;
|
||||
this.fullLineDelimiter = fullLineDelimiter;
|
||||
|
|
@ -207,7 +215,8 @@ public class ScriptRunner {
|
|||
|
||||
private void println(Object o) {
|
||||
_logBuffer.append(o);
|
||||
s_logger.debug(_logBuffer.toString());
|
||||
if (verbosity)
|
||||
s_logger.debug(_logBuffer.toString());
|
||||
_logBuffer = new StringBuffer();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package com.cloud.utils.db;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
|
@ -996,19 +997,32 @@ public class Transaction {
|
|||
private static DataSource s_usageDS;
|
||||
private static DataSource s_awsapiDS;
|
||||
private static DataSource s_simulatorDS;
|
||||
|
||||
static {
|
||||
// Initialize with assumed db.properties file
|
||||
initDataSource("db.properties");
|
||||
}
|
||||
|
||||
public static void initDataSource(String propsFileName) {
|
||||
try {
|
||||
final File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
|
||||
final Properties dbProps;
|
||||
|
||||
if(EncryptionSecretKeyChecker.useEncryption()){
|
||||
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
|
||||
dbProps = new EncryptableProperties(encryptor);
|
||||
} else {
|
||||
dbProps = new Properties();
|
||||
File dbPropsFile = new File(propsFileName);
|
||||
if (!dbPropsFile.exists()) {
|
||||
dbPropsFile = PropertiesUtil.findConfigFile(propsFileName);
|
||||
}
|
||||
dbProps.load(new FileInputStream(dbPropsFile));
|
||||
|
||||
final Properties dbProps;
|
||||
if (EncryptionSecretKeyChecker.useEncryption()) {
|
||||
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
|
||||
dbProps = new EncryptableProperties(encryptor);
|
||||
} else {
|
||||
dbProps = new Properties();
|
||||
}
|
||||
try {
|
||||
dbProps.load(new FileInputStream(dbPropsFile));
|
||||
} catch (IOException e) {
|
||||
s_logger.fatal("Unable to load db properties file, pl. check the classpath and file path configuration", e);
|
||||
return;
|
||||
}
|
||||
|
||||
// FIXME: If params are missing...default them????
|
||||
final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive"));
|
||||
final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle"));
|
||||
|
|
@ -1021,6 +1035,7 @@ public class Transaction {
|
|||
final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect"));
|
||||
final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery");
|
||||
final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level");
|
||||
|
||||
int isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
|
||||
if (cloudIsolationLevel == null) {
|
||||
isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
|
||||
|
|
@ -1035,14 +1050,16 @@ public class Transaction {
|
|||
} else {
|
||||
s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ". Using read uncommitted");
|
||||
}
|
||||
|
||||
final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow"));
|
||||
final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle"));
|
||||
final long cloudTimeBtwEvictionRunsMillis = Long.parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis"));
|
||||
final long cloudMinEvcitableIdleTimeMillis = Long.parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis"));
|
||||
final boolean cloudPoolPreparedStatements = Boolean.parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements"));
|
||||
final String url = dbProps.getProperty("db.cloud.url.params");
|
||||
|
||||
final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL"));
|
||||
if(useSSL){
|
||||
if (useSSL) {
|
||||
System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore"));
|
||||
System.setProperty("javax.net.ssl.keyStorePassword", dbProps.getProperty("db.cloud.keyStorePassword"));
|
||||
System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore"));
|
||||
|
|
@ -1051,14 +1068,19 @@ public class Transaction {
|
|||
|
||||
final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
|
||||
cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle);
|
||||
final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+cloudHost + ":" + cloudPort + "/" + cloudDbName +
|
||||
"?autoReconnect="+cloudAutoReconnect + (url != null ? "&" + url : "")+ (useSSL ? "&useSSL=true" : ""), cloudUsername, cloudPassword);
|
||||
|
||||
final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + cloudDbName +
|
||||
"?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : ""), cloudUsername, cloudPassword);
|
||||
|
||||
final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null);
|
||||
|
||||
final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory(cloudConnectionFactory, cloudConnectionPool, poolableObjFactory,
|
||||
cloudValidationQuery, false, false, isolationLevel);
|
||||
|
||||
// Default Data Source for CloudStack
|
||||
s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool());
|
||||
|
||||
// configure the usage db
|
||||
// Configure the usage db
|
||||
final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive"));
|
||||
final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle"));
|
||||
final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait"));
|
||||
|
|
@ -1068,50 +1090,60 @@ public class Transaction {
|
|||
final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port"));
|
||||
final String usageDbName = dbProps.getProperty("db.usage.name");
|
||||
final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect"));
|
||||
|
||||
final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
|
||||
usageMaxWait, usageMaxIdle);
|
||||
final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+usageHost + ":" + usagePort + "/" + usageDbName +
|
||||
"?autoReconnect="+usageAutoReconnect, usageUsername, usagePassword);
|
||||
|
||||
final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + usageHost + ":" + usagePort + "/" + usageDbName +
|
||||
"?autoReconnect=" + usageAutoReconnect, usageUsername, usagePassword);
|
||||
|
||||
final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory(usageConnectionFactory, usageConnectionPool,
|
||||
new StackKeyedObjectPoolFactory(), null, false, false);
|
||||
|
||||
// Data Source for usage server
|
||||
s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool());
|
||||
|
||||
//configure awsapi db
|
||||
|
||||
// Configure awsapi db
|
||||
final String awsapiDbName = dbProps.getProperty("db.awsapi.name");
|
||||
final GenericObjectPool awsapiConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
|
||||
usageMaxWait, usageMaxIdle);
|
||||
final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+cloudHost + ":" + cloudPort + "/" + awsapiDbName +
|
||||
"?autoReconnect="+usageAutoReconnect, cloudUsername, cloudPassword);
|
||||
usageMaxWait, usageMaxIdle);
|
||||
final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + awsapiDbName +
|
||||
"?autoReconnect=" + usageAutoReconnect, cloudUsername, cloudPassword);
|
||||
final PoolableConnectionFactory awsapiPoolableConnectionFactory = new PoolableConnectionFactory(awsapiConnectionFactory, awsapiConnectionPool,
|
||||
new StackKeyedObjectPoolFactory(), null, false, false);
|
||||
new StackKeyedObjectPoolFactory(), null, false, false);
|
||||
|
||||
// Data Source for awsapi
|
||||
s_awsapiDS = new PoolingDataSource(awsapiPoolableConnectionFactory.getPool());
|
||||
|
||||
try{
|
||||
// configure the simulator db
|
||||
final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive"));
|
||||
final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle"));
|
||||
final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait"));
|
||||
final String simulatorUsername = dbProps.getProperty("db.simulator.username");
|
||||
final String simulatorPassword = dbProps.getProperty("db.simulator.password");
|
||||
final String simulatorHost = dbProps.getProperty("db.simulator.host");
|
||||
final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port"));
|
||||
final String simulatorDbName = dbProps.getProperty("db.simulator.name");
|
||||
final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));
|
||||
final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
|
||||
simulatorMaxWait, simulatorMaxIdle);
|
||||
final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+simulatorHost + ":" + simulatorPort + "/" + simulatorDbName +
|
||||
"?autoReconnect="+simulatorAutoReconnect, simulatorUsername, simulatorPassword);
|
||||
final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool,
|
||||
new StackKeyedObjectPoolFactory(), null, false, false);
|
||||
s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
|
||||
} catch (Exception e){
|
||||
s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
|
||||
|
||||
try {
|
||||
// Configure the simulator db
|
||||
final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive"));
|
||||
final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle"));
|
||||
final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait"));
|
||||
final String simulatorUsername = dbProps.getProperty("db.simulator.username");
|
||||
final String simulatorPassword = dbProps.getProperty("db.simulator.password");
|
||||
final String simulatorHost = dbProps.getProperty("db.simulator.host");
|
||||
final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port"));
|
||||
final String simulatorDbName = dbProps.getProperty("db.simulator.name");
|
||||
final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));
|
||||
|
||||
final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
|
||||
simulatorMaxWait, simulatorMaxIdle);
|
||||
|
||||
final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName +
|
||||
"?autoReconnect=" + simulatorAutoReconnect, simulatorUsername, simulatorPassword);
|
||||
|
||||
final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool,
|
||||
new StackKeyedObjectPoolFactory(), null, false, false);
|
||||
s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
|
||||
} catch (Exception e) {
|
||||
s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
s_ds = getDefaultDataSource("cloud");
|
||||
s_usageDS = getDefaultDataSource("cloud_usage");
|
||||
s_simulatorDS = getDefaultDataSource("cloud_simulator");
|
||||
s_logger.warn("Unable to load db configuration, using defaults with 5 connections. Please check your configuration", e);
|
||||
s_logger.warn("Unable to load db configuration, using defaults with 5 connections. Falling back on assumed datasource on localhost:3306 using username:password=cloud:cloud. Please check your configuration", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue