2023-06-15 09:23:48 +00:00
< ? php
namespace App\Actions\Server ;
use App\Models\InstanceSettings ;
use App\Models\Server ;
class UpdateCoolify
{
public Server $server ;
public string $latest_version ;
public string $current_version ;
public function __invoke ( bool $force )
{
try {
2023-06-23 11:13:02 +00:00
$settings = InstanceSettings :: get ();
2023-06-15 09:23:48 +00:00
ray ( 'Running InstanceAutoUpdateJob' );
$localhost_name = 'localhost' ;
$this -> server = Server :: where ( 'name' , $localhost_name ) -> firstOrFail ();
$this -> latest_version = get_latest_version_of_coolify ();
$this -> current_version = config ( 'version' );
2023-08-08 09:51:36 +00:00
ray ( 'latest version:' . $this -> latest_version . " current version: " . $this -> current_version . ' force: ' . $force );
2023-06-23 11:13:02 +00:00
if ( $settings -> next_channel ) {
ray ( 'next channel enabled' );
$this -> latest_version = 'next' ;
}
2023-06-15 09:23:48 +00:00
if ( $force ) {
$this -> update ();
} else {
2023-06-23 11:13:02 +00:00
if ( ! $settings -> is_auto_update_enabled ) {
2023-06-15 09:23:48 +00:00
throw new \Exception ( 'Auto update is disabled' );
}
if ( $this -> latest_version === $this -> current_version ) {
throw new \Exception ( 'Already on latest version' );
}
if ( version_compare ( $this -> latest_version , $this -> current_version , '<' )) {
throw new \Exception ( 'Latest version is lower than current version?!' );
}
$this -> update ();
}
2023-08-17 14:26:55 +00:00
send_internal_notification ( 'InstanceAutoUpdateJob done on: ' . $this -> server -> ip . " (fqdn: { $this -> server -> fqdn } ) " . ' to version: ' . $this -> latest_version . ' from version: ' . $this -> current_version );
2023-06-15 09:23:48 +00:00
return ;
2023-08-17 14:26:55 +00:00
} catch ( \Exception $th ) {
2023-06-15 09:23:48 +00:00
ray ( 'InstanceAutoUpdateJob failed' );
2023-08-17 14:26:55 +00:00
ray ( $th -> getMessage ());
send_internal_notification ( 'InstanceAutoUpdateJob failed: ' . $th -> getMessage ());
2023-06-15 09:23:48 +00:00
return ;
}
}
2023-08-08 09:51:36 +00:00
2023-06-15 09:23:48 +00:00
private function update ()
{
2023-08-09 13:57:53 +00:00
if ( is_dev ()) {
2023-06-23 11:13:02 +00:00
ray ( " Running update on local docker container. Updating to $this->latest_version " );
2023-06-15 09:23:48 +00:00
remote_process ([
" sleep 10 "
], $this -> server );
ray ( 'Update done' );
return ;
} else {
ray ( 'Running update on production server' );
remote_process ([
" curl -fsSL https://cdn.coollabs.io/coolify/upgrade.sh -o /data/coolify/source/upgrade.sh " ,
" bash /data/coolify/source/upgrade.sh $this->latest_version "
], $this -> server );
return ;
}
}
}