ew_deployer_config/Classes/Tasks/Remote.php
2025-04-17 20:53:18 +02:00

166 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Evoweb\DeployerConfig\Tasks;
use function Deployer\get;
use function Deployer\parse;
use function Deployer\run;
use function Deployer\Support\str_contains;
use function Deployer\test;
use function Deployer\writeln;
class Remote extends AbstractTasks
{
protected array $tasks = [
'remote:prepare',
'remote:change_group',
'remote:writable',
'remote:db_compare',
'remote:fix_folder_structure',
'remote:clear_cache',
'remote:clear_opcache',
];
/**
* Prepare remote path exists
*/
public function prepare(): void
{
if (!$this->hasArray('prepare_dirs')) {
return;
}
$prepareDirs = $this->get('prepare_dirs');
foreach ($prepareDirs as $dir) {
if (test('[ ! -e "{{remote_path}}/' . $dir . '" ]')) {
run($this->getSudo() . ' mkdir -p {{remote_path}}/' . $dir);
}
}
}
/**
* Change usergroup for the hole project
*/
public function changeGroup(): void
{
if ($this->get('http_group')) {
$sudo = get('writable_use_sudo') ? 'sudo' : '';
$runOpts = [];
if ($sudo) {
$runOpts['tty'] = $this->get('writable_tty', false);
}
$path = parse('{{remote_path}}/releases/{{release_name}}/');
run($sudo . ' chgrp -H -R ' . $this->get('http_group') . ' ' . $path, $runOpts);
run($sudo . ' chmod -R 2755 ' . $path, $runOpts);
}
}
/**
* Set files and/or folders writable
*/
public function writable(): void
{
if ($this->hasArray('writable_dirs') || $this->hasArray('writable_files')) {
$sudo = get('writable_use_sudo') ? 'sudo' : '';
$runOpts = [];
if ($sudo) {
$runOpts['tty'] = get('writable_tty', false);
}
$recursive = $this->get('writable_recursive') ? '-R' : '';
$httpGroup = $this->get('http_group', false);
$path = parse('{{remote_path}}/releases/{{release_name}}/');
if ($this->hasArray('writable_dirs')) {
foreach ($this->get('writable_dirs') as $dir) {
if (test('[ ! -d "' . $path . $dir . '" ]')) {
run('mkdir -p ' . $path . $dir);
}
run($sudo . ' chmod -R 2775 ' . $path . $dir);
if ($httpGroup) {
run(
$sudo . ' chgrp -H ' . $recursive . ' ' . $httpGroup . ' ' . $path . $dir,
$runOpts
);
}
}
}
if ($this->hasArray('writable_files')) {
foreach ($this->get('writable_files') as $file) {
if (test('[ -e "' . $path . $file . '" ]')) {
run($sudo . ' chmod g+w ' . $path . $file);
if ($httpGroup) {
run($sudo . ' chgrp ' . $httpGroup . ' ' . $path . $file, $runOpts);
}
}
}
}
}
}
/**
* Database migration
*/
public function dbCompare(): void
{
$path = parse('{{app_container_path}}') ?: parse('{{remote_path}}');
$php = $this->getSudo() . parse('{{bin/php}} ');
if (!str_contains($php, 'TYPO3_CONTEXT')) {
$php = $this->getApplicationContext() . $php;
}
$result = run(
$php . $this->getTYPO3Bin($path, parse('{{remote_path}}')) . ' database:updateschema "*.add,*.change"'
);
writeln($result);
}
/**
* Fix folder structure
*/
public function fixFolderStructure(): void
{
$path = parse('{{app_container_path}}') ?: parse('{{remote_path}}');
$php = $this->getSudo() . parse('{{bin/php}} ');
if (!str_contains($php, 'TYPO3_CONTEXT')) {
$php = $this->getApplicationContext() . $php;
}
$result = run(
$php . $this->getTYPO3Bin($path, parse('{{remote_path}}')) . ' install:fixfolderstructure'
);
writeln($result);
}
/**
* Clear cache via typo3 cache:flush
*/
public function clearCache(): void
{
$path = parse('{{app_container_path}}') ?: parse('{{remote_path}}');
$php = $this->getSudo() . parse('{{bin/php}} ');
if (!str_contains($php, 'TYPO3_CONTEXT')) {
$php = $this->getApplicationContext() . $php;
}
$result = run($php . $this->getTYPO3Bin($path, parse('{{remote_path}}')) . ' cache:flush');
writeln($result);
}
/**
* Clear opcache via curl on {CI_HOST}/cache.php
*/
public function clearOpcache(): void
{
$webDomain = rtrim($this->get('web_domain'), '/');
$htaccess = $this->has('htaccess') ? '--user ' . $this->get('htaccess') : '';
run('curl ' . $htaccess . ' -sk ' . $webDomain . '/cache.php');
}
}