create_dbs.pl
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:7k
- #!/usr/bin/perl
- use Getopt::Long;
- use strict;
- my $database_user = '';
- my $database_user_password = '';
- my $mysql_flag = 0;
- my $mysql_command = '';
- my $mysqlshow_command = '';
- my $ptodo_flag = 0;
- my $SQL_BASE = './';
- my $foo;
- if ( $ARGV[ 0 ] eq '' ) {
- die <<"USAGE";
- create_dbs.pl [options]
- --user=username - username to be used
- --password=password - password to be used
- --mysql - mysql database
- --mysql_command=path - path to mysql command
- --mysqlshow_command=path - path to mysqlshow command
- --ptodo_db - create a prometheus todo database
- --sql_base=path - base directory for the sql
- USAGE
- }
- my $result = GetOptions(
- 'user=s', $database_user,
- 'password=s', $database_user_password,
- 'mysql', $mysql_flag,
- 'mysql_command=s', $mysql_command,
- 'mysqlshow_command=s', $mysql_command,
- 'ptodo_db', $ptodo_flag,
- 'sql_base=s', $SQL_BASE,
- 'session_db', $foo,
- 'user_db', $foo,
- 'domain_db', $foo,
- );
- if ( $mysql_command eq '' ) {
- $mysql_command = find_command('mysql');
- }
- if ( $mysqlshow_command eq '' ) {
- $mysqlshow_command = find_command('mysqlshow');
- }
- print "Option confirmation : n";
- display_option( 'Database Username ', $database_user );
- display_option( 'Database Password ', $database_user_password );
- if ( $mysql_flag ) {
- display_option( 'Mysql ', $mysql_flag );
- display_option( 'Mysql Command ', $mysql_command );
- display_option( 'Mysqlshow Command ', $mysqlshow_command );
- }
- display_option( 'Prometheus Database ', $ptodo_flag );
- if ( $mysql_flag ) {
- if ( $mysql_command eq '' ) {
- die "The mysql command is required : n" .
- "--mysql-command=path_to_mysql is a overriden";
- }
- if ( $mysqlshow_command eq '' ) {
- die "The mysqlshow command is required :n" .
- "--mysqlshow-command=path_to_mysqlshow is a overriden";
- }
- if ( $ptodo_flag ) {
- create_mysql_ptodo_dbs();
- }
- }
- exit();
- sub display_option {
- my $option_name = shift;
- my $option_value = shift;
- print $option_name . ' - ' . $option_value . "n";
- }
- sub find_command {
- my $target_command = shift;
- my @path_elems = ();
- my $path_elem = '';
- @path_elems = split( /:/, $ENV{ "PATH" } );
- foreach $path_elem ( @path_elems ) {
- my $test_item = '';
- $test_item = $path_elem . '/' . $target_command;
- if ( -x $test_item ) {
- return $test_item;
- }
- }
- }
- sub get_mysql_dbs {
- my @mysql_dbs = ();
- open( MYSQL_SHOW,
- $mysqlshow_command . ' ' .
- '--user=' . $database_user . ' ' .
- '--password=' . $database_user_password .
- ' |'
- ) or die "$!n";
- while( <MYSQL_SHOW> ) {
- my $input_line = $_;
- if ( $input_line =~ /+/ ) { next; }
- if ( $input_line =~ /Databases/ ) { next; }
- if ( $input_line =~ /|s*(.*)s*|/ ) {
- my $target = $1;
- while( $target =~ / $/ ) {
- $target =~ s/ $//g;
- }
- push( @mysql_dbs, $target );
- }
- }
- close( MYSQL_SHOW );
- return @mysql_dbs;
- }
- sub get_mysql_dbs_tables {
- my $dbs = shift;
- my @mysql_tables = ();
- my $command = $mysqlshow_command . ' ' .
- '--user=' . $database_user . ' ' .
- '--password=' . $database_user_password . ' ' .
- $dbs . ' % ';
- open( MYSQL_SHOW, $command . ' |') or die "$!n";
- while( <MYSQL_SHOW> ) {
- my $input_line = $_;
- if ( $input_line =~ /+/ ) { next; }
- if ( $input_line =~ /Tables/ ) { next; }
- if ( $input_line =~ /|s*(.*)s*|/ ) {
- my $target = $1;
- while( $target =~ / $/ ) {
- $target =~ s/ $//g;
- }
- push( @mysql_tables, $target );
- }
- }
- close( MYSQL_SHOW );
- return @mysql_tables;
- }
- sub get_mysql_database_existence {
- my $target_dbs = shift;
- my @mysql_dbs = get_mysql_dbs();
- my $item = '';
- foreach $item ( @mysql_dbs ) {
- #print $item . "***n";
- if ( $item eq $target_dbs ) {
- # dbs is already there...
- return 1;
- }
- }
- return 0;
- }
- sub get_mysql_table_existence {
- my $target_dbs = shift;
- my $target_table = shift;
- my @mysql_tables = get_mysql_dbs_tables( $target_dbs );
- my $item = '';
- foreach $item ( @mysql_tables ) {
- #print $item . "***n";
- if ( $item eq $target_table ) {
- # dbs is already there...
- return 1;
- }
- }
- return 0;
- }
- sub run_mysql_script {
- my $script = shift;
- my $target_dbs = shift;
- my @command = ();
- @command = (
- $mysql_command,
- '--user=' . $database_user,
- '--password=' . $database_user_password
- );
- if ( $target_dbs ne "" ) { push( @command, $target_dbs ); }
- push( @command, ">> create_log.log 2>> create_log.stderr" );
- push( @command, "< $script" );
- system( join( ' ', @command ) );
- }
- sub create_mysql_ptodo_dbs {
- my $target_dbs = 'prometheus_todo';
- my $target_dbs_create =
- $SQL_BASE . '/database/mysql/Todo/create_prometheus_todo.sql';
- my $db_there = 0;
- $db_there = get_mysql_database_existence( $target_dbs );
- if ( $db_there == 1 ) {
- die
- "There seems to be a issue : $target_dbs is already theren" .
- "and since i don't know how to handle this i'm exitingn";
- }
- run_mysql_script( $target_dbs_create );
- $db_there = get_mysql_database_existence( $target_dbs );
- if ( $db_there == 1 ) {
- print "$target_dbs - Database was created!n";
- } else {
- die
- "Hmm the database is still not there after i tried ton" .
- "create it, i'm guessing that there is something afoot!n";
- }
- my @dbs_tables = (
- $SQL_BASE . '/table/mysql/Todo/todo_items_table.sql',
- $SQL_BASE . '/table/mysql/Todo/user_todo_items_table.sql',
- $SQL_BASE . '/table/mysql/Todo/todo_sections_table.sql',
- $SQL_BASE . '/table/mysql/Todo/user_todo_sections_table.sql',
- $SQL_BASE . '/table/mysql/Todo/todo_status_table.sql',
- $SQL_BASE . '/table/mysql/Todo/user_todo_status_table.sql',
- );
- my $table = '';
- foreach $table ( @dbs_tables ) {
- run_mysql_script( $table, $target_dbs );
- }
- my @table_cheks = (
- 'todo_items_table',
- 'user_todo_items_table',
- 'todo_sections_table',
- 'user_todo_sections_table',
- 'todo_status_table',
- 'user_todo_status_table',
- );
- foreach $table ( @table_cheks ) {
- if (
- get_mysql_table_existence( $target_dbs, $table )
- ) {
- # Table is there
- print "Table : $table checked out good - we are gon";
- } else {
- die "Table : $table checked DID NOT out good - we are NO gon";
- }
- }
- print "Okay looks like we have a $target_dbs - database to use now, have fun!n";
- }