-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refining some features of docker and docker_compose classes
- Loading branch information
Showing
2 changed files
with
134 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,94 @@ | ||
""" | ||
Python3.6+ only | ||
""" | ||
__VERSION__='0.0.1' | ||
__VERSION__='0.0.2' | ||
|
||
|
||
from pie import * | ||
|
||
|
||
def _make_list_parameter_safe(ls): | ||
return list(ls) if ls is not None else [] | ||
|
||
|
||
class DockerCompose: | ||
def __init__(self, docker_compose_filename, project_name=None): | ||
self.docker_compose_filename = docker_compose_filename | ||
self.project_name = project_name | ||
def __init__(self,docker_compose_filename,project_name=None): | ||
self.docker_compose_filename=docker_compose_filename | ||
self.project_name=project_name | ||
|
||
def cmd(self, compose_cmd, compose_options=[], options=[]): | ||
cops = [f'-f {self.docker_compose_filename}'] | ||
|
||
def cmd(self,compose_cmd,compose_options=None,options=None): | ||
""" | ||
Builds a command and runs it like this: | ||
docker-compose -f <self.docker_compose_filename> [-p <self.project_name>] <compose_options> <compose_cmd> <cmd_options> | ||
""" | ||
compose_options=_make_list_parameter_safe(compose_options) | ||
options=_make_list_parameter_safe(options) | ||
c_ops=[f'-f {self.docker_compose_filename}'] | ||
if self.project_name: | ||
cops.append(f'-p {self.project_name}') | ||
cops.extend(compose_options) | ||
compose_options_str = ' '.join(cops) | ||
options_str = ' '.join(options) | ||
c = f'docker-compose {compose_options_str} {compose_cmd} {options_str}' | ||
c_ops.append(f'-p {self.project_name}') | ||
c_ops.extend(compose_options) | ||
compose_options_str=' '.join(c_ops) | ||
options_str=' '.join(options) | ||
c=f'docker-compose {compose_options_str} {compose_cmd} {options_str}' | ||
# --no-ansi | ||
print(c) | ||
cmd(c) | ||
return cmd(c) | ||
|
||
|
||
def build(self,service=None,compose_options=None,options=None): | ||
""" | ||
Builds a command and runs it like this (see `cmd` for exact pre-`compose_options` content): | ||
docker-compose ... <compose_options> build <options> [<service(s)...>] | ||
""" | ||
compose_options=_make_list_parameter_safe(compose_options) | ||
options=_make_list_parameter_safe(options) | ||
if isinstance(service,str): | ||
options.append(service) | ||
elif isinstance(service,(list,tuple)): | ||
options.extend(service) | ||
return self.cmd('build',compose_options=compose_options,options=options) | ||
|
||
|
||
class Service: | ||
""" | ||
Named service commands. Single service only. | ||
""" | ||
def __init__(self,compose_obj,name): | ||
self.compose_obj=compose_obj | ||
self.name=name | ||
|
||
def cmd(self,compose_cmd,compose_options=None,options=None,container_cmd=''): | ||
options=_make_list_parameter_safe(options) | ||
options.append(self.service_name) | ||
options.append(container_cmd) | ||
return self.compose_obj.cmd(compose_cmd,compose_options=compose_options,options=options) | ||
|
||
def build(self,compose_options=None,options=None): | ||
return self.compose_obj.build(self.service_name,compose_options=compose_options,options=options) | ||
|
||
def up(self,compose_options=None,options=None): | ||
options=_make_list_parameter_safe(options) | ||
options.append(self.name) | ||
return self.compose_obj.cmd('up',compose_options=compose_options,options=options) | ||
|
||
def start(self,compose_options=None): | ||
options=[self.name] | ||
return self.compose_obj.cmd('start',compose_options=compose_options,options=options) | ||
|
||
def stop(self,compose_options=None,options=None): | ||
options=_make_list_parameter_safe(options) | ||
options.append(self.name) | ||
return self.compose_obj.cmd('stop',compose_options=compose_options,options=options) | ||
|
||
def service(self,name): | ||
return self.Service(self,name) | ||
|
||
def service(self, service_name): | ||
return DockerComposeService(self, service_name) | ||
|
||
@classmethod | ||
def set_ignore_orphans_env_variable(cls, value): | ||
def set_ignore_orphans_env_variable(cls,value): | ||
"""If you use multiple docker compose files in the same project, docker compose thinks that some services have been orphaned, but really it's just that docker compose doesn't know about the other docker compose files""" | ||
env.set('COMPOSE_IGNORE_ORPHANS', 'True' if value else 'False') | ||
env.set('COMPOSE_IGNORE_ORPHANS','True' if value else 'False') | ||
|
||
|
||
class DockerComposeService: | ||
def __init__(self, compose_obj, service_name): | ||
self.compose_obj = compose_obj | ||
self.service_name = service_name | ||
|
||
def cmd(self, compose_cmd, compose_options=[], options=[], container_cmd=''): | ||
options_ext = list(options) | ||
options_ext.extend([self.service_name, container_cmd]) | ||
self.compose_obj.cmd(compose_cmd, compose_options, options_ext) |