* +----------------------------------------------------------------------+ * | Copyright (c) 2000-2010 InterWorx L.L.C., All Rights Reserved. | * +----------------------------------------------------------------------+ * | Redistribution and use in source form, with or without modification | * | is NOT permitted without consent from the copyright holder. | * | | * | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND | * | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | * | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | * | PARTICULAR PURPOSE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | * | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | * | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | * | PROFITS; OF BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | * | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | * | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | * | DAMAGE. | * +----------------------------------------------------------------------+ * * * @package InterWorx * @author Dustin Williams * @id $Id$ */ require_once( '../include/iworx.conf.php' ); require_once( 'Console/Getopt.php' ); if( IW::Env()->isInMaintenanceMode() ) { IWorxConsole::echoVerbose( 'InterWorx is in maintenance mode' ); IWorxConsole::echoVerbose( 'Try your request again in a few minutes' ); exit( CLI::MAINTENANCE ); } $MYSQL_VERBOSE = false; $MYSQL_REMOVE = false; $MYSQL_STORAGE = false; $MYSQL_HUMAN = false; $MYSQL_CHGRP = false; $MYSQL_GID = ''; $MYSQL_DATABASE = ''; $MYSQL_STARTONBOOT = false; if( !iworx_is_valid_license() ) { $message = iworx_get_license_error_string(); IWorxLog::log( $message, IWorxLog::ALERT ); IWorxConsole::println( $message ); exit( 666 ); } if( IWorxConsole::suidroot() ) { IWorxLog::log( 'script begin', IWorxLog::INFO ); $exit = mysqlx( $argv ); IWorxLog::log( 'script end', IWorxLog::INFO ); } else { $exit = 9999; IWorxLog::log( 'could not start script. SUID root failed', IWorxLog::ERR ); } exit( $exit ); /** * Run the mysql. * * @param string $argv * @return integer|null */ function mysqlx( $argv ) { global $MYSQL_DATABASE; global $MYSQL_STORAGE; global $MYSQL_HUMAN; global $MYSQL_CHGRP; global $MYSQL_GID; global $MYSQL_STARTONBOOT; mysql_setopts( $argv ); $status = 0; if( $MYSQL_STORAGE ) { if( !$MYSQL_DATABASE ) { IWorxLog::log( 'a database name is required', IWorxLog::ERR ); $status = 2; } else { $DatabaseServer = MySQL::getLocalServer(); $db = IWorxDbUtil::db_connect( $DatabaseServer->getDsn() ); $result = IWorxDbUtil::db_query( $db, "SHOW VARIABLES LIKE 'datadir'" ); $row = $result->fetchRow( DB_FETCHMODE_OBJECT ); $datadir = $row->Value; assert( !empty( $datadir ) ); assert( !stristr( $MYSQL_DATABASE, '.' ) ); $dir = $datadir . $MYSQL_DATABASE; $du = Ini::get( Ini::BIN, 'du' ); if( $MYSQL_HUMAN ) { $cmd = "{$du} -s --si $dir"; } else { $cmd = "{$du} -bs $dir"; } IWorxExec::exec( $cmd, $result, $retval ); if( $retval != 0 ) { IWorxLog::log( "disk usage command failed on $dir", IWorxLog::ERR ); return 2; } list( $space, $noo ) = preg_split( '/[\s]+/', $result[0] ); IWorxConsole::iworx_print( $space ); }//end if }//end if $do_quota = Ini::get( Ini::MYSQL, 'group_quota' ) === '1'; if( $MYSQL_CHGRP && $MYSQL_DATABASE && $MYSQL_GID && $do_quota ) { $db_dir = Ini::get( Ini::DIR, 'mysql_base' ) . "/$MYSQL_DATABASE"; IWorxFileSys::chgrp( $MYSQL_GID, $db_dir, true ); IWorxExec::exec( Ini::get( Ini::BIN, 'chmod' ) . " g+s $db_dir", $result, $retval ); } if( $MYSQL_STARTONBOOT ) { $status = mysql_startonboot() ? 0 : 1; } return $status; } /** * Set the service to start-on-boot. * * @return 0 if start-on-boot set properly, 1 otherwise */ function mysql_startonboot() { global $MYSQL_STARTONBOOT; if( $MYSQL_STARTONBOOT == 'yes' || $MYSQL_STARTONBOOT == 'no' ) { $sob = ( $MYSQL_STARTONBOOT == 'yes' ) ? true : false; return MySQL::startOnBoot( $sob ); } else if( $MYSQL_STARTONBOOT == 'status' ) { return MySQL::isStartedOnBoot(); } } /** * Set any global options (like debug). * * @param mixed $argv */ function mysql_setopts( $argv ) { global $MYSQL_VERBOSE; global $MYSQL_DATABASE; global $MYSQL_STORAGE; global $MYSQL_HUMAN; global $MYSQL_CHGRP; global $MYSQL_GID; global $MYSQL_STARTONBOOT; IWorxLog::log( implode( ' ', $argv ), IWorxLog::NOTICE ); $getopt = new Console_Getopt(); $args = IWorxConsole::get_argv(); $options = $getopt->getopt( $args, 'dnqsvo:l:u:rcg:', array( 'debug', 'nodebug', 'storage', 'human', 'verbose', 'database=', 'chgrp', 'gid=', 'config-file-params', 'config-params', 'start-on-boot=' ) ); if( PEAR::isError( $options ) ) { IWorxLog::log( substr( $options->getMessage(), 14 ), IWorxLog::WARN ); print( $argv[0] . ' ' . substr( $options->getMessage(), 14 ) . "\n" ); exit(); } foreach( $options[0] as $opt ) { switch( $opt[0] ) { case 'd': case '--debug': Ini::set( 'iworx', 'debug', true ); break; case 'n': case '--nodebug': Ini::set( 'iworx', 'debug', false ); break; case 's': case '--storage': $MYSQL_STORAGE = true; break; case 'h': case '--human': $MYSQL_HUMAN = true; break; case 'v': case '--verbose': $MYSQL_VERBOSE = true; break; case 'b': case '--database': $MYSQL_DATABASE = $opt[1]; break; case 'c': case '--chgrp': $MYSQL_CHGRP = true; break; case 'g': case '--gid': $MYSQL_GID = $opt[1]; break; case '--config-file-params': $params = MySQL::readConfigFile(); foreach( $params as $section => $values ) { print( "[$section]\n" ); foreach( $values as $key => $value ) { if( $value !== '' ) { print( "$key=$value\n" ); } else { print( "$key\n" ); } } } break; case '--config-params': // change this to use driver shit later $params = MySQL::getConfigParams(); foreach( $params as $name => $value ) { print( "$name $value\n" ); } break; case '--start-on-boot': $MYSQL_STARTONBOOT = $opt[1]; break; }//end switch }//end foreach opts }