From 7046b27b43b61c908e957de055fef8a8c41387d2 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 5 Dec 2023 16:13:45 +0100 Subject: [PATCH] wip #6555 @1 --- app/Models/FluidbookExternalInstallServer.php | 74 +++++++++++++++---- bin/startup.sh | 7 ++ resources/hosting/docker-compose.yml | 18 +++++ .../hosting/nginxproxy/docker-compose.yml | 36 +++++++++ resources/hosting/nginxproxy/update | 5 ++ resources/hosting/updateall | 4 + 6 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 resources/hosting/docker-compose.yml create mode 100644 resources/hosting/nginxproxy/docker-compose.yml create mode 100644 resources/hosting/nginxproxy/update create mode 100644 resources/hosting/updateall diff --git a/app/Models/FluidbookExternalInstallServer.php b/app/Models/FluidbookExternalInstallServer.php index a74f42cae..ae2647323 100644 --- a/app/Models/FluidbookExternalInstallServer.php +++ b/app/Models/FluidbookExternalInstallServer.php @@ -10,6 +10,7 @@ use Cubist\Backpack\Magic\Models\ExternalServer; use Cubist\Net\Transfer\Local; use Cubist\Util\Files\Files; use Cubist\Util\Json; +use Cubist\Util\Text; use Illuminate\Support\Facades\DB; // __('!! Serveurs externes') @@ -24,10 +25,15 @@ class FluidbookExternalInstallServer extends ExternalServer protected static $_permissionBase = 'fluidbook-external-install-server'; - protected static $hostingProtocols = ['hosting', 'ushosting']; protected $_operations = [ServerOperation::class]; + protected static $hostingBasePaths = [ + 'hosting' => '/mnt/hosting/', + 'hosting2' => '/mnt/hosting2', + 'ushosting' => '/application/usstorage/hosting/' + ]; + private static $id; protected static function _getOneServer($server) @@ -37,17 +43,53 @@ class FluidbookExternalInstallServer extends ExternalServer public function setFields() { - - $nothostingProtocols = array_diff(array_keys($this->getProtocols(), static::$hostingProtocols)); + $nothostingProtocols = array_diff(array_keys($this->getProtocols(), $this->getHostingProtocols())); parent::setFields(); $this->getField('base_url')->setAttribute('when', ['protocol' => $nothostingProtocols]); - $this->addField('subdomains', Textarea::class, __('Sous-domaines'), ['when' => ['protocol' => static::$hostingProtocols]]); - $this->addField('php', Checkbox::class, __('Activer le support de PHP'), ['default' => false, 'when' => ['protocol' => static::$hostingProtocols]]); + $this->addField('subdomains', Textarea::class, __('Sous-domaines'), ['when' => ['protocol' => $this->getHostingProtocols()]]); + $this->addField('php', Checkbox::class, __('Activer le support de PHP'), ['default' => false, 'when' => ['protocol' => $this->getHostingProtocols()]]); $this->addField('redirections', Table::class, __('Redirections'), ['entity_singular' => __('redirection'), 'columns' => ['from' => __('De'), 'to' => __('Vers')], 'when' => ['protocol' => 'hosting']]); $this->addField('allows_root', Checkbox::class, __('Autoriser le chargement à la racine (sur le chemin de base)'), ['default' => false]); } + public function onSaving(): bool + { + if ($this->isHosting()) { + $servers = static::withoutGlobalScopes()->where('protocol', $this->getProtocol())->get(); + foreach ($servers as $server) { + $d = Files::mkdir(protected_path($this->getProtocol()) . '/server_' . $server->id); + } + } + + return parent::onSaving(); + } + + protected function isHosting() + { + return in_array($this->getProtocol(), $this->getHostingProtocols()); + } + + public function getBaseURL() + { + if ($this->isHosting()) { + $subdomains = $this->getSubdomains(); + if (count($subdomains)) { + return 'https://' . $subdomains[0] . '/'; + } + return ''; + } + return parent::getBaseURL(); + } + + /** + * @return string[] + */ + public function getSubdomains() + { + return Text::explodeNewLines($this->subdomains); + } + public function onSaved(): bool { $this->updateHtaccess(); @@ -56,8 +98,7 @@ class FluidbookExternalInstallServer extends ExternalServer protected function updateHtaccess() { - if ($this->getProtocol() === 'hosting') { - + if ($this->isHosting()) { $redirections = Json::isJson($this->redirections) ? json_decode($this->redirections, true) : $this->redirections; $content = ' @@ -82,9 +123,13 @@ class FluidbookExternalInstallServer extends ExternalServer public function getProtocols() { - return ['FTP' => __('FTP non sécurisé'), 'SFTP' => 'SFTP', 'hosting' => 'Hosting', 'ushosting' => 'US Hosting']; + return ['FTP' => __('FTP non sécurisé'), 'SFTP' => 'SFTP', 'hosting' => 'Hosting', 'hosting2' => 'Hosting #2', 'ushosting' => 'US Hosting']; } + protected function getHostingProtocols() + { + return array_keys(static::$hostingBasePaths); + } /** * @return \Cubist\Net\Transfer\Driver @@ -95,12 +140,13 @@ class FluidbookExternalInstallServer extends ExternalServer if (null !== $res) { return $res; } - if ($this->getProtocol() === 'hosting') { - return new Local($this, '/mnt/hosting/'); - } - if ($this->getProtocol() === 'ushosting') { - return new Local($this, '/application/usstorage/hosting/'); - } + return new Local($this, $this->getHostingBasePath()); + } + + protected function getHostingBasePath() + { + + return static::$hostingBasePaths[$this->getProtocol()]; } public static function verifyServerConnexion($id, $data = []) diff --git a/bin/startup.sh b/bin/startup.sh index b94fbc0aa..4aa50f28f 100644 --- a/bin/startup.sh +++ b/bin/startup.sh @@ -47,6 +47,13 @@ else sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 -o uid=1002 -o gid=33 -o allow_other fluidbook@clockwork.cubedesigners.com:/data/fluidbook/hosting /mnt/hosting fi +mkdir -p /mnt/hosting2 +if mountpoint -q "/mnt/hosting2"; then + : +else + sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 -o uid=1002 -o gid=33 -o allow_other fluidbook@clockwork.cubedesigners.com:/data/fluidbook/hosting2 /mnt/hosting2 +fi + mkdir -p /application/usstorage if mountpoint -q "/application/usstorage"; then : diff --git a/resources/hosting/docker-compose.yml b/resources/hosting/docker-compose.yml new file mode 100644 index 000000000..124a2bdd6 --- /dev/null +++ b/resources/hosting/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.1' +services: + webserver: + container_name: fluidbook-$hosting-server-$sid + image: $image + volumes: + - '$path:/usr/local/apache2/htdocs' + environment: + VIRTUAL_HOST: $domains + LETSENCRYPT_HOST: $domains + networks: + - fluidbook-$hosting + restart: unless-stopped + +networks: + fluidbook-$hosting: + external: + name: fluidbook-$hosting diff --git a/resources/hosting/nginxproxy/docker-compose.yml b/resources/hosting/nginxproxy/docker-compose.yml new file mode 100644 index 000000000..4b631fe38 --- /dev/null +++ b/resources/hosting/nginxproxy/docker-compose.yml @@ -0,0 +1,36 @@ +version: '2' +services: + nginx-proxy: + image: nginxproxy/nginx-proxy + container_name: fluidbook-$hosting-proxy + networks: + - fluidbook-$hosting + - "$ip:80:80" + - "$ip:443:443" + volumes: + - /var/run/docker.sock:/tmp/docker.sock:ro + - ./config/timeout.conf:/etc/nginx/conf.d/timeout.conf:ro + - ./config/custom_proxy_settings.conf:/etc/nginx/conf.d/custom_proxy_settings.conf:ro + - ./vhosts:/etc/nginx/vhost.d + - ./html:/usr/share/nginx/html + - ./certs:/etc/nginx/certs + restart: unless-stopped + + acme-companion: + image: nginxproxy/acme-companion + container_name: nginx-proxy-acme + networks: + - fluidbook-$hosting + environment: + - DEFAULT_EMAIL=vincent@cubedesigners.com + volumes_from: + - nginx-proxy + volumes: + - ./certs:/etc/nginx/certs:rw + - ./acme:/etc/acme.sh + - /var/run/docker.sock:/var/run/docker.sock:ro + +networks: + fluidbook-$hosting: + external: + name: fluidbook-$hosting diff --git a/resources/hosting/nginxproxy/update b/resources/hosting/nginxproxy/update new file mode 100644 index 000000000..29cddbda5 --- /dev/null +++ b/resources/hosting/nginxproxy/update @@ -0,0 +1,5 @@ +#!/bin/sh +cd /docker/fluidbook-hosting/nginxproxy/ +docker-compose pull +docker-compose down +docker-compose up -d diff --git a/resources/hosting/updateall b/resources/hosting/updateall new file mode 100644 index 000000000..74733e326 --- /dev/null +++ b/resources/hosting/updateall @@ -0,0 +1,4 @@ +#!/bin/sh +docker network create fluidbook-$hosting +/docker/fluidbook-$hosting/nxginxproxy/update + -- 2.39.5