Install.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Hansson <daniel@techandme.se>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Thomas Pulzer <t.pulzer@kniel.de>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Core\Command\Maintenance;
  33. use bantu\IniGetWrapper\IniGetWrapper;
  34. use InvalidArgumentException;
  35. use OC\Console\TimestampFormatter;
  36. use OC\Migration\ConsoleOutput;
  37. use OC\Setup;
  38. use OC\SystemConfig;
  39. use Symfony\Component\Console\Command\Command;
  40. use Symfony\Component\Console\Helper\QuestionHelper;
  41. use Symfony\Component\Console\Input\InputInterface;
  42. use Symfony\Component\Console\Input\InputOption;
  43. use Symfony\Component\Console\Output\OutputInterface;
  44. use Symfony\Component\Console\Question\Question;
  45. use Throwable;
  46. use function get_class;
  47. class Install extends Command {
  48. public function __construct(
  49. private SystemConfig $config,
  50. private IniGetWrapper $iniGetWrapper,
  51. ) {
  52. parent::__construct();
  53. }
  54. protected function configure(): void {
  55. $this
  56. ->setName('maintenance:install')
  57. ->setDescription('install Nextcloud')
  58. ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
  59. ->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
  60. ->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
  61. ->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
  62. ->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'Login to connect to the database')
  63. ->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
  64. ->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null)
  65. ->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'Login of the admin account', 'admin')
  66. ->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
  67. ->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'E-Mail of the admin account')
  68. ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
  69. }
  70. protected function execute(InputInterface $input, OutputInterface $output): int {
  71. // validate the environment
  72. $setupHelper = \OCP\Server::get(\OC\Setup::class);
  73. $sysInfo = $setupHelper->getSystemInfo(true);
  74. $errors = $sysInfo['errors'];
  75. if (count($errors) > 0) {
  76. $this->printErrors($output, $errors);
  77. // ignore the OS X setup warning
  78. if (count($errors) !== 1 ||
  79. (string)$errors[0]['error'] !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk! ') {
  80. return 1;
  81. }
  82. }
  83. // validate user input
  84. $options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
  85. if ($output->isVerbose()) {
  86. // Prepend each line with a little timestamp
  87. $timestampFormatter = new TimestampFormatter(null, $output->getFormatter());
  88. $output->setFormatter($timestampFormatter);
  89. $migrationOutput = new ConsoleOutput($output);
  90. } else {
  91. $migrationOutput = null;
  92. }
  93. // perform installation
  94. $errors = $setupHelper->install($options, $migrationOutput);
  95. if (count($errors) > 0) {
  96. $this->printErrors($output, $errors);
  97. return 1;
  98. }
  99. if ($setupHelper->shouldRemoveCanInstallFile()) {
  100. $output->writeln('<warn>Could not remove CAN_INSTALL from the config folder. Please remove this file manually.</warn>');
  101. }
  102. $output->writeln("Nextcloud was successfully installed");
  103. return 0;
  104. }
  105. /**
  106. * @param InputInterface $input
  107. * @param OutputInterface $output
  108. * @param string[] $supportedDatabases
  109. * @return array
  110. */
  111. protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
  112. $db = strtolower($input->getOption('database'));
  113. if (!in_array($db, $supportedDatabases)) {
  114. throw new InvalidArgumentException("Database <$db> is not supported. " . implode(", ", $supportedDatabases) . " are supported.");
  115. }
  116. $dbUser = $input->getOption('database-user');
  117. $dbPass = $input->getOption('database-pass');
  118. $dbName = $input->getOption('database-name');
  119. $dbPort = $input->getOption('database-port');
  120. if ($db === 'oci') {
  121. // an empty hostname needs to be read from the raw parameters
  122. $dbHost = $input->getParameterOption('--database-host', '');
  123. } else {
  124. $dbHost = $input->getOption('database-host');
  125. }
  126. if ($dbPort) {
  127. // Append the port to the host so it is the same as in the config (there is no dbport config)
  128. $dbHost .= ':' . $dbPort;
  129. }
  130. if ($input->hasParameterOption('--database-pass')) {
  131. $dbPass = (string) $input->getOption('database-pass');
  132. }
  133. $adminLogin = $input->getOption('admin-user');
  134. $adminPassword = $input->getOption('admin-pass');
  135. $adminEmail = $input->getOption('admin-email');
  136. $dataDir = $input->getOption('data-dir');
  137. if ($db !== 'sqlite') {
  138. if (is_null($dbUser)) {
  139. throw new InvalidArgumentException("Database account not provided.");
  140. }
  141. if (is_null($dbName)) {
  142. throw new InvalidArgumentException("Database name not provided.");
  143. }
  144. if (is_null($dbPass)) {
  145. /** @var QuestionHelper $helper */
  146. $helper = $this->getHelper('question');
  147. $question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
  148. $question->setHidden(true);
  149. $question->setHiddenFallback(false);
  150. $dbPass = $helper->ask($input, $output, $question);
  151. }
  152. }
  153. if (is_null($adminPassword)) {
  154. /** @var QuestionHelper $helper */
  155. $helper = $this->getHelper('question');
  156. $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
  157. $question->setHidden(true);
  158. $question->setHiddenFallback(false);
  159. $adminPassword = $helper->ask($input, $output, $question);
  160. }
  161. if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
  162. throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
  163. }
  164. $options = [
  165. 'dbtype' => $db,
  166. 'dbuser' => $dbUser,
  167. 'dbpass' => $dbPass,
  168. 'dbname' => $dbName,
  169. 'dbhost' => $dbHost,
  170. 'adminlogin' => $adminLogin,
  171. 'adminpass' => $adminPassword,
  172. 'adminemail' => $adminEmail,
  173. 'directory' => $dataDir
  174. ];
  175. if ($db === 'oci') {
  176. $options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
  177. }
  178. return $options;
  179. }
  180. /**
  181. * @param OutputInterface $output
  182. * @param array<string|array> $errors
  183. */
  184. protected function printErrors(OutputInterface $output, array $errors): void {
  185. foreach ($errors as $error) {
  186. if (is_array($error)) {
  187. $output->writeln('<error>' . $error['error'] . '</error>');
  188. if (isset($error['hint']) && !empty($error['hint'])) {
  189. $output->writeln('<info> -> ' . $error['hint'] . '</info>');
  190. }
  191. if (isset($error['exception']) && $error['exception'] instanceof Throwable) {
  192. $this->printThrowable($output, $error['exception']);
  193. }
  194. } else {
  195. $output->writeln('<error>' . $error . '</error>');
  196. }
  197. }
  198. }
  199. private function printThrowable(OutputInterface $output, Throwable $t): void {
  200. $output->write('<info>Trace: ' . $t->getTraceAsString() . '</info>');
  201. $output->writeln('');
  202. if ($t->getPrevious() !== null) {
  203. $output->writeln('');
  204. $output->writeln('<info>Previous: ' . get_class($t->getPrevious()) . ': ' . $t->getPrevious()->getMessage() . '</info>');
  205. $this->printThrowable($output, $t->getPrevious());
  206. }
  207. }
  208. }