Files
moko-platform/deploy/deploy-dolibarr.php
T
Jonathan Miller b3d9ee8255
Platform: moko-platform CI / Gate 2: Unit Tests (8.1) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.2) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.3) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 3: Self-Health Check (push) Blocked by required conditions
Platform: moko-platform CI / Gate 4: Governance (push) 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
Generic: Repo Health / Site Health (push) Has been skipped
Generic: Repo Health / Access control (push) Successful in 2s
Platform: moko-platform CI / Gate 1: Code Quality (push) Failing after 36s
refactor(cli): migrate 64 legacy scripts to CliFramework (#235)
Wrap all CLI tools in cli/, automation/, maintenance/, deploy/, and
release/ in classes extending CliFramework. Replaces manual $argv
parsing with configure()/addArgument(), moves logic into run(): int,
and converts fwrite(STDERR,...) to $this->log(). Two CLIApp subclasses
(generate_dolibarr_version_txt, generate_joomla_update_xml) converted
to extend CliFramework directly.

Every script now gets free --help, --verbose, --quiet, --dry-run,
--json, --no-color, banners, coloured logging, and progress bars.

Authored-by: Moko Consulting
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-31 11:39:10 -05:00

282 lines
6.6 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/deploy-dolibarr.php
* VERSION: 09.21.07
* BRIEF: Deploy Dolibarr module files to a remote server via SFTP/rsync
*/
declare(strict_types=1);
require_once __DIR__ . '/../lib/Enterprise/CliFramework.php';
use MokoEnterprise\CliFramework;
class DeployDolibarrCli extends CliFramework
{
private string $source = '';
private const MODULE_DIRS = [
'core/modules',
'class',
'lib',
'sql',
'langs',
'css',
'js',
'img',
];
private const EXCLUDES = [
'.git/',
'vendor/',
'tests/',
'node_modules/',
];
protected function configure(): void
{
$this->setDescription('Deploy Dolibarr module files to a remote server via SFTP/rsync');
$this->addArgument('--source', 'Local source directory', '');
$this->addArgument('--config', 'Path to sftp-config.json', '');
}
protected function run(): int
{
$configPath = $this->getArgument('--config');
$this->source = $this->getArgument('--source');
if ($configPath === '' || $this->source === '') {
$this->log('ERROR', 'Usage: deploy-dolibarr.php --source <local-path> --config <sftp-config.json> [--dry-run] [--verbose]');
return 1;
}
if (!is_dir($this->source)) {
$this->log('ERROR', "Source directory does not exist: {$this->source}");
return 1;
}
$moduleName = $this->detectModuleName();
if ($moduleName === null) {
$this->log('ERROR', 'Could not auto-detect module name. Expected core/modules/mod*.class.php');
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;
}
$remoteBase = "{$remotePath}/htdocs/custom/{$moduleName}";
$this->log('INFO', "Deploying Dolibarr module: {$moduleName}");
$this->log('INFO', "Source: {$this->source}");
$this->log('INFO', "Target: {$user}@{$host}:{$remoteBase}");
if ($this->dryRun) {
$this->log('INFO', '*** DRY RUN — no changes will be made ***');
}
$failed = 0;
// Deploy subdirectories
foreach (self::MODULE_DIRS as $dir) {
$localDir = rtrim($this->source, '/\\') . '/' . $dir . '/';
if (!is_dir($localDir)) {
if ($this->verbose) {
$this->log('INFO', "SKIP: {$dir} (not present in source)");
}
continue;
}
$remoteTarget = "{$remoteBase}/{$dir}/";
$result = $this->rsyncDir($localDir, $remoteTarget, $host, $user, $port, $sshKey);
if (!$result) {
$failed++;
}
}
// Deploy root PHP files
$rootPhpFiles = glob(rtrim($this->source, '/\\') . '/*.php');
if (!empty($rootPhpFiles)) {
$this->log('INFO', 'Syncing root PHP files...');
$sourceRoot = rtrim($this->source, '/\\') . '/';
$remoteTarget = "{$remoteBase}/";
$sshCmd = "ssh -p {$port}";
if ($sshKey !== '') {
$sshCmd .= " -i " . escapeshellarg($sshKey);
}
$cmd = $this->buildRsyncCommand(
$sshCmd,
$sourceRoot,
"{$user}@{$host}:{$remoteTarget}",
['--include=*.php', '--exclude=*/', '--exclude=.*']
);
if ($this->verbose) {
$this->log('INFO', "CMD: {$cmd}");
}
$output = [];
$exitCode = 0;
exec($cmd, $output, $exitCode);
if ($exitCode !== 0) {
$this->log('ERROR', "rsync failed for root PHP files (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', "Deployment completed with {$failed} error(s).");
return 1;
}
$this->log('INFO', 'Deployment completed successfully.');
return 0;
}
private function detectModuleName(): ?string
{
$pattern = rtrim($this->source, '/\\') . '/core/modules/mod*.class.php';
$matches = glob($pattern);
if (empty($matches)) {
return null;
}
$filename = basename($matches[0]);
// mod{ModuleName}.class.php -> extract ModuleName, lowercase it
if (preg_match('/^mod(.+)\.class\.php$/', $filename, $m)) {
return strtolower($m[1]);
}
return null;
}
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 rsyncDir(string $localDir, string $remoteTarget, string $host, string $user, int $port, string $sshKey): bool
{
$dirName = basename(rtrim($localDir, '/'));
$sshCmd = "ssh -p {$port}";
if ($sshKey !== '') {
$sshCmd .= " -i " . escapeshellarg($sshKey);
}
$cmd = $this->buildRsyncCommand($sshCmd, $localDir, "{$user}@{$host}:{$remoteTarget}");
$this->log('INFO', "Syncing: {$dirName}");
if ($this->verbose) {
$this->log('INFO', "CMD: {$cmd}");
}
$output = [];
$exitCode = 0;
exec($cmd, $output, $exitCode);
if ($exitCode !== 0) {
$this->log('ERROR', "rsync failed for {$dirName} (exit code {$exitCode})");
foreach ($output as $line) {
$this->log('ERROR', " {$line}");
}
return false;
}
if ($this->verbose) {
foreach ($output as $line) {
$this->log('INFO', " {$line}");
}
}
return true;
}
private function buildRsyncCommand(string $sshCmd, string $source, string $dest, array $extraArgs = []): string
{
$parts = ['rsync', '-rlptz', '--delete'];
foreach (self::EXCLUDES as $exclude) {
$parts[] = '--exclude=' . $exclude;
}
foreach ($extraArgs as $arg) {
$parts[] = $arg;
}
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 DeployDolibarrCli();
exit($app->execute());