You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Install.php 8.6KB

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