-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5470b16
commit 68ffb65
Showing
9 changed files
with
348 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.