Snippets

Oleksiy Kalinichenko ROBO / Example / Docker-Compose

Created by Oleksiy Kalinichenko last modified
<?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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.