Skip to content

gufo.thor.services.base

BaseService definition.

Attributes:

Name Type Description
loader Loader[BaseService]

The service loader.

BaseService

Bases: ABC

Base class for the service declaration.

Attributes:

Name Type Description
name str

Service name

is_noc bool

True, if the service is belongs to NOC

dependencies Optional[Tuple[BaseService, ...]]

Optional list of dependencies

compose_image str

docker image name. Override get_compose_image to implement config-depended behavior.

compose_depends_condition ComposeDependsCondition

Condition for all dependend services. Override get_compose_dependens_condition to implement custom behavior.

compose_healthcheck Optional[Dict[str, Any]]

Healtcheck section, if any. Override get_compose_healthcheck to implement custom behavior.

compose_command Optional[str]

command section, if any. Override get_compose_command to implement custom behavior.

compose_entrypoint Optional[str]

entrypoint section, if any. Override get_compose_entrypoint to implement custom behavior.

compose_working_dir Optional[str]

working_dir section if any. Override get_compose_working_dir to implement custom behavior.

compose_volumes Optional[List[str]]

volumes section, if any. Override get_compose_volumes to implement custom behavior.

compose_volumes_config Optional[Dict[str, Dict[str, Any]]]

Global volumes section, if any. Override get_compose_volumes_config to implement custom behavior.

compose_environment Optional[Dict[str, str]]

environment section, if any. Override get_compose_environment to implement custom behavior.

compose_extra Optional[Dict[str, Any]]

Additional parameters to be merged with the compose config. Override get_compose_extra to implement custom behavior.

service_discovery Optional[Dict[str, Union[int, Dict[str, Any]]]]

Optional name -> port mappings. Override get_service_discovery to implement custom behavior.

allow_scale bool

If the service allows running multiple instances.

require_slots bool

If the service requires slots creation.

expose_http_prefix Optional[str]

Optional prefix to be exposed on edge proxy. Override get_exposed_http_prefix to implement custom behavior.

require_http_auth bool

True, if the service protected by http auth proxy.

rewrite_http_prefix Optional[str]

If set, rewrite matched http prefix before passing request to upstream.

get_compose_command(config, svc)

Get docker-compose.yml command section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[str]

Command, if not empty

get_compose_config(config, svc)

Generate config for docker-compose target.

The following functions are used to build config:

  • get_compose_image -vto build image section.
  • iter_dependencies - to build depends_on section.
  • get_compose_working_dir - to build working_dir section.
  • get_compose_command - to build command section.
  • get_compose_entrypoint - to build entrypoint section.
  • get_compose_volumes - to build volumes section.
  • get_compose_networks - to build networks section.
  • get_compose_ports - to build ports section.
  • get_compose_environment - to build environments section.
  • get_compose_healthcheck - to build healthcheck section.
  • get_compose_extra to add the extra parameters to the result.
  • get_compose_logging - to build logging section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Dict[str, Any]

dict of the docker-compose configuration.

get_compose_depends_condition(config, svc)

Get condition for all dependend services.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
ComposeDependsCondition

Dependency start condition.

get_compose_entrypoint(config, svc)

Get docker-compose.yml entrypoint section.

Args: config: Gufo Thor config instance svc: Service's config from services part, if any.

Returns: Entrypoint, if not empty

get_compose_environment(config, svc)

Get docker-compose.yml environment section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[Dict[str, str]]

Dict of environment, if not empty

get_compose_extra(config, svc)

Get dict to be merged with compose config/.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[Dict[str, Any]]

Dict of extra settings.

get_compose_healthcheck(config, svc)

Get docker-compose.yml healthcheck section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[Dict[str, Any]]

Dict of healthcheck, if not empty

get_compose_image(config, svc)

Get docker-compose.yml image section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
str

Image name.

get_compose_logging(config, svc)

Get docker-compose.yml logging section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[Dict[str, Any]]

Dict of logging, if not empty

get_compose_networks(config, svc)

Get docker-compose.yml networks section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[Dict[str, Any]]

Networks dict, if not empty

get_compose_ports(config, svc)

Get docker-compose.yml ports section.

Args: config: Gufo Thor config instance svc: Service's config from services part, if any.

Returns: List of ports config, if not empty

get_compose_volumes(config, svc)

Get docker-compose.yml volumes section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[List[str]]

List of volumes config, if not empty.

get_compose_volumes_config(config, svc)

Get docker-compose.yml global volumes section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[Dict[str, Dict[str, Any]]]

Dict of name -> volumes config

get_compose_working_dir(config, svc)

Get docker-compose.yml working_dir section.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[str]

Working dir, if not empty

get_deps_dot() staticmethod

Build dependencies graph in dot format.

Returns:

Type Description
str

Dependencies graph.

get_expose_http_prefix(config, svc)

Iterate over exposed http paths.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

get_service_discovery(config, svc)

Get name to port mappings for service discovery.

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required

Returns:

Type Description
Optional[Dict[str, Union[int, Dict[str, Any]]]]

Service name -> port mappings

iter_dependencies()

Iterator yielding the name of dependencies.

Returns:

Type Description
Iterable[BaseService]

An iterable of strings which represent the names dependencies.

prepare_compose_config(config, svc, services)

Prepare service configs.

Called after the get_compose_dirs

Parameters:

Name Type Description Default
config Config

Gufo Thor config instance

required
svc Optional[ServiceConfig]

Service's config from services part, if any.

required
services List[BaseService]

List of all services.

required

render_file(path, tpl, **kwargs) classmethod

Apply a context to the template and write to file.

Parameters:

Name Type Description Default
path Path

File path

required
tpl str

Template name (relative to gufo.thor.templates)

required
kwargs Union[str, int, List[Any]]

Template context

{}

resolve(services) staticmethod

Resolve services to all dependencies.

Parameters:

Name Type Description Default
services Iterable[str]

Iterable of basic services.

required

Returns:

Type Description
List[BaseService]

Iterable of all basic services and their dependencies.

ComposeDependsCondition

Bases: Enum

depends_on condition.

Attributes:

Name Type Description
STARTED

Run dependends after the service is started.

HEALTHY

Run dependends after the service is started and entered the healhy state.

COMPLETED_SUCCESSFULLY

Run dependends after the service is started and terminated successfully.