66e728b078
Platform: moko-platform CI / Gate 2: Unit Tests (8.1) (pull_request) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.2) (pull_request) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.1) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.3) (pull_request) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.2) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 3: Self-Health Check (pull_request) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.3) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 4: Governance (pull_request) Blocked by required conditions
Platform: moko-platform CI / Gate 3: Self-Health Check (push) Blocked by required conditions
Platform: moko-platform CI / Gate 5: Template Integrity (pull_request) Blocked by required conditions
Platform: moko-platform CI / Gate 4: Governance (push) Blocked by required conditions
Platform: moko-platform CI / CI Summary (pull_request) Blocked by required conditions
Platform: moko-platform CI / Gate 5: Template Integrity (push) Blocked by required conditions
Platform: moko-platform CI / CI Summary (push) Blocked by required conditions
Generic: Repo Health / Release configuration (push) Blocked by required conditions
Generic: Repo Health / Scripts governance (push) Blocked by required conditions
Generic: Repo Health / Repository health (push) Blocked by required conditions
Universal: PR Check / Build RC Package (pull_request) Blocked by required conditions
Generic: Repo Health / Release configuration (pull_request) Blocked by required conditions
Generic: Repo Health / Scripts governance (pull_request) Blocked by required conditions
Generic: Repo Health / Repository health (pull_request) Blocked by required conditions
Generic: Repo Health / Access control (push) Successful in 18s
Generic: Repo Health / Site Health (push) Has been skipped
Universal: PR Check / Branch Policy (pull_request) Successful in 3s
Universal: Auto Version Bump / Version Bump (push) Failing after 27s
Universal: Secret Scanning / Gitleaks Secret Scan (pull_request) Successful in 28s
Universal: PR Check / Validate PR (pull_request) Failing after 6s
Generic: Repo Health / Site Health (pull_request) Has been skipped
Generic: Repo Health / Access control (pull_request) Successful in 3s
Platform: moko-platform CI / Gate 1: Code Quality (pull_request) Failing after 1m7s
Platform: moko-platform CI / Gate 1: Code Quality (push) Failing after 1m7s
Auto-fixed 5006 tab-indent and line-ending errors via phpcbf, then manually broke 100 lines exceeding 150-char limit. All 74 files in cli/, automation/, maintenance/, deploy/ now pass PHPCS PSR-12 clean. Authored-by: Moko Consulting Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
189 lines
5.5 KiB
PHP
189 lines
5.5 KiB
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
|
|
*
|
|
* This file is part of a Moko Consulting project.
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
* FILE INFORMATION
|
|
* DEFGROUP: MokoPlatform.Scripts.Deploy
|
|
* INGROUP: MokoPlatform
|
|
* REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform
|
|
* PATH: /deploy/rollback-joomla.php
|
|
* VERSION: 09.22.00
|
|
* BRIEF: Rollback a Joomla deployment by restoring from a pre-deploy snapshot
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../lib/Enterprise/CliFramework.php';
|
|
|
|
use MokoEnterprise\CliFramework;
|
|
|
|
class RollbackJoomlaCli extends CliFramework
|
|
{
|
|
private const JOOMLA_DIRS = [
|
|
'administrator/components',
|
|
'administrator/language',
|
|
'administrator/modules',
|
|
'administrator/templates',
|
|
'components',
|
|
'language',
|
|
'layouts',
|
|
'libraries',
|
|
'media',
|
|
'modules',
|
|
'plugins',
|
|
'templates',
|
|
];
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this->setDescription('Rollback a Joomla deployment by restoring from a pre-deploy snapshot');
|
|
$this->addArgument('--config', 'Path to sftp-config.json', '');
|
|
$this->addArgument('--snapshot-dir', 'Path to snapshot directory', '');
|
|
}
|
|
|
|
protected function run(): int
|
|
{
|
|
$configPath = $this->getArgument('--config');
|
|
$snapshotDir = $this->getArgument('--snapshot-dir');
|
|
|
|
if ($configPath === '' || $snapshotDir === '') {
|
|
$this->log('ERROR', 'Usage: rollback-joomla.php --config <sftp-config.json> --snapshot-dir <path> [--dry-run] [--verbose]');
|
|
return 1;
|
|
}
|
|
|
|
if (!is_dir($snapshotDir)) {
|
|
$this->log('ERROR', "Snapshot directory does not exist: {$snapshotDir}");
|
|
return 1;
|
|
}
|
|
|
|
$config = $this->loadConfig($configPath);
|
|
if ($config === null) {
|
|
return 1;
|
|
}
|
|
|
|
$host = $config['host'] ?? '';
|
|
$user = $config['user'] ?? '';
|
|
$port = (int) ($config['port'] ?? 22);
|
|
$remotePath = rtrim($config['remote_path'] ?? '', '/');
|
|
$sshKey = $config['ssh_key_file'] ?? '';
|
|
|
|
if ($host === '' || $user === '' || $remotePath === '') {
|
|
$this->log('ERROR', 'Config must contain host, user, and remote_path.');
|
|
return 1;
|
|
}
|
|
|
|
$this->log('INFO', 'Starting Joomla rollback from snapshot...');
|
|
$this->log('INFO', "Snapshot: {$snapshotDir}");
|
|
$this->log('INFO', "Target: {$user}@{$host}:{$remotePath}");
|
|
|
|
if ($this->dryRun) {
|
|
$this->log('INFO', '*** DRY RUN — no changes will be made ***');
|
|
}
|
|
|
|
$failed = 0;
|
|
|
|
foreach (self::JOOMLA_DIRS as $dir) {
|
|
$localDir = rtrim($snapshotDir, '/\\') . '/' . $dir . '/';
|
|
|
|
if (!is_dir($localDir)) {
|
|
if ($this->verbose) {
|
|
$this->log('INFO', "SKIP: {$dir} (not present in snapshot)");
|
|
}
|
|
continue;
|
|
}
|
|
|
|
$remoteTarget = "{$remotePath}/{$dir}/";
|
|
$sshCmd = "ssh -p {$port}";
|
|
if ($sshKey !== '') {
|
|
$sshCmd .= " -i " . escapeshellarg($sshKey);
|
|
}
|
|
|
|
$cmd = $this->buildRsyncCommand($sshCmd, $localDir, "{$user}@{$host}:{$remoteTarget}");
|
|
|
|
$this->log('INFO', "Restoring: {$dir}");
|
|
if ($this->verbose) {
|
|
$this->log('INFO', "CMD: {$cmd}");
|
|
}
|
|
|
|
$output = [];
|
|
$exitCode = 0;
|
|
exec($cmd, $output, $exitCode);
|
|
|
|
if ($exitCode !== 0) {
|
|
$this->log('ERROR', "rsync failed for {$dir} (exit code {$exitCode})");
|
|
foreach ($output as $line) {
|
|
$this->log('ERROR', " {$line}");
|
|
}
|
|
$failed++;
|
|
} else {
|
|
if ($this->verbose) {
|
|
foreach ($output as $line) {
|
|
$this->log('INFO', " {$line}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($failed > 0) {
|
|
$this->log('ERROR', "Rollback completed with {$failed} error(s).");
|
|
return 1;
|
|
}
|
|
|
|
$this->log('INFO', 'Rollback completed successfully.');
|
|
return 0;
|
|
}
|
|
|
|
private function loadConfig(string $path): ?array
|
|
{
|
|
if (!is_file($path)) {
|
|
$this->log('ERROR', "Config file not found: {$path}");
|
|
return null;
|
|
}
|
|
|
|
$raw = file_get_contents($path);
|
|
if ($raw === false) {
|
|
$this->log('ERROR', "Could not read config file: {$path}");
|
|
return null;
|
|
}
|
|
|
|
// Strip // comments (sftp-config.json style)
|
|
$cleaned = preg_replace('#^\s*//.*$#m', '', $raw);
|
|
$config = json_decode($cleaned, true);
|
|
|
|
if (!is_array($config)) {
|
|
$this->log('ERROR', 'Invalid JSON in config file.');
|
|
return null;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
private function buildRsyncCommand(string $sshCmd, string $source, string $dest): string
|
|
{
|
|
$parts = ['rsync', '-rlptz', '--delete', '--exclude=configuration.php'];
|
|
|
|
if ($this->dryRun) {
|
|
$parts[] = '--dry-run';
|
|
}
|
|
|
|
if ($this->verbose) {
|
|
$parts[] = '-v';
|
|
}
|
|
|
|
$parts[] = '-e';
|
|
$parts[] = escapeshellarg($sshCmd);
|
|
$parts[] = escapeshellarg($source);
|
|
$parts[] = escapeshellarg($dest);
|
|
|
|
return implode(' ', $parts);
|
|
}
|
|
}
|
|
|
|
$app = new RollbackJoomlaCli();
|
|
exit($app->execute());
|