Skip to content

Commit

Permalink
Merge branch 'release/3.0.0-alpha.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Dec 2, 2018
2 parents 3633a6f + 5100c10 commit a89ab4d
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 86 deletions.
37 changes: 37 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: factorial/phabalicious-test-runner

# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/mysql:9.4

working_directory: ~/repo

steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "composer.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run: php /composer.phar install

- save_cache:
paths:
- ./vendor
key: v1-dependencies-{{ checksum "composer.json" }}

# run tests!
- run: cd tests; ../vendor/bin/phpunit --exclude-group docker .
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Most notably the handling of arguments and options has changed a lot. Fabric gav
docker:
service: web

The `name` will be discarded, if a `service`-entry is set.


### Changed

Expand Down
2 changes: 1 addition & 1 deletion bin/phab
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ $output = $container->get(ConsoleOutput::class);

/** @var Application $application */
$application = $container->get(Application::class);
$application->setVersion('3.0.0');
$application->setVersion('3.0.0-alpha.2');
$application->setName('phabalicious');
$application->setDefaultCommand('list');

Expand Down
27 changes: 27 additions & 0 deletions build/create-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
GH_USER=factorial-io
GH_PATH=`cat ~/.ghtoken`
GH_REPO=phabalicious
GH_TARGET=master
ASSETS_PATH=./
VERSION=`git describe --tags | sed 's/-[0-9]-g[a-z0-9]\{7\}//'`
echo "Releasing ${VERSION} ..."
cd ..
composer build-phar
cd build

res=`curl --user "$GH_USER:$GH_PATH" -X POST https://api.github.com/repos/${GH_USER}/${GH_REPO}/releases \
-d "
{
\"tag_name\": \"v$VERSION\",
\"target_commitish\": \"$GH_TARGET\",
\"name\": \"v$VERSION\",
\"body\": \"new version $VERSION\",
\"draft\": false,
\"prerelease\": false
}"`
echo Create release result: ${res}
rel_id=`echo ${res} | python -c 'import json,sys;print(json.load(sys.stdin)["id"])'`
file_name=phabalicious-${VERSION}.phar

curl --user "$GH_USER:$GH_PATH" -X POST https://uploads.github.com/repos/${GH_USER}/${GH_REPO}/releases/${rel_id}/assets?name=${file_name}\
--header 'Content-Type: text/javascript ' --upload-file ${ASSETS_PATH}/phabalicious.phar
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ Phabalicious is the successor of the python tool [fabalicious](https://github.co
## Build from source

* Clone the repository
* run `composer install`
* run `composer build-phar`
* run `composer install-phar` this will copy the app to /usr/local/bin and make it executable.

## Add it via composer.json

* run `composer require factorial.io/phabalicious`
* run `composer require factorial-io/phabalicious`
* then you can run phabalicious via `./vendor/factorial-io/fabablicious/bin/phab` (or create a symbolic link)

## Running pha
## Running phab

* Run `phab list` to get a list of all available commands.
* run `phab help <command>` to get some help for a given command.
28 changes: 27 additions & 1 deletion src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use Phabalicious\Exception\MismatchedVersionException;
use Phabalicious\Exception\ValidationFailedException;
use Phabalicious\Exception\MissingHostConfigException;
use Phabalicious\ShellProvider\ShellProviderInterface;
use Psr\Log\NullLogger;
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

abstract class BaseCommand extends BaseOptionsCommand
{
Expand Down Expand Up @@ -175,4 +177,28 @@ public function runCommand(string $command, array $args, InputInterface $origina
return $cmd->run($input, $output);
}

}
/**
* @param ShellProviderInterface $shell
* @param array $command
* @return Process
*/
protected function startInteractiveShell(ShellProviderInterface $shell, array $command = [])
{
/** @var Process $process */
$process = $shell->createShellProcess($command, ['tty' => true]);
$stdin = fopen('php://stdin', 'r');
$process->setInput($stdin);
$process->setTimeout(0);
$process->setTty(true);
$process->start();
$process->wait(function ($type, $buffer) {
if ($type == Process::ERR) {
fwrite(STDERR, $buffer);
} else {
fwrite(STDOUT, $buffer);
}
});

return $process;
}
}
22 changes: 12 additions & 10 deletions src/Command/DrushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace Phabalicious\Command;

use Phabalicious\Configuration\HostConfig;
use Phabalicious\Exception\EarlyTaskExitException;
use Phabalicious\Method\TaskContext;
use Phabalicious\Method\TaskContextInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -49,14 +46,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$context = new TaskContext($this, $input, $output);
$context->set('command', implode(' ', $input->getArgument('drush')));

try {
$this->getMethods()->runTask('drush', $this->getHostConfig(), $context);
} catch (EarlyTaskExitException $e) {
return 1;
}
// Allow methods to override the used shellProvider:
$host_config = $this->getHostConfig();
$this->getMethods()->runTask('drush', $host_config, $context);
$shell = $context->getResult('shell', $host_config->shell());
$command = $context->getResult('command');

return $context->getResult('exitCode', 0);
}
if (!$command) {
throw new \RuntimeException('No command-arguments returned for drush-command!');
}

$output->writeln('<info>Starting drush on `' . $host_config['configName'] . '`');

$process = $this->startInteractiveShell($shell, $command);
return $process->getExitCode();
}
}
19 changes: 2 additions & 17 deletions src/Command/ShellCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln('<info>Starting shell on `' . $host_config['configName'] . '`');

/** @var Process $process */
$process = $shell->createShellProcess([], ['tty' => true]);
$stdin = fopen('php://stdin', 'r');
$process->setInput($stdin);
$process->setTimeout(0);
$process->setTty(true);
$process->start();
$process->wait(function ($type, $buffer) {
if ($type == Process::ERR) {
fwrite(STDERR, $buffer);
} else {
fwrite(STDOUT, $buffer);
}
});

$process = $this->startInteractiveShell($shell);
return $process->getExitCode();
}

}
}
4 changes: 4 additions & 0 deletions src/Configuration/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ public function getDockerConfig(string $config_name)

$data = $this->dockerHosts[$config_name];
$data = $this->resolveInheritance($data, $this->dockerHosts);
$data = $this->applyDefaults($data, [
'tmpFolder' => '/tmp',
]);
if (!empty($data['inheritOnly'])) {
return $data;
}
Expand Down Expand Up @@ -597,6 +600,7 @@ private function validateDockerConfig(array $data, $config_name)
$validation->deprecate(['runLocally']);
$validation->hasKey('shellProvider', 'The name of the shell-provider to use');
$validation->hasKey('rootFolder', 'The rootFolder to start with');
$validation->hasKey('tmpFolder', 'The rootFolder to use');

if ($errors->hasErrors()) {
throw new ValidationFailedException($errors);
Expand Down
34 changes: 29 additions & 5 deletions src/Method/DockerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public function validateConfig(array $config, ValidationErrorBagInterface $error
}
}

public function alterConfig(ConfigurationService $configuration_service, array &$data)
{
if (!empty($data['docker']['service'])) {
unset($data['docker']['name']);
}
}

/**
* @param HostConfig $host_config
* @param TaskContextInterface $context
Expand Down Expand Up @@ -165,6 +172,9 @@ public function getInternalTasks()
/**
* @param HostConfig $hostconfig
* @param TaskContextInterface $context
* @throws ValidationFailedException
* @throws \Phabalicious\Exception\MismatchedVersionException
* @throws \Phabalicious\Exception\MissingDockerHostConfigException
*/
public function waitForServices(HostConfig $hostconfig, TaskContextInterface $context)
{
Expand All @@ -177,6 +187,13 @@ public function waitForServices(HostConfig $hostconfig, TaskContextInterface $co
$container_name = $this->getDockerContainerName($hostconfig, $context);
$shell = $docker_config->shell();

if (!$this->isContainerRunning($docker_config, $container_name)) {
throw new \RuntimeException(sprintf(
'Docker container %s not running, check your `host.docker.name` configuration!',
$container_name
));
}

while ($tries < $max_tries) {
$error_log_level = new ScopedErrorLogLevel($shell, LogLevel::NOTICE);
$result = $shell->run(sprintf('#!docker exec %s #!supervisorctl status', $container_name), true, false);
Expand Down Expand Up @@ -247,11 +264,16 @@ private function copySSHKeys(HostConfig $hostconfig, TaskContextInterface $conte
}
if (count($files) > 0) {
$docker_config = $this->getDockerConfig($hostconfig, $context);
$root_folder = $docker_config['rootFolder'] . '/' . $hostconfig['docker']['projectFolder'];

/** @var ShellProviderInterface $shell */
$shell = $docker_config->shell();
$container_name = $this->getDockerContainerName($hostconfig, $context);
if (!$this->isContainerRunning($docker_config, $container_name)) {
throw new \RuntimeException(sprintf('Docker container %s not running', $container_name));
throw new \RuntimeException(sprintf(
'Docker container %s not running, check your `host.docker.name` configuration!',
$container_name
));
}
$shell->run(sprintf('#!docker exec %s mkdir -p /root/.ssh', $container_name));

Expand All @@ -264,15 +286,17 @@ private function copySSHKeys(HostConfig $hostconfig, TaskContextInterface $conte
$data['source'] = $temp_file;
$temp_files[] = $temp_file;
} elseif ($data['source'][0] !== '/') {
$data['source'] = realpath(
$data['source'] =
$context->getConfigurationService()->getFabfilePath() .
'/' .
$data['source']
);
$data['source'];
}
$temp_file = $docker_config['tmpFolder'] . '/' . 'phab.tmp.' . basename($data['source']);
$shell->putFile($data['source'], $temp_file, $context);

$shell->run(sprintf('#!docker cp %s %s:%s', $data['source'], $container_name, $dest));
$shell->run(sprintf('#!docker cp %s %s:%s', $temp_file, $container_name, $dest));
$shell->run(sprintf('#!docker exec %s #!chmod %s %s', $container_name, $data['permissions'], $dest));
$shell->run(sprintf('rm %s', $temp_file));
}
$shell->run(sprintf('#!docker exec %s #!chmod 700 /root/.ssh', $container_name));
$shell->run(sprintf('#!docker exec %s #!chown -R root /root/.ssh', $container_name));
Expand Down
13 changes: 6 additions & 7 deletions src/Method/DrushMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Phabalicious\Validation\ValidationErrorBag;
use Phabalicious\Validation\ValidationErrorBagInterface;
use Phabalicious\Validation\ValidationService;
use webignition\ReadableDuration\ReadableDuration;

class DrushMethod extends BaseMethod implements MethodInterface
{
Expand Down Expand Up @@ -234,8 +233,12 @@ public function drush(HostConfig $host_config, TaskContextInterface $context)
/** @var ShellProviderInterface $shell */
$shell = $this->getShell($host_config, $context);
$shell->cd($host_config['siteFolder']);
$result = $shell->run('#!drush ' . $command);
$context->setResult('exitCode', $result->getExitCode());
$context->setResult('shell', $shell);
$command = sprintf('cd %s; #!drush %s', $host_config['siteFolder'], $command);
$command = $shell->expandCommand($command);
$context->setResult('command', [
$command
]);
}

private function handleModules(
Expand Down Expand Up @@ -399,7 +402,6 @@ public function listBackups(HostConfig $host_config, TaskContextInterface $conte
if ($tokens) {
$result[] = $tokens;
}

}

$existing = $context->getResult('files', []);
Expand Down Expand Up @@ -612,8 +614,5 @@ private function setupConfigurationManagement(HostConfig $host_config, TaskConte
}

$shell->cd($cwd);



}
}
13 changes: 3 additions & 10 deletions src/ShellProvider/LocalShellProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function validateConfig(array $config, ValidationErrorBagInterface $error
);
}

public function createShellProcess(array $command = [], $options = []): Process
public function createShellProcess(array $command = [], array $options = []): Process
{
$shell_command = $this->getShellCommand($options);
if (count($command) > 0) {
Expand Down Expand Up @@ -154,13 +154,7 @@ public function run(string $command, $capture_output = false, $throw_exception_o
} while (empty($exit_code));

$exit_code = intval(str_replace(self::RESULT_IDENTIFIER, '', $exit_code), 10);
foreach ($lines as $line) {
if (!$capture_output && $this->output) {
$this->output->writeln($line);
} elseif ($capture_output) {
$this->logger->debug($line);
}
}

$cr = new CommandResult($exit_code, $lines);
if ($cr->failed() && !$capture_output && $throw_exception_on_error) {
$cr->throwException(sprintf('`%s` failed!', $command));
Expand Down Expand Up @@ -235,5 +229,4 @@ public function createTunnelProcess(HostConfig $target_config, array $prefix = [
{
throw new \InvalidArgumentException('Local shells cannot handle tunnels!');
}

}
}
2 changes: 1 addition & 1 deletion src/ShellProvider/ShellProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function runProcess(array $cmd, TaskContextInterface $context, $interacti

public function getShellCommand(array $options = []): array;

public function createShellProcess(array $command = []): Process;
public function createShellProcess(array $command = [], array $options = []): Process;

public function createTunnelProcess(HostConfig $target_config, array $prefix = []);

Expand Down
Loading

0 comments on commit a89ab4d

Please sign in to comment.