Skip to content

Commit

Permalink
Merge pull request #7 from rh-messaging-qe/fgiorgetti-remote-docker
Browse files Browse the repository at this point in the history
UPD: Enhanced docker service to handle containers running on a remote…
  • Loading branch information
enkeys authored Nov 13, 2018
2 parents d3d3d45 + 5e84c3e commit 65b1764
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
16 changes: 11 additions & 5 deletions messaging_components/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ class ServiceFactory(object):

@staticmethod
def create_service(executor: Executor, service_name: str=None, **kwargs) -> Service:

if service_name:
ServiceFactory._logger.debug("Creating ServiceSystem - name: %s - executor: %s"
% (service_name, executor.__class__.__name__))
return ServiceSystem(name=service_name, executor=executor)
elif isinstance(executor, ExecutorContainer):
ServiceFactory._logger.debug("Creating ServiceDocker - name: %s - executor: %s"
% (executor.container_name, executor.__class__.__name__))
return ServiceDocker(name=executor.container_name, executor=executor)
else:
container_name = None
if isinstance(executor, ExecutorContainer):
container_name = executor.container_name
elif isinstance(executor, ExecutorAnsible) and executor.ansible_connection == 'docker':
container_name = executor.ansible_host

if container_name:
ServiceFactory._logger.debug("Creating ServiceDocker - name: %s - executor: %s"
% (container_name, executor.__class__.__name__))
return ServiceDocker(name=container_name, executor=executor)

ServiceFactory._logger.debug("Unable to determine Service")
raise ValueError('Unable to determine service for server component')
24 changes: 16 additions & 8 deletions messaging_components/services/service_docker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from enum import Enum
from iqa_common.executor import Command, Execution, ExecutorAnsible, CommandAnsible, ExecutorContainer, CommandContainer
from typing import Union

from iqa_common.executor import Command, Execution, ExecutorAnsible, CommandAnsible, ExecutorContainer, \
CommandContainer, Executor
from iqa_common.utils.docker_util import DockerUtil
from messaging_abstract.component import Service, ServiceStatus
import logging
Expand All @@ -11,9 +14,13 @@ class ServiceDocker(Service):
So startup and shutdown are done by managing current state of related
docker container name.
"""

_logger = logging.getLogger(__name__)

def __init__(self, name: str, executor: Union[ExecutorAnsible, ExecutorContainer]):
super().__init__(name, executor)
self.docker_host = executor.docker_host
self.docker_util = DockerUtil(docker_host=executor.docker_host)

class ServiceDockerState(Enum):
STARTED = ('start', 'started')
STOPPED = ('stop', 'stopped')
Expand All @@ -30,7 +37,7 @@ def status(self) -> ServiceStatus:
:rtype: ServiceStatus
"""
try:
container = DockerUtil.get_container(self.name)
container = self.docker_util.get_container(self.name)
if not container:
ServiceDocker._logger.debug("Service: %s - Status: UNKNOWN" % self.name)
return ServiceStatus.UNKNOWN
Expand Down Expand Up @@ -83,14 +90,15 @@ def _create_command(self, service_state: ServiceDockerState):
if service_state == self.ServiceDockerState.RESTARTED:
restart = 'yes'

print('name=%s state=%s restart=%s' % (self.name, state, restart))
return CommandAnsible('name=%s state=%s restart=%s' % (self.name, state, restart),
print('name=%s state=%s restart=%s docker_host=%s'
% (self.name, state, restart, self.docker_host))

docker_host_opt = 'docker_host=%s' % self.docker_host if self.docker_host else ''
return CommandAnsible('name=%s state=%s restart=%s %s'
% (self.name, state, restart, docker_host_opt),
ansible_module='docker_container',
stdout=True,
timeout=self.TIMEOUT)
elif isinstance(self.executor, ExecutorContainer):
state = service_state.system_state
return CommandContainer([], docker_command=state, stdout=True, timeout=self.TIMEOUT)
else:
state = service_state.system_state
return Command(['docker', state, self.name], stdout=True, timeout=self.TIMEOUT)

0 comments on commit 65b1764

Please sign in to comment.