Skip to content

Commit

Permalink
ASK what to do? (download, upgrade, abort) More stability if occ fail…
Browse files Browse the repository at this point in the history
…s early. Separate description and message.
  • Loading branch information
VicDeo committed Dec 15, 2015
1 parent c1c24fc commit 9f80fee
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 39 deletions.
7 changes: 6 additions & 1 deletion app/config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Owncloud\Updater\Utils\FilesystemHelper;
use Owncloud\Updater\Utils\Locator;
use Owncloud\Updater\Utils\OccRunner;
use Owncloud\Updater\Utils\Registry;
use Owncloud\Updater\Command\BackupDataCommand;
use Owncloud\Updater\Command\BackupDbCommand;
use Owncloud\Updater\Command\CheckpointCommand;
Expand Down Expand Up @@ -73,6 +74,10 @@
return new OccRunner($c['utils.locator']);
};

$c['utils.registry'] = function($c){
return new Registry();
};

$c['utils.appmanager'] = function($c){
return new AppManager($c['utils.occrunner']);
};
Expand Down Expand Up @@ -187,7 +192,7 @@
};

$c['application'] = function($c){
$application = new Application('ownCloud updater', '0.1');
$application = new Application('ownCloud updater', '1.0');
$application->setContainer($c);
$application->addCommands($c['commands']);
return $application;
Expand Down
9 changes: 8 additions & 1 deletion src/Command/CheckSystemCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

class CheckSystemCommand extends Command {

protected $message = 'Checking system health.';

protected function configure(){
$this
->setName('upgrade:checkSystem')
Expand All @@ -40,6 +42,8 @@ protected function configure(){
protected function execute(InputInterface $input, OutputInterface $output){
$locator = $this->container['utils.locator'];
$fsHelper = $this->container['utils.filesystemhelper'];
$occRunner = $this->container['utils.occrunner'];

$collection = new Collection();

$rootDirItems= $locator->getRootDirItems();
Expand All @@ -61,7 +65,10 @@ protected function execute(InputInterface $input, OutputInterface $output){

if (count($notReadableFiles) || count($notWritableFiles)){
$output->writeln('<info>Please check if owner and permissions fot these files are correct.</info>');
$output->writeln('See https://doc.owncloud.org/server/9.0/admin_manual/installation/installation_wizard.html#strong-perms-label for details.</info>');
$output->writeln('<info>See https://doc.owncloud.org/server/9.0/admin_manual/installation/installation_wizard.html#strong-perms-label for details.</info>');
return 2;
} else {
$output->writeln(' - file permissions are ok.');
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@
namespace Owncloud\Updater\Command;

class Command extends \Symfony\Component\Console\Command\Command{
/**
* @var \Pimple\Container
*/
protected $container;

/**
* @var string
*/
protected $message = '';

public function getMessage(){
return $this->message;
}

public function setContainer($container){
$this->container = $container;
}
Expand Down
45 changes: 25 additions & 20 deletions src/Command/DetectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use GuzzleHttp\Event\ProgressEvent;
use Owncloud\Updater\Utils\Fetcher;
use Owncloud\Updater\Utils\Feed;
use Owncloud\Updater\Utils\ConfigReader;
use Owncloud\Updater\Utils\ZipExtractor;

class DetectCommand extends Command {

Expand Down Expand Up @@ -70,6 +70,9 @@ protected function configure(){
}

protected function execute(InputInterface $input, OutputInterface $output){
$registry = $this->container['utils.registry'];
$registry->set('feed', false);

$locator = $this->container['utils.locator'];
$fsHelper = $this->container['utils.filesystemhelper'];
try{
Expand All @@ -84,6 +87,20 @@ protected function execute(InputInterface $input, OutputInterface $output){
$feed = $this->fetcher->getFeed();
if ($feed->isValid()){
$output->writeln($feed->getVersionString() . ' is found online');

$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
'What would you do next?',
['download', 'upgrade', 'abort'],
'1'
);
$action = $helper->ask($input, $output, $question);

if ($action === 'abort'){
$output->writeln('Abort has been choosed. Exiting.');
return 128;
}

$path = $this->fetcher->getBaseDownloadPath($feed);
$fileExists = $this->isCached($feed, $output);
if (!$fileExists){
Expand All @@ -102,34 +119,22 @@ protected function execute(InputInterface $input, OutputInterface $output){
$fileExists = true;
}
}
if ($action === 'download'){
$output->writeln('Downloading has been completed. Exiting.');
return 64;
}
if ($fileExists){
$fullExtractionPath = $locator->getExtractionBaseDir() . '/' . $feed->getVersion();
if (!file_exists($fullExtractionPath)){
try {
$fsHelper->mkdir($fullExtractionPath, true);
} catch (\Exception $ex) {
$output->writeln('Unable create directory ' . $fullExtractionPath);
}
}
$output->writeln('Extracting source into ' . $fullExtractionPath);

$zipExtractor = new ZipExtractor($path, $fullExtractionPath);
try {
$zipExtractor->extract();
} catch (\Exception $ex) {
$output->writeln('Extraction has been failed');
$fsHelper->removeIfExists($locator->getExtractionBaseDir());
}
$registry->set('feed', $feed);
}
} else {
$output->writeln('No updates found online.');
if ($input->getOption('exit-if-none')){
exit(4);
return 4;
}
}
} catch (\Exception $e){
$this->getApplication()->getLogger()->error($e->getMessage());
exit(2);
return 2;
}
}

Expand Down
42 changes: 36 additions & 6 deletions src/Command/ExecuteCoreUpgradeScriptsCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
Expand Down Expand Up @@ -26,6 +27,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Owncloud\Updater\Utils\OccRunner;
use Owncloud\Updater\Utils\ZipExtractor;

class ExecuteCoreUpgradeScriptsCommand extends Command {

Expand All @@ -42,17 +44,45 @@ public function __construct($occRunner){
protected function configure(){
$this
->setName('upgrade:executeCoreUpgradeScripts')
->setDescription('execute core upgrade scripts
[danger, might take long]')
;
->setDescription('execute core upgrade scripts [danger, might take long]');
}

protected function execute(InputInterface $input, OutputInterface $output){
$locator = $this->container['utils.locator'];
$fsHelper = $this->container['utils.filesystemhelper'];

$plain = $this->occRunner->run('upgrade');
$output->writeln($plain);
$registry = $this->container['utils.registry'];
$fetcher = $this->container['utils.fetcher'];

$feed = $registry->get('feed');

if ($feed){
$path = $fetcher->getBaseDownloadPath($feed);
$fullExtractionPath = $locator->getExtractionBaseDir() . '/' . $feed->getVersion();

if (!file_exists($fullExtractionPath)){
try{
$fsHelper->mkdir($fullExtractionPath, true);
} catch (\Exception $ex){

$output->writeln('Unable create directory ' . $fullExtractionPath);
}
}
$output->writeln('Extracting source into ' . $fullExtractionPath);

$zipExtractor = new ZipExtractor($path, $fullExtractionPath);
try{
$zipExtractor->extract();
} catch (\Exception $e){
$output->writeln('Extraction has been failed');
$fsHelper->removeIfExists($locator->getExtractionBaseDir());
throw $e;
}

$plain = $this->occRunner->run('upgrade');
$output->writeln($plain);

}

}

}
4 changes: 2 additions & 2 deletions src/Command/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StartCommand extends Command {
[ 'command' => 'upgrade:info'],
[ 'command' => 'upgrade:checkSystem'],
[ 'command' => 'upgrade:detect', '--exit-if-none' => '1'],
[ 'command' => 'upgrade:maintenanceMode', '--on' => '1'],
//[ 'command' => 'upgrade:maintenanceMode', '--on' => '1'],
[ 'command' => 'upgrade:backupDb'],
[ 'command' => 'upgrade:backupData'],
[ 'command' => 'upgrade:preUpgradeRepair'],
Expand All @@ -47,7 +47,7 @@ class StartCommand extends Command {
[ 'command' => 'upgrade:postUpgradeRepair'],
[ 'command' => 'upgrade:restartWebServer'],
[ 'command' => 'upgrade:updateConfig'],
[ 'command' => 'upgrade:maintenanceMode', '--off' => '1'],
//[ 'command' => 'upgrade:maintenanceMode', '--off' => '1'],
[ 'command' => 'upgrade:postUpgradeCleanup'],
];

Expand Down
42 changes: 33 additions & 9 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use \Symfony\Component\Process\Exception\ProcessFailedException;

class Application extends \Symfony\Component\Console\Application {

Expand Down Expand Up @@ -82,8 +83,23 @@ public function doRun(InputInterface $input, OutputInterface $output){

$configReader = $this->diContainer['utils.configReader'];
$commandName = $this->getCommandName($input);
if (!in_array($commandName, ['upgrade:executeCoreUpgradeScripts', 'upgrade:checkpoint'])){
$configReader->init();
if (!in_array(
$commandName,
['upgrade:executeCoreUpgradeScripts', 'upgrade:checkpoint', 'upgrade:maintenanceMode', 'help', 'list']
)
){
try {
$configReader->init();
} catch (ProcessFailedException $e){
$this->logException($e);
$output->writeln("<error>Initialization failed with message:</error>");
$output->writeln($e->getProcess()->getOutput());
$output->writeln('<info>Use upgrade:checkpoint --list to view a list of checkpoints</info>');
$output->writeln('<info>upgrade:checkpoint --restore [checkpointid] to revert to the last checkpoint</info>');
$output->writeln('Please attach your update.log to the issues you reporting.');
return 1;
}

}
return parent::doRun($input, $output);
} catch (\Exception $e) {
Expand All @@ -93,13 +109,21 @@ public function doRun(InputInterface $input, OutputInterface $output){
}

protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output){
$command->setContainer($this->getContainer());
$commandName = $this->getCommandName($input);
$this->getLogger()->info('Execution of ' . $commandName . ' command started');
$message = sprintf('<info>%s</info>', $command->getDescription());
$output->writeln($message);
$exitCode = parent::doRunCommand($command, $input, $output);
$this->getLogger()->info('Execution of ' . $commandName . ' command stopped. Exit code is ' . $exitCode);
if ($command instanceof \Owncloud\Updater\Command\Command){
$command->setContainer($this->getContainer());
$commandName = $this->getCommandName($input);
$this->getLogger()->info('Execution of ' . $commandName . ' command started');
if (!empty($command->getMessage())){
$message = sprintf('<info>%s</info>', $command->getMessage());
$output->writeln($message);
}
$exitCode = parent::doRunCommand($command, $input, $output);
$this->getLogger()->info(
'Execution of ' . $commandName . ' command stopped. Exit code is ' . $exitCode
);
} else {
$exitCode = parent::doRunCommand($command, $input, $output);
}
return $exitCode;
}

Expand Down
40 changes: 40 additions & 0 deletions src/Utils/Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace Owncloud\Updater\Utils;

class Registry {

protected $objects = array();

function set($name, $object){
$this->objects[$name] = $object;
}

function get($name){
if (isset($this->objects[$name])){
return $this->objects[$name];
}
return null;
}

}

0 comments on commit 9f80fee

Please sign in to comment.