Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Nuß committed Jan 8, 2018
2 parents b534fcd + 1e3cb1d commit b487bec
Show file tree
Hide file tree
Showing 18 changed files with 343 additions and 43 deletions.
10 changes: 9 additions & 1 deletion bin/magedev
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
*
* @license https://opensource.org/licenses/mit-license MIT License
*/
require __DIR__ . '/../vendor/autoload.php';
$loader = require (__DIR__ . '/../vendor/autoload.php');

$src = getcwd() . "/.magedev/Docker/";
$loader->setPsr4('TeamNeusta\\Magedev\\Docker\\', $src);
$loader->addPsr4('TeamNeusta\\Magedev\\Docker\\', $src, true);

$loader->register();
$loader->setUseIncludePath(true);

$container = require(__DIR__ . '/../src/Runtime/Container.php');

$application = $container['application'];
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"autoload": {
"psr-4" : {
"TeamNeusta\\Magedev\\Docker\\" : "src/Docker/",
"TeamNeusta\\Magedev\\" : "src/",
"TeamNeusta\\Magedev\\Test\\" : "tests/"
}
Expand Down
132 changes: 132 additions & 0 deletions docs/custom-containers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Custom containers

Lets see how you can change existing preconfigured docker containers with custom ones, e.g. to change version of specific services or add other custom containers. Magedev does not use an orchestrator like `docker-composer` or `Dockerfile`s directly. Instead all configuration is written in PHP to avoid dependency to `docker-composer`. This configuration is done in `src/Docker/Container` and `src/Docker/Image` respectively. You can change these classes and replace them with custom ones.

## Example: changing Version of mysql

Create a file `.magedev/Docker/Image/Repository/Mysql.php` in your project root with following content. The original file path is `src/Docker/Image/Repository/Mysql.php` and you may copy the content from there as well.

<?php
/**
* This file is part of the teamneusta/php-cli-magedev package.
*
* Copyright (c) 2017 neusta GmbH | Ein team neusta Unternehmen
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*
* @license https://opensource.org/licenses/mit-license MIT License
*/

namespace TeamNeusta\Magedev\Docker\Image\Repository;

use TeamNeusta\Magedev\Docker\Image\AbstractImage;

/**
* Class Mysql.
*/
class Mysql extends AbstractImage
{
/**
* getBuildName.
*
* @return string
*/
public function getBuildName()
{
return $this->nameBuilder->buildName(
$this->getName()
);
}

/**
* configure.
*/
public function configure()
{
$this->name('mysql');
$this->from('mysql:5.6');

$uid = getmyuid();
$this->run('usermod -u '.$uid.' mysql');

// addresses permission error, cannot bind socket
$this->run("chmod -R 777 /var/run/mysqld/");

$this->addFile("var/Docker/mysql/mysql.cnf", "/etc/mysql/conf.d/z99-docker.cnf");
$this->run("chmod 644 /etc/mysql/conf.d/z99-docker.cnf");

$this->addFile("var/Docker/mysql/my.cnf","/root/.my.cnf");
$this->addFile("var/Docker/mysql/my.cnf","/var/www/.my.cnf");
}
}

Change this file e.g. to use mysql:5.5 by changing this line:

$this->from('mysql:5.5');

## Example using custom MongoDB container

.magedev/Docker/Container/Repository/MongoDB.php


<?php
/**
* This file is part of the teamneusta/php-cli-magedev package.
*
* Copyright (c) 2017 neusta GmbH | Ein team neusta Unternehmen
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*
* @license https://opensource.org/licenses/mit-license MIT License
*/

namespace TeamNeusta\Magedev\Docker\Container\Repository;

use TeamNeusta\Magedev\Docker\Container\AbstractContainer;

/**
* Class: MongoDB.
*
* @see AbstractContainer
*/
class MongoDB extends AbstractContainer
{
/**
* getName.
*/
public function getName()
{
return 'mongodb';
}

/**
* getImage.
*/
public function getImage()
{
return 'mongo:3.0';
}
}

Add this to your `magedev.json` to include the custom container and link it:

"docker": {
"containers": [
"ElasticSearch",
"Mailcatcher",
"Main",
"Mysql",
"Redis",
"Varnish",
"MongoDB"
],
"links": {
"main": ["mysql", "redis", "elasticsearch", "mongodb"]
}
]

## Apply changes

To apply changes made to docker configuration use the reinit command:

magedev docker:reinit
1 change: 1 addition & 0 deletions src/Commands/Docker/DestroyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function configure()
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$this->getApplication()->find('docker:stop')->execute($input, $output);
$this->dockerService->getManager()->destroyContainers();
parent::execute($input, $output);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Commands/Init/PermissionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public function execute(InputInterface $input, OutputInterface $output)
}

$commands = [
'mkdir -p /var/www/html',
'mkdir -p /var/www/.composer',
'mkdir -p /var/www/.ssh',
'mkdir -p /var/www/modules',
'mkdir -p /var/www/composer-cache',
'chown -R www-data:users /var/www/html',
'chown -R www-data:users /var/www/.composer',
'chown -R www-data:users /var/www/.ssh',
Expand Down
20 changes: 19 additions & 1 deletion src/Commands/Magento/AlignConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ class AlignConfigCommand extends AbstractCommand
*/
protected $config;

/**
* @var \TeamNeusta\Magedev\Services\DockerService
*/
protected $dockerService;

/**
* __construct.
*
* @param \TeamNeusta\Magedev\Runtime\Config $config
*/
public function __construct(
\TeamNeusta\Magedev\Runtime\Config $config
\TeamNeusta\Magedev\Runtime\Config $config,
\TeamNeusta\Magedev\Services\DockerService $dockerService
) {
$this->config = $config;
$this->dockerService = $dockerService;
parent::__construct();
}

Expand Down Expand Up @@ -66,7 +73,9 @@ public function execute(InputInterface $input, OutputInterface $output)

if ($magentoVersion == '2') {
$this->updateMagento2Credentials($wd, 'magento', 'magento', 'magento', 'mysql');
$this->removeCustomAdminUrlPath();
}
$this->getApplication()->find('magento:cache:clean')->execute($input, $output);
}

/**
Expand Down Expand Up @@ -137,4 +146,13 @@ public function updateMagento2Credentials($wd, $db, $username, $password, $host)
$content = "<?php\nreturn ".var_export($data, true).";\n";
file_put_contents($envFile, $content);
}

/**
* removeCustomAdminUrlPath
*/
public function removeCustomAdminUrlPath()
{
$cmd = "mysql --execute=\"delete from core_config_data where (path='admin/url/use_custom_path' OR path='admin/url/custom_path');\"";
$this->dockerService->execute($cmd);
}
}
4 changes: 2 additions & 2 deletions src/Commands/Magento/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ protected function configure()
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->input->getArgument(self::ARGUMENT_MAGENTO_COMMAND);
$arguments = $this->input->getArgument(self::ARGUMENT_MAGENTO_COMMAND_ARGS);
$command = $input->getArgument(self::ARGUMENT_MAGENTO_COMMAND);
$arguments = $input->getArgument(self::ARGUMENT_MAGENTO_COMMAND_ARGS);

$shellCommand = sprintf(
'bin/magento %s %s',
Expand Down
10 changes: 10 additions & 0 deletions src/Commands/Magento/SetBaseUrlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function execute(InputInterface $input, OutputInterface $output)
throw new \Exception('could not determine base_url');
}
$this->updateBaseUrl($baseUrl);
$this->deleteBaseLinkUrls();
}
$this->getApplication()->find('magento:cache:clean')->execute($input, $output);
}
Expand All @@ -106,4 +107,13 @@ public function updateBaseUrl($baseUrl, $scopeId = 0)
$cmd = "mysql --execute=\"update core_config_data set value='".$baseUrl."' where (path='web/unsecure/base_url' OR path='web/secure/base_url') AND scope_id=".$scopeId.';"';
$this->dockerService->execute($cmd);
}

/**
* deleteBaseLinkUrls
*/
public function deleteBaseLinkUrls()
{
$cmd = "mysql --execute=\"delete from core_config_data where (path='web/unsecure/base_link_url' OR path='web/secure/base_link_url');\"";
$this->dockerService->execute($cmd);
}
}
33 changes: 23 additions & 10 deletions src/Docker/Api/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,26 @@ class Image
*/
protected $image;

/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;

/**
* __construct.
*
* @param \Docker\Manager\ImageManager $imageManager
* @param \TeamNeusta\Magedev\Docker\Image\AbstractImage $image
* @param \Docker\Manager\ImageManager $imageManager
* @param \TeamNeusta\Magedev\Docker\Image\AbstractImage $image
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
public function __construct(
\Docker\Manager\ImageManager $imageManager,
\TeamNeusta\Magedev\Docker\Image\AbstractImage $image
\TeamNeusta\Magedev\Docker\Image\AbstractImage $image,
\Symfony\Component\Console\Output\OutputInterface $output
) {
$this->imageManager = $imageManager;
$this->image = $image;
$this->output = $output;
}

/**
Expand Down Expand Up @@ -79,15 +87,15 @@ public function build()
$name = $this->image->getBuildName();
if (!$this->exists()) {
$contextBuilder = $this->image->getContextBuilder();
// TODO: move this somewhere else
/* foreach ($this->context->getEnvVars() as $key => $value) { */
/* $contextBuilder->env($key, $value); */
/* } */
$context = $contextBuilder->getContext();

$buildName = $this->image->getBuildName();
$this->output->writeln("<fg=green>Building image " . $buildName . "...</>");

$buildStream = $this->imageManager->build($context->read(), [
't' => $this->image->getBuildName(),
't' => $buildName,
'rm' => true,
'nocache' => false,
'nocache' => true,
], ImageManager::FETCH_STREAM);

$buildStream->onFrame(function (BuildInfo $buildInfo) {
Expand All @@ -110,6 +118,7 @@ public function pull()
{
$name = $this->image->getBuildName();
if (!$this->exists()) {
$this->output->writeln("<fg=green>Pulling image " . $name . "...</>");
$buildStream = $this->imageManager->create(
null,
[
Expand All @@ -128,7 +137,11 @@ public function pull()
*/
public function destroy()
{
// TODO
if ($this->exists()) {
$name = $this->image->getBuildName();
$this->output->writeln("<fg=red>Deleting image " . $name . "...</>");
$this->imageManager->remove($name);
}
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/Docker/Api/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace TeamNeusta\Magedev\Docker\Api;

use Symfony\Component\Console\Output\OutputInterface;

/**
* Class ImageFactory.
*/
Expand All @@ -21,16 +23,24 @@ class ImageFactory
*/
protected $imageManager;

/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;

/**
* __construct.
*
* @param \Docker\Manager\ImageManager $imageManager
* @param \TeamNeusta\Magedev\Docker\Image\AbstractImage $image
* @param \Docker\Manager\ImageManager $imageManager
* @param \TeamNeusta\Magedev\Docker\Image\AbstractImage $image
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
public function __construct(
\Docker\Manager\ImageManager $imageManager
\Docker\Manager\ImageManager $imageManager,
\Symfony\Component\Console\Output\OutputInterface $output
) {
$this->imageManager = $imageManager;
$this->output = $output;
}

/**
Expand All @@ -40,6 +50,6 @@ public function __construct(
*/
public function create(\TeamNeusta\Magedev\Docker\Image\AbstractImage $image)
{
return new Image($this->imageManager, $image);
return new Image($this->imageManager, $image, $this->output);
}
}
Loading

0 comments on commit b487bec

Please sign in to comment.