2020-07-29 07:48:51 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
global $cli;
|
|
|
|
|
2020-07-31 18:31:29 +12:00
|
|
|
use Appwrite\Docker\Compose;
|
2020-08-01 00:49:44 +12:00
|
|
|
use Appwrite\Docker\Env;
|
2020-07-29 07:48:51 +12:00
|
|
|
use Utopia\CLI\Console;
|
2020-07-29 16:03:28 +12:00
|
|
|
use Utopia\Config\Config;
|
2020-07-29 18:56:39 +12:00
|
|
|
use Utopia\View;
|
2020-07-29 07:48:51 +12:00
|
|
|
|
|
|
|
$cli
|
|
|
|
->task('install')
|
|
|
|
->desc('Install Appwrite')
|
|
|
|
->action(function () {
|
|
|
|
/**
|
|
|
|
* 1. Start - DONE
|
2020-07-31 18:31:29 +12:00
|
|
|
* 2. Check for older setup and get older version - DONE
|
2020-07-29 07:48:51 +12:00
|
|
|
* 2.1 If older version is equal or bigger(?) than current version, **stop setup**
|
2020-07-31 18:31:29 +12:00
|
|
|
* 2.2. Get ENV vars - DONE
|
2020-07-29 07:48:51 +12:00
|
|
|
* 2.2.1 Fetch from older docker-compose.yml file
|
|
|
|
* 2.2.2 Fetch from older .env file (manually parse)
|
2020-07-29 19:29:34 +12:00
|
|
|
* 2.3 Use old ENV vars as default values
|
2020-07-29 07:48:51 +12:00
|
|
|
* 2.4 Ask for all required vars not given as CLI args and if in interactive mode
|
|
|
|
* 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
|
2020-07-31 18:31:29 +12:00
|
|
|
* 4. Drop new docker-compose.yml setup (located inside the container, no network dependencies with appwrite.io) - DONE
|
2020-07-29 18:56:39 +12:00
|
|
|
* 5. Run docker-compose up -d - DONE
|
2020-07-29 07:48:51 +12:00
|
|
|
* 6. Run data migration
|
|
|
|
*/
|
2020-07-29 16:03:28 +12:00
|
|
|
$vars = Config::getParam('variables');
|
2020-07-31 18:31:29 +12:00
|
|
|
$path = '/usr/src/code/appwrite';
|
|
|
|
$version = null;
|
2020-08-01 00:49:44 +12:00
|
|
|
$defaultHTTPPort = '80';
|
|
|
|
$defaultHTTPSPort = '443';
|
2020-07-29 07:48:51 +12:00
|
|
|
|
|
|
|
Console::success('Starting Appwrite installation...');
|
|
|
|
|
2020-07-31 18:31:29 +12:00
|
|
|
// 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);
|
2020-08-01 00:49:44 +12:00
|
|
|
$appwrite = $compose->getService('appwrite');
|
|
|
|
$version = ($appwrite) ? $appwrite->getImageVersion() : $version;
|
|
|
|
$ports = $compose->getService('traefik')->getPorts();
|
2020-07-31 18:31:29 +12:00
|
|
|
|
|
|
|
if($version) {
|
|
|
|
foreach($compose->getServices() as $service) { // Fetch all env vars from previous compose file
|
|
|
|
if(!$service) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$env = $service->getEnvironment()->list();
|
|
|
|
|
2020-08-01 00:49:44 +12:00
|
|
|
foreach ($env as $key => $value) {
|
|
|
|
foreach($vars as &$var) {
|
|
|
|
if($var['name'] === $key) {
|
|
|
|
$var['default'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = @file_get_contents($path.'/.env');
|
|
|
|
|
|
|
|
if($data !== false) { // Fetch all env vars from previous .env file
|
|
|
|
$env = new Env($data);
|
|
|
|
|
|
|
|
foreach ($env->list() as $key => $value) {
|
|
|
|
foreach($vars as &$var) {
|
|
|
|
if($var['name'] === $key) {
|
|
|
|
$var['default'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 18:31:29 +12:00
|
|
|
}
|
|
|
|
|
2020-08-01 00:49:44 +12:00
|
|
|
foreach ($ports as $key => $value) {
|
|
|
|
if($value === $defaultHTTPPort) {
|
|
|
|
$defaultHTTPPort = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($value === $defaultHTTPSPort) {
|
|
|
|
$defaultHTTPSPort = $key;
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 18:31:29 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 00:49:44 +12:00
|
|
|
$httpPort = Console::confirm('Choose your server HTTP port: (default: '.$defaultHTTPPort.')');
|
|
|
|
$httpPort = ($httpPort) ? $httpPort : $defaultHTTPPort;
|
2020-07-29 19:29:34 +12:00
|
|
|
|
2020-08-01 00:49:44 +12:00
|
|
|
$httpsPort = Console::confirm('Choose your server HTTPS port: (default: '.$defaultHTTPSPort.')');
|
|
|
|
$httpsPort = ($httpsPort) ? $httpsPort : $defaultHTTPPort;
|
2020-07-29 19:29:34 +12:00
|
|
|
|
2020-07-29 07:48:51 +12:00
|
|
|
$input = [];
|
|
|
|
|
|
|
|
foreach($vars as $key => $var) {
|
|
|
|
if(!$var['required']) {
|
2020-07-29 16:03:28 +12:00
|
|
|
$input[$var['name']] = $var['default'];
|
2020-07-29 07:48:51 +12:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-29 16:03:28 +12:00
|
|
|
$input[$var['name']] = Console::confirm($var['question'].' (default: \''.$var['default'].'\')');
|
2020-07-29 07:48:51 +12:00
|
|
|
|
|
|
|
if(empty($input[$key])) {
|
2020-07-29 16:03:28 +12:00
|
|
|
$input[$var['name']] = $var['default'];
|
2020-07-29 07:48:51 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:29:34 +12:00
|
|
|
$templateForCompose = new View(__DIR__.'/../views/install/compose.phtml');
|
|
|
|
$templateForEnv = new View(__DIR__.'/../views/install/env.phtml');
|
2020-07-29 18:56:39 +12:00
|
|
|
|
|
|
|
$templateForCompose
|
|
|
|
->setParam('httpPort', $httpPort)
|
|
|
|
->setParam('httpsPort', $httpsPort)
|
|
|
|
->setParam('version', APP_VERSION_STABLE)
|
|
|
|
;
|
2020-07-29 07:48:51 +12:00
|
|
|
|
2020-07-29 18:56:39 +12:00
|
|
|
$templateForEnv
|
|
|
|
->setParam('vars', $input)
|
|
|
|
;
|
2020-07-29 07:48:51 +12:00
|
|
|
|
2020-08-01 00:50:39 +12:00
|
|
|
if(!file_put_contents($path.'/docker-compose.yml', $templateForCompose->render(false))) {
|
2020-07-29 18:56:39 +12:00
|
|
|
Console::error('Failed to save Docker Compose file');
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-08-01 00:50:39 +12:00
|
|
|
if(!file_put_contents($path.'/.env', $templateForEnv->render(false))) {
|
2020-07-29 18:56:39 +12:00
|
|
|
Console::error('Failed to save environment variables file');
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-07-29 07:48:51 +12:00
|
|
|
|
2020-07-29 16:03:28 +12:00
|
|
|
$stdout = '';
|
|
|
|
$stderr = '';
|
2020-07-29 07:48:51 +12:00
|
|
|
|
2020-09-16 08:26:34 +12:00
|
|
|
Console::execute("docker-compose -f {$path}.'/docker-compose.yml up -d --remove-orphans", null, $stdout, $stderr);
|
2020-07-29 18:56:39 +12:00
|
|
|
|
|
|
|
if ($stderr !== '') {
|
2020-07-29 07:48:51 +12:00
|
|
|
Console::error("Failed to install Appwrite dockers");
|
|
|
|
} else {
|
|
|
|
Console::success("Appwrite installed successfully");
|
|
|
|
}
|
|
|
|
});
|