Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Oct 15, 2024
1 parent 863e359 commit 6809b12
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Method/BaseMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function fallback(string $task, HostConfig $config, TaskContextInterface

public function isRunningAppRequired(HostConfig $host_config, TaskContextInterface $context, string $task)
{
if ($task == 'appCreate') {
if ($task === 'appCreate') {
$stage = $context->get('currentStage');
return AppDefaultStages::stageNeedsRunningApp($stage);
}
Expand Down
9 changes: 0 additions & 9 deletions src/Method/ScottyCtlCreateOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Phabalicious\Configuration\HostConfig;
use Phabalicious\Configuration\Storage\Node;
use Phabalicious\ShellProvider\CommandResult;
use Phabalicious\ShellProvider\ShellProviderInterface;
use Webmozart\Assert\Assert;

class ScottyCtlCreateOptions extends ScottyCtlOptions
Expand Down Expand Up @@ -39,13 +37,6 @@ public function __construct(HostConfig $host_config, TaskContextInterface $conte
}
}

public function runInShell(ShellProviderInterface $shell, string $command, array $add_data): CommandResult
{
return $shell->run(sprintf(
'#!scottyctl %s',
implode(' ', $this->build($command, $add_data))
));
}

protected function buildImpl(array $data, string $command): array
{
Expand Down
16 changes: 14 additions & 2 deletions src/Method/ScottyCtlOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use InvalidArgumentException;
use Phabalicious\Configuration\HostConfig;
use Phabalicious\ShellProvider\CommandResult;
use Phabalicious\ShellProvider\ShellProviderInterface;
use Phabalicious\Utilities\Utilities;

class ScottyCtlOptions
{

protected array $data;


public function __construct(
protected readonly HostConfig $hostConfig,
protected readonly TaskContextInterface $context
Expand All @@ -30,7 +33,7 @@ public function __construct(
$this->context->getPasswordManager()->registerCustomSecretToObfuscate($this->data['access-token']);
}

$this->data['appName'] = $hostConfig['configName'];
$this->data['app-name'] = $scotty_data['app-name'] ?? '%host.configName%';
}

public function build($command, $additional_data = []): array
Expand All @@ -55,8 +58,17 @@ protected function buildImpl(array $data, string $command): array
$options[] = $data['access-token'];
}
$options[] = $command;
$options[] = $data['appName'];
$options[] = $data['app-name'];

return $options;
}

public function runInShell(ShellProviderInterface $shell, string $command, array $add_data = []): CommandResult
{
return $shell->run(sprintf(
'#!scottyctl %s',
implode(' ', $this->build($command, $add_data))
), true, false);
}

}
49 changes: 40 additions & 9 deletions src/Method/ScottyMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,57 @@ public function scaffoldApp(HostConfig $host_config, TaskContext $context): stri
return $project_folder;
}

protected function destroyApp(HostConfig $host_config, TaskContext $context): void
{
$options = new ScottyCtlOptions($host_config, $context);
$result = $options->runInShell($host_config->shell(), 'destroy');
if ($result->failed()) {
$result->throwException('Failed to run scottyctl destroy');
}
}

protected function createApp(HostConfig $host_config, TaskContext $context): void
{
$app_folder = $this->scaffoldApp($host_config, $context);
$this->runScottyCtl($host_config, $context, 'create', $app_folder);
$options = new ScottyCtlCreateOptions($host_config, $context);
$result = $options->runInShell($host_config->shell(), 'create', ['app_folder' => $app_folder]);
if ($result->failed()) {
$result->throwException('Failed to run scottyctl create');
}
}

public function deploy(HostConfig $host_config, TaskContext $context): void
{
$this->createApp($host_config, $context);
}

/**
* @throws \Phabalicious\Exception\FailedShellCommandException
*/
protected function runScottyCtl(HostConfig $host_config, TaskContext $context, string $command, string $app_folder): void
public function appCheckExisting(HostConfig $host_config, TaskContext $context): bool
{
$options = new ScottyCtlCreateOptions($host_config, $context);
$result = $options->runInShell($host_config->shell(), $command, ['app_folder' => $app_folder]);
if ($result->failed()) {
$result->throwException('Failed to run scottyctl ' . $command);
$options = new ScottyCtlOptions($host_config, $context);
$result = $options->runInShell($host_config->shell(), 'info')->succeeded();
$context->setResult('appExists', $result);
return $result;
}

public function appCreate(HostConfig $host_config, TaskContext $context): void
{
$stage = $context->get('currentStage');
if (!$stage) {
throw new \InvalidArgumentException('Missing current stage');
}
if ($stage === 'installCode') {
$this->createApp($host_config, $context);
}
}

public function appDestroy(HostConfig $host_config, TaskContext $context): void
{
$stage = $context->get('currentStage');
if (!$stage) {
throw new \InvalidArgumentException('Missing current stage');
}
if ($stage === 'deleteCode') {
$this->destroyApp($host_config, $context);
}
}
}
4 changes: 2 additions & 2 deletions tests/ScottyMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public function testScottyCtlOptions(): void
'--server',
'http://localhost:21342',
'--access-token',
'my-secret',
'hello-world',
'create',
'hostA',
'phab-scotty-test',
'--folder',
'/app/folder',
'--service',
Expand Down
2 changes: 2 additions & 0 deletions tests/assets/scotty-tests/nginx/assets/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ services:
environment:
APP_OTHER_SECRET: my-other-secret
APP_SECRET: {{host.secrets.APP_SECRET}}
volumes:
- ./html:/usr/share/nginx/html
9 changes: 7 additions & 2 deletions tests/assets/scotty-tests/nginx/fabfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ scotty:
password: admin
registry: factorial
server: http://localhost:21342
access-token: my-secret
access-token: hello-world
services:
nginx: 80

Expand All @@ -28,12 +28,17 @@ hosts:
secrets:
APP_SECRET: my-deepest-secret
scotty:
app-name: phab-scotty-test
environment:
APP_SECRET: "%host.secrets.APP_SECRET%"
scaffold:
html:
- ./assets/html/index.html
assets:
- ./assets/docker-compose.yaml
- ./assets/index.html
scaffold:
- copy_assets(%rootFolder%)
- copy_assets(%rootFolder%/html, html)
hostB:
inheritsFrom: hostA
scotty:
Expand Down

0 comments on commit 6809b12

Please sign in to comment.