/** * @inheritdoc */ public function run(&$cmdParams, &$params) { $controller = $this->controller; $res = true; $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : ''; $dir = !empty($cmdParams[1]) ? $cmdParams[1] : ''; $recursive = !empty($cmdParams[2]) ? $cmdParams[2] : false; if (empty($connectionId) || empty($dir)) { Log::throwException('sftpRmdir: Please specify a valid connection id and directory'); } /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */ $connParams = $controller->getConnectionParams($connectionId); $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK); $controller->stdout(' Removing directory ' . ($recursive ? '(recursive) ' : '')); $controller->stdout($dir, Console::FG_CYAN); if (!$controller->dryRun) { // the getConnection method is provided by the SftpConnectReqs Behavior /** @noinspection PhpUndefinedMethodInspection */ /** @var $connection Net_SFTP|resource */ $connection = $controller->getConnection($connectionId); $sftpHelper = new SftpHelper($connectionId, $connection, $connParams); if (!$recursive) { $res = $sftpHelper->rmdir($dir); } else { $res = $sftpHelper->delete($dir, true); } if (!$res) { Log::logger()->addError('sftpRmdir: error removing directory {dir} (recursive: {recursive})', ['dir' => $dir, 'recursive' => $recursive ? 'yes' : 'no']); } $sftpHelper->flushCache(); } else { $controller->stdout(' [dry run]', Console::FG_YELLOW); } $controller->stdout("\n"); return $res; }
/** * @inheritdoc */ public function run(&$cmdParams, &$params) { $controller = $this->controller; $res = true; $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : ''; $file = !empty($cmdParams[1]) ? $cmdParams[1] : ''; if (empty($connectionId) || empty($file)) { Log::throwException('sftpRm: Please specify a valid connection id and file'); } /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */ $connParams = $controller->getConnectionParams($connectionId); $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK); $controller->stdout(' Removing file '); $controller->stdout($file, Console::FG_CYAN); if (!$controller->dryRun) { // the getConnection method is provided by the SftpConnectReqs Behavior /** @noinspection PhpUndefinedMethodInspection */ /** @var $connection Net_SFTP|resource */ $connection = $controller->getConnection($connectionId); $sftpHelper = new SftpHelper($connectionId, $connection, $connParams); if ($sftpHelper->isFile($file)) { $res = $sftpHelper->delete($file, false); if (!$res) { Log::logger()->addError('sftpRm: error removing file {file}', ['file' => $file]); } } else { $controller->stdout("\n"); $controller->warn('sftpRm: ' . $file . ' is not a file'); } $sftpHelper->flushCache(); } else { $controller->stdout(' [dry run]', Console::FG_YELLOW); } $controller->stdout("\n"); return $res; }
/** * Load build script configuration (if present) and ask/set current environment (if needed) * * The current environment name is saved in params['environment'] */ private function _loadScriptConfig() { $env = ''; $configFile = Yii::getAlias('@buildScripts/config.php'); if (file_exists($configFile)) { Log::logger()->addDebug('Loading build script configuration: {configFile}', ['configFile' => $configFile]); /** @noinspection PhpIncludeInspection */ $config = (require $configFile); $envConfig = []; $commonConfig = !empty($config['config']['common']) ? $config['config']['common'] : []; if (!empty($config['environments']) && is_array($config['environments'])) { Log::logger()->addDebug('Available environments: {environments}', ['environments' => implode(', ', array_keys($config['environments']))]); if (!property_exists($this->controller, 'env') || empty($this->controller->env)) { if ($this->controller->interactive) { $env = $this->controller->select('Please select your current environment', $config['environments']); } elseif (isset($config['defaultEnvironment'])) { $env = $config['defaultEnvironment']; } } else { $env = $this->controller->env; } if (!isset($config['environments'][$env])) { Log::throwException('Invalid environment: ' . $env); } Log::logger()->addDebug('Using environment: {env}', ['env' => $env]); // Save the current environment name in params['environment']: $this->_params['environment'] = $env; $envConfig = !empty($config['config'][$env]) ? $config['config'][$env] : []; } $flatCommonConfig = $this->flattenArray($commonConfig); $flatEnvConfig = $this->flattenArray($envConfig); $flatConfig = array_merge($flatCommonConfig, $flatEnvConfig); $this->_configParams = array_keys($flatConfig); $this->_params = array_merge($this->_params, $flatConfig); } }
/** * Log the error message and throw \yii\console\Exception * * @param string $message * @param array $context * * @throws \yii\console\Exception */ public static function throwException($message, array $context = []) { Log::logger()->addCritical($message, $context); throw new Exception($message); }
/** * Returns the content of the given directory * * @param string $dir * @param bool $recursive * @param bool $log whether to log or not the action * * @return array */ public function nlist($dir, $recursive = false, $log = true) { $res = $this->_getFromCache(__FUNCTION__, $dir); if ($res === null) { switch ($this->_connType) { case SftpHelper::TYPE_SFTP: default: $res = $this->_connection->nlist($dir, $recursive); break; case SftpHelper::TYPE_FTP: if ($recursive) { Console::stdout("\n"); Log::throwException('Recursive not supported for nlist in ftp mode'); } $res = @ftp_nlist($this->_connection, $dir); if (is_array($res)) { // remove the $dir prefix from the results to match the format // of $this->_connection->nlist $res = preg_replace('/^' . str_replace('/', '\\/', $dir) . '[\\/]/', '', $res); } elseif (!$res && $this->isDir($dir)) { // for some reason ftp_nlist returns false if the directory is empty... $res = []; } break; } $this->_addToCache(__FUNCTION__, $dir, $res); if ($log) { Log::logger()->addDebug('Performing ' . __METHOD__ . ' // result: {res}', ['res' => var_export($res, true), 'path' => $dir]); } } return $res; }
/** * For each version that is newer of the current DeploYii home version and that requires * changes, tells the user which changes need to be performed manually and if possible * updates it automatically. * * To update a file it is possible to amend it manually or to remove it; if the file is missing * it will be replaced with the original one from the home-dist folder. * * If some manual changes are required, it is possible to simply delete the VERSION file once * the changes have been applied. * * @param string $homeVersion The DeploYii home version * * @throws \yii\console\Exception if the home folder requires to be updated manually */ public static function checkHomeVersion($homeVersion) { $changeLog = ''; $requiredActions = ''; $home = Shell::getHomeDir(); $homeDist = __DIR__ . '/../home-dist'; $changes = array_reverse(self::$_homeChangeList); foreach ($changes as $version => $list) { if (version_compare($version, $homeVersion, '>')) { // files to be manually updated by the user: if (isset($list['mod'])) { foreach ($list['mod'] as $relFilePath => $logMessage) { // If the destination file does not exists, add it from the dist folder // instead of requesting the user to manually update it: $destFile = $home . DIRECTORY_SEPARATOR . $relFilePath; if (!file_exists($destFile)) { $list['add'][$relFilePath] = $logMessage; unset($list['mod'][$relFilePath]); } else { $requiredActions .= " - [{$version}] {$relFilePath}: {$logMessage}\n"; } } } // files/directories to be added: (if no manual actions are required) if (empty($requiredActions) && isset($list['add'])) { foreach ($list['add'] as $relPath => $logMessage) { $srcPath = $homeDist . DIRECTORY_SEPARATOR . $relPath; $destPath = $home . DIRECTORY_SEPARATOR . $relPath; if (!is_dir($srcPath) && !file_exists($destPath) && file_exists($srcPath)) { $changeLog .= " - [{$version}] Adding {$relPath} ({$logMessage})\n"; copy($srcPath, $destPath); } elseif (is_dir($srcPath) && !is_dir($destPath)) { $changeLog .= " - [{$version}] Adding directory: {$relPath} ({$logMessage})\n"; FileHelper::copyDirectory($srcPath, $destPath); } elseif (is_dir($srcPath) && is_dir($destPath)) { $requiredActions .= " - [{$version}] {$relPath}: {$logMessage}\n"; } } } // files/directories to be removed: (if no manual actions are required) if (empty($requiredActions) && isset($list['rem'])) { foreach ($list['rem'] as $relPath => $logMessage) { $destPath = $home . DIRECTORY_SEPARATOR . $relPath; if (!is_dir($destPath) && file_exists($destPath)) { $changeLog .= " - [{$version}] Removing {$relPath} ({$logMessage})\n"; unlink($destPath); } elseif (is_dir($destPath)) { $requiredActions .= " - [{$version}] {$relPath}: {$logMessage}\n"; } } } } } if (!empty($requiredActions)) { Log::throwException("Your DeploYii home folder needs to be manually updated to " . DEPLOYII_VERSION . ":\n" . $requiredActions . "When done delete the VERSION file and run DeploYii again.\n"); } elseif (!empty($changeLog)) { self::updateHomeVersion(); $message = "Your DeploYii home folder has been updated to: " . DEPLOYII_VERSION . ":\n" . $changeLog; Log::logger()->addInfo($message); Console::stdout("---------------------------------------------------------------\n" . $message . "---------------------------------------------------------------\n"); sleep(2); } }
/** * Log the given string and send it to stdout * * @param string $string * @param int $level (optional) if null is passed, level will be set to Logger::DEBUG */ public function logStdout($string, $level = Logger::DEBUG) { $level = $level === null ? Logger::DEBUG : $level; Log::logger()->addRecord($level, trim($string)); $args = func_get_args(); unset($args[1]); // remove the $level parameter to be compatible with the stdout method call_user_func_array(['parent', 'stdout'], $args); }
private function _put($srcFullPath, $srcRelPath) { $destRelPath = $this->_destDir . '/' . $srcRelPath; $this->controller->stdout("\n - " . $srcRelPath . "\t => " . $destRelPath); if (!$this->controller->dryRun) { if (is_dir($srcFullPath)) { $files = FileHelper::findFiles($srcFullPath, $this->_options); foreach ($files as $foundPath) { $relativePath = substr($foundPath, strlen($this->_srcBaseDir) + 1); $this->_sftpHelper->mkdir($destRelPath); $this->_put($foundPath, $relativePath, $this->_srcBaseDir, $this->_destDir, $this->_options); } } elseif (FileHelper::filterPath($srcFullPath, $this->_options)) { $this->_sftpHelper->mkdir(dirname($destRelPath), -1, true); if ($this->_overwrite || !$this->_sftpHelper->fileExists($destRelPath)) { $res = $this->_sftpHelper->put($destRelPath, $srcFullPath); if (!$res) { Log::logger()->addError('sftpPut: error uploading file {from} to {to}', ['from' => $srcFullPath, 'to' => $destRelPath]); } } else { $this->controller->stdout(' [skipped]', Console::FG_PURPLE); } } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } }