Install.php 7.7 KB

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