Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v4-next' into patricio-deploy-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ijpatricio committed May 3, 2023
2 parents 7fe50a9 + 1ad7d88 commit 7c13a92
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 42 deletions.
10 changes: 7 additions & 3 deletions app/Console/Commands/SyncBunny.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function handle()
$compose_file = "docker-compose.yml";
$compose_file_prod = "docker-compose.prod.yml";
$upgrade_script = "upgrade.sh";
$docker_install_script = "install-docker.sh";
$production_env = ".env.production";

PendingRequest::macro('storage', function ($file) {
Expand All @@ -53,8 +54,9 @@ public function handle()
Http::pool(fn (Pool $pool) => [
$pool->storage(file: "$parent_dir/$compose_file")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$compose_file"),
$pool->storage(file: "$parent_dir/$compose_file_prod")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$compose_file_prod"),
$pool->storage(file: "$parent_dir/scripts/$upgrade_script")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$upgrade_script"),
$pool->storage(file: "$parent_dir/$production_env")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$production_env"),
$pool->storage(file: "$parent_dir/scripts/$upgrade_script")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$upgrade_script"),
$pool->storage(file: "$parent_dir/scripts/$docker_install_script")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$docker_install_script"),
]);

$res = Http::withHeaders([
Expand All @@ -64,13 +66,15 @@ public function handle()
"url" => "$bunny_cdn/$bunny_cdn_path/$compose_file",
"url" => "$bunny_cdn/$bunny_cdn_path/$compose_file_prod",
"url" => "$bunny_cdn/$bunny_cdn_path/$upgrade_script",
"url" => "$bunny_cdn/$bunny_cdn_path/$production_env"
"url" => "$bunny_cdn/$bunny_cdn_path/$production_env",
"url" => "$bunny_cdn/$bunny_cdn_path/$docker_install_script"
]);
if ($res->ok()) {
echo "All files uploaded & purged...\n";
return;
}
throw new \Exception("Something went wrong.");
} catch (\Exception $e) {
echo "Something went wrong.\n";
echo $e->getMessage();
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/Destination/New/StandaloneDocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function submit()

$server = Server::find($this->server_id);

instantRemoteProcess($server, ['docker network create --attachable ' . $this->network], throwError: false);
instantRemoteProcess(['docker network create --attachable ' . $this->network], $server, throwError: false);
return redirect()->route('destination.show', $docker->uuid);
}
}
14 changes: 7 additions & 7 deletions app/Http/Livewire/ForceUpgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ class ForceUpgrade extends Component
{
public function upgrade()
{
//if (env('APP_ENV') === 'local') {
if (config('app.env') === 'local') {
$server = Server::where('ip', 'coolify-testing-host')->first();
if (!$server) {
return;
}
instantRemoteProcess($server, [
instantRemoteProcess([
"sleep 2"
]);
], $server);
remoteProcess([
"sleep 10"
], $server, ActivityTypes::INLINE->value);
Expand All @@ -32,15 +31,16 @@ public function upgrade()
return;
}

instantRemoteProcess($server, [
instantRemoteProcess([
"curl -fsSL $cdn/docker-compose.yml -o /data/coolify/source/docker-compose.yml",
"curl -fsSL $cdn/docker-compose.prod.yml -o /data/coolify/source/docker-compose.prod.yml",
"curl -fsSL $cdn/.env.production -o /data/coolify/source/.env.production",
"curl -fsSL $cdn/upgrade.sh -o /data/coolify/source/upgrade.sh",
]);
instantRemoteProcess($server, [
], $server);

instantRemoteProcess([
"docker compose -f /data/coolify/source/docker-compose.yml -f /data/coolify/source/docker-compose.prod.yml pull",
]);
], $server);

remoteProcess([
"bash /data/coolify/source/upgrade.sh $latestVersion"
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/Project/Application/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function delete()
}
public function stop()
{
instantRemoteProcess($this->destination->server, ["docker rm -f {$this->application->uuid}"]);
instantRemoteProcess(["docker rm -f {$this->application->uuid}"], $this->destination->server);
if ($this->application->status != 'exited') {
$this->application->status = 'exited';
$this->application->save();
Expand Down
28 changes: 22 additions & 6 deletions app/Http/Livewire/Server/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Livewire\Server;

use App\Enums\ActivityTypes;
use App\Models\Server;
use Illuminate\Support\Facades\Validator;
use Livewire\Component;
Expand All @@ -28,15 +29,30 @@ public function mount()
public function installDocker()
{
$config = base64_encode('{ "live-restore": true }');
instantRemoteProcess($this->server, [
"curl https://releases.rancher.com/install-docker/23.0.sh | sh"
]);
remoteProcess([
"curl https://releases.rancher.com/install-docker/23.0.sh | sh",
"echo '{$config}' | base64 -d > /etc/docker/daemon.json",
"systemctl restart docker"
], $this->server, ActivityTypes::INLINE->value);
}
public function checkServer()
{
$this->uptime = instantRemoteProcess($this->server, ['uptime']);
$this->dockerVersion = instantRemoteProcess($this->server, ['docker version|head -2|grep -i version'], false);
$this->dockerComposeVersion = instantRemoteProcess($this->server, ['docker compose version|head -2|grep -i version'], false);
try {
$this->uptime = instantRemoteProcess(['uptime'], $this->server, false);
if (!$this->uptime) {
$this->uptime = 'Server not reachable.';
throw new \Exception('Server not reachable.');
}
$this->dockerVersion = instantRemoteProcess(['docker version|head -2|grep -i version'], $this->server, false);
if (!$this->dockerVersion) {
$this->dockerVersion = 'Not installed.';
}
$this->dockerComposeVersion = instantRemoteProcess(['docker compose version|head -2|grep -i version'], $this->server, false);
if (!$this->dockerComposeVersion) {
$this->dockerComposeVersion = 'Not installed.';
}
} catch (\Exception $e) {
}
}
public function submit()
{
Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/ContainerStatusJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function checkAllServers()
$not_found_applications = $applications;
$containers = collect();
foreach ($servers as $server) {
$output = instantRemoteProcess($server, ['docker ps -a -q --format \'{{json .}}\'']);
$output = instantRemoteProcess(['docker ps -a -q --format \'{{json .}}\''], $server);
$containers = $containers->concat(formatDockerCmdOutputToJson($output));
}
foreach ($containers as $container) {
Expand Down Expand Up @@ -67,7 +67,7 @@ protected function checkContainerStatus()
return;
}
if ($application->destination->server) {
$container = instantRemoteProcess($application->destination->server, ["docker inspect --format '{{json .State}}' {$this->container_id}"]);
$container = instantRemoteProcess(["docker inspect --format '{{json .State}}' {$this->container_id}"], $application->destination->server);
$container = formatDockerCmdOutputToJson($container);
$application->status = $container[0]['Status'];
$application->save();
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/DockerCleanupDanglingImagesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle(): void
try {
$servers = Server::all();
foreach ($servers as $server) {
instantRemoteProcess($server, ['docker image prune -f']);
instantRemoteProcess(['docker image prune -f'], $server);
}
} catch (\Exception $e) {
Log::error($e->getMessage());
Expand Down
15 changes: 5 additions & 10 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -15,14 +16,9 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
// @TODO: Is this the best place to run the seeder?
// if (env('APP_ENV') === 'production') {
// dump('Seed default data.');
// Process::run('php artisan db:seed --class=ProductionSeeder --force');
// } else {
// dump('Not in production environment.');
// }
//
if (config('app.env') === 'production' && Str::contains(config('coolify.version'), ['nightly'])) {
Process::run('php artisan migrate:fresh --force --seed --seeder=ProductionSeeder');
}
}

/**
Expand All @@ -31,9 +27,8 @@ public function register(): void
public function boot(): void
{
Queue::after(function (JobProcessed $event) {
// @TODO: Remove `coolify-builder` container after the remoteProcess job is finishged and remoteProcess->type == `deployment`.
// @TODO: Remove `coolify-builder` container after the remoteProcess job is finishged and remoteProcess->type == `deployment`.
if ($event->job->resolveName() === CoolifyTask::class) {

}
});
}
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function formatDockerLabelsToJson($rawOutput): Collection
}
}
if (!function_exists('instantRemoteProcess')) {
function instantRemoteProcess(Server $server, array $command, $throwError = true)
function instantRemoteProcess(array $command, Server $server, $throwError = true)
{
$command_string = implode("\n", $command);
$private_key_location = savePrivateKeyForServer($server);
Expand Down
11 changes: 7 additions & 4 deletions resources/views/livewire/server/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
Submit
</button>
<button wire:click.prevent='checkServer'>Check Server</button>
<button wire:click.prevent='installDocker'>Install Docker</button>
{{-- <button wire:click.prevent='installDocker'>Install Docker</button> --}}
</div>
</form>
@isset($uptime)
<p>Connection OK</p>
<p>Uptime: {{ $uptime }}</p>
<p>{{ $dockerVersion }}</p>
<p>{{ $dockerComposeVersion }}</p>
@endisset
@isset($dockerVersion)
<p>Docker Engine: {{ $dockerVersion }}</p>
@endisset
@isset($dockerComposeVersion)
<p>Docker Compose: {{ $dockerComposeVersion }}</p>
@endisset

</div>
5 changes: 5 additions & 0 deletions scripts/install-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
curl https://releases.rancher.com/install-docker/23.0.sh | sh
echo "Docker installed successfully"
echo '{ "live-restore": true }' >/etc/docker/daemon.json
systemctl restart docker
8 changes: 4 additions & 4 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
## Do not modify this file. You will lost the ability to installation and autoupdate!

###########
## Always run "php artisan app:sync-to-bunny-cdn --env=secrets" if you update this file.
## Always run "php artisan app:sync-to-bunny-cdn --env=secrets" or "scripts/run sync-bunny" if you update this file.
###########

VERSION="1.0.0"
DOCKER_VERSION="23.0"

CDN="https://coolify-cdn.b-cdn.net/files"
OS_TYPE=$(cat /etc/os-release | grep -w "ID" | cut -d "=" -f 2 | tr -d '"')
OS_VERSION=$(cat /etc/os-release | grep -w "VERSION_ID" | cut -d "=" -f 2 | tr -d '"')
Expand All @@ -18,9 +20,7 @@ fi

if ! [ -x "$(command -v docker)" ]; then
echo "Docker is not installed. Installing Docker..."
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
rm get-docker.sh
curl https://releases.rancher.com/install-docker/${DOCKER_VERSION}.sh | sh
echo "Docker installed successfully"
fi

Expand Down
3 changes: 1 addition & 2 deletions scripts/run
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ function help {
compgen -A function | cat -n
}
function sync-bunny {
bash vendor/bin/spin exec -u webuser coolify php artisan sync:bunny --env=secret ||
php artisan sync:bunny --env=secrets
php artisan sync:bunny --env=secrets
}
function queue {
bash vendor/bin/spin exec -u webuser coolify php artisan queue:listen
Expand Down

0 comments on commit 7c13a92

Please sign in to comment.