Snippets
Created by
Oleksiy Kalinichenko
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | <?php
use Dotenv\Dotenv;
use Dotenv\Exception\InvalidPathException;
use DrupalFinder\DrupalFinder;
use Robo\ResultData;
use Robo\Tasks;
use Tivie\OS\Detector as OsDetector;
/**
* Robo task runner for "docker-compose".
*
* @see http://robo.li/
*/
class RoboFile extends Tasks {
use \Droath\RoboDockerCompose\Task\loadTasks;
/**
* @var string
*/
protected $projectRoot;
/**
* @var \Dotenv\Dotenv
*/
protected $dotEnv;
/**
* @var array
*/
protected $dotEnvVariables = [
'COMPOSER_PROJECT_NAME',
'PROJECT_NAME',
'PROJECT_BASE_URL',
'PROJECT_STAGE_URL',
'DB_NAME',
'DB_USER',
'DB_PASSWORD',
'DB_ROOT_PASSWORD',
'DB_HOST',
'DB_DRIVER',
];
/**
* @var \DrupalFinder\DrupalFinder
*/
protected $drupalFinder;
/**
* @var OSDetector
*/
protected $osDetector;
/**
* @var array
*/
protected $supportedOSs;
/**
* RoboFile constructor.
*/
public function __construct() {
$this->projectRoot = dirname(__DIR__, 2);
$this->dotEnv = new Dotenv($this->projectRoot);
$this->drupalFinder = new DrupalFinder();
$this->osDetector = new OsDetector();
// Try to detect Drupal root.
$this->drupalFinder->locateRoot($this->projectRoot);
// Set supported OSs.
$this->supportedOSs = [
\Tivie\OS\LINUX,
\Tivie\OS\MACOSX,
\Tivie\OS\WINDOWS,
];
}
/**
* Run Docker.
*
* @return \Robo\ResultData
*/
public function runDocker(): \Robo\ResultData {
$this->io()->title('Start running Docker Containers!');
// Get project required ".env" variables.
try {
$this->dotEnv->load();
}
catch (InvalidPathException $e) {
return new ResultData(ResultData::EXITCODE_ERROR, 'Error: Can not load ".env" file.');
}
// Check project required ".env" variables.
$dotenv_variables = [];
foreach ($this->dotEnvVariables as $dotenv_variable_name) {
if (!($dotenv_variable_value = getenv($dotenv_variable_name))) {
return new ResultData(ResultData::EXITCODE_ERROR, 'Error: Variable "' . $dotenv_variable_name . '" is missed.');
}
else {
$dotenv_variables[] = [$dotenv_variable_name, $dotenv_variable_value];
}
}
// Print project required ".env" variables.
$this->io()->section('.env Variables:');
$this->io()->table(['.env "Name"', '.env "Value"'], $dotenv_variables);
// Check Drupal project directory.
if (!($drupal_root = $this->drupalFinder->getDrupalRoot())) {
return new ResultData(ResultData::EXITCODE_ERROR, 'Error: Drupal project directory is missed.');
}
// Print Drupal project directory.
$this->io()->section('Drupal project directory:');
$this->say($drupal_root);
// Check is it supported OS.
if (!in_array($os_type = $this->osDetector->getType(), $this->supportedOSs)) {
return new ResultData(ResultData::EXITCODE_ERROR, 'Error: Current OS is not supported.');
}
// Print supported OS.
$this->io()->newLine();
$this->io()->section('Current / Host OS:');
$this->say($this->osDetector->getKernelName());
$this->io()->newLine();
// Try to run Docker containers on supported OS.
if ($os_type === \Tivie\OS\LINUX) {
$this->io()->section('Running Docker-Compose on LINUX:');
$this->runDockerOnLinux();
}
if ($os_type === \Tivie\OS\MACOSX) {
$this->io()->section('Running Docker-Compose on MACOS:');
$this->runDockerOnMac();
}
elseif ($os_type === \Tivie\OS\WINDOWS) {
$this->io()->section('Running Docker-Compose on WINDOWS:');
$this->runDockerOnWin();
}
return new ResultData(ResultData::EXITCODE_OK, 'Docker is running!');
}
/**
* Run Docker on Mac OS.
*/
protected function runDockerOnMac() {
// Command equivalent: `docker-composer up -d -remove-orphans`
$this->taskDockerComposeUp()
->detachedMode()
->file("{$this->projectRoot}/ci/docker-compose.dev.yml")
->file("{$this->projectRoot}/ci/docker-compose.dev.mac.yml")
->projectName(getenv('COMPOSER_PROJECT_NAME'))
->run();
}
/**
* Run Docker on Windows.
*/
protected function runDockerOnWin() {
// TODO: Add code for Windows
$this->say('Please add code to run docker containers on Windows');
}
/**
* Run Docker on GNU / Linux
*/
protected function runDockerOnLinux() {
// TODO: Add code for Linux
$this->say('Please add code to run docker containers on GNU/Linux');
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.