Skip to content

Commit

Permalink
Add test cases for SSH key+config setup
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-helmich committed Dec 14, 2023
1 parent 42d0ff0 commit 7501cf1
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 15 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"require": {
"php": "^8.2",
"mittwald/api-client": "^2.0",
"composer/semver": "^3.4"
"composer/semver": "^3.4",
"league/flysystem": "^3.0"
},
"require-dev": {
"deployer/deployer": "^7.3",
"vimeo/psalm": "^5.15",
"phpunit/phpunit": "^10.5"
"phpunit/phpunit": "^10.5",
"league/flysystem-memory": "^3.0"
},
"autoload-dev": {
"psr-4": {
Expand Down
267 changes: 266 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/Recipes/BaseRecipe.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Mittwald\Deployer\Recipes;

use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Mittwald\ApiClient\Generated\V2\Client;
use Mittwald\ApiClient\Generated\V2\Schemas\Project\Project;
use Mittwald\ApiClient\MittwaldAPIV2Client;
Expand All @@ -22,6 +24,17 @@ public static function getClient(): Client
return MittwaldAPIV2Client::newWithToken(get_str('mittwald_token'));
}

public static function getFilesystem(): Filesystem
{
if (has('mittwald_filesystem')) {
$fs = get('mittwald_filesystem');
assert($fs instanceof Filesystem);
return $fs;
}

return new Filesystem(new LocalFilesystemAdapter(getcwd()));
}

public static function getProject(): Project
{
return Project::buildFromInput(get_array('mittwald_project'), validate: false);
Expand Down
18 changes: 8 additions & 10 deletions src/Recipes/SSHUserRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Mittwald\Deployer\Util\SSH\SSHPublicKey;
use function Deployer\{after, currentHost, has, info, runLocally, selectedHosts, set, Support\parse_home_dir, task};
use function Mittwald\Deployer\get_str;
use function Mittwald\Deployer\get_str_nullable;

class SSHUserRecipe
{
Expand All @@ -30,7 +31,7 @@ public static function setup(): void

set('mittwald_ssh_public_key', function (): string {
if (has('mittwald_ssh_public_key_file')) {
return file_get_contents(parse_home_dir(get_str('mittwald_ssh_public_key_file')));
return BaseRecipe::getFilesystem()->read(parse_home_dir(get_str('mittwald_ssh_public_key_file')));
}

// Need to do this in case `ssh_copy_id` contains a tilde that needs to be expanded
Expand Down Expand Up @@ -167,7 +168,7 @@ public static function assertSSHConfig(): void
$sshConfig = static::buildSSHConfigForSelectedHosts();

$renderer = new SSHConfigRenderer($sshConfig);
$renderer->renderToFile();
$renderer->renderToFile(BaseRecipe::getFilesystem());

static::assertLocalSSHPrivateKey();

Expand Down Expand Up @@ -200,20 +201,17 @@ private static function buildSSHConfigForSelectedHosts(): SSHConfig

private static function determineSSHPrivateKeyForHost(Host $host): string
{
/** @var mixed $privateKeyFile */
$privateKeyFile = $host->get('mittwald_ssh_private_key_file');
$privateKeyFile = get_str_nullable('mittwald_ssh_private_key_file');
if (is_string($privateKeyFile)) {
return $privateKeyFile;
}

/** @var mixed $privateKeyContents */
$privateKeyContents = $host->get('mittwald_ssh_private_key');
$privateKeyContents = get_str_nullable('mittwald_ssh_private_key');
if (is_string($privateKeyContents)) {
return './.mw-deployer/id_rsa';
}

/** @var mixed $publicKeyFile */
$publicKeyFile = $host->get('ssh_copy_id');
$publicKeyFile = get_str_nullable('ssh_copy_id');
if (is_string($publicKeyFile)) {
/** @var string $privateKeyFile */
$privateKeyFile = str_replace('.pub', '', $publicKeyFile);
Expand All @@ -229,14 +227,14 @@ private static function assertLocalSSHPrivateKey(): void
static::assertLocalSSHDirectory();

if (has('mittwald_ssh_private_key')) {
file_put_contents('./.mw-deployer/id_rsa', get_str('mittwald_ssh_private_key'));
BaseRecipe::getFilesystem()->write('./.mw-deployer/id_rsa', get_str('mittwald_ssh_private_key'));
}
}

private static function assertLocalSSHDirectory(): void
{
if (!is_dir('./.mw-deployer')) {
mkdir('./.mw-deployer', permissions: 0755, recursive: true);
BaseRecipe::getFilesystem()->createDirectory('./.mw-deployer');
}
}
}
Loading

0 comments on commit 7501cf1

Please sign in to comment.