Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat(traffic): pass traffic env + proxy log mount to Sentinel

Aditya Tripathi1ce21d717480
diff --git a/app/Actions/Server/StartSentinel.php b/app/Actions/Server/StartSentinel.php
index cec90288e1..82f620ed34 100644
--- a/app/Actions/Server/StartSentinel.php
+++ b/app/Actions/Server/StartSentinel.php
@@ -10,6 +10,27 @@ class StartSentinel
{
use AsAction;
+ public static function sentinelTrafficEnvironment(Server $server): array
+ {
+ if (! $server->isTrafficAnalyticsEnabled()) {
+ return [];
+ }
+
+ $logPath = rtrim($server->proxyPath(), '/').'/access.log';
+ $env = [
+ 'TRAFFIC_ENABLED' => 'true',
+ 'TRAFFIC_PROXY_TYPE' => 'auto',
+ 'TRAFFIC_ACCESS_LOG_PATH' => $logPath,
+ 'GEOIP_ENABLED' => 'true',
+ ];
+ $license = data_get($server, 'settings.geoip_maxmind_license_key');
+ if (filled($license)) {
+ $env['GEOIP_MAXMIND_LICENSE_KEY'] = $license;
+ }
+
+ return $env;
+ }
+
public function handle(Server $server, bool $restart = false, ?string $latestVersion = null, ?string $customImage = null)
{
if ($server->isSwarm() || $server->isBuildServer()) {
@@ -36,6 +57,7 @@ class StartSentinel
'COLLECTOR_REFRESH_RATE_SECONDS' => $refreshRate,
'COLLECTOR_RETENTION_PERIOD_DAYS' => $metricsHistory,
];
+ $environments = array_merge($environments, self::sentinelTrafficEnvironment($server));
$labels = [
'coolify.managed' => 'true',
];
@@ -48,7 +70,10 @@ class StartSentinel
}
$dockerEnvironments = implode(' ', array_map(fn ($key, $value) => '-e '.escapeshellarg("$key=$value"), array_keys($environments), $environments));
$dockerLabels = implode(' ', array_map(fn ($key, $value) => "$key=$value", array_keys($labels), $labels));
- $dockerCommand = "docker run -d $dockerEnvironments --name coolify-sentinel -v /var/run/docker.sock:/var/run/docker.sock -v $mountDir:/app/db --pid host --health-cmd \"curl --fail http://127.0.0.1:8888/api/health || exit 1\" --health-interval 10s --health-retries 3 --add-host=host.docker.internal:host-gateway --label $dockerLabels $image";
+ $trafficMount = $server->isTrafficAnalyticsEnabled()
+ ? '-v '.escapeshellarg($server->proxyPath().':'.$server->proxyPath().':ro').' '
+ : '';
+ $dockerCommand = "docker run -d $dockerEnvironments --name coolify-sentinel -v /var/run/docker.sock:/var/run/docker.sock -v $mountDir:/app/db {$trafficMount}--pid host --health-cmd \"curl --fail http://127.0.0.1:8888/api/health || exit 1\" --health-interval 10s --health-retries 3 --add-host=host.docker.internal:host-gateway --label $dockerLabels $image";
instant_remote_process([
'docker rm -f coolify-sentinel || true',
diff --git a/tests/Feature/TrafficAnalytics/StartSentinelTrafficTest.php b/tests/Feature/TrafficAnalytics/StartSentinelTrafficTest.php
new file mode 100644
index 0000000000..e78e6fd7cd
--- /dev/null
+++ b/tests/Feature/TrafficAnalytics/StartSentinelTrafficTest.php
@@ -0,0 +1,32 @@
+<?php
+
+use App\Actions\Server\StartSentinel;
+use App\Models\Server;
+use App\Models\User;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+
+uses(RefreshDatabase::class);
+
+beforeEach(function () {
+ $user = User::factory()->create();
+ $this->team = $user->teams()->first();
+});
+
+it('produces no traffic env when disabled', function () {
+ $server = Server::factory()->create(['team_id' => $this->team->id]);
+ expect(StartSentinel::sentinelTrafficEnvironment($server))->toBe([]);
+});
+
+it('produces traffic + geoip env when enabled', function () {
+ $server = Server::factory()->create(['team_id' => $this->team->id]);
+ $server->settings->is_traffic_analytics_enabled = true;
+ $server->settings->geoip_maxmind_license_key = 'lic';
+ $server->settings->save();
+
+ $env = StartSentinel::sentinelTrafficEnvironment($server->fresh());
+ expect($env['TRAFFIC_ENABLED'])->toBe('true');
+ expect($env['TRAFFIC_PROXY_TYPE'])->toBe('auto');
+ expect($env['GEOIP_ENABLED'])->toBe('true');
+ expect($env['GEOIP_MAXMIND_LICENSE_KEY'])->toBe('lic');
+ expect($env)->toHaveKey('TRAFFIC_ACCESS_LOG_PATH');
+});