Skip to content

Commit

Permalink
Use arrays for cmd args. Try to switch to web-executor
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Jun 9, 2016
1 parent b9da449 commit 9525714
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 29 deletions.
13 changes: 4 additions & 9 deletions src/Command/MaintenanceModeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessUtils;
use Owncloud\Updater\Utils\OccRunner;

class MaintenanceModeCommand extends Command {
Expand Down Expand Up @@ -58,18 +57,14 @@ protected function configure(){
}

protected function execute(InputInterface $input, OutputInterface $output){
$mode = '';
$args = [];
if ($input->getOption('on')){
$mode = '--on';
$args = ['--on' => ''];
} elseif ($input->getOption('off')){
$mode = '--off';
$args = ['--off' => ''];
}

if ($mode !== ''){
$mode = ProcessUtils::escapeArgument($mode);
}

$response = $this->occRunner->run('maintenance:mode ' . $mode);
$response = $this->occRunner->run('maintenance:mode', $args);
$output->writeln($response);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public function dispatch() {

protected function isLoggedIn() {
/** @var ConfigReader $configReader */
// TODO: configReader executes occ config:list to read OC secret and fails
// TODO: include config.php instead?
$configReader = $this->container['utils.configReader'];
$configReader->init();
$storedSecret = isset($configReader->get(['system'])['updater.secret']) ? $configReader->get(['system'])['updater.secret'] : null;
Expand Down
13 changes: 6 additions & 7 deletions src/Utils/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace Owncloud\Updater\Utils;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessUtils;
use Owncloud\Updater\Utils\OccRunner;

class AppManager {
Expand All @@ -47,7 +46,7 @@ public function __construct(OccRunner $occRunner){

public function disableApp($appId){
try{
$this->occRunner->run('app:disable ' . ProcessUtils::escapeArgument($appId));
$this->occRunner->run('app:disable', ['app-id' => $appId]);
} catch (\Exception $e){
return false;
}
Expand All @@ -56,7 +55,7 @@ public function disableApp($appId){

public function enableApp($appId){
try{
$this->occRunner->run('app:enable ' . ProcessUtils::escapeArgument($appId));
$this->occRunner->run('app:enable', ['app-id' => $appId]);
array_unshift($this->disabledApps, $appId);
} catch (\Exception $e){
return false;
Expand All @@ -68,7 +67,7 @@ public function enableApp($appId){
* @param OutputInterface|null $output
*/
public function disableNotShippedApps(OutputInterface $output = null){
$notShippedApps = $this->occRunner->runJson('app:list --shipped false');
$notShippedApps = $this->occRunner->runJson('app:list', ['--shipped' => 'false']);
$appsToDisable = array_keys($notShippedApps['enabled']);
foreach ($appsToDisable as $appId){
$result = $this->disableApp($appId);
Expand Down Expand Up @@ -101,20 +100,20 @@ public function getAllApps(){
}

public function getNotShippedApps(){
$shippedApps = $this->occRunner->runJson('app:list --shipped false');
$shippedApps = $this->occRunner->runJson('app:list', ['--shipped' => 'false']);
$allApps = array_merge(array_keys($shippedApps['enabled']), array_keys($shippedApps['disabled']));
return $allApps;
}

public function getShippedApps(){
$shippedApps = $this->occRunner->runJson('app:list --shipped true');
$shippedApps = $this->occRunner->runJson('app:list', ['--shipped' => 'true']);
$allApps = array_merge(array_keys($shippedApps['enabled']), array_keys($shippedApps['disabled']));
return $allApps;
}

public function getAppPath($appId){
try {
$response = $this->occRunner->run('app:getpath ' . ProcessUtils::escapeArgument($appId));
$response = $this->occRunner->run('app:getpath', ['app-id' => $appId]);
} catch (\Exception $e) {
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/ConfigReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getEdition(){
* @throws \UnexpectedValueException
*/
private function load(){
$this->cache = $this->occRunner->runJson('config:list --private');
$this->cache = $this->occRunner->runJson('config:list', ['--private' => '']);
$this->isLoaded = true;
}

Expand Down
71 changes: 59 additions & 12 deletions src/Utils/OccRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

namespace Owncloud\Updater\Utils;

use GuzzleHttp\Client;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessUtils;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Owncloud\Updater\Utils\Locator;

Expand All @@ -31,17 +33,72 @@ class OccRunner {
*/
protected $locator;

/**
* @var bool
*/
protected $canUseProcess;

/**
*
* @param Locator $locator
*/
public function __construct(Locator $locator){
$this->locator = $locator;

$disabled = explode(',', ini_get('disable_functions'));
$this->canUseProcess = function_exists('proc_open') && !in_array('proc_open', $disabled);
$this->canUseProcess = false;
}

public function run($command, $args = [], $extra = ''){
if ($this->canUseProcess){
$cmdLine = trim($command . ' ' . $extra);
foreach ($args as $optionTitle => $optionValue){
if (strpos($optionTitle, '--') === 0){
$line = trim("$optionTitle $optionValue");
} else {
$line = $optionValue;
}
$escapedLine = ProcessUtils::escapeArgument($line);
$cmdLine .= " $escapedLine";
}
return $this->runAsProcess($cmdLine);
} else {
return $this->runAsRequest($command, $args);
}
}

public function runJson($command, $args = []){
$plain = $this->run($command, $args, '--output=json');
$decoded = json_decode($plain, true);
if (!is_array($decoded)){
throw new \UnexpectedValueException('Could not parse a response for ' . $command . '. Please check if the current shell user can run occ command. Raw output: ' . PHP_EOL . $plain);
}
return $decoded;
}

private function runAsRequest($command, $args){
$client = new Client();
$request = $client->createRequest(
'POST',
// TODO Get endpoint somehow :-/
'http://localhost/~deo/oc-tmp/index.php/occ/' . $command,
[
'timeout' => 0
]
);

$query = $request->getQuery();
foreach ($args as $optionTitle => $optionValue){
$query[$optionTitle] = $optionValue;
}
$response = $client->send($request);
return $response->getBody()->getContents();
}

public function run($args){
private function runAsProcess($cmdLine){
$occPath = $this->locator->getPathToOccFile();
$cmd = "php $occPath --no-warnings $args";
$cmd = "php $occPath --no-warnings $cmdLine";
$process = new Process($cmd);
$process->setTimeout(null);
$process->run();
Expand All @@ -51,14 +108,4 @@ public function run($args){
}
return $process->getOutput();
}

public function runJson($args){
$plain = $this->run($args . ' --output "json"');
$decoded = json_decode($plain, true);
if (!is_array($decoded)){
throw new \UnexpectedValueException('Could not parse a response for ' . $args . '. Please check if the current shell user can run occ command. Raw output: ' . PHP_EOL . $plain);
}
return $decoded;
}

}

0 comments on commit 9525714

Please sign in to comment.