Install.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Symfony\Component\Console\Command\Command;
  39. use Symfony\Component\Console\Helper\QuestionHelper;
  40. use Symfony\Component\Console\Input\InputInterface;
  41. use Symfony\Component\Console\Input\InputOption;
  42. use Symfony\Component\Console\Output\OutputInterface;
  43. use Symfony\Component\Console\Question\Question;
  44. class Install extends Command {
  45. /** @var SystemConfig */
  46. private $config;
  47. /** @var IniGetWrapper */
  48. private $iniGetWrapper;
  49. public function __construct(SystemConfig $config, IniGetWrapper $iniGetWrapper) {
  50. parent::__construct();
  51. $this->config = $config;
  52. $this->iniGetWrapper = $iniGetWrapper;
  53. }
  54. protected function configure() {
  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, 'User name 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, 'User name 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. $server = \OC::$server;
  73. $setupHelper = new Setup(
  74. $this->config,
  75. $this->iniGetWrapper,
  76. $server->getL10N('lib'),
  77. $server->query(Defaults::class),
  78. $server->getLogger(),
  79. $server->getSecureRandom(),
  80. \OC::$server->query(Installer::class)
  81. );
  82. $sysInfo = $setupHelper->getSystemInfo(true);
  83. $errors = $sysInfo['errors'];
  84. if (count($errors) > 0) {
  85. $this->printErrors($output, $errors);
  86. // ignore the OS X setup warning
  87. if (count($errors) !== 1 ||
  88. (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! ') {
  89. return 1;
  90. }
  91. }
  92. // validate user input
  93. $options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
  94. // perform installation
  95. $errors = $setupHelper->install($options);
  96. if (count($errors) > 0) {
  97. $this->printErrors($output, $errors);
  98. return 1;
  99. }
  100. $output->writeln("Nextcloud was successfully installed");
  101. return 0;
  102. }
  103. /**
  104. * @param InputInterface $input
  105. * @param OutputInterface $output
  106. * @param string[] $supportedDatabases
  107. * @return array
  108. */
  109. protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
  110. $db = strtolower($input->getOption('database'));
  111. if (!in_array($db, $supportedDatabases)) {
  112. throw new InvalidArgumentException("Database <$db> is not supported.");
  113. }
  114. $dbUser = $input->getOption('database-user');
  115. $dbPass = $input->getOption('database-pass');
  116. $dbName = $input->getOption('database-name');
  117. $dbPort = $input->getOption('database-port');
  118. if ($db === 'oci') {
  119. // an empty hostname needs to be read from the raw parameters
  120. $dbHost = $input->getParameterOption('--database-host', '');
  121. } else {
  122. $dbHost = $input->getOption('database-host');
  123. }
  124. if ($dbPort) {
  125. // Append the port to the host so it is the same as in the config (there is no dbport config)
  126. $dbHost .= ':' . $dbPort;
  127. }
  128. if ($input->hasParameterOption('--database-pass')) {
  129. $dbPass = (string) $input->getOption('database-pass');
  130. }
  131. $adminLogin = $input->getOption('admin-user');
  132. $adminPassword = $input->getOption('admin-pass');
  133. $adminEmail = $input->getOption('admin-email');
  134. $dataDir = $input->getOption('data-dir');
  135. if ($db !== 'sqlite') {
  136. if (is_null($dbUser)) {
  137. throw new InvalidArgumentException("Database user not provided.");
  138. }
  139. if (is_null($dbName)) {
  140. throw new InvalidArgumentException("Database name not provided.");
  141. }
  142. if (is_null($dbPass)) {
  143. /** @var QuestionHelper $helper */
  144. $helper = $this->getHelper('question');
  145. $question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
  146. $question->setHidden(true);
  147. $question->setHiddenFallback(false);
  148. $dbPass = $helper->ask($input, $output, $question);
  149. }
  150. }
  151. if (is_null($adminPassword)) {
  152. /** @var QuestionHelper $helper */
  153. $helper = $this->getHelper('question');
  154. $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
  155. $question->setHidden(true);
  156. $question->setHiddenFallback(false);
  157. $adminPassword = $helper->ask($input, $output, $question);
  158. }
  159. if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
  160. throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
  161. }
  162. $options = [
  163. 'dbtype' => $db,
  164. 'dbuser' => $dbUser,
  165. 'dbpass' => $dbPass,
  166. 'dbname' => $dbName,
  167. 'dbhost' => $dbHost,
  168. 'adminlogin' => $adminLogin,
  169. 'adminpass' => $adminPassword,
  170. 'adminemail' => $adminEmail,
  171. 'directory' => $dataDir
  172. ];
  173. if ($db === 'oci') {
  174. $options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
  175. }
  176. return $options;
  177. }
  178. /**
  179. * @param OutputInterface $output
  180. * @param $errors
  181. */
  182. protected function printErrors(OutputInterface $output, $errors) {
  183. foreach ($errors as $error) {
  184. if (is_array($error)) {
  185. $output->writeln('<error>' . (string)$error['error'] . '</error>');
  186. $output->writeln('<info> -> ' . (string)$error['hint'] . '</info>');
  187. } else {
  188. $output->writeln('<error>' . (string)$error . '</error>');
  189. }
  190. }
  191. }
  192. }