Passed
Push — master ( 5bb75c...645e3e )
by Roeland
31:07 queued 15:37
created

Install::printThrowable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Daniel Hansson <[email protected]>
8
 * @author Daniel Kesselberg <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Jörn Friedrich Dreyer <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 * @author Thomas Pulzer <[email protected]>
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
32
namespace OC\Core\Command\Maintenance;
33
34
use bantu\IniGetWrapper\IniGetWrapper;
35
use InvalidArgumentException;
36
use OC\Installer;
37
use OC\Setup;
38
use OC\SystemConfig;
39
use OCP\Defaults;
40
use Symfony\Component\Console\Command\Command;
41
use Symfony\Component\Console\Helper\QuestionHelper;
42
use Symfony\Component\Console\Input\InputInterface;
43
use Symfony\Component\Console\Input\InputOption;
44
use Symfony\Component\Console\Output\OutputInterface;
45
use Symfony\Component\Console\Question\Question;
46
use Throwable;
47
use function get_class;
48
49
class Install extends Command {
50
51
	/** @var SystemConfig */
52
	private $config;
53
	/** @var IniGetWrapper  */
54
	private $iniGetWrapper;
55
56
	public function __construct(SystemConfig $config, IniGetWrapper $iniGetWrapper) {
57
		parent::__construct();
58
		$this->config = $config;
59
		$this->iniGetWrapper = $iniGetWrapper;
60
	}
61
62
	protected function configure() {
63
		$this
64
			->setName('maintenance:install')
65
			->setDescription('install Nextcloud')
66
			->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
67
			->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
68
			->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
69
			->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
70
			->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database')
71
			->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
72
			->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null)
73
			->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin')
74
			->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
75
			->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'E-Mail of the admin account')
76
			->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
77
	}
78
79
	protected function execute(InputInterface $input, OutputInterface $output): int {
80
81
		// validate the environment
82
		$server = \OC::$server;
83
		$setupHelper = new Setup(
84
			$this->config,
85
			$this->iniGetWrapper,
86
			$server->getL10N('lib'),
87
			$server->query(Defaults::class),
88
			$server->getLogger(),
89
			$server->getSecureRandom(),
90
			\OC::$server->query(Installer::class)
91
		);
92
		$sysInfo = $setupHelper->getSystemInfo(true);
93
		$errors = $sysInfo['errors'];
94
		if (count($errors) > 0) {
95
			$this->printErrors($output, $errors);
96
97
			// ignore the OS X setup warning
98
			if (count($errors) !== 1 ||
99
				(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! ') {
100
				return 1;
101
			}
102
		}
103
104
		// validate user input
105
		$options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
106
107
		// perform installation
108
		$errors = $setupHelper->install($options);
109
		if (count($errors) > 0) {
110
			$this->printErrors($output, $errors);
111
			return 1;
112
		}
113
		$output->writeln("Nextcloud was successfully installed");
114
		return 0;
115
	}
116
117
	/**
118
	 * @param InputInterface $input
119
	 * @param OutputInterface $output
120
	 * @param string[] $supportedDatabases
121
	 * @return array
122
	 */
123
	protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
124
		$db = strtolower($input->getOption('database'));
125
126
		if (!in_array($db, $supportedDatabases)) {
127
			throw new InvalidArgumentException("Database <$db> is not supported.");
128
		}
129
130
		$dbUser = $input->getOption('database-user');
131
		$dbPass = $input->getOption('database-pass');
132
		$dbName = $input->getOption('database-name');
133
		$dbPort = $input->getOption('database-port');
134
		if ($db === 'oci') {
135
			// an empty hostname needs to be read from the raw parameters
136
			$dbHost = $input->getParameterOption('--database-host', '');
137
		} else {
138
			$dbHost = $input->getOption('database-host');
139
		}
140
		if ($dbPort) {
141
			// Append the port to the host so it is the same as in the config (there is no dbport config)
142
			$dbHost .= ':' . $dbPort;
0 ignored issues
show
Bug introduced by
Are you sure $dbPort of type string|string[]|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
			$dbHost .= ':' . /** @scrutinizer ignore-type */ $dbPort;
Loading history...
143
		}
144
		if ($input->hasParameterOption('--database-pass')) {
145
			$dbPass = (string) $input->getOption('database-pass');
146
		}
147
		$adminLogin = $input->getOption('admin-user');
148
		$adminPassword = $input->getOption('admin-pass');
149
		$adminEmail = $input->getOption('admin-email');
150
		$dataDir = $input->getOption('data-dir');
151
152
		if ($db !== 'sqlite') {
153
			if (is_null($dbUser)) {
154
				throw new InvalidArgumentException("Database user not provided.");
155
			}
156
			if (is_null($dbName)) {
157
				throw new InvalidArgumentException("Database name not provided.");
158
			}
159
			if (is_null($dbPass)) {
160
				/** @var QuestionHelper $helper */
161
				$helper = $this->getHelper('question');
162
				$question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
0 ignored issues
show
Bug introduced by
Are you sure $dbUser of type boolean|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

162
				$question = new Question('What is the password to access the database with user <'./** @scrutinizer ignore-type */ $dbUser.'>?');
Loading history...
163
				$question->setHidden(true);
164
				$question->setHiddenFallback(false);
165
				$dbPass = $helper->ask($input, $output, $question);
166
			}
167
		}
168
169
		if (is_null($adminPassword)) {
170
			/** @var QuestionHelper $helper */
171
			$helper = $this->getHelper('question');
172
			$question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
173
			$question->setHidden(true);
174
			$question->setHiddenFallback(false);
175
			$adminPassword = $helper->ask($input, $output, $question);
176
		}
177
178
		if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
179
			throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
0 ignored issues
show
Bug introduced by
Are you sure $adminEmail of type boolean|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

179
			throw new InvalidArgumentException('Invalid e-mail-address <' . /** @scrutinizer ignore-type */ $adminEmail . '> for <' . $adminLogin . '>.');
Loading history...
180
		}
181
182
		$options = [
183
			'dbtype' => $db,
184
			'dbuser' => $dbUser,
185
			'dbpass' => $dbPass,
186
			'dbname' => $dbName,
187
			'dbhost' => $dbHost,
188
			'adminlogin' => $adminLogin,
189
			'adminpass' => $adminPassword,
190
			'adminemail' => $adminEmail,
191
			'directory' => $dataDir
192
		];
193
		if ($db === 'oci') {
194
			$options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
195
		}
196
		return $options;
197
	}
198
199
	/**
200
	 * @param OutputInterface $output
201
	 * @param $errors
202
	 */
203
	protected function printErrors(OutputInterface $output, $errors) {
204
		foreach ($errors as $error) {
205
			if (is_array($error)) {
206
				$output->writeln('<error>' . $error['error'] . '</error>');
207
				if (isset($error['hint']) && !empty($error['hint'])) {
208
					$output->writeln('<info> -> ' . $error['hint'] . '</info>');
209
				}
210
				if (isset($error['exception']) && $error['exception'] instanceof Throwable) {
211
					$this->printThrowable($output, $error['exception']);
212
				}
213
			} else {
214
				$output->writeln('<error>' . $error . '</error>');
215
			}
216
		}
217
	}
218
219
	private function printThrowable(OutputInterface $output, Throwable $t): void {
220
		$output->write('<info>Trace: ' . $t->getTraceAsString() . '</info>');
221
		$output->writeln('');
222
		if ($t->getPrevious() !== null) {
223
			$output->writeln('');
224
			$output->writeln('<info>Previous: ' . get_class($t->getPrevious()) . ': ' . $t->getPrevious()->getMessage() . '</info>');
225
			$this->printThrowable($output, $t->getPrevious());
226
		}
227
	}
228
}
229