create_dbs.pl
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:16k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. #!/usr/bin/perl
  2. use Getopt::Long;
  3. use strict;
  4. $| = 1;
  5. my $database_user             = '';
  6. my $database_user_password    = '';
  7. my $mysql_flag                = 0;
  8. my $mysql_new_flag            = 0;
  9. my $mysql_command             = '';
  10. my $mysql_noshow              = 0;
  11. my $mysqlshow_command         = '';
  12. my $session_flag              = 0;
  13. my $user_flag                 = 0;
  14. my $domain_flag               = 0;
  15. my $admin_privileges_flag     = 0;
  16. my $mail_settings_flag        = 0;
  17. my $customer_flag             = 0;
  18. my $user_settings_flag        = 0;
  19. my $SQL_BASE                  = './';
  20. if ( $ARGV[ 0 ] eq '' ) {
  21.    die <<"USAGE";
  22. create_dbs.pl [options]
  23.    --user=username            - username to be used
  24.    --password=password        - password to be used
  25.    --mysql                    - mysql database
  26.    --mysql_command=path       - path to mysql command
  27.    --mysql_noshow             - disable the mysqlshow command, will
  28.                                 use alternate method for checking
  29.                                 database/table creation
  30.    --mysqlshow_command=path   - path to mysqlshow command
  31.    --mysql_new                - turns on support for mysql versions
  32.                                 greater than 3.22.27 otherwise it 
  33.                                 will fail
  34.    --session_db               - create a session database
  35.    --user_db                  - create a user database
  36.    --domain_db                - create a domain database
  37.    --admin_privileges_db      - create a admin privileges database
  38.    --mail_settings_db         - create a mail settings database
  39.    --customer_db              - create a customer database
  40.    --user_settings_db         - create a user settings database
  41.    --sql_base=path            - base directory to the sql
  42. USAGE
  43. }
  44. my $foo;
  45. my $result = GetOptions(
  46.    'user=s',                     $database_user,
  47.    'password=s',                 $database_user_password,
  48.    'mysql',                      $mysql_flag,
  49.    'mysql_new',                  $mysql_new_flag,
  50.    'mysql_command=s',            $mysql_command,
  51.    'mysql_noshow',               $mysql_noshow,
  52.    'mysqlshow_command=s',        $mysqlshow_command,
  53.    'session_db',                 $session_flag,
  54.    'user_db',                    $user_flag,
  55.    'domain_db',                  $domain_flag,
  56.    'admin_privileges_db',        $admin_privileges_flag,
  57.    'customer_db',                $customer_flag,
  58.    'sql_base=s',                 $SQL_BASE,
  59.    'mail_settings_db',           $mail_settings_flag,
  60.    'user_settings_db',           $user_settings_flag,
  61. );
  62. print "BASE : " . $SQL_BASE . "n";
  63. if ( $mysql_command eq '' ) {
  64.    $mysql_command       = find_command('mysql');
  65. }
  66. if ( $mysqlshow_command eq '' ) {
  67.    $mysqlshow_command   = find_command('mysqlshow');
  68. }
  69. print "Option confirmation : n";
  70. display_option( 'Database Username    ',     $database_user );
  71. display_option( 'Database Password    ',     $database_user_password );
  72. if ( $mysql_flag ) {
  73. display_option( 'Mysql                ',     $mysql_flag );
  74. display_option( 'Mysql     Command    ',     $mysql_command );
  75. display_option( 'Mysqlshow Command    ',     $mysqlshow_command );
  76. }
  77. display_option( 'Sessions             ',     $session_flag );
  78. display_option( 'Users                ',     $user_flag );
  79. display_option( 'User Settings        ',     $user_settings_flag );
  80. display_option( 'Domains              ',     $domain_flag );
  81. display_option( 'Admin Privileges     ',     $admin_privileges_flag );
  82. display_option( 'Mail Settings        ',     $mail_settings_flag );
  83. display_option( 'Customer             ',     $customer_flag );
  84. print "n";
  85. if ( $mysql_flag ) {
  86.    if ( $mysql_command eq '' ) {
  87.       die "The mysql command is required : n" .
  88.           "--mysql-command=path_to_mysql is a overriden";
  89.    }
  90.    if ( $mysqlshow_command eq '' ) {
  91.       die "The mysqlshow command is required :n" .
  92.           "--mysqlshow-command=path_to_mysqlshow is a overriden";
  93.    }
  94.    if ( $session_flag ) {
  95.       create_mysql_session_dbs();
  96.    }
  97.    if ( $user_flag ) {
  98.       create_mysql_user_dbs();
  99.    }
  100.    if ( $user_settings_flag ) {
  101.       create_mysql_user_settings_dbs();
  102.    }
  103.    if ( $domain_flag ) {
  104.       create_mysql_domain_dbs();
  105.    }
  106.    if ( $admin_privileges_flag ) {
  107.       create_mysql_admin_privileges_dbs();
  108.    }
  109.    if ( $mail_settings_flag ) {
  110.       create_mysql_mail_settings_dbs();
  111.    }
  112.    if ( $customer_flag ) {
  113.       create_mysql_customer_dbs();
  114.    }
  115. }
  116. exit();
  117. sub print_log {
  118.    my $mesg = shift;
  119.    open( LOG_CREATE, ">> create_log.log" );
  120.    print LOG_CREATE . "*" x 40 . "n";
  121.    print LOG_CREATE . $mesg . "n";
  122.    close( LOG_CREATE );
  123.    open( LOG_CREATE, ">> create_log.stderr" );
  124.    print LOG_CREATE . "*" x 40 . "n";
  125.    print LOG_CREATE . $mesg . "n";
  126.    close( LOG_CREATE );
  127. }
  128. sub display_option {
  129.    my $option_name      = shift;
  130.    my $option_value     = shift;
  131.    print $option_name . ' - ' . $option_value . "n";
  132. }
  133. sub find_command {
  134.    my $target_command = shift;
  135.    my @path_elems = ();
  136.    my $path_elem  = '';
  137.    @path_elems = split( /:/, $ENV{ "PATH" } );
  138.    foreach $path_elem ( @path_elems ) {
  139.       my $test_item = '';
  140.       $test_item = $path_elem . '/' . $target_command;
  141.       if ( -x $test_item ) {
  142.          return $test_item;
  143.       }
  144.    }
  145. }
  146. sub get_mysql_dbs {
  147.    my @mysql_dbs = ();
  148.    # Simon/24may2000 - mysqlshow is half-borked on most
  149.    #     of my systems, so this is an alternate method 
  150.    #     to retrieve databases
  151.    #
  152.    #     The real problem occurs in "get_mysql_dbs_tables",
  153.    #     see comments there for a better explanation.
  154.    if ( $mysql_noshow )
  155.    {
  156.       open( MYSQL_SHOW, 
  157.          $mysql_command . ' ' .
  158.             '--user=' . $database_user . ' ' .
  159.             '--password=' . $database_user_password . ' ' .
  160.             '--batch --execute='show databases' |'
  161.       ) or die "$!n";
  162.       # Eat first line, yummy.
  163.       <MYSQL_SHOW>;
  164.       while( <MYSQL_SHOW> ) {
  165.          my $input_line = $_;
  166.          chomp($input_line);
  167.          push( @mysql_dbs, $input_line );
  168.       }
  169.    }
  170.    else
  171.    {
  172.       open( MYSQL_SHOW, 
  173.          $mysqlshow_command . ' ' . 
  174.             '--user=' . $database_user . ' ' .
  175.             '--password=' . $database_user_password .
  176.             ' |'
  177.       ) or die "$!n";
  178.       while( <MYSQL_SHOW> ) {
  179.          my $input_line = $_;
  180.          if ( $input_line =~ /+/ ) { next; }
  181.          if ( $input_line =~ /Databases/ ) { next; }
  182.          if ( $input_line =~ /|s*(.*)s*|/ ) {
  183.             my $target = $1;
  184.             while( $target =~ / $/ ) {
  185.                $target =~ s/ $//g;
  186.             }
  187.             push( @mysql_dbs, $target );
  188.          }
  189.       }
  190.    }
  191.    close( MYSQL_SHOW );
  192.    return @mysql_dbs;
  193. }
  194. sub get_mysql_dbs_tables {
  195.    my $dbs = shift;
  196.    my @mysql_tables = ();
  197.    # Simon/24may2000 - 
  198.    #     When mysqlshow --user=blah --password=blah database_name
  199.    #     is executed on my system, sometimes, it will simply
  200.    #     display the database name, rather than the tables it contains.
  201.    #     Obviously, this isn't ideal, so I've added a couple of extra
  202.    #     routines to get around it.
  203.    $mysql_noshow = detect_bad_mysqlshow();
  204.    # Simon/24may2000 - mysqlshow is half-borked on most
  205.    #     of my systems, so this is an alternate method 
  206.    #     to retrieve table listings
  207.    if ( $mysql_noshow )
  208.    {
  209.       open( MYSQL_SHOW, 
  210.          $mysql_command . ' ' .
  211.             '--user=' . $database_user . ' ' .
  212.             '--password=' . $database_user_password . ' ' .
  213.             '--batch --execute "show tables"' . ' ' .
  214.             $dbs . ' |'
  215.       ) or die "$!n";
  216.       <MYSQL_SHOW>;
  217.       while( <MYSQL_SHOW> ) {
  218.          my $input_line = $_;
  219.          chomp( $input_line );
  220.          push( @mysql_tables, $input_line );  
  221.       }
  222.       close( MYSQL_SHOW );
  223.    }
  224.    else
  225.    {
  226.       my $command = $mysqlshow_command . ' ' . 
  227.             '--user=' . $database_user . ' ' .
  228.             '--password=' . $database_user_password . ' ' .
  229.             $dbs ;
  230.       if ( $mysql_new_flag == 1 ) {
  231.          # % Doesn't work on all versions of mysql
  232.          # The percent sign is a table wild card . ' % ';
  233.          $command .= ' % ';
  234.       }
  235.       #print_log( $command );
  236.       open( MYSQL_SHOW, $command . ' |') or die "$!n";
  237.       while( <MYSQL_SHOW> ) {
  238.          #print_log( 'OUTPUT: ' . $_ );
  239.          my $input_line = $_;
  240.          if ( $input_line =~ /+/ ) { next; }
  241.          if ( $input_line =~ /Tables/ ) { next; }
  242.          if ( $input_line =~ /|s*(.*)s*|/ ) {
  243.             my $target = $1;
  244.             while( $target =~ / $/ ) {
  245.                $target =~ s/ $//g;
  246.             }
  247.             push( @mysql_tables, $target );
  248.          }
  249.       }
  250.    }
  251.    close( MYSQL_SHOW );
  252.    return @mysql_tables;
  253. }
  254. sub get_mysql_database_existence {
  255.    my $target_dbs = shift;
  256.    my @mysql_dbs = get_mysql_dbs();
  257.    my $item = '';
  258.    foreach $item ( @mysql_dbs ) {
  259.       #print $item . "***n";
  260.       if ( $item eq $target_dbs ) {
  261.          # dbs is already there...
  262.          return 1;
  263.       }
  264.    }
  265.    return 0;
  266. }
  267. sub get_mysql_table_existence {
  268.    my $target_dbs       = shift;
  269.    my $target_table     = shift;
  270.    my @mysql_tables = get_mysql_dbs_tables( $target_dbs );
  271.    my $item = '';
  272.    foreach $item ( @mysql_tables ) {
  273.       #print $item . "***n";
  274.       if ( $item eq $target_table ) {
  275.          # dbs is already there...
  276.          return 1;
  277.       }
  278.    }
  279.    return 0;
  280. }
  281. sub run_mysql_script {
  282.    my $script = shift;
  283.    my $target_dbs = shift;
  284.    my @command = ();
  285.    @command = (
  286.       $mysql_command,
  287.       '--user=' . $database_user,
  288.       '--password=' . $database_user_password
  289.    );
  290.    if ( $target_dbs ne "" ) { push( @command, $target_dbs ); }
  291.    push( @command, ">> create_log.log 2>> create_log.stderr" );
  292.    push( @command, "< $script" );
  293.    system( join( ' ', @command ) );
  294. }
  295. sub create_mysql_dbs {
  296.    my $target_dbs          = shift;
  297.    my $target_dbs_create   = shift;
  298.    my $dbs_tables          = shift;
  299.    my $table_cheks         = shift;
  300.    my $db_there = 0;
  301.    $db_there = get_mysql_database_existence( $target_dbs );
  302.    if ( $db_there == 1 ) {
  303.       die
  304.          "There seems to be a issue : $target_dbs is already theren" .
  305.          "and since i don't know how to handle this i'm exitingn";
  306.    }
  307.    run_mysql_script( $target_dbs_create );
  308.    $db_there = get_mysql_database_existence( $target_dbs );
  309.    if ( $db_there == 1 ) {
  310.       print "$target_dbs - Database was created!n";
  311.    } else {
  312.       die 
  313.        "Hmm the database $target_dbs is still not there after i tried ton" .
  314.        "create it, i'm guessing that there is something afoot!n";
  315.    }
  316.    my $table = '';
  317.    foreach $table ( @$dbs_tables ) {
  318.       if ( -f 'pre_' . $table ) {
  319.          run_mysql_script( 'pre_' . $table, $target_dbs );
  320.       }
  321.       run_mysql_script( $table, $target_dbs );
  322.       if ( -f 'post_' . $table ) {
  323.          run_mysql_script( 'post_' . $table, $target_dbs );
  324.       }
  325.    }
  326.    foreach $table ( @$table_cheks ) {
  327.       if ( get_mysql_table_existence( $target_dbs, $table ) ) {
  328.          # Table is there
  329.          print "tTable : $table checked out good tt[ OK ]n";
  330.       } else {
  331.          die "tTable : $table checked DID NOT out good t[ ERROR ]n";
  332.       }
  333.    }
  334.    print "$target_dbs - " .
  335.          "Okay looks like we have a database to use now, have fun!nn";
  336. }
  337. # Simon/23may2000 - 
  338. #     Adding get_mysql_version to retrieve mysql's version number, so that
  339. #     we can build in a bit of backward compatibility.
  340. sub detect_bad_mysqlshow {
  341.    my $badversion = $mysql_noshow;
  342.    if ( ! $mysql_noshow )
  343.    {
  344.       print "tDetected faulty 'mysqlshow', using alternate routine.t[ WARNING ]n";
  345.       open( BAD_MYSQLVIEW,  
  346.          $mysqlshow_command . ' ' . 
  347.             '--user=' . $database_user . ' ' .
  348.             '--password=' . $database_user_password .
  349.             ' |'
  350.       ) or die "$!n";
  351.       while( <BAD_MYSQLVIEW> ) {
  352.          my $input_line = $_;
  353.          if ( $input_line =~ /+/ ) { next; }
  354.          if ( $input_line =~ /Databases/ ) { 
  355.             $badversion = 1; 
  356.          }
  357.       }
  358.    }
  359.    return $badversion;
  360. }
  361. sub create_mysql_session_dbs {
  362.    my $target_dbs          = 'prometheus_sessions';
  363.    my $target_dbs_create   = $SQL_BASE .
  364.    '/database/mysql/Sessions/create_prometheus_sessions.sql';
  365.    my @dbs_tables = ( 
  366.       $SQL_BASE . '/table/mysql/Sessions/sessions_table.sql',
  367.       $SQL_BASE . '/table/mysql/Sessions/sessions_data_table.sql',
  368.    );
  369.    my @table_cheks = (
  370.       'sessions_table',
  371.       'sessions_data_table',
  372.    );
  373.    create_mysql_dbs(
  374.       $target_dbs,
  375.       $target_dbs_create,
  376.       @dbs_tables,
  377.       @table_cheks,
  378.    );
  379. }
  380. sub create_mysql_user_dbs {
  381.    my $target_dbs          = 'prometheus_users';
  382.    my $target_dbs_create   = 
  383.       $SQL_BASE . '/database/mysql/User/create_prometheus_users.sql';
  384.    my @dbs_tables = ( 
  385.       $SQL_BASE . '/table/mysql/User/user_table.sql',
  386.       $SQL_BASE . '/table/mysql/User/user_theme_table.sql',
  387.    );
  388.    my @table_cheks = (
  389.       'user_table',
  390.       'user_theme_table',
  391.    );
  392.    create_mysql_dbs(
  393.       $target_dbs,
  394.       $target_dbs_create,
  395.       @dbs_tables,
  396.       @table_cheks
  397.    );
  398. }
  399. sub create_mysql_user_settings_dbs {
  400.    my $target_dbs          = 'prometheus_settings';
  401.    my $target_dbs_create   = 
  402.       $SQL_BASE . '/database/mysql/PrometheusSettings/create_prometheus_settings.sql';
  403.    my @dbs_tables = ( 
  404.       $SQL_BASE . '/table/mysql/PrometheusSettings/user_theme_table.sql',
  405.       #$SQL_BASE . '/table/mysql/PrometheusSettings/user_language_table.sql',
  406.    );
  407.    my @table_cheks = (
  408.       'user_theme_table',
  409.       #'user_language_table',
  410.    );
  411.    create_mysql_dbs(
  412.       $target_dbs,
  413.       $target_dbs_create,
  414.       @dbs_tables,
  415.       @table_cheks
  416.    );
  417. }
  418. sub create_mysql_domain_dbs {
  419.    my $target_dbs          = 'prometheus_domains';
  420.    my $target_dbs_create   = 
  421.       $SQL_BASE . '/database/mysql/Domain/create_prometheus_domains.sql';
  422.    my @dbs_tables = ( 
  423.       $SQL_BASE . '/table/mysql/Domain/domain_table.sql',
  424.    );
  425.    my @table_cheks = (
  426.       'domain_table',
  427.    );
  428.    create_mysql_dbs(
  429.       $target_dbs,
  430.       $target_dbs_create,
  431.       @dbs_tables,
  432.       @table_cheks
  433.    );
  434. }
  435. sub create_mysql_admin_privileges_dbs {
  436.    my $target_dbs          = 'prometheus_privileges';
  437.    my $target_dbs_create   = 
  438.       $SQL_BASE . '/database/mysql/AdminPrivileges/create_prometheus_privileges.sql';
  439.    my @dbs_tables = ( 
  440.       $SQL_BASE . '/table/mysql/AdminPrivileges/admin_privileges_table.sql',
  441.    );
  442.    my @table_cheks = (
  443.       'admin_privileges_table',
  444.    );
  445.    create_mysql_dbs(
  446.       $target_dbs,
  447.       $target_dbs_create,
  448.       @dbs_tables,
  449.       @table_cheks
  450.    );
  451. }
  452. sub create_mysql_mail_settings_dbs {
  453.    my $target_dbs          = 'mail_settings';
  454.    my $target_dbs_create   = 
  455.       $SQL_BASE . '/database/mysql/MailServerSettings/create_mail_settings.sql';
  456.    my @dbs_tables = ( 
  457.       $SQL_BASE . '/table/mysql/MailServerSettings/server_settings_table.sql',
  458.       $SQL_BASE . '/table/mysql/MailServerSettings/user_signatures_table.sql',
  459.    );
  460.    my @table_cheks = (
  461.       'server_settings_table',
  462.       'user_signatures_table',
  463.    );
  464.    create_mysql_dbs(
  465.       $target_dbs,
  466.       $target_dbs_create,
  467.       @dbs_tables,
  468.       @table_cheks
  469.    );
  470. }
  471. sub create_mysql_customer_dbs {
  472.    my $target_dbs          = 'prometheus_customers';
  473.    my $target_dbs_create   = 
  474.       $SQL_BASE . '/database/mysql/CustomerInformation/create_prometheus_customers.sql';
  475.    my @dbs_tables = ( 
  476.       $SQL_BASE . '/table/mysql/CustomerInformation/address_table.sql',
  477.       $SQL_BASE . '/table/mysql/CustomerInformation/ccard_table.sql',
  478.       $SQL_BASE . '/table/mysql/CustomerInformation/customer_notes.sql',
  479.       $SQL_BASE . '/table/mysql/CustomerInformation/customer_services_table.sql',
  480.       $SQL_BASE . '/table/mysql/CustomerInformation/personal_table.sql',
  481.       $SQL_BASE . '/table/mysql/CustomerInformation/phone_num_table.sql',
  482.       $SQL_BASE . '/table/mysql/CustomerInformation/service_table.sql',
  483.       $SQL_BASE . '/table/mysql/CustomerInformation/services_completed_table.sql'
  484.    );
  485.    my @table_cheks = (
  486.       'address_table',
  487.       'ccard_table',
  488.       'customer_notes',
  489.       'customer_services_table',
  490.       'personal_table',
  491.       'phone_num_table',
  492.       'service_type_table',
  493.       'services_completed_table'
  494.    );
  495.    create_mysql_dbs(
  496.       $target_dbs,
  497.       $target_dbs_create,
  498.       @dbs_tables,
  499.       @table_cheks
  500.    );
  501. }