Install.php 8.3 KB

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