You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
6.1 KiB

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