Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Commands/BaseSlaveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@

abstract class BaseSlaveCommand extends Command {

use LockableTrait;
use LockableTrait {
lock as protected;
}

protected function configure(): void
{
Expand Down
24 changes: 17 additions & 7 deletions src/Commands/ProtocolStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int
});

// ── Stage 2: Infrastructure provisioning ────────────────
$runner->run('Infrastructure provisioning', function() use ($app, $arrInput, $arrInput1, $nullOutput, $repo_dir, $environment, $strategy, $isDev) {
$runner->run('Infrastructure provisioning', function() use ($app, $arrInput, $arrInput1, $nullOutput, $repo_dir, $environment, $strategy, $isDev, $nodeConfig, $nodeData) {

$configRemote = Json::read('configuration.remote', false, $repo_dir);
$configRepo = Config::repo($repo_dir);
$hasConfigRepo = $configRemote || is_dir($configRepo);

// If config repo has a remote but hasn't been cloned yet, clone it silently
if ($configRemote && !is_dir($configRepo)) {
$basedir = dirname($repo_dir) . DIRECTORY_SEPARATOR;
$foldername = basename($repo_dir) . '-config';
$configPath = $basedir . $foldername;
Shell::run("git clone " . escapeshellarg($configRemote) . " " . escapeshellarg($configPath) . " 2>&1");
// Clone to the resolved config repo path (Config::repo handles slave context)
Shell::run("git clone " . escapeshellarg($configRemote) . " " . escapeshellarg($configRepo) . " 2>&1");

// Switch to environment branch if needed
if ($environment) {
$currentBranch = Git::branch($configPath);
$currentBranch = Git::branch($configRepo);
if ($environment !== $currentBranch) {
Shell::run("git -C " . escapeshellarg($configPath) . " checkout " . escapeshellarg($environment) . " 2>/dev/null");
Shell::run("git -C " . escapeshellarg($configRepo) . " checkout " . escapeshellarg($environment) . " 2>/dev/null");
}
}
}

// Create bridge symlink so relative paths from release dirs resolve correctly
if ($nodeConfig && is_dir($configRepo)) {
$releasesDir = $nodeData['bluegreen']['releases_dir'] ?? null;
if ($releasesDir && is_dir($releasesDir)) {
$configFolderName = basename(rtrim($configRepo, '/'));
$bridgeLink = rtrim($releasesDir, '/') . '/' . $configFolderName;
if (!file_exists($bridgeLink)) {
Shell::run("ln -s " . escapeshellarg('../' . $configFolderName) . " " . escapeshellarg($bridgeLink));
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/Helpers/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,19 @@ public static function requireRepo(string $repo_dir, OutputInterface $output): ?
*/
public static function repo( $repo_dir )
{
$foldername = basename($repo_dir).'-config';
// Detect slave context: resolve config path relative to the node's
// repo_dir, not the active release directory
$base_dir = $repo_dir;
$nodeInfo = \Gitcd\Utils\NodeConfig::findByActiveDir($repo_dir);
if ($nodeInfo) {
$base_dir = rtrim($nodeInfo[1]['repo_dir'] ?? $repo_dir, '/') . '/';
}

$foldername = basename(rtrim($base_dir, '/')). '-config';

$path = Json::read('configuration.local', '..'.DIRECTORY_SEPARATOR.$foldername.DIRECTORY_SEPARATOR, $repo_dir);
if (strpos($path, '..')!==false) {
$path = $repo_dir.$path;
$path = $base_dir.$path;
}
return Dir::realpath($path);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Utils/NodeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,31 @@ public static function resolveSlaveNode(?string $projectName = null, ?string $re

return [$projectName, $data, $activeDir];
}

/**
* Find a slave node config by checking if a directory is inside
* any node's releases directory or matches a node's repo_dir.
*
* @return array|null [$projectName, $nodeData] or null
*/
public static function findByActiveDir(string $activeDir): ?array
{
$activeDir = rtrim($activeDir, '/');
foreach (self::listProjects() as $project) {
$data = self::load($project);
if (($data['node_type'] ?? '') !== 'slave') {
continue;
}
// Check if activeDir is inside the releases directory
$releasesDir = rtrim($data['bluegreen']['releases_dir'] ?? '', '/');
if ($releasesDir && str_starts_with($activeDir, $releasesDir)) {
return [$project, $data];
}
// Also match repo_dir directly
if (rtrim($data['repo_dir'] ?? '', '/') === $activeDir) {
return [$project, $data];
}
}
return null;
}
}
Loading