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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. $output->writeln("Nextcloud was successfully installed");
  102. return 0;
  103. }
  104. /**
  105. * @param InputInterface $input
  106. * @param OutputInterface $output
  107. * @param string[] $supportedDatabases
  108. * @return array
  109. */
  110. protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
  111. $db = strtolower($input->getOption('database'));
  112. if (!in_array($db, $supportedDatabases)) {
  113. throw new InvalidArgumentException("Database <$db> is not supported.");
  114. }
  115. $dbUser = $input->getOption('database-user');
  116. $dbPass = $input->getOption('database-pass');
  117. $dbName = $input->getOption('database-name');
  118. $dbPort = $input->getOption('database-port');
  119. if ($db === 'oci') {
  120. // an empty hostname needs to be read from the raw parameters
  121. $dbHost = $input->getParameterOption('--database-host', '');
  122. } else {
  123. $dbHost = $input->getOption('database-host');
  124. }
  125. if ($dbPort) {
  126. // Append the port to the host so it is the same as in the config (there is no dbport config)
  127. $dbHost .= ':' . $dbPort;
  128. }
  129. if ($input->hasParameterOption('--database-pass')) {
  130. $dbPass = (string) $input->getOption('database-pass');
  131. }
  132. $adminLogin = $input->getOption('admin-user');
  133. $adminPassword = $input->getOption('admin-pass');
  134. $adminEmail = $input->getOption('admin-email');
  135. $dataDir = $input->getOption('data-dir');
  136. if ($db !== 'sqlite') {
  137. if (is_null($dbUser)) {
  138. throw new InvalidArgumentException("Database user not provided.");
  139. }
  140. if (is_null($dbName)) {
  141. throw new InvalidArgumentException("Database name not provided.");
  142. }
  143. if (is_null($dbPass)) {
  144. /** @var QuestionHelper $helper */
  145. $helper = $this->getHelper('question');
  146. $question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
  147. $question->setHidden(true);
  148. $question->setHiddenFallback(false);
  149. $dbPass = $helper->ask($input, $output, $question);
  150. }
  151. }
  152. if (is_null($adminPassword)) {
  153. /** @var QuestionHelper $helper */
  154. $helper = $this->getHelper('question');
  155. $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
  156. $question->setHidden(true);
  157. $question->setHiddenFallback(false);
  158. $adminPassword = $helper->ask($input, $output, $question);
  159. }
  160. if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
  161. throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
  162. }
  163. $options = [
  164. 'dbtype' => $db,
  165. 'dbuser' => $dbUser,
  166. 'dbpass' => $dbPass,
  167. 'dbname' => $dbName,
  168. 'dbhost' => $dbHost,
  169. 'adminlogin' => $adminLogin,
  170. 'adminpass' => $adminPassword,
  171. 'adminemail' => $adminEmail,
  172. 'directory' => $dataDir
  173. ];
  174. if ($db === 'oci') {
  175. $options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
  176. }
  177. return $options;
  178. }
  179. /**
  180. * @param OutputInterface $output
  181. * @param $errors
  182. */
  183. protected function printErrors(OutputInterface $output, $errors) {
  184. foreach ($errors as $error) {
  185. if (is_array($error)) {
  186. $output->writeln('<error>' . $error['error'] . '</error>');
  187. if (isset($error['hint']) && !empty($error['hint'])) {
  188. $output->writeln('<info> -> ' . $error['hint'] . '</info>');
  189. }
  190. if (isset($error['exception']) && $error['exception'] instanceof Throwable) {
  191. $this->printThrowable($output, $error['exception']);
  192. }
  193. } else {
  194. $output->writeln('<error>' . $error . '</error>');
  195. }
  196. }
  197. }
  198. private function printThrowable(OutputInterface $output, Throwable $t): void {
  199. $output->write('<info>Trace: ' . $t->getTraceAsString() . '</info>');
  200. $output->writeln('');
  201. if ($t->getPrevious() !== null) {
  202. $output->writeln('');
  203. $output->writeln('<info>Previous: ' . get_class($t->getPrevious()) . ': ' . $t->getPrevious()->getMessage() . '</info>');
  204. $this->printThrowable($output, $t->getPrevious());
  205. }
  206. }
  207. }