From a447f2129759dcf4c9a55a899171a72f1ef0a840 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Fri, 31 Jul 2020 09:31:29 +0300 Subject: [PATCH] Fetch old version and env vars --- app/tasks/install.php | 41 ++++++++++++++++++++++--- src/Appwrite/Docker/Compose/Service.php | 23 ++++++++++++-- src/Appwrite/Docker/Env.php | 10 ++++++ tests/unit/Docker/ComposeTest.php | 2 ++ 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/app/tasks/install.php b/app/tasks/install.php index 815cd57967..59af8ba286 100644 --- a/app/tasks/install.php +++ b/app/tasks/install.php @@ -2,6 +2,7 @@ global $cli; +use Appwrite\Docker\Compose; use Utopia\CLI\Console; use Utopia\Config\Config; use Utopia\View; @@ -12,9 +13,9 @@ $cli ->action(function () { /** * 1. Start - DONE - * 2. Check for older setup and get older version + * 2. Check for older setup and get older version - DONE * 2.1 If older version is equal or bigger(?) than current version, **stop setup** - * 2.2. Get ENV vars + * 2.2. Get ENV vars - DONE * 2.2.1 Fetch from older docker-compose.yml file * 2.2.2 Fetch from older .env file (manually parse) * 2.3 Use old ENV vars as default values @@ -22,14 +23,46 @@ $cli * Otherwise, just use default vars. - DONE * 3. Ask user to backup important volumes, env vars, and SQL tables * In th future we can try and automate this for smaller/medium size setups - * 4. Drop new docker-compose.yml setup (located inside the container, no network dependencies with appwrite.io) + * 4. Drop new docker-compose.yml setup (located inside the container, no network dependencies with appwrite.io) - DONE * 5. Run docker-compose up -d - DONE * 6. Run data migration */ $vars = Config::getParam('variables'); + $path = '/usr/src/code/appwrite'; + $version = null; Console::success('Starting Appwrite installation...'); + // Create directory with write permissions + if (null !== $path && !\file_exists(\dirname($path))) { + if (!@\mkdir(\dirname($path), 0755, true)) { + Console::error('Can\'t create directory '.\dirname($path)); + exit(1); + } + } + + $data = @file_get_contents($path.'/docker-compose.yml'); + + if($data !== false) { + $compose = new Compose($data); + $service = $compose->getService('appwrite'); + $version = ($service) ? $service->getImageVersion() : $version; + + if($version) { + foreach($compose->getServices() as $service) { // Fetch all env vars from previous compose file + if(!$service) { + continue; + } + + $env = $service->getEnvironment()->list(); + + var_dump($env); + } + + // Fetch all env vars from previous .env file + } + } + $httpPort = Console::confirm('Choose your server HTTP port: (default: 80)'); $httpPort = ($httpPort) ? $httpPort : 80; @@ -64,8 +97,6 @@ $cli ->setParam('vars', $input) ; - $path = '/usr/src/code'; - if(!file_put_contents($path.'/docker-compose.yml', $templateForCompose->render(false))) { Console::error('Failed to save Docker Compose file'); exit(1); diff --git a/src/Appwrite/Docker/Compose/Service.php b/src/Appwrite/Docker/Compose/Service.php index 19a3cd6955..6334059475 100644 --- a/src/Appwrite/Docker/Compose/Service.php +++ b/src/Appwrite/Docker/Compose/Service.php @@ -17,11 +17,11 @@ class Service public function __construct(array $service) { $this->service = $service; - $this->service['environment'] = isset($this->service['environment']) ? new Env(implode("\n", $this->service['environment'])) : null; + $this->service['environment'] = isset($this->service['environment']) ? new Env(implode("\n", $this->service['environment'])) : new Env(''); } /** - * @return array + * @return string */ public function getContainerName(): string { @@ -29,10 +29,27 @@ class Service } /** - * @return array + * @return string */ public function getImage(): string { return (isset($this->service['image'])) ? $this->service['image'] : ''; } + + /** + * @return string + */ + public function getImageVersion(): string + { + $image = $this->getImage(); + return substr($image, strpos($image, ':')+1); + } + + /** + * @return string + */ + public function getEnvironment(): Env + { + return $this->service['environment']; + } } diff --git a/src/Appwrite/Docker/Env.php b/src/Appwrite/Docker/Env.php index 47ef46e017..c1f25d5e4c 100644 --- a/src/Appwrite/Docker/Env.php +++ b/src/Appwrite/Docker/Env.php @@ -53,6 +53,16 @@ class Env return (isset($this->vars[$key])) ? $this->vars[$key] : ''; } + /** + * Get All Vars + * + * @return array + */ + public function list(): array + { + return $this->vars; + } + /** * @return string */ diff --git a/tests/unit/Docker/ComposeTest.php b/tests/unit/Docker/ComposeTest.php index 59cf3cffb4..a682269a07 100644 --- a/tests/unit/Docker/ComposeTest.php +++ b/tests/unit/Docker/ComposeTest.php @@ -39,6 +39,8 @@ class ComposeTest extends TestCase $this->assertCount(17, $this->object->getServices()); $this->assertEquals('appwrite-telegraf', $this->object->getService('telegraf')->getContainerName()); $this->assertEquals('appwrite', $this->object->getService('appwrite')->getContainerName()); + $this->assertEquals('', $this->object->getService('appwrite')->getImageVersion()); + $this->assertEquals('2.2', $this->object->getService('traefik')->getImageVersion()); } public function testNetworks()