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 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('admin-email', null, InputOption::VALUE_OPTIONAL, 'E-Mail of the admin account')
  66. ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
  67. }
  68. protected function execute(InputInterface $input, OutputInterface $output) {
  69. // validate the environment
  70. $server = \OC::$server;
  71. $setupHelper = new Setup(
  72. $this->config,
  73. $server->getIniWrapper(),
  74. $server->getL10N('lib'),
  75. $server->query(Defaults::class),
  76. $server->getLogger(),
  77. $server->getSecureRandom(),
  78. \OC::$server->query(Installer::class)
  79. );
  80. $sysInfo = $setupHelper->getSystemInfo(true);
  81. $errors = $sysInfo['errors'];
  82. if (count($errors) > 0) {
  83. $this->printErrors($output, $errors);
  84. // ignore the OS X setup warning
  85. if(count($errors) !== 1 ||
  86. (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! ') {
  87. return 1;
  88. }
  89. }
  90. // validate user input
  91. $options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
  92. // perform installation
  93. $errors = $setupHelper->install($options);
  94. if (count($errors) > 0) {
  95. $this->printErrors($output, $errors);
  96. return 1;
  97. }
  98. $output->writeln("Nextcloud was successfully installed");
  99. return 0;
  100. }
  101. /**
  102. * @param InputInterface $input
  103. * @param OutputInterface $output
  104. * @param string[] $supportedDatabases
  105. * @return array
  106. */
  107. protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
  108. $db = strtolower($input->getOption('database'));
  109. if (!in_array($db, $supportedDatabases)) {
  110. throw new InvalidArgumentException("Database <$db> is not supported.");
  111. }
  112. $dbUser = $input->getOption('database-user');
  113. $dbPass = $input->getOption('database-pass');
  114. $dbName = $input->getOption('database-name');
  115. $dbPort = $input->getOption('database-port');
  116. if ($db === 'oci') {
  117. // an empty hostname needs to be read from the raw parameters
  118. $dbHost = $input->getParameterOption('--database-host', '');
  119. } else {
  120. $dbHost = $input->getOption('database-host');
  121. }
  122. $dbTablePrefix = 'oc_';
  123. if ($input->hasParameterOption('--database-table-prefix')) {
  124. $dbTablePrefix = (string) $input->getOption('database-table-prefix');
  125. $dbTablePrefix = trim($dbTablePrefix);
  126. }
  127. if ($input->hasParameterOption('--database-pass')) {
  128. $dbPass = (string) $input->getOption('database-pass');
  129. }
  130. $adminLogin = $input->getOption('admin-user');
  131. $adminPassword = $input->getOption('admin-pass');
  132. $adminEmail = $input->getOption('admin-email');
  133. $dataDir = $input->getOption('data-dir');
  134. if ($db !== 'sqlite') {
  135. if (is_null($dbUser)) {
  136. throw new InvalidArgumentException("Database user not provided.");
  137. }
  138. if (is_null($dbName)) {
  139. throw new InvalidArgumentException("Database name not provided.");
  140. }
  141. if (is_null($dbPass)) {
  142. /** @var QuestionHelper $helper */
  143. $helper = $this->getHelper('question');
  144. $question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
  145. $question->setHidden(true);
  146. $question->setHiddenFallback(false);
  147. $dbPass = $helper->ask($input, $output, $question);
  148. }
  149. }
  150. if (is_null($adminPassword)) {
  151. /** @var QuestionHelper $helper */
  152. $helper = $this->getHelper('question');
  153. $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
  154. $question->setHidden(true);
  155. $question->setHiddenFallback(false);
  156. $adminPassword = $helper->ask($input, $output, $question);
  157. }
  158. if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
  159. throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
  160. }
  161. $options = [
  162. 'dbtype' => $db,
  163. 'dbuser' => $dbUser,
  164. 'dbpass' => $dbPass,
  165. 'dbname' => $dbName,
  166. 'dbhost' => $dbHost,
  167. 'dbport' => $dbPort,
  168. 'dbtableprefix' => $dbTablePrefix,
  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>' . (string)$error['error'] . '</error>');
  187. $output->writeln('<info> -> ' . (string)$error['hint'] . '</info>');
  188. } else {
  189. $output->writeln('<error>' . (string)$error . '</error>');
  190. }
  191. }
  192. }
  193. }