Skip to content

Commit

Permalink
sentinel updates
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Oct 14, 2024
1 parent fdeb935 commit bea4921
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
48 changes: 32 additions & 16 deletions app/Actions/Server/StartSentinel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,48 @@ class StartSentinel
{
use AsAction;

public function handle(Server $server, $version = 'latest', bool $restart = false)
public function handle(Server $server, $version = 'next', bool $restart = false)
{
if ($restart) {
StopSentinel::run($server);
}
$metrics_history = $server->settings->metrics_history_days;
$refresh_rate = $server->settings->metrics_refresh_rate_seconds;
$metrics_history = $server->settings->sentinel_metrics_history_days;
$refresh_rate = $server->settings->sentinel_metrics_refresh_rate_seconds;
$token = $server->settings->sentinel_token;
$fqdn = InstanceSettings::get()->fqdn;
if (str($fqdn)->startsWith('http')) {
throw new \Exception('You should use https to run Sentinel.');
$endpoint = InstanceSettings::get()->fqdn;
if (isDev()) {
$endpoint = 'http://host.docker.internal:8000';
} else {
if (str($endpoint)->startsWith('http')) {
throw new \Exception('You should use https to run Sentinel.');
}
}
if (! $endpoint) {
throw new \Exception('You should set FQDN in Instance Settings.');
}
$environments = [
'TOKEN' => $token,
'ENDPOINT' => InstanceSettings::get()->fqdn,
'ENDPOINT' => $endpoint,
'COLLECTOR_ENABLED' => 'true',
'COLLECTOR_REFRESH_RATE_SECONDS' => $refresh_rate,
'COLLECTOR_RETENTION_PERIOD_DAYS' => $metrics_history
'COLLECTOR_RETENTION_PERIOD_DAYS' => $metrics_history,
];
$docker_environments = "-e \"" . implode("\" -e \"", array_map(fn($key, $value) => "$key=$value", array_keys($environments), $environments)) . "\"";
ray($docker_environments);
return true;
// instant_remote_process([
// "docker run --rm --pull always -d $docker_environments --name coolify-sentinel -v /var/run/docker.sock:/var/run/docker.sock -v /data/coolify/sentinel:/app/sentinel --pid host --health-cmd \"curl --fail http://127.0.0.1:8888/api/health || exit 1\" --health-interval 10s --health-retries 3 ghcr.io/coollabsio/sentinel:$version",
// 'chown -R 9999:root /data/coolify/sentinel',
// 'chmod -R 700 /data/coolify/sentinel',
// ], $server, true);
if (isDev()) {
data_set($environments, 'GIN_MODE', 'debug');
}
$mount_dir = '/data/coolify/sentinel';
if (isDev()) {
$mount_dir = '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/sentinel';
}
$docker_environments = '-e "'.implode('" -e "', array_map(fn ($key, $value) => "$key=$value", array_keys($environments), $environments)).'"';
$docker_command = "docker run --pull always --rm -d $docker_environments --name coolify-sentinel -v /var/run/docker.sock:/var/run/docker.sock -v $mount_dir:/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 ghcr.io/coollabsio/sentinel:$version";

return instant_remote_process([
'docker rm -f coolify-sentinel || true',
"mkdir -p $mount_dir",
$docker_command,
"chown -R 9999:root $mount_dir",
"chmod -R 700 $mount_dir",
], $server, true);
}
}
21 changes: 16 additions & 5 deletions app/Livewire/Server/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public function mount(Server $server)
$this->server->settings->delete_unused_volumes = $server->settings->delete_unused_volumes;
$this->server->settings->delete_unused_networks = $server->settings->delete_unused_networks;
}
public function regenerateSentinelToken() {

public function regenerateSentinelToken()
{
try {
$this->server->generateSentinelToken();
$this->server->settings->refresh();
Expand All @@ -110,6 +112,7 @@ public function regenerateSentinelToken() {
return handleError($e, $this);
}
}

public function updated($field)
{
if ($field === 'server.settings.docker_cleanup_frequency') {
Expand Down Expand Up @@ -186,25 +189,26 @@ public function instantSave()
public function getPushData()
{
try {
if (!isDev()) {
if (! isDev()) {
throw new \Exception('This feature is only available in dev mode.');
}
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $this->server->settings->sentinel_token,
'Authorization' => 'Bearer '.$this->server->settings->sentinel_token,
])->post('http://host.docker.internal:8888/api/push', [
'data' => 'test',
]);
if ($response->successful()) {
$this->dispatch('success', 'Push data sent.');

return;
}
$error = data_get($response->json(), 'error');
throw new \Exception($error);

} catch(\Throwable $e) {
} catch (\Throwable $e) {
return handleError($e, $this);
}
}

public function restartSentinel()
{
try {
Expand Down Expand Up @@ -285,6 +289,7 @@ public function submit()
return handleError($e, $this);
}
}

public function manualCleanup()
{
try {
Expand All @@ -302,4 +307,10 @@ public function manualCloudflareConfig()
$this->server->refresh();
$this->dispatch('success', 'Cloudflare Tunnels enabled.');
}

public function startSentinel()
{
StartSentinel::run($this->server);
$this->dispatch('success', 'Sentinel started.');
}
}
7 changes: 3 additions & 4 deletions resources/views/livewire/server/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,10 @@ functional issues.</p>
{{-- @endif --}}
</div>
@if (isDev())
<x-forms.button wire:click="getPushData"> Push Test </x-forms.button>
{{-- <div class="w-64">
<div class="w-64">
<x-forms.checkbox instantSave id="server.settings.is_metrics_enabled" label="Enable Metrics" />
<x-forms.button>Start Sentinel</x-forms.button>
</div> --}}
<x-forms.button wire:click="startSentinel">Start Sentinel</x-forms.button>
</div>
<div class="flex flex-col gap-2">
<div class="flex flex-wrap gap-2 sm:flex-nowrap items-end">
<x-forms.input type="password" id="server.settings.sentinel_token" label="Metrics token"
Expand Down

0 comments on commit bea4921

Please sign in to comment.