2010-03-04 16:55:45 +00:00
|
|
|
=begin
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2010-03-05 20:23:05 +00:00
|
|
|
Stop the Vitro application, delete all MySQL tables from the Vitro database, and
|
|
|
|
start the application again.
|
2010-03-04 16:55:45 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Parameters:
|
2010-03-05 20:23:05 +00:00
|
|
|
tomcat_stop_command
|
|
|
|
A "shell" command that will stop the Tomcat server.
|
|
|
|
tomcat_stop_delay
|
|
|
|
Number of seconds to wait after the tomcat_stop_command returns before
|
|
|
|
proceeding.
|
|
|
|
tomcat_start_command
|
|
|
|
A "shell" command that will start the Tomcat server.
|
|
|
|
tomcat_start_delay
|
|
|
|
Number of seconds to wait after the tomcat_start_command returns before
|
|
|
|
proceeding.
|
|
|
|
mysql_username
|
|
|
|
A user account that has authority to drop the Vitro database in MySQL.
|
|
|
|
mysql_password
|
|
|
|
The password for mysql_username.
|
|
|
|
database_name
|
|
|
|
The name of the Vitro database in MySQL.
|
2010-03-04 16:55:45 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
=end
|
|
|
|
require 'open-uri'
|
2010-03-08 22:21:13 +00:00
|
|
|
require File.expand_path('property_file_reader', File.dirname(File.expand_path(__FILE__)))
|
2010-03-04 16:55:45 +00:00
|
|
|
|
|
|
|
class DatabaseCleanser
|
|
|
|
# ------------------------------------------------------------------------------------
|
|
|
|
private
|
|
|
|
# ------------------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Confirm that the parameters are reasonable.
|
|
|
|
#
|
|
|
|
def sanity_checks_on_parameters()
|
|
|
|
# Check that all necessary properties are here.
|
2010-03-05 20:23:05 +00:00
|
|
|
raise("Properties file must contain a value for 'tomcat_stop_command'") if @tomcat_stop_command == nil
|
|
|
|
raise("Properties file must contain a value for 'tomcat_stop_delay'") if @tomcat_stop_delay == nil
|
|
|
|
raise("Properties file must contain a value for 'tomcat_start_command'") if @tomcat_start_command == nil
|
|
|
|
raise("Properties file must contain a value for 'tomcat_start_delay'") if @tomcat_start_delay == nil
|
2010-03-18 21:15:54 +00:00
|
|
|
raise("Properties file must contain a value for 'website_url'") if @website_url == nil
|
2010-03-04 16:55:45 +00:00
|
|
|
raise("Properties file must contain a value for 'mysql_username'") if @mysql_username == nil
|
|
|
|
raise("Properties file must contain a value for 'mysql_password'") if @mysql_password == nil
|
|
|
|
raise("Properties file must contain a value for 'database_name'") if @database_name == nil
|
|
|
|
|
|
|
|
# Check that we can connect to the MySQL database.
|
|
|
|
args = []
|
|
|
|
args << "--user=#{@mysql_username}"
|
|
|
|
args << "--password=#{@mysql_password}"
|
|
|
|
args << "--database=#{@database_name}"
|
2010-03-08 22:21:13 +00:00
|
|
|
args << "--execute=show databases;"
|
2010-03-04 16:55:45 +00:00
|
|
|
result = system("mysql", *args)
|
|
|
|
raise("Can't find the 'mysql' command!") if result == nil
|
|
|
|
raise("Can't connect to MySQL database.") if !result
|
|
|
|
raise("Error connecting to MySQL database.") if $?.exitstatus != 0
|
|
|
|
end
|
|
|
|
|
2010-03-05 20:23:05 +00:00
|
|
|
# Issue the Tomcat stop command and pause for it to take effect.
|
2010-03-04 16:55:45 +00:00
|
|
|
#
|
2010-03-05 15:45:54 +00:00
|
|
|
def stop_the_webapp()
|
|
|
|
puts " Stopping the webapp..."
|
2010-03-05 20:23:05 +00:00
|
|
|
system(@tomcat_stop_command)
|
|
|
|
puts " Waiting #{@tomcat_stop_delay} seconds..."
|
|
|
|
sleep(@tomcat_stop_delay)
|
2010-03-05 15:45:54 +00:00
|
|
|
puts " ... stopped."
|
|
|
|
end
|
|
|
|
|
2010-03-05 20:23:05 +00:00
|
|
|
# Issue the Tomcat start command, pause for it to take effect, and wait
|
|
|
|
# until the server responds to an HTTP request.
|
|
|
|
#
|
2010-03-05 15:45:54 +00:00
|
|
|
def start_the_webapp()
|
|
|
|
puts " Starting the webapp..."
|
2010-03-05 20:23:05 +00:00
|
|
|
system(@tomcat_start_command)
|
|
|
|
puts " Waiting #{@tomcat_start_delay} seconds..."
|
|
|
|
sleep(@tomcat_start_delay)
|
2010-03-18 21:15:54 +00:00
|
|
|
begin
|
|
|
|
open(@website_url){|f|}
|
|
|
|
rescue Timeout::Error
|
|
|
|
puts ">>> HTTP request timed out!"
|
|
|
|
raise
|
|
|
|
end
|
2010-03-05 15:45:54 +00:00
|
|
|
puts " ... started."
|
|
|
|
end
|
|
|
|
|
2010-03-05 20:23:05 +00:00
|
|
|
# Tell MySQL to drop the database and re-create it.
|
|
|
|
#
|
2010-03-05 15:45:54 +00:00
|
|
|
def drop_database_and_create_again()
|
2010-03-05 20:23:05 +00:00
|
|
|
args = []
|
|
|
|
args << "--user=#{@mysql_username}"
|
|
|
|
args << "--password=#{@mysql_password}"
|
|
|
|
args << "--database=#{@database_name}"
|
|
|
|
args << "--execute=drop database #{@database_name}; create database #{@database_name} character set utf8;"
|
|
|
|
result = system("mysql", *args)
|
|
|
|
raise("Can't find the 'mysql' command!") if result == nil
|
|
|
|
raise("Can't clean the MySQL database: command was 'mysql' #{args}") if !result
|
|
|
|
raise("Error code from MySQL: #{$?.exitstatus}, command was 'mysql' #{args}") if $?.exitstatus != 0
|
2010-03-05 15:45:54 +00:00
|
|
|
end
|
|
|
|
|
2010-03-04 16:55:45 +00:00
|
|
|
# ------------------------------------------------------------------------------------
|
|
|
|
public
|
|
|
|
# ------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Get the parameters and check them
|
|
|
|
#
|
|
|
|
def initialize(properties)
|
2010-03-05 20:23:05 +00:00
|
|
|
@tomcat_stop_command = properties['tomcat_stop_command']
|
|
|
|
@tomcat_stop_delay = properties['tomcat_stop_delay'].to_i
|
|
|
|
@tomcat_start_command = properties['tomcat_start_command']
|
|
|
|
@tomcat_start_delay = properties['tomcat_start_delay'].to_i
|
2010-03-18 21:15:54 +00:00
|
|
|
@website_url = properties['website_url']
|
2010-03-04 16:55:45 +00:00
|
|
|
@mysql_username = properties['mysql_username']
|
|
|
|
@mysql_password = properties['mysql_password']
|
|
|
|
@database_name = properties['database_name']
|
|
|
|
|
|
|
|
sanity_checks_on_parameters()
|
|
|
|
end
|
2010-03-05 15:45:54 +00:00
|
|
|
|
|
|
|
# Cleanse the database.
|
|
|
|
#
|
|
|
|
def cleanse()
|
|
|
|
stop_the_webapp()
|
|
|
|
drop_database_and_create_again()
|
|
|
|
start_the_webapp()
|
|
|
|
end
|
2010-03-04 16:55:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# ------------------------------------------------------------------------------------
|
|
|
|
# Standalone calling.
|
2010-03-08 22:21:13 +00:00
|
|
|
#
|
|
|
|
# Do this if this program was called from the command line. That is, if the command
|
|
|
|
# expands to the path of this file.
|
2010-03-04 16:55:45 +00:00
|
|
|
# ------------------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
|
2010-03-08 22:21:13 +00:00
|
|
|
if File.expand_path($0) == File.expand_path(__FILE__)
|
|
|
|
if ARGV.length == 0
|
|
|
|
raise("No arguments - usage is: ruby database_cleanser.rb <properties_file>")
|
|
|
|
end
|
|
|
|
if !File.file?(ARGV[0])
|
|
|
|
raise "File does not exist: '#{ARGV[0]}'."
|
|
|
|
end
|
|
|
|
|
|
|
|
properties = PropertyFileReader.read(ARGV[0])
|
2010-03-04 16:55:45 +00:00
|
|
|
|
2010-03-08 22:21:13 +00:00
|
|
|
dc = DatabaseCleanser.new(properties)
|
|
|
|
dc.cleanse()
|
|
|
|
end
|