Skip to content

Commit

Permalink
Implement occ status command via return codes (Fixes: nextcloud#35830)
Browse files Browse the repository at this point in the history
Running `./occ status -e` will write any output. However, it will:

exit 0 during normal operation,
exit 1 when in maintenance mode,
exit 2 when `./occ upgrade` is needed.
  • Loading branch information
Lee Garrett committed Dec 22, 2022
1 parent 5b64b81 commit e7b514c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
30 changes: 27 additions & 3 deletions core/Command/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\IConfig;
use OCP\Util;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Status extends Base {
Expand All @@ -47,22 +48,45 @@ protected function configure() {

$this
->setDescription('show some status information')
;
->addOption(
'exit-code',
'e',
InputOption::VALUE_NONE,
'exit with 0 if running in normal mode, 1 when in maintenance mode, 2 when `./occ upgrade` is needed. Does not write any output to STDOUT.'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {

$maintenanceMode = $this->config->getSystemValueBool('maintenance', false);
$needUpgrade = Util::needUpgrade();

if ($input->getOption('exit-code')) {

# check maintenance mode first
if ($maintenanceMode === true) {
return 1;

# check DB upgrade next
} elseif ($needUpgrade == true) {
return 2;
} else {
return 0;
}
} else {
$values = [
'installed' => $this->config->getSystemValueBool('installed', false),
'version' => implode('.', Util::getVersion()),
'versionstring' => OC_Util::getVersionString(),
'edition' => '',
'maintenance' => $this->config->getSystemValueBool('maintenance', false),
'needsDbUpgrade' => Util::needUpgrade(),
'maintenance' => $maintenanceMode,
'needsDbUpgrade' => $needUpgrade,
'productname' => $this->themingDefaults->getProductName(),
'extendedSupport' => Util::hasExtendedSupport()
];

$this->writeArrayInOutputFormat($input, $output, $values);
}
return 0;
}
}
3 changes: 2 additions & 1 deletion lib/private/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private function writeMaintenanceModeInfo(
InputInterface $input, ConsoleOutputInterface $output
) {
if ($input->getArgument('command') !== '_completion'
&& $input->getArgument('command') !== 'maintenance:mode') {
&& $input->getArgument('command') !== 'maintenance:mode'
&& $input->getArgument('command') !== 'status' ) {
$errOutput = $output->getErrorOutput();
$errOutput->writeln(
'<comment>Nextcloud is in maintenance mode, hence the database isn\'t accessible.' . PHP_EOL .
Expand Down

0 comments on commit e7b514c

Please sign in to comment.