Skip to content

Commit

Permalink
Use Docker for Elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
mischabraam committed Dec 4, 2023
1 parent 5470b16 commit 68ffb65
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 45 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Here are a few key differences compared to the original Valet:
- Rabbitmq (on/off mode)
- Xdebug (on/off mode)
- Memcache (on/off mode)
- Elasticsearch v6, ~~v7~~, ~~v8~~ (on/off mode)
- Elasticsearch v6, v7, ~~v8~~ (on/off mode) using Docker
- Opensearch (on/off mode)
- ~~Ioncube~~
- Rewrite/unrewrite public domain to local environment
Expand All @@ -70,6 +70,7 @@ Here are a few key differences compared to the original Valet:
- Use command `valet-plus elasticsearch|es use <version>` instead of `valet-plus use elasticsearch|es <version>`.
- Use `127.0.0.1` as Redis host instead of `/tmp/redis.sock`.
- Choose which binaries to install (default all) and self-update on `valet-plus install` command.
- Adds dependency on Docker for Elasticsearch, see https://docs.docker.com/desktop/install/mac-install/



Expand Down
202 changes: 202 additions & 0 deletions cli/ValetPlus/AbstractDockerService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

declare(strict_types=1);

namespace WeProvide\ValetPlus;

use DomainException;
use Illuminate\Support\Collection;
use Valet\CommandLine;
use Valet\Filesystem;
use function Valet\info;
use function Valet\output;

/**
* Docker container service. Please note that container names are restricted to the following characters [a-zA-Z0-9][a-zA-Z0-9_.-].
* Obviously this has a dependency on the installation of Docker. For convenience install Docker Desktop.
*/
abstract class AbstractDockerService
{
/** @var string */
const DOCKER_COMPOSE_STUB = __DIR__ . '/../stubs/%s/%s/docker-compose.yml';
/** @var string */
const DOCKER_COMPOSE_PATH = VALET_HOME_PATH . '/Docker/%s/%s/docker-compose.yml';

/** @var CommandLine */
protected $cli;
/** @var Filesystem */
protected $files;
/** @var string */
protected $serviceName;

/**
* @param CommandLine $cli
* @param Filesystem $files
*/
public function __construct(
CommandLine $cli,
Filesystem $files
) {
$this->cli = $cli;
$this->files = $files;
}

/**
* Returns a collection of names of the running docker containers.
*
* @return Collection
*/
public function getAllRunningContainers(): Collection
{
$command = 'docker ps --format=\'{{.Names}}\'';
$onError = function ($exitCode, $errorOutput) {
output($errorOutput);

throw new DomainException('Docker was unable to check which containers are running.');
};

return collect(array_filter(explode(PHP_EOL, $this->cli->runAsUser($command, $onError))));
}

/**
* Runs a docker command from the path where the docker-compose.yml is located.
*
* @param $command
* @param $dir
* @return $this
*/
public function runCommand($command, $dir)
{
$cwd = getcwd();
@chdir($dir);
$this->cli->runAsUser($command);
@chdir($cwd);

return $this;
}

/**
* Starts the docker container by the service's name. Creates the container first, if it doesn't exist yet.
*
* @param $name
* @return $this
*/
public function upContainer($name)
{
info("Docker up version {$name}...");
$installPath = $this->getComposeInstallPath($name);
$installDir = $this->getComposeInstallDir($name);

// Copy docker-compose.yml stub to installation/running path
if (!$this->files->isDir($installDir)) {
$this->files->mkdirAsUser($installDir);
}
$this->files->copyAsUser(
$this->getComposeStubPath($name),
$installPath
);

// Start the container the directory where the docker-compose.yml exists.
$this->runCommand(
'docker compose up --detach',
$installDir
);

return $this;
}

/**
* Stops the docker container by the service's name.
*
* @param $name
* @return $this
*/
public function stopContainer($name)
{
info("Docker stop version {$name}...");
$this->runCommand(
'docker compose stop',
$this->getComposeInstallDir($name)
);

return $this;
}

/**
* Stop and remove containers, networks, images and volumes by the service's name.
*
* @param $name
* @return $this
*/
public function downContainer($name)
{
info("Docker down version {$name}...");
$this->runCommand(
'docker compose down --volumes --rmi all',
$this->getComposeInstallDir($name)
);

return $this;
}

/**
* Returns the short class name in lowercase.
*
* @return string
*/
protected function getServiceName(): string
{
if (!$this->serviceName) {
try {
// We store the service's name in a property to prevent a lot of reflection (which is slow).
$this->serviceName = strtolower((new \ReflectionClass($this))->getShortName());
} catch (\ReflectionException $reflectionException) {
echo 'Ohoh reflection exception';
die();
}
}

return $this->serviceName;
}

/**
* Returns path of the docker-compose.yml stub file for the service.
*
* @param $name
* @return string
*/
protected function getComposeStubPath($name): string
{
return sprintf(
static::DOCKER_COMPOSE_STUB,
$this->getServiceName(),
$name
);
}

/**
* Returns installation path of the docker-compose.yml stub file for the service.
*
* @param $name
* @return string
*/
protected function getComposeInstallPath($name): string
{
return sprintf(
static::DOCKER_COMPOSE_PATH,
$this->getServiceName(),
$name
);
}

/**
* Returns the directory of the installation path of the docker-compose.yml stub file for the service.
*
* @param $name
* @return string
*/
protected function getComposeInstallDir($name): string
{
return dirname($this->getComposeInstallPath($name));
}
}
1 change: 1 addition & 0 deletions cli/ValetPlus/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function getConfigClassName(): string
{
if (!$this->configClassName) {
try {
// We store the service's name in a property to prevent a lot of reflection (which is slow).
$this->configClassName = strtolower((new \ReflectionClass($this))->getShortName());
} catch (\ReflectionException $reflectionException) {
echo 'Ohoh reflection exception';
Expand Down
Loading

0 comments on commit 68ffb65

Please sign in to comment.