public function RestartById($c) { if (!count(DB::query('SELECT * from ' . self::TABLE . ' WHERE id="' . $c . '"'))) { io::out("Work with id={$c} is not exists", IO::MESSAGE_FAIL); return; } $list = DB::query('SELECT * FROM ' . self::TABLE . ' where isnull(finished_at) and not isnull(locked_at) and isnull(failed_at) and id=' . $c . ' ORDER BY run_at DESC'); if (count($list)) { IO::out("This is working now...You cant restart!", IO::MESSAGE_FAIL); return; } if (IO::YES == io::dialog('Do you really want to restart work with id ' . $c . '?', IO::NO | IO::YES, IO::NO)) { DB::query("UPDATE " . self::TABLE . " set attempts='1',finished_at=null, locked_at=null, \n failed_at=null, run_at=now() WHERE id='" . $c . "'"); $php_path = exec("which php"); if (empty($php_path)) { return $this->log("###" . date("c") . " Call from console PHP executable not found"); } if (!is_executable($php_path)) { return $this->log("###" . date("c") . " Call from console {$php_path} could not be executed"); } exec($php_path . ' ' . trim(escapeshellarg(Config::get('ROOT_DIR') . "/vendors/delayedjob/JobHandler.php"), "'") . ' >> ' . Config::get('ROOT_DIR') . '/logs/delayedjob.log 2>&1 &'); io::done('Restarting...'); } else { io::done('Cancel restart'); } IO::out(""); }
function process() { Console::initCore(); if ($r = ArgsHolder::get()->getOption('count')) { $this->count = $r; } if (($c = ArgsHolder::get()->shiftCommand()) == 'help') { return $this->cmdHelp(); } try { IO::out(""); $sql = 'SELECT * FROM ' . self::TABLE . ' where not isnull(finished_at) and not isnull(locked_at) and isnull(failed_at) ORDER BY run_at DESC'; if ($this->count) { $list = DB::query($sql . ' LIMIT ' . $this->count); } else { $list = DB::query($sql); } if (!count($list)) { IO::out("No finished work!", IO::MESSAGE_FAIL); return; } io::out(sprintf("%-10s %-7s %-3s %-20s %-20s %-19s %-4s %-5s", "~CYAN~id", "queue", "pr", "run_at", "locked_at", "finished_at", "att", "call_to~~~")); foreach ($list as $l) { $handler = unserialize($l["handler"]); io::out(sprintf("%-4s %-7s %-3s %-20s %-20s %-20s %-3s %-5s", $l["id"], $l["queue"], $l["priority"], $l["run_at"], $l["locked_at"], $l["finished_at"], $l["attempts"], $handler["class"] . "::" . $handler["method"] . "(...)")); } } catch (Exception $e) { io::out($e->getMessage(), IO::MESSAGE_FAIL); return; } IO::out(""); }
function process() { Console::initCore(); if (($c = ArgsHolder::get()->shiftCommand()) == 'help') { return $this->cmdHelp(); } try { $format = "%-25s %s"; IO::out(""); $s = DB::query('SELECT * FROM ' . self::TABLE . ' where id="' . $c . '"'); if (!count($s)) { io::out("Work with id={$c} is not exists", IO::MESSAGE_FAIL); return; } io::out(sprintf($format, "~CYAN~id~~~", $c)); io::out(sprintf($format, "~CYAN~queue~~~", $this->emp($s[0]['queue']))); io::out(sprintf($format, "~CYAN~priority~~~", $this->emp($s[0]['priority']))); io::out(sprintf($format, "~CYAN~run at~~~", $this->emp($s[0]['run_at']))); io::out(sprintf($format, "~CYAN~locked at~~~", $this->emp($s[0]['locked_at']))); io::out(sprintf($format, "~CYAN~finished at~~~", $this->emp($s[0]['finished_at']))); io::out(sprintf($format, "~CYAN~failed at~~~", $this->emp($s[0]['failed_at']))); io::out(sprintf($format, "~CYAN~attemts~~~", $this->emp($s[0]['attempts']))); $handler = unserialize($s[0]["handler"]); io::out(sprintf($format, "~CYAN~call~~~", $handler["class"] . "::" . $handler["method"]) . "(...)"); if (isset($handler["param"])) { io::out(sprintf($format, "~CYAN~params~~~", trim(self::walker($handler['param']), ','))); } } catch (Exception $e) { io::out($e->getMessage(), IO::MESSAGE_FAIL); return; } IO::out(""); }
function cmdUnlock() { IO::out('Unlocking Package Manager', false); $pm = new PM(); if ($pm->unlock(true)) { io::done(); } }
function cmdList() { $list = $this->rl->getList(); if (!count($list)) { return IO::out('Repository List empty.'); } io::out('~WHITE~List of Repositories~~~:'); for ($i = 0, $c = count($list); $i < $c; $i++) { io::out($list[$i]); } io::out(); io::out('Total: ' . $c); }
public function deleteById($c) { if (!DB::query('SELECT * from ' . self::TABLE . ' WHERE id="' . $c . '"')) { io::out("Work with id={$c} is not exists", IO::MESSAGE_FAIL); return; } $list = DB::query('SELECT * FROM ' . self::TABLE . ' where isnull(finished_at) and not isnull(locked_at) and isnull(failed_at) and id=' . $c . ' ORDER BY run_at DESC'); if (count($list)) { IO::out("This is working now...You cant delete!"); return; } if (IO::YES == io::dialog('Do you really want to delete work with id ' . $c . '? ', IO::NO | IO::YES, IO::NO)) { DB::query("DELETE FROM " . self::TABLE . " WHERE id='" . $c . "'"); io::done('Deleting...'); } else { io::done('Cancel delete'); } }
function process() { if (!($c = ArgsHolder::get()->shiftCommand())) { return io::out('Incorrect parameter', IO::MESSAGE_FAIL) | 1; } $root = Config::get('ROOT_DIR'); $file = $root . '/includes/env/' . strtolower($c) . '_env.php'; if (!file_exists($file)) { return io::out('Mode ' . $c . ' not exists', IO::MESSAGE_FAIL) | 1; } IO::out('Updating Loader', false); $loader = fopen($root . '/includes/env/Loader.php', 'w'); flock($loader, LOCK_EX); $put = '<?php require_once("' . $c . '_env.php");'; fwrite($loader, $put); flock($loader, LOCK_UN); fclose($loader); io::done(); io::out('Backup config.ini', false); if (copy($root . '/config/config.ini', $root . '/config/config.ini.bak')) { io::done(); } else { return IO::out('Can\'t backup file config.ini', IO::MESSAGE_FAIL) | 1; } IO::out('Updating config.ini', false); $file_array = file($root . '/config/config.ini'); $str = null; foreach ($file_array as $fa) { if (preg_match('/\\[\\s*config\\s*:\\s*\\S+\\]/', $fa, $match)) { $str .= '[config : ' . $c . ']' . PHP_EOL; } else { $str .= $fa; } } $config = fopen($root . '/config/config.ini', 'w'); flock($config, LOCK_EX); fwrite($config, $str); flock($config, LOCK_UN); fclose($config); io::done(); IO::done('Enviroments set to ~WHITE~' . $c . '~~~'); }
public function cmdUserlist() { if (!count(ACL::getGroups())) { return io::out('There is no groups yet.'); } $users = ACL::getUsers(); if ($group = ArgsHolder::get()->shiftCommand()) { if (!in_array($group, array_values(ACL::getGroups()))) { return io::out("No such group {$group}", IO::MESSAGE_FAIL) | 3; } IO::out("~WHITE~User(s) from group " . $group . "~~~:"); foreach ($users[$group] as $u) { IO::out(" " . $u); } } else { foreach (array_keys($users) as $g) { IO::out("~WHITE~Group " . $g . "~~~:"); foreach ($users[$g] as $u) { IO::out(" " . $u); } } } }
/** * Обработка исключений, возникающих в процессе работы консоли. * * @param Exception $e исключение для обработки */ static function processException($e) { if ($e instanceof IOException) { IO::out('IO error : ' . $e->getMessage(), IO::MESSAGE_FAIL); } else { IO::out($e, IO::MESSAGE_FAIL); } exit(254); }
public function cmdRemove() { if (($name = ArgsHolder::get()->shiftCommand()) === false) { return io::out('Incorrect param count', IO::MESSAGE_FAIL); } if (file_exists($this->root_dir . '/controllers/' . $name . '.php')) { if (IO::YES == io::dialog('Realy Delete controller,models,pages with name ~WHITE~' . $name . '~~~?', IO::NO | IO::YES, IO::NO)) { IO::out('~WHITE~Removing:~~~'); self::rRem($this->root_dir . '/controllers/' . $name . '.php'); io::done(' controllers/' . $name . '.php'); self::rRem($this->root_dir . $this->models_dir . '/' . $name); io::done(' ' . $this->models_dir . '/' . $name); self::rRem($this->root_dir . '/pages/' . $name); io::done(' pages/' . $name); } } else { io::out('Controller with name ~WHITE~' . $name . '~~~ not exist ', IO::MESSAGE_FAIL); } }
public function createTar($root, $name, $separate = null) { if (file_exists($root . '/' . $name . "_" . date('Ymd') . ".tar.bz2")) { $filename = $name . "_" . date('Ymd_H_i_s') . '.tar'; } else { $filename = $name . "_" . date('Ymd') . '.tar'; } //$cmd="tar -cvf ".$root."/".$filename." ".$root." --exclude='*web*' --exclude='*~'"; chdir($root); IO::out('Packing main... ' . "\t", false); if ($this->all) { $cmd = "tar --exclude='.svn' --exclude='*~' -cvf " . $root . "/" . $filename . " * 2>&1"; } else { $cmd = "tar --exclude='web' --exclude='.svn' --exclude='*~' -cvf " . $root . "/" . $filename . " * 2>&1"; } exec($cmd, $out, $return); if ($return == 0) { io::done(); } if (IO::getVerboseLevel() == IO::MESSAGE_INFO || $return) { foreach ($out as $o) { io::out($o); } } if ($return) { io::out('Return code ' . $return, IO::MESSAGE_FAIL); io::out('Executed command: ' . $cmd); return; } if (!$this->all) { $cmd = "tar --exclude='*~' --exclude='.svn' -uvf " . $root . "/" . $filename . " ./web/css/ ./web/js/ 2>&1"; io::out('Adding web/css, web/js...', false); exec($cmd, $out, $return); if ($return == 0 && !$this->all) { io::done(); } if (IO::getVerboseLevel() == IO::MESSAGE_INFO || $return) { foreach ($out as $o) { io::out($o); } } if ($return) { io::out('Return code ' . $return, IO::MESSAGE_FAIL); io::out('Executed command: ' . $cmd); return; } } io::out('Bzip ' . $filename . '...', false); $cmd = "bzip2 -9 " . $root . "/" . $filename; exec($cmd, $out, $return); if ($return == 0) { io::done(); } if (IO::getVerboseLevel() == IO::MESSAGE_INFO || $return) { foreach ($out as $o) { io::out($o); } } if ($return) { io::out('Return code ' . $return, IO::MESSAGE_FAIL); io::out('Executed command: ' . $cmd); return; } return $filename . ".bz2"; }
/** * Oбработка исключений возникших при работе комманды */ static function processException($e, $cmdName = '') { IO::out('Command Exception ~WHITE~' . $cmdName . '~~~', IO::MESSAGE_FAIL); return Console::processException($e); }
/** * Снимает блокировку системы. * */ function cmdUnlock() { IO::out('Unlocking Package Manager', false); if (PackageManager::get()->unlock(true)) { io::done(); } }
public function cmdPassword() { try { $login = ArgsHolder::get()->shiftCommand(); $password = ArgsHolder::get()->shiftCommand(); if ($login === false) { return io::out('Incorrect param count', IO::MESSAGE_FAIL) | 1; } if ($user = User::findBy("login", $login)) { if (!$password) { IO::out('New password: '******'Confirm New password: '******'Passwords not match.', IO::MESSAGE_FAIL) | 2; } } $user->setPassword($password); } else { return io::out(PHP_EOL . 'User ~WHITE~' . $login . '~~~ not found', IO::MESSAGE_FAIL) | 3; } } catch (UserException $e) { return io::out($e->getMessage(), IO::MESSAGE_FAIL) | 127; } }
/** * Удаление пакета */ static function uninstall($package) { $nvr = PackageManager::parseNRV($package); // проверка обратных зависимостей $res = Deps::isNoNeeded($nvr, $unstatisfied); if (count($res) == 0) { return IO::out('Packages to uninstall not found', IO::MESSAGE_FAIL); } if ($res === false) { io::out('Unable uninstall package ~WHITE~' . $nvr['name'] . '~~~ because', IO::MESSAGE_FAIL); foreach ($unstatisfied as $p => $d) { io::out('Package ' . $p . ' require ' . implode(', ', array_keys($d))); } return false; } io::out('Packages to be removed: ', false); foreach ($res as $p) { io::out($p->name . '(' . $p->version . ') ', false); } io::out(); if (IO::NO == io::dialog('Continue?', IO::YES | IO::NO, IO::NO)) { return 1; } foreach ($res as $p) { io::out('~WHITE~Undeploying ' . $p->name . '(' . $p->version . '):~~~'); $r = Deployer::undeploy(Deployer::getPackageRollbackDir($p)); if ($r || IO::OK != IO::dialog('Some part of rollback failed. Remove anyway?', IO::YES | IO::NO, IO::YES)) { Deployer::getPackageRollbackDir($p)->delete(); $p->file->delete(); PackageManager::get()->packagesSequence->removePackage($p->name, $p->version); } } }
function process() { $root = Config::get('ROOT_DIR'); $file_array = file_get_contents($root . '/config/config.ini'); try { //Look for php-cgi in the system $php_path = exec("which php-cgi"); if (empty($php_path) || !is_executable($php_path)) { IO::out('Look for PHP-CGI in the system.', IO::MESSAGE_FAIL); IO::out("You can't to check hosting because PHP-CGI not found."); return; } $out = exec("echo \"<?php echo 'php-cgi test' ?>\" | {$php_path} -q "); if ($out != "php-cgi test") { IO::out('Look for PHP-CGI in the system.', IO::MESSAGE_FAIL); IO::out("You can't to check hosting because PHP-CGI not found."); return; } IO::out('Look for PHP-CGI in the system.', IO::MESSAGE_OK); $toini = array(); foreach ($this->for_check as $k => $v) { $output = array(); $subcmd = '<?php $r=ini_get("' . $k . '");if($r) echo $r; else echo ((int)$r);'; exec("echo '" . $subcmd . "' | {$php_path} -q", $output); if ($k == 'error_log') { IO::out($k . ' = ' . $output[0], IO::MESSAGE_OK); continue; } if ($k == 'error_log' || $k == 'max_execution_time' || $k == 'max_input_time' || $k == 'upload_max_filesize' || $k == 'memory_limit' || $k == 'post_max_size') { $ch = str_replace('m', '', strtolower($output[0])); if ((int) $ch >= (int) $v) { IO::out($k . ' = ' . $output[0], IO::MESSAGE_OK); continue; } else { io::out($k . ' = ' . $output[0] . ' this must be >=' . $v, IO::MESSAGE_FAIL); continue; } } if ($v == $output[0]) { IO::out($k . ' = ' . $output[0], IO::MESSAGE_OK); } else { $out = array(); $subcmd = '<?php ini_set("' . $k . '","' . $v . '");$r=ini_get("' . $k . '");if($r=="' . $v . '") echo 111; else echo print_r($r);'; exec("echo '" . $subcmd . "' | {$php_path} -q", $out); if ($out[0] == 111) { $toini[$k] = $v; IO::out($k . ' = ' . $output[0] . ', but you can set "' . $v . '"', IO::MESSAGE_WARN); } else { io::out($k . ' = ' . $output[0], IO::MESSAGE_FAIL); } } } $output = array(); $subcmd = '<?php $r=ini_get("upload_max_filesize");if($r) echo $r; else echo ((int)$r);'; exec("echo '" . $subcmd . "' | {$php_path} -q", $output); $umf = str_replace('m', '', strtolower($output[0])); $output = array(); $subcmd = '<?php $r=ini_get("post_max_size");if($r) echo $r; else echo ((int)$r);'; exec("echo '" . $subcmd . "' | {$php_path} -q", $output); $pms = str_replace('m', '', strtolower($output[0])); $output = array(); $subcmd = '<?php $r=ini_get("memory_limit");if($r) echo $r; else echo ((int)$r);'; exec("echo '" . $subcmd . "' | {$php_path} -q", $output); $ml = str_replace('m', '', strtolower($output[0])); if ((int) $pms < (int) $umf) { IO::out('post_max_size < upload_max_filesize', IO::MESSAGE_FAIL); } if ((int) $pms > (int) $ml) { IO::out('post_max_size > memory_limit', IO::MESSAGE_FAIL); } if ((int) $pms >= (int) $umf && (int) $pms < (int) $ml) { IO::out('upload_max_filesize(' . $umf . ')=< ini.post_max_size(' . $pms . ') < ini.memory_limit(' . $ml . ')', IO::MESSAGE_OK); } if (is_writable('/tmp')) { IO::out('Temp dir is writable.', IO::MESSAGE_OK); } else { IO::out('Temp dir is not writable!', IO::MESSAGE_FAIL); } IO::out(); IO::out('Check for extensions:'); exec("{$php_path} -m", $output); foreach ($this->ext as $e) { if ($e == 'gd') { if (!in_array($e, $output)) { if (!in_array('imagick', $output)) { io::out('Extension ' . $e . ' or imagick are not loaded.', IO::MESSAGE_FAIL); } else { io::out('Extension imagick is loaded.', IO::MESSAGE_OK); } } else { io::out('Extension ' . $e . ' is loaded.', IO::MESSAGE_OK); } continue; } if (!in_array($e, $output)) { io::out('Extension ' . $e . ' is not loaded.', IO::MESSAGE_FAIL); } else { if ($e == 'apc') { io::out('Extension ' . $e . ' is loaded.You can set "apc.enabled=On" in php.ini.', IO::MESSAGE_OK); continue; } else { io::out('Extension ' . $e . ' is loaded.', IO::MESSAGE_OK); } } } IO::out(); IO::out('Check for modes:'); IO::out('Check for Production:'); foreach ($this->mod_production as $k => $v) { $output = array(); $subcmd = '<?php ini_set("' . $k . '",' . (int) $v . ');$r=ini_get("' . $k . '");if($r==' . $v . ') echo 1; else echo (int)$r;'; exec("echo '" . $subcmd . "' | {$php_path} -q", $output); if ($output[0]) { IO::out($k . ' = ' . $v, IO::MESSAGE_OK); } else { IO::out($k . ' = ' . $output[0], IO::MESSAGE_FAIL); } } IO::out(); IO::out('Check for Development:'); foreach ($this->mod_dev as $k => $v) { $output = array(); $subcmd = '<?php ini_set("' . $k . '",' . (int) $v . ');$r=ini_get("' . $k . '");if($r==' . $v . ') echo 1; else echo (int)$r;'; exec("echo '" . $subcmd . "' | {$php_path} -q", $output); if ($output[0]) { IO::out($k . ' = ' . $v, IO::MESSAGE_OK); } else { IO::out($k . ' = ' . $output[0], IO::MESSAGE_FAIL); } } IO::out(); if (ArgsHolder::get()->getOption('generate-ini')) { if (!empty($toini)) { io::out('Generating php.ini', false); $res = array(); if (file_exists(Config::get('ROOT_DIR') . '/php.ini')) { $file = file(Config::get('ROOT_DIR') . '/php.ini'); for ($i = 0; $i < count($file); $i++) { if ($file[$i] != "\n") { $res[strtok($file[$i], '=')] = strtok('='); } } } foreach ($toini as $k => $v) { if (!isset($res[$k])) { $res[$k] = $v; } if ($res[$k] != $toini[$k]) { $res[$k] = $toini[$k]; } } foreach ($res as $key => $val) { $write[] = $key . '=' . $val; } $f = fopen(Config::get('ROOT_DIR') . '/php.ini', 'w'); if (flock($f, LOCK_EX)) { fwrite($f, implode("\n", $write)); flock($f, LOCK_UN); } else { echo "Couldn't get the lock!"; } fclose($f); IO::done(); } } } catch (exception $e) { io::out($e->getmessage(), IO::MESSAGE_FAIL); return; } }
/** * Выводит ассоциативный массив в две колонке * * @param array $opts */ static function outOptions($opts) { $maxKey = 0; $maxValue = 0; foreach ($opts as $k => $v) { if ($maxKey < ($m = strlen($k))) { $maxKey = $m; } if ($maxValue < ($m = strlen($v))) { $maxValue = $m; } } $format = " %-" . $maxKey . "s %s"; foreach ($opts as $k => $v) { $v = trim($v); if (strpos($v, PHP_EOL) === false) { IO::out(sprintf($format, $k, $v)); } else { $v = explode(PHP_EOL, $v); IO::out(sprintf($format, $k, array_shift($v))); foreach ($v as $l) { IO::out(sprintf($format, '', $l)); } } } }
function rename() { if (!count($this->list)) { return; } foreach ($this->list as $s => $t) { $source = $this->target->getFile($s); io::info("\t", false); if (!$source->exists()) { io::out('Source ' . $source . ' not found', IO::MESSAGE_WARN); continue; } io::info('[~PURPLE~M~~~] ' . $s . ' ==> ' . $t); $r = rename($this->target->getFile($s), $this->target->getFile($t)); if (!$r) { IO::out('Throws exception', IO::MESSAGE_FAIL); } elseif (!is_null($this->backupfile)) { file_put_contents($this->backupfile, $t . ' ' . $s . PHP_EOL . ($this->backupfile->exists() ? file_get_contents($this->backupfile) : '')); } } }
function clean() { IO::out('>>>Rollbak clean: ' . $this->count(), false); file_put_contents($this->file, ''); io::done(); }