Install.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. private SystemConfig $config;
  51. private IniGetWrapper $iniGetWrapper;
  52. public function __construct(SystemConfig $config, IniGetWrapper $iniGetWrapper) {
  53. parent::__construct();
  54. $this->config = $config;
  55. $this->iniGetWrapper = $iniGetWrapper;
  56. }
  57. protected function configure() {
  58. $this
  59. ->setName('maintenance:install')
  60. ->setDescription('install Nextcloud')
  61. ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
  62. ->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
  63. ->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
  64. ->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
  65. ->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database')
  66. ->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
  67. ->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null)
  68. ->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin')
  69. ->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
  70. ->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'E-Mail of the admin account')
  71. ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
  72. }
  73. protected function execute(InputInterface $input, OutputInterface $output): int {
  74. // validate the environment
  75. $server = \OC::$server;
  76. $setupHelper = new Setup(
  77. $this->config,
  78. $this->iniGetWrapper,
  79. $server->getL10N('lib'),
  80. $server->query(Defaults::class),
  81. $server->get(LoggerInterface::class),
  82. $server->getSecureRandom(),
  83. \OC::$server->query(Installer::class)
  84. );
  85. $sysInfo = $setupHelper->getSystemInfo(true);
  86. $errors = $sysInfo['errors'];
  87. if (count($errors) > 0) {
  88. $this->printErrors($output, $errors);
  89. // ignore the OS X setup warning
  90. if (count($errors) !== 1 ||
  91. (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! ') {
  92. return 1;
  93. }
  94. }
  95. // validate user input
  96. $options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
  97. if ($output->isVerbose()) {
  98. // Prepend each line with a little timestamp
  99. $timestampFormatter = new TimestampFormatter(null, $output->getFormatter());
  100. $output->setFormatter($timestampFormatter);
  101. $migrationOutput = new ConsoleOutput($output);
  102. } else {
  103. $migrationOutput = null;
  104. }
  105. // perform installation
  106. $errors = $setupHelper->install($options, $migrationOutput);
  107. if (count($errors) > 0) {
  108. $this->printErrors($output, $errors);
  109. return 1;
  110. }
  111. if ($setupHelper->shouldRemoveCanInstallFile()) {
  112. $output->writeln('<warn>Could not remove CAN_INSTALL from the config folder. Please remove this file manually.</warn>');
  113. }
  114. $output->writeln("Nextcloud was successfully installed");
  115. return 0;
  116. }
  117. /**
  118. * @param InputInterface $input
  119. * @param OutputInterface $output
  120. * @param string[] $supportedDatabases
  121. * @return array
  122. */
  123. protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
  124. $db = strtolower($input->getOption('database'));
  125. if (!in_array($db, $supportedDatabases)) {
  126. throw new InvalidArgumentException("Database <$db> is not supported. " . implode(", ", $supportedDatabases) . " are supported.");
  127. }
  128. $dbUser = $input->getOption('database-user');
  129. $dbPass = $input->getOption('database-pass');
  130. $dbName = $input->getOption('database-name');
  131. $dbPort = $input->getOption('database-port');
  132. if ($db === 'oci') {
  133. // an empty hostname needs to be read from the raw parameters
  134. $dbHost = $input->getParameterOption('--database-host', '');
  135. } else {
  136. $dbHost = $input->getOption('database-host');
  137. }
  138. if ($dbPort) {
  139. // Append the port to the host so it is the same as in the config (there is no dbport config)
  140. $dbHost .= ':' . $dbPort;
  141. }
  142. if ($input->hasParameterOption('--database-pass')) {
  143. $dbPass = (string) $input->getOption('database-pass');
  144. }
  145. $adminLogin = $input->getOption('admin-user');
  146. $adminPassword = $input->getOption('admin-pass');
  147. $adminEmail = $input->getOption('admin-email');
  148. $dataDir = $input->getOption('data-dir');
  149. if ($db !== 'sqlite') {
  150. if (is_null($dbUser)) {
  151. throw new InvalidArgumentException("Database user not provided.");
  152. }
  153. if (is_null($dbName)) {
  154. throw new InvalidArgumentException("Database name not provided.");
  155. }
  156. if (is_null($dbPass)) {
  157. /** @var QuestionHelper $helper */
  158. $helper = $this->getHelper('question');
  159. $question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
  160. $question->setHidden(true);
  161. $question->setHiddenFallback(false);
  162. $dbPass = $helper->ask($input, $output, $question);
  163. }
  164. }
  165. if (is_null($adminPassword)) {
  166. /** @var QuestionHelper $helper */
  167. $helper = $this->getHelper('question');
  168. $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
  169. $question->setHidden(true);
  170. $question->setHiddenFallback(false);
  171. $adminPassword = $helper->ask($input, $output, $question);
  172. }
  173. if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
  174. throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
  175. }
  176. $options = [
  177. 'dbtype' => $db,
  178. 'dbuser' => $dbUser,
  179. 'dbpass' => $dbPass,
  180. 'dbname' => $dbName,
  181. 'dbhost' => $dbHost,
  182. 'adminlogin' => $adminLogin,
  183. 'adminpass' => $adminPassword,
  184. 'adminemail' => $adminEmail,
  185. 'directory' => $dataDir
  186. ];
  187. if ($db === 'oci') {
  188. $options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
  189. }
  190. return $options;
  191. }
  192. /**
  193. * @param OutputInterface $output
  194. * @param $errors
  195. */
  196. protected function printErrors(OutputInterface $output, $errors) {
  197. foreach ($errors as $error) {
  198. if (is_array($error)) {
  199. $output->writeln('<error>' . $error['error'] . '</error>');
  200. if (isset($error['hint']) && !empty($error['hint'])) {
  201. $output->writeln('<info> -> ' . $error['hint'] . '</info>');
  202. }
  203. if (isset($error['exception']) && $error['exception'] instanceof Throwable) {
  204. $this->printThrowable($output, $error['exception']);
  205. }
  206. } else {
  207. $output->writeln('<error>' . $error . '</error>');
  208. }
  209. }
  210. }
  211. private function printThrowable(OutputInterface $output, Throwable $t): void {
  212. $output->write('<info>Trace: ' . $t->getTraceAsString() . '</info>');
  213. $output->writeln('');
  214. if ($t->getPrevious() !== null) {
  215. $output->writeln('');
  216. $output->writeln('<info>Previous: ' . get_class($t->getPrevious()) . ': ' . $t->getPrevious()->getMessage() . '</info>');
  217. $this->printThrowable($output, $t->getPrevious());
  218. }
  219. }
  220. }