/** * Run user update * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ protected function userUpdate(InputInterface $input, OutputInterface $output) { // GIT pulls and other stuff // ################## // SSH Git repo update // ################## $reposDirectory = $this->getApplication()->getConfigValue('config', 'ssh_conf_path', '/opt/conf/ssh'); if (is_dir($reposDirectory) && is_dir($reposDirectory . '/.git')) { // SSH Git repo exists, update now $originalCwd = getcwd(); \CliTools\Utility\PhpUtility::chdir($reposDirectory); try { // Update git repository $this->outputBlock($output, 'Running git update of ' . $reposDirectory); $command = new CommandBuilder('git', 'pull'); $command->executeInteractive(); $command = new \CliTools\Shell\CommandBuilder\SelfCommandBuilder(); $command->addArgument('user:rebuildsshconfig'); $command->executeInteractive(); } catch (\RuntimeException $e) { $msg = 'Running git update of ' . $reposDirectory . '... FAILED'; $output->writeln('<error>' . $msg . '</error>'); } \CliTools\Utility\PhpUtility::chdir($originalCwd); } }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $database = $input->getArgument('db'); $dumpFile = $input->getArgument('file'); if (!is_file($dumpFile) || !is_readable($dumpFile)) { $output->writeln('<p-error>File is not readable</p-error>'); return 1; } $dumpFileType = PhpUtility::getMimeType($dumpFile); $output->writeln('<h2>Restoring dump "' . $dumpFile . '" into database "' . $database . '"</h2>'); if (DatabaseConnection::databaseExists($database)) { // Dropping $output->writeln('<p>Dropping database</p>'); $query = 'DROP DATABASE IF EXISTS ' . DatabaseConnection::sanitizeSqlDatabase($database); DatabaseConnection::exec($query); } // Creating $output->writeln('<p>Creating database</p>'); $query = 'CREATE DATABASE ' . DatabaseConnection::sanitizeSqlDatabase($database); DatabaseConnection::exec($query); // Inserting putenv('USER='******'MYSQL_PWD=' . DatabaseConnection::getDbPassword()); $commandMysql = new CommandBuilder('mysql', '--user=%s %s --one-database', array(DatabaseConnection::getDbUsername(), $database)); // Set server connection details if ($input->getOption('host')) { $commandMysql->addArgumentTemplate('-h %s', $input->getOption('host')); } if ($input->getOption('port')) { $commandMysql->addArgumentTemplate('-P %s', $input->getOption('port')); } $commandFile = new CommandBuilder(); $commandFile->addArgument($dumpFile); $commandFile->addPipeCommand($commandMysql); switch ($dumpFileType) { case 'application/x-bzip2': $output->writeln('<p>Using BZIP2 decompression</p>'); $commandFile->setCommand('bzcat'); break; case 'application/gzip': case 'application/x-gzip': $output->writeln('<p>Using GZIP decompression</p>'); $commandFile->setCommand('gzcat'); break; case 'application/x-lzma': case 'application/x-xz': $output->writeln('<p>Using LZMA decompression</p>'); $commandFile->setCommand('xzcat'); break; default: $output->writeln('<p>Using plaintext (no decompression)</p>'); $commandFile->setCommand('cat'); break; } $output->writeln('<p>Reading dump</p>'); $commandFile->executeInteractive(); $output->writeln('<h2>Database "' . $database . '" restored</h2>'); return 0; }
/** * Stop last docker containers from previous run * * @param string $path Path */ protected function stopContainersFromPrevRun($path) { $currentPath = getcwd(); try { $this->output->writeln('<p>Trying to stop last running docker container in "' . $path . '"</p>'); // Jump into last docker dir \CliTools\Utility\PhpUtility::chdir($path); // Stop docker containers $command = new CommandBuilder(null, 'stop'); $this->executeDockerCompose($command); // Jump back \CliTools\Utility\PhpUtility::chdir($currentPath); } catch (\Exception $e) { } }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $paramList = $this->getFullParameterList(); $path = UnixUtility::findFileInDirectortyTree('Makefile'); if (!empty($path)) { $path = dirname($path); $this->output->writeln('<comment>Found Makefile directory: ' . $path . '</comment>'); // Switch to directory of docker-compose.yml PhpUtility::chdir($path); $command = new CommandBuilder('make'); if (!empty($paramList)) { $command->setArgumentList($paramList); } $command->executeInteractive(); } else { $this->output->writeln('<error>No Makefile found in tree</error>'); return 1; } return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void * @throws \Exception */ public function execute(InputInterface $input, OutputInterface $output) { $cliSyncFilePath = getcwd() . '/' . AbstractCommand::CONFIG_FILE; if (file_exists($cliSyncFilePath)) { $this->output->writeln('<p-error>Configuration file ' . AbstractCommand::CONFIG_FILE . ' already exists</p-error>'); return 1; } // fetch example $content = PhpUtility::fileGetContents(CLITOOLS_ROOT_FS . '/conf/clisync.yml'); // store in current working dir PhpUtility::filePutContents($cliSyncFilePath, $content); // Start editor with file (if $EDITOR is set) try { $editor = new EditorCommandBuilder(); $editor->addArgument($cliSyncFilePath)->executeInteractive(); } catch (\Exception $e) { $this->output->writeln('<p-error>' . $e->getMessage() . '</p-error>'); } $this->output->writeln('<info>Successfully created ' . AbstractCommand::CONFIG_FILE . ' </info>'); }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $composerCmd = $this->getApplication()->getConfigValue('bin', 'composer'); $paramList = $this->getFullParameterList(); $composerJsonPath = UnixUtility::findFileInDirectortyTree('composer.json'); if (!empty($composerJsonPath)) { $path = dirname($composerJsonPath); $this->output->writeln('<comment>Found composer.json directory: ' . $path . '</comment>'); // Switch to directory of docker-compose.yml PhpUtility::chdir($path); $command = new CommandBuilder(); $command->parse($composerCmd); if (!empty($paramList)) { $command->setArgumentList($paramList); } $command->executeInteractive(); } else { $this->output->writeln('<error>No composer.json found in tree</error>'); return 1; } return 0; }
/** * Download file */ protected function downloadUpdate() { $output = $this->output; // Progress counter $progress = function ($downloadTotal, $downoadProgress) use($output) { static $counter = 0; if ($counter % 30 === 0) { $output->write('<info>.</info>'); } $counter++; }; $data = \CliTools\Utility\PhpUtility::curlFetch($this->updateUrl, $progress); $tmpFile = tempnam(sys_get_temp_dir(), 'ct'); file_put_contents($tmpFile, $data); $this->cliToolsUpdatePath = $tmpFile; }
/** * Execute docker compose run * * @param string $containerName Container name * @param CommandBuilderInterface $command Command * * @return int|null|void */ protected function executeDockerComposeRun($containerName, CommandBuilderInterface $command) { // Search updir for docker-compose.yml $path = $this->getDockerPath(); if (!empty($path)) { // Switch to directory of docker-compose.yml PhpUtility::chdir($path); $this->output->writeln('<info>Executing "' . $command->getCommand() . '" in docker container "' . $containerName . '" ...</info>'); $dockerCommand = new CommandBuilder('docker-compose', 'run --rm %s', array($containerName)); $dockerCommand->append($command, false); $dockerCommand->executeInteractive(); } else { $this->output->writeln('<p-error>No docker-compose.yml found in tree</p-error>'); return 1; } return 0; }
/** * Add exclude (pattern) list to rsync command * * @param CommandBuilder $command Rsync Command * @param array $list List of excludes */ protected function rsyncAddExcludeList(CommandBuilder $command, $list) { $rsyncFilter = $this->tempDir . '/.rsync-exclude'; PhpUtility::filePutContents($rsyncFilter, implode("\n", $list)); $command->addArgumentTemplate('--exclude-from=%s', $rsyncFilter); // cleanup rsync file $command->getExecutor()->addFinisherCallback(function () use($rsyncFilter) { unlink($rsyncFilter); }); }
/** * Build and startup docker instance * * @param string $path Path */ protected function startDockerInstance($path) { $this->setTerminalTitle('Start docker'); $this->output->writeln('<comment>Building docker containers "' . $path . '"</comment>'); PhpUtility::chdir($path); $command = new SelfCommandBuilder(); $command->addArgument('docker:up'); $command->executeInteractive(); }