Install.php 7.1 KB

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