Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:rokde/rancherize
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Kummer committed Nov 28, 2019
2 parents 0f7091e + c8e57ed commit 1727328
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Events;

use Rancherize\Blueprint\Infrastructure\Service\Service;
use Rancherize\Blueprint\Infrastructure\Service\ServiceYamlDefinition;
use Symfony\Component\EventDispatcher\Event;

/**
* Class ServiceWriterServicePreparedEvent
* @package Rancherize\Blueprint\Infrastructure\Service\Events
*/
class ServiceWriterWritePreparedEvent extends Event {

const NAME = 'service-writer.prepared-to-write';

/**
* @var Service
*/
private $service;

/**
* @var int
*/
private $fileVersion = 2;
/**
* @var ServiceYamlDefinition
*/
private $definition;

/**
*
* /**
* ServiceWriterServicePreparedEvent constructor.
* @param Service $service
* @param ServiceYamlDefinition $definition
*/
public function __construct( Service $service, ServiceYamlDefinition $definition ) {
$this->service = $service;
$this->definition = $definition;
}

/**
* @return Service
*/
public function getService() {
return $this->service;
}

/**
* @return int
*/
public function getFileVersion(): int
{
return $this->fileVersion;
}

/**
* @return ServiceYamlDefinition
*/
public function getDefinition(): ServiceYamlDefinition
{
return $this->definition;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function __construct( Service $service ) {
* @return string
*/
public function getNetworkMode(): string {
$targetNetworkMode = $this->service->getNetworkModeObject();
if($targetNetworkMode instanceof self)
return $targetNetworkMode->getNetworkMode();

return container( 'shared-network-mode' ) . $this->service->getName();
}
}
85 changes: 85 additions & 0 deletions app/Blueprint/Infrastructure/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class Service {
*/
protected $copyVolumesFrom = [];

/**
* @var Service
*/
protected $mantleService;

const RESTART_UNLESS_STOPPED = 0;
const RESTART_NEVER = 1;
/**
Expand Down Expand Up @@ -247,6 +252,9 @@ public function addVolumeFrom(Service $service) {
* @return \int[]
*/
public function getExposedPorts(): array {
if($this->isMantled())
return [];

return $this->exposedPorts;
}

Expand All @@ -255,6 +263,11 @@ public function getExposedPorts(): array {
* @param int $externalPort
*/
public function expose(int $internalPort, int $externalPort) {
if($this->isMantled()) {
$this->mantleService->expose($internalPort, $externalPort);
return;
}

$this->exposedPorts[$internalPort] = $externalPort;
}

Expand Down Expand Up @@ -358,6 +371,10 @@ public function setKeepStdin(bool $keepStdin) {
* @return Service[]
*/
public function getLinks(): array {
if($this->isMantled()) {
return [];
}

return $this->links;
}

Expand All @@ -369,6 +386,11 @@ public function addLink(Service $service, $name = null) {
if($service === $this)
return;

if($this->isMantled()) {
$this->mantleService->addLink($service, $name);
return;
}

if($name === null) {
$this->links[] = $service;
return;
Expand All @@ -382,6 +404,11 @@ public function addLink(Service $service, $name = null) {
* @param Service $service
*/
public function addLinksFrom(Service $service) {
if($this->isMantled()) {
$this->mantleService->addLinksFrom($service);
return;
}

$links = $service->getLinks();
foreach($links as $name => $link) {
if (is_numeric($name)) {
Expand All @@ -406,6 +433,10 @@ public function addLinksFrom(Service $service) {
* @return \string[]
*/
public function getExternalLinks(): array {
if($this->isMantled()) {
return [];
}

return $this->externalLinks;
}

Expand All @@ -414,6 +445,11 @@ public function getExternalLinks(): array {
* @param string $name
*/
public function addExternalLink(string $externalLink, string $name = null) {
if($this->isMantled()) {
$this->mantleService->addExternalLink($externalLink, $name);
return;
}

if($name === null) {
$this->externalLinks[] = $externalLink;
return;
Expand Down Expand Up @@ -457,6 +493,11 @@ public function getLabels(): array {
return $this->labels;
}

public function copyLabels(Service $copyFrom)
{
$this->labels = $copyFrom->labels;
}

/**
* @param string $name
* @param string $label
Expand All @@ -469,16 +510,40 @@ public function addLabel(string $name, string $label) {
* @return Service[]
*/
public function getSidekicks(): array {
if($this->isMantled()) {
return [];
}

return $this->sidekicks;
}

public function copySidekicks(Service $copyFrom)
{
if($this->isMantled()) {
$this->mantleService->copySidekicks($copyFrom);
return;
}

$this->sidekicks = $copyFrom->sidekicks;
}

/**
* @param Service $sidekicks
*/
public function addSidekick(Service $sidekicks) {
if($this->isMantled()) {
$this->mantleService->addSidekick($sidekicks);
return;
}

$this->sidekicks[] = $sidekicks;
}

public function resetSidekicks()
{
$this->sidekicks = [];
}

/**
* @return bool
*/
Expand Down Expand Up @@ -528,6 +593,14 @@ public function setWorkDir( string $workDir ) {
$this->workDir = $workDir;
}

/**
* @return DefaultNetworkMode|NetworkMode
*/
public function getNetworkModeObject()
{
return $this->networkMode;
}

/**
* @return string
*/
Expand Down Expand Up @@ -572,5 +645,17 @@ public function addCopyVolumesFrom(Service $service)
$this->copyVolumesFrom[] = $service;
}

/**
* @param Service $mantleService
* @return Service
*/
public function setMantleService(Service $mantleService): Service
{
$this->mantleService = $mantleService;
return $this;
}

protected function isMantled() {
return ($this->mantleService instanceof Service);
}
}
15 changes: 13 additions & 2 deletions app/Blueprint/Infrastructure/Service/ServiceWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use Rancherize\Blueprint\Infrastructure\Dockerfile\Dockerfile;
use Rancherize\Blueprint\Infrastructure\Dockerfile\DockerfileWriter;
use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterServicePreparedEvent;
use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterWritePreparedEvent;
use Rancherize\Configuration\Exceptions\FileNotFoundException;
use Rancherize\File\FileLoader;
use Rancherize\File\FileWriter;
Expand Down Expand Up @@ -180,13 +181,23 @@ public function write(Service $service, FileWriter $fileWriter) {

$rancherContentPreparedEvent = new ServiceWriterServicePreparedEvent($service, $content, $volumeDefinitions, $rancherContent);
$this->event->dispatch(ServiceWriterServicePreparedEvent::NAME, $rancherContentPreparedEvent);

// might have been changed by the listeners
$rancherContent = $rancherContentPreparedEvent->getRancherContent();
$content = $rancherContentPreparedEvent->getDockerContent();
$volumeDefinitions = $rancherContentPreparedEvent->getVolumeDefinition();

$this->writeYaml($this->path . '/docker-compose.yml', $service, $fileWriter, $content, $volumeDefinitions);
$this->writeYaml($this->path . '/rancher-compose.yml', $service, $fileWriter, $rancherContent);

$definition = ServiceYamlDefinition::make($content, $rancherContent, $volumeDefinitions);
$this->event->dispatch(ServiceWriterWritePreparedEvent::NAME,
new ServiceWriterWritePreparedEvent(
$service,
$definition
)
);

$this->writeYaml($this->path . '/docker-compose.yml', $service, $fileWriter, $definition->dockerComposeEntry, $definition->volumeDefinition);
$this->writeYaml($this->path . '/rancher-compose.yml', $service, $fileWriter, $definition->rancherComposeEntry);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions app/Blueprint/Infrastructure/Service/ServiceYamlDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service;

/**
* Class ServiceYamlDefinition
* @package Rancherize\Blueprint\Infrastructure\Service
*/
class ServiceYamlDefinition
{

/**
* @var array
*/
public $dockerComposeEntry = [];

/**
* @var array
*/
public $rancherComposeEntry = [];

/**
* @var array
*/
public $volumeDefinition = [];

/**
* @param $dockerComposeEntry
* @param $rancherComposeEntry
* @param $volumeDefinition
* @return ServiceYamlDefinition
*/
static function make($dockerComposeEntry, $rancherComposeEntry, $volumeDefinition) {
$definition = new self;
$definition->dockerComposeEntry = $dockerComposeEntry;
$definition->rancherComposeEntry = $rancherComposeEntry;
$definition->volumeDefinition = $volumeDefinition;
return $definition;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ public function setImageVersion($version)

$this->setImage('ipunktbs/laravel-queue-worker:' . $version);
}
}
}
Loading

0 comments on commit 1727328

Please sign in to comment.