Skip to content

Commit

Permalink
Merge pull request #5 from sagarnasit/fix-service-command-enable
Browse files Browse the repository at this point in the history
Add global-services to service command
  • Loading branch information
rahulsprajapati committed Sep 25, 2018
2 parents bb9aae0 + 8b0dd36 commit ce461f1
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 5 deletions.
28 changes: 28 additions & 0 deletions service-command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
<?php

if ( ! defined( 'SERVICE_TEMPLATE_ROOT' ) ) {
define( 'SERVICE_TEMPLATE_ROOT', __DIR__ . '/templates' );
}

if ( ! defined( 'GLOBAL_DB' ) ) {
define( 'GLOBAL_DB', 'global-db' );
}

if ( ! defined( 'GLOBAL_DB_CONTAINER' ) ) {
define( 'GLOBAL_DB_CONTAINER', 'ee-global-db' );
}

if ( ! defined( 'GLOBAL_FRONTEND_NETWORK' ) ) {
define( 'GLOBAL_FRONTEND_NETWORK', 'ee-global-frontend-network' );
}

if ( ! defined( 'GLOBAL_BACKEND_NETWORK' ) ) {
define( 'GLOBAL_BACKEND_NETWORK', 'ee-global-backend-network' );
}

if ( ! defined( 'GLOBAL_REDIS' ) ) {
define( 'GLOBAL_REDIS', 'global-redis' );
}

if ( ! defined( 'GLOBAL_REDIS_CONTAINER' ) ) {
define( 'GLOBAL_REDIS_CONTAINER', 'ee-global-redis' );
}

if ( ! class_exists( 'EE' ) ) {
return;
}
Expand Down
27 changes: 22 additions & 5 deletions src/Service_Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Symfony\Component\Filesystem\Filesystem;

/**
* Manages global services of EasyEngine.
*
Expand All @@ -17,6 +19,8 @@ class Service_Command extends EE_Command {
*/
private $whitelisted_services = [
'nginx-proxy',
'mariadb',
'redis',
];

/**
Expand All @@ -43,9 +47,16 @@ public function __construct() {
*
*/
public function enable( $args, $assoc_args ) {
$service = $this->filter_service( $args );
$service = $this->filter_service( $args );
$service_name = "global-$service";
$container = "ee-$service_name";

if ( EE_PROXY_TYPE === $container ) {
\EE\Service\Utils\nginx_proxy_check();
} else {
\EE\Service\Utils\init_global_container( $service_name );
}

EE::exec( "docker-compose start $service", true, true );
}

/**
Expand All @@ -58,6 +69,8 @@ private function filter_service( $args ) {
EE::error( "Unable to find global EasyEngine service $args[0]" );
}

$services = array_values( $services );

return $services[0];
}

Expand Down Expand Up @@ -115,8 +128,12 @@ public function restart( $args, $assoc_args ) {
*/
public function reload( $args, $assoc_args ) {
$service = $this->filter_service( $args );
$command = $this->service_reload_command( $service );
EE::exec( "docker-compose exec $service $command", true, true );
$command = $this->service_reload_command( $service );
if ( $command ) {
EE::exec( "docker-compose exec $service $command", true, true );
} else {
EE::warning( "$service can not be reloaded." );
}
}

/**
Expand All @@ -132,6 +149,6 @@ private function service_reload_command( string $service ) {
'nginx-proxy' => "sh -c 'nginx -t && service nginx reload'",
];

return $command_map[ $service ];
return array_key_exists( $service, $command_map ) ? $command_map[ $service ] : false;
}
}
143 changes: 143 additions & 0 deletions src/helper/service-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace EE\Service\Utils;

use EE;
use Symfony\Component\Filesystem\Filesystem;

/**
* Boots up the container if it is stopped or not running.
* @throws EE\ExitException
*/
function nginx_proxy_check() {

$proxy_type = EE_PROXY_TYPE;
if ( 'running' !== EE::docker()::container_status( $proxy_type ) ) {
/**
* Checking ports.
*/
$port_80_status = get_curl_info( 'localhost', 80, true );
$port_443_status = get_curl_info( 'localhost', 443, true );

// if any/both the port/s is/are occupied.
if ( ! ( $port_80_status && $port_443_status ) ) {
EE::error( 'Cannot create/start proxy container. Please make sure port 80 and 443 are free.' );
} else {

$fs = new Filesystem();

if ( ! $fs->exists( EE_CONF_ROOT . '/docker-compose.yml' ) ) {
generate_global_docker_compose_yml( $fs );
}

$EE_CONF_ROOT = EE_CONF_ROOT;
if ( ! EE::docker()::docker_network_exists( GLOBAL_BACKEND_NETWORK ) &&
! EE::docker()::create_network( GLOBAL_BACKEND_NETWORK ) ) {
EE::error( 'Unable to create network ' . GLOBAL_BACKEND_NETWORK );
}
if ( ! EE::docker()::docker_network_exists( GLOBAL_FRONTEND_NETWORK ) &&
! EE::docker()::create_network( GLOBAL_FRONTEND_NETWORK ) ) {
EE::error( 'Unable to create network ' . GLOBAL_FRONTEND_NETWORK );
}
if ( EE::docker()::docker_compose_up( EE_CONF_ROOT, [ 'global-nginx-proxy' ] ) ) {
$fs->dumpFile( "$EE_CONF_ROOT/nginx/conf.d/custom.conf", file_get_contents( EE_ROOT . '/templates/custom.conf.mustache' ) );
EE::success( "$proxy_type container is up." );
} else {
EE::error( "There was some error in starting $proxy_type container. Please check logs." );
}
}
}
}

/**
* Function to start global conainer if it is not running.
*
* @param string $container Global container to be brought up.
*/
function init_global_container( $service, $container = '' ) {

if ( empty( $container ) ) {
$container = 'ee-' . $service;
}
if ( ! EE::docker()::docker_network_exists( GLOBAL_BACKEND_NETWORK ) &&
! EE::docker()::create_network( GLOBAL_BACKEND_NETWORK ) ) {
EE::error( 'Unable to create network ' . GLOBAL_BACKEND_NETWORK );
}

$fs = new Filesystem();

if ( ! $fs->exists( EE_CONF_ROOT . '/docker-compose.yml' ) ) {
generate_global_docker_compose_yml( $fs );
}

if ( 'running' !== EE::docker()::container_status( $container ) ) {
chdir( EE_CONF_ROOT );
EE::docker()::boot_container( $container, 'docker-compose up -d ' . $container );
}
}

/**
* Generates global docker-compose.yml at EE_CONF_ROOT
*
* @param Filesystem $fs Filesystem object to write file
*/
function generate_global_docker_compose_yml( Filesystem $fs ) {
$img_versions = EE\Utils\get_image_versions();

$data = [
'services' => [
[
'name' => 'global-nginx-proxy',
'container_name' => EE_PROXY_TYPE,
'image' => 'easyengine/nginx-proxy:' . $img_versions['easyengine/nginx-proxy'],
'restart' => 'always',
'ports' => [
'80:80',
'443:443',
],
'environment' => [
'LOCAL_USER_ID=' . posix_geteuid(),
'LOCAL_GROUP_ID=' . posix_getegid(),
],
'volumes' => [
EE_CONF_ROOT . '/nginx/certs:/etc/nginx/certs',
EE_CONF_ROOT . '/nginx/dhparam:/etc/nginx/dhparam',
EE_CONF_ROOT . '/nginx/conf.d:/etc/nginx/conf.d',
EE_CONF_ROOT . '/nginx/htpasswd:/etc/nginx/htpasswd',
EE_CONF_ROOT . '/nginx/vhost.d:/etc/nginx/vhost.d',
EE_CONF_ROOT . '/nginx/html:/usr/share/nginx/html',
'/var/run/docker.sock:/tmp/docker.sock:ro',
],
'networks' => [
'global-frontend-network',
],
],
[
'name' => GLOBAL_DB,
'container_name' => GLOBAL_DB_CONTAINER,
'image' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'],
'restart' => 'always',
'environment' => [
'MYSQL_ROOT_PASSWORD=' . \EE\Utils\random_password(),
],
'volumes' => [ './app/db:/var/lib/mysql' ],
'networks' => [
'global-backend-network',
],
],
[
'name' => GLOBAL_REDIS,
'container_name' => GLOBAL_REDIS_CONTAINER,
'image' => 'easyengine/redis:' . $img_versions['easyengine/redis'],
'restart' => 'always',
'volumes' => [ EE_CONF_ROOT . '/services/redis:/data' ],
'networks' => [
'global-backend-network',
],
],
],
];

$contents = EE\Utils\mustache_render( SERVICE_TEMPLATE_ROOT . '/global_docker_compose.yml.mustache', $data );
$fs->dumpFile( EE_CONF_ROOT . '/docker-compose.yml', $contents );
}
57 changes: 57 additions & 0 deletions templates/global_docker_compose.yml.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: '3.5'

services:

{{#services}}
{{name}}:
container_name: {{container_name}}
image: {{image}}
{{#ports.0}}
ports:
{{#ports}}
- "{{.}}"
{{/ports}}
{{/ports.0}}
{{#depends_on}}
depends_on:
- {{.}}
{{/depends_on}}
{{#restart}}
restart: {{.}}
{{/restart}}
{{#command}}
command: {{.}}
{{/command}}
{{#labels.0}}
labels:
{{#labels}}
- "{{.}}"
{{/labels}}
{{/labels.0}}
{{#volumes.0}}
volumes:
{{#volumes}}
- "{{.}}"
{{/volumes}}
{{/volumes.0}}
{{#environment.0}}
environment:
{{#environment}}
- {{.}}
{{/environment}}
{{/environment.0}}
{{#networks.0}}
networks:
{{#networks}}
- {{.}}
{{/networks}}
{{/networks.0}}
{{/services}}

networks:
global-frontend-network:
external:
name: ee-global-frontend-network
global-backend-network:
external:
name: ee-global-backend-network

0 comments on commit ce461f1

Please sign in to comment.