Skip to content

Commit

Permalink
Refining some features of docker and docker_compose classes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkerz committed Jan 12, 2021
1 parent 16b16df commit 3ee8360
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 46 deletions.
82 changes: 60 additions & 22 deletions pie_docker.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
"""
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 Docker:
def __init__(self,options=None):
self.options=list(options) if options is not None else []
self.options=_make_list_parameter_safe(options)


def cmd(self,docker_cmd,cmd_options=None):
"""
Builds a command and runs it like this:
docker <self.options> <docker_cmd> <cmd_options>
"""
docker_options_str=' '.join(self.options)
cmd_options_str=' '.join(cmd_options) if cmd_options is not None else ''
c=f'docker {docker_options_str} {docker_cmd} {cmd_options_str}'
print(c)
cmd(c)


def build(self,context,options=None):
"""
Builds a command and runs it like this:
docker <self.options> build <options> <context>
"""
ops=list(options) if options is not None else []
ops=_make_list_parameter_safe(options)
ops.append(context)
self.cmd('build',ops)

Expand All @@ -27,7 +42,7 @@ def run(self,image,cmd_and_args=None,options=None):
Builds a command and runs it like this:
docker <self.options> run <options> <image> <cmd_and_args>
"""
ops=list(options) if options is not None else []
ops=_make_list_parameter_safe(options)
ops.append(image)
if cmd_and_args:
ops.append(cmd_and_args)
Expand All @@ -38,7 +53,7 @@ def exec(self,container,cmd_and_args=None,options=None):
Builds a command and runs it like this:
docker <self.options> exec <options> <container> <cmd_and_args>
"""
ops=list(options) if options is not None else []
ops=_make_list_parameter_safe(options)
ops.append(container)
if cmd_and_args:
ops.append(cmd_and_args)
Expand All @@ -49,29 +64,52 @@ def stop(self,containers,options=None):
Builds a command and runs it like this:
docker <self.options> stop <options> <containers>
"""
ops=list(options) if options is not None else []
ops=_make_list_parameter_safe(options)
if isinstance(containers,str):
containers=[containers]
ops.extend(containers)
self.cmd('stop',ops)

def volume_create(self,name,options=None):
ops=list(options) if options is not None else []
ops.append(name)
self.cmd('volume create',ops)

def volume_rm(self,name,options=None):
ops=list(options) if options is not None else []
ops.append(name)
self.cmd('volume rm',ops)
class Volume:
"""
Named volume commands
"""
def __init__(self,docker_obj,name):
self.docker_obj=docker_obj
self.name=name

def create(self,options=None):
ops=_make_list_parameter_safe(options)
ops.append(self.name)
self.docker_obj.cmd('volume create',ops)

def rm(self,options=None):
ops=_make_list_parameter_safe(options)
ops.append(self.name)
self.docker_obj.cmd('volume rm',ops)

def cmd(self,docker_cmd,cmd_options=None):
def volume(self,name):
return self.Volume(self,name)


class Network:
"""
Builds a command and runs it like this:
docker <self.options> <docker_cmd> <cmd_options>
Named network commands
"""
docker_options_str=' '.join(self.options)
cmd_options_str=' '.join(cmd_options) if cmd_options is not None else ''
c=f'docker {docker_options_str} {docker_cmd} {cmd_options_str}'
print(c)
cmd(c)
def __init__(self,docker_obj,name):
self.docker_obj=docker_obj
self.name=name

def create(self,options=None):
ops=_make_list_parameter_safe(options)
ops.append(self.name)
self.docker_obj.cmd('network create',ops)

def rm(self,options=None):
ops=_make_list_parameter_safe(options)
ops.append(self.name)
self.docker_obj.cmd('network rm',ops)

def network(self,name):
return self.Network(self,name)
98 changes: 74 additions & 24 deletions pie_docker_compose.py
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)

0 comments on commit 3ee8360

Please sign in to comment.