mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 19:04:03 +00:00
Add vhost_is_written test
This commit is contained in:
parent
094dc924e4
commit
2a33d0ec85
7 changed files with 105 additions and 7 deletions
|
|
@ -1,5 +1,3 @@
|
||||||
# certbot -n --staging --nginx -d tenancytest1.ml --preferred-challenges http --must-staple --agree-tos --email samuel.stancl@gmail.com
|
|
||||||
|
|
||||||
# Catch-all HTTP (:80) server
|
# Catch-all HTTP (:80) server
|
||||||
server {
|
server {
|
||||||
listen 80 default_server;
|
listen 80 default_server;
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,6 @@ namespace Stancl\Tenancy\Interfaces;
|
||||||
|
|
||||||
interface ServerConfigManager
|
interface ServerConfigManager
|
||||||
{
|
{
|
||||||
|
public function addVhost(string $domain, string $file): bool;
|
||||||
|
public function deployCertificate(string $domain): bool;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,37 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy\ServerConfigManagers;
|
namespace Stancl\Tenancy\ServerConfigManagers;
|
||||||
|
|
||||||
|
use Symfony\Component\Process\Process;
|
||||||
use Stancl\Tenancy\Interfaces\ServerConfigManager;
|
use Stancl\Tenancy\Interfaces\ServerConfigManager;
|
||||||
|
|
||||||
class NginxConfigManager implements ServerConfigManager
|
class NginxConfigManager implements ServerConfigManager
|
||||||
{
|
{
|
||||||
|
public function addVhost(string $domain, string $file): bool
|
||||||
|
{
|
||||||
|
$f = fopen($file, 'a');
|
||||||
|
fwrite($f, $this->getVhostText($domain));
|
||||||
|
fclose($f);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getVhostText(string $domain)
|
||||||
|
{
|
||||||
|
return str_replace('%host%', $domain, config('tenancy.server.nginx.vhost'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deployCertificate(string $domain): bool
|
||||||
|
{
|
||||||
|
$process = new Process(array_merge([
|
||||||
|
config('tenancy.server.certbot_path'),
|
||||||
|
'-n',
|
||||||
|
'--nginx',
|
||||||
|
'--agree-tos',
|
||||||
|
'-d', $domain,
|
||||||
|
'--preferred-challenges', 'http',
|
||||||
|
], config('tenancy.server.nginx.extra_certbot_args')));
|
||||||
|
|
||||||
|
$process->run();
|
||||||
|
|
||||||
|
return $process->isSuccessful();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,16 @@ class ServerManager
|
||||||
return config('tenancy.server.file.path.prefix') . $this->tenantManager->tenant['uuid'] . config('tenancy.server.file.path.suffix');
|
return config('tenancy.server.file.path.prefix') . $this->tenantManager->tenant['uuid'] . config('tenancy.server.file.path.suffix');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
public function createVhost(string $domain)
|
||||||
{
|
{
|
||||||
|
$this->serverConfigManager->addVhost($domain, $this->getConfigFilePath());
|
||||||
|
$this->serverConfigManager->deployCertificate($domain);
|
||||||
|
if (method_exists($this->serverConfigManager, 'postCertDeploymentChanges')) {
|
||||||
|
$this->serverConfigManager->postCertDeploymentChanges();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete()
|
public function deleteVhost()
|
||||||
{
|
{
|
||||||
// todo
|
// todo
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,18 @@ return [
|
||||||
// results in: '/etc/nginx/sites-available/tenants/tenant' . $uuid . '.conf'
|
// results in: '/etc/nginx/sites-available/tenants/tenant' . $uuid . '.conf'
|
||||||
]
|
]
|
||||||
*/
|
*/
|
||||||
]
|
],
|
||||||
|
'nginx' => [
|
||||||
|
'vhost' => "
|
||||||
|
server {
|
||||||
|
include includes/tenancy;
|
||||||
|
server_name %host%;
|
||||||
|
}",
|
||||||
|
'extra_certbot_args' => [
|
||||||
|
'--must-staple',
|
||||||
|
// '--staging', // obtains a fake cert intended for testing certbot
|
||||||
|
// '--email', 'your@email', // if you haven't created an account in certbot yet
|
||||||
|
],
|
||||||
|
],
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -39,4 +39,56 @@ class ServerManagerTest extends TestCase
|
||||||
|
|
||||||
$this->assertSame("/etc/foo{$uuid}bar", $this->serverManager->getConfigFilePath());
|
$this->assertSame("/etc/foo{$uuid}bar", $this->serverManager->getConfigFilePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function vhost_is_written()
|
||||||
|
{
|
||||||
|
[$tmpfile, $path, $vhost] = $this->setupCreateVhost();
|
||||||
|
|
||||||
|
$this->serverManager->createVhost('localhost');
|
||||||
|
|
||||||
|
$vhost = str_replace('%host%', 'localhost', $vhost);
|
||||||
|
|
||||||
|
$this->assertContains($vhost, fread($tmpfile, filesize($path)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function cert_is_deployed()
|
||||||
|
{
|
||||||
|
[$tmpfile, $path, $vhost] = $this->setupCreateVhost();
|
||||||
|
|
||||||
|
$this->serverManager->createVhost('localhost');
|
||||||
|
|
||||||
|
dump(fread($tmpfile, filesize($path)));
|
||||||
|
// todo
|
||||||
|
// The following error was encountered:\n
|
||||||
|
// [Errno 13] Permission denied: '/var/log/letsencrypt/.certbot.lock'\n
|
||||||
|
// Either run as root, or set --config-dir, --work-dir, and --logs-dir to writeable paths.\n
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setupCreateVhost()
|
||||||
|
{
|
||||||
|
$tmpfile = tmpfile();
|
||||||
|
$path = stream_get_meta_data($tmpfile)['uri'];
|
||||||
|
|
||||||
|
$vhost = "server {
|
||||||
|
include includes/tenancy;
|
||||||
|
server_name %host%;
|
||||||
|
}";
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.server.nginx' => [
|
||||||
|
'vhost' => $vhost,
|
||||||
|
'extra_certbot_args' => [
|
||||||
|
'--staging'
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'tenancy.server.file' => [
|
||||||
|
'single' => true,
|
||||||
|
'path' => $path,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
return [$tmpfile, $path, $vhost];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,9 @@ class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
|
|
||||||
Redis::connection('tenancy')->flushdb();
|
Redis::connection('tenancy')->flushdb();
|
||||||
|
|
||||||
tenant()->create('phpunit.localhost');
|
tenant()->create('localhost');
|
||||||
|
|
||||||
tenancy()->init('phpunit.localhost');
|
tenancy()->init('localhost');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue