Add sudo support for migrate commands when not running as root

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-12-23 10:12:49 +00:00
parent 192811e48a
commit b444f10c8e
4 changed files with 87 additions and 71 deletions

View File

@@ -9,6 +9,7 @@ use Symfony\Component\Process\Exception\ProcessFailedException;
class Migrate_Begin_Command extends Command
{
use PrivilegedCommandTrait;
protected $signature = 'migrate:begin';
protected $description = 'Begin a migration session by creating a MySQL snapshot';
@@ -68,7 +69,7 @@ class Migrate_Begin_Command extends Command
try {
// Step 1: Stop MySQL using supervisorctl
$this->info('[1] Stopping MySQL server...');
shell_exec('supervisorctl stop mysql 2>&1');
$this->shell_exec_privileged('supervisorctl stop mysql 2>&1');
// Wait a moment for process to die
sleep(3);
@@ -76,16 +77,16 @@ class Migrate_Begin_Command extends Command
// Step 2: Remove old backup if exists
if (is_dir($this->backup_dir)) {
$this->info('[2] Removing old backup...');
$this->run_command(['rm', '-rf', $this->backup_dir]);
$this->run_privileged_command(['rm', '-rf', $this->backup_dir]);
}
// Step 3: Copy MySQL data directory
$this->info('[3] Creating snapshot of MySQL data...');
$this->run_command(['cp', '-r', $this->mysql_data_dir, $this->backup_dir]);
$this->run_privileged_command(['cp', '-r', $this->mysql_data_dir, $this->backup_dir]);
// Step 4: Start MySQL again using supervisorctl
$this->info('[4] Starting MySQL server...');
shell_exec('supervisorctl start mysql 2>&1');
$this->shell_exec_privileged('supervisorctl start mysql 2>&1');
// Step 5: Wait for MySQL to be ready
$this->info('[5] Waiting for MySQL to be ready...');
@@ -146,31 +147,15 @@ class Migrate_Begin_Command extends Command
} catch (\Exception $e) {
$this->error('[ERROR] Failed to create snapshot: ' . $e->getMessage());
// Try to restart MySQL if it's stopped
$this->info('Attempting to restart MySQL...');
shell_exec('supervisorctl start mysql 2>&1');
$this->shell_exec_privileged('supervisorctl start mysql 2>&1');
return 1;
}
}
/**
* Run a shell command and check for errors
*/
protected function run_command(array $command, bool $throw_on_error = true): string
{
$process = new Process($command);
$process->setTimeout(60);
$process->run();
if ($throw_on_error && !$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
}
/**
* Wait for MySQL to be ready for connections
*/