Install.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christian Kampka <christian@kampka.net>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Thomas Pulzer <t.pulzer@kniel.de>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Core\Command\Maintenance;
  29. use InvalidArgumentException;
  30. use OC\Setup;
  31. use OCP\IConfig;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Helper\QuestionHelper;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Input\InputOption;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. use Symfony\Component\Console\Question\Question;
  38. class Install extends Command {
  39. /**
  40. * @var IConfig
  41. */
  42. private $config;
  43. public function __construct(IConfig $config) {
  44. parent::__construct();
  45. $this->config = $config;
  46. }
  47. protected function configure() {
  48. $this
  49. ->setName('maintenance:install')
  50. ->setDescription('install Nextcloud')
  51. ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
  52. ->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
  53. ->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
  54. ->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
  55. ->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database')
  56. ->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
  57. ->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_)', null)
  58. ->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin')
  59. ->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
  60. ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
  61. }
  62. protected function execute(InputInterface $input, OutputInterface $output) {
  63. // validate the environment
  64. $server = \OC::$server;
  65. $setupHelper = new Setup($this->config, $server->getIniWrapper(),
  66. $server->getL10N('lib'), $server->getThemingDefaults(), $server->getLogger(),
  67. $server->getSecureRandom());
  68. $sysInfo = $setupHelper->getSystemInfo(true);
  69. $errors = $sysInfo['errors'];
  70. if (count($errors) > 0) {
  71. $this->printErrors($output, $errors);
  72. // ignore the OS X setup warning
  73. if(count($errors) !== 1 ||
  74. (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! ') {
  75. return 1;
  76. }
  77. }
  78. // validate user input
  79. $options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
  80. // perform installation
  81. $errors = $setupHelper->install($options);
  82. if (count($errors) > 0) {
  83. $this->printErrors($output, $errors);
  84. return 1;
  85. }
  86. $output->writeln("Nextcloud was successfully installed");
  87. return 0;
  88. }
  89. /**
  90. * @param InputInterface $input
  91. * @param OutputInterface $output
  92. * @param string[] $supportedDatabases
  93. * @return array
  94. */
  95. protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
  96. $db = strtolower($input->getOption('database'));
  97. if (!in_array($db, $supportedDatabases)) {
  98. throw new InvalidArgumentException("Database <$db> is not supported.");
  99. }
  100. $dbUser = $input->getOption('database-user');
  101. $dbPass = $input->getOption('database-pass');
  102. $dbName = $input->getOption('database-name');
  103. $dbPort = $input->getOption('database-port');
  104. if ($db === 'oci') {
  105. // an empty hostname needs to be read from the raw parameters
  106. $dbHost = $input->getParameterOption('--database-host', '');
  107. } else {
  108. $dbHost = $input->getOption('database-host');
  109. }
  110. $dbTablePrefix = 'oc_';
  111. if ($input->hasParameterOption('--database-table-prefix')) {
  112. $dbTablePrefix = (string) $input->getOption('database-table-prefix');
  113. $dbTablePrefix = trim($dbTablePrefix);
  114. }
  115. if ($input->hasParameterOption('--database-pass')) {
  116. $dbPass = (string) $input->getOption('database-pass');
  117. }
  118. $adminLogin = $input->getOption('admin-user');
  119. $adminPassword = $input->getOption('admin-pass');
  120. $dataDir = $input->getOption('data-dir');
  121. if ($db !== 'sqlite') {
  122. if (is_null($dbUser)) {
  123. throw new InvalidArgumentException("Database user not provided.");
  124. }
  125. if (is_null($dbName)) {
  126. throw new InvalidArgumentException("Database name not provided.");
  127. }
  128. if (is_null($dbPass)) {
  129. /** @var QuestionHelper $helper */
  130. $helper = $this->getHelper('question');
  131. $question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
  132. $question->setHidden(true);
  133. $question->setHiddenFallback(false);
  134. $dbPass = $helper->ask($input, $output, $question);
  135. }
  136. }
  137. if (is_null($adminPassword)) {
  138. /** @var QuestionHelper $helper */
  139. $helper = $this->getHelper('question');
  140. $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
  141. $question->setHidden(true);
  142. $question->setHiddenFallback(false);
  143. $adminPassword = $helper->ask($input, $output, $question);
  144. }
  145. $options = [
  146. 'dbtype' => $db,
  147. 'dbuser' => $dbUser,
  148. 'dbpass' => $dbPass,
  149. 'dbname' => $dbName,
  150. 'dbhost' => $dbHost,
  151. 'dbport' => $dbPort,
  152. 'dbtableprefix' => $dbTablePrefix,
  153. 'adminlogin' => $adminLogin,
  154. 'adminpass' => $adminPassword,
  155. 'directory' => $dataDir
  156. ];
  157. return $options;
  158. }
  159. /**
  160. * @param OutputInterface $output
  161. * @param $errors
  162. */
  163. protected function printErrors(OutputInterface $output, $errors) {
  164. foreach ($errors as $error) {
  165. if (is_array($error)) {
  166. $output->writeln('<error>' . (string)$error['error'] . '</error>');
  167. $output->writeln('<info> -> ' . (string)$error['hint'] . '</info>');
  168. } else {
  169. $output->writeln('<error>' . (string)$error . '</error>');
  170. }
  171. }
  172. }
  173. }