Skip to content

Commit

Permalink
Privatization method and attribute (#4)
Browse files Browse the repository at this point in the history
* test:adding test and example

* docs: update README.md

* chore: update gitignore

* refactor: run with __main__

* fix: args add file type

* refactor: privatization method and attribute
  • Loading branch information
touero authored Dec 17, 2023
1 parent 11541e0 commit b5165b0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.gitignore
.idea/
__pycache__/
dist/
easier_docker.egg-info/
venv/
5 changes: 5 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
exclude *.log
exclude __pycache__
exclude .idea
exclude .git
exclude venv
exclude dist
exclude build
exclude *.egg-info
9 changes: 6 additions & 3 deletions easierdocker/config_run.py → easierdocker/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
import json
import os
from argparse import ArgumentParser
from argparse import ArgumentParser, FileType

from easierdocker.config import Config
from easierdocker.log_re import log
Expand All @@ -10,10 +9,14 @@

def main():
parser = ArgumentParser()
parser.add_argument('--config', '-c', help='configuration file path')
parser.add_argument('--config', '-c', help='configuration file path: yaml, yml and json', required=True)
args = parser.parse_args()
config_path = os.path.abspath(args.config) if args.config else None
config = Config(config_path).load_file()
log(f"config =\n {json.dumps(config, sort_keys=False, indent=4, separators=(',', ': '))}")
easier_docker = EasierDocker(config)
easier_docker.start()


if __name__ == "__main__":
main()
34 changes: 25 additions & 9 deletions easierdocker/easier_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,33 @@

class EasierDocker:
def __init__(self, config: dict):
self._config = config
self._image_name = self._config['image']
self._container_name = self._config['name']
try:
self._client = docker.from_env()
except Exception as e:
if isinstance(e, DockerException):
raise DockerConnectionError
raise e
self.config = config
self.image_name = self.config['image']
self.container_name = self.config['name']

def _get_image(self):
@property
def config(self):
return self._config

@property
def client(self):
return self._client

@property
def image_name(self):
return self._image_name

@property
def container_name(self):
return self._container_name

def __get_image(self):
log(f'Find docker image: [{self.image_name}] locally...')
try:
self._client.images.get(self.image_name)
Expand All @@ -39,7 +55,7 @@ def _get_image(self):
else:
log(str(e))

def _get_container(self) -> Union[Container, None]:
def __get_container(self) -> Union[Container, None]:
log(f'Find docker container: [{self.container_name}] locally...')
containers = self._client.containers.list(all=True)
for container in containers:
Expand All @@ -54,7 +70,7 @@ def _get_container(self) -> Union[Container, None]:
log(f'ContainerNotFound: [{self.container_name}], it will be created')
return None

def _run_container(self):
def __run_container(self):
try:
container: Container = self._client.containers.run(**self.config)
log(f'Container name: [{container.name}] is running')
Expand All @@ -71,9 +87,9 @@ def _run_container(self):
raise e

def start(self):
self._get_image()
container = self._get_container()
self.__get_image()
container = self.__get_container()
if container:
container.start()
else:
self._run_container()
self.__run_container()
44 changes: 36 additions & 8 deletions easierdocker/easier_docker.pyi
Original file line number Diff line number Diff line change
@@ -1,32 +1,60 @@
import docker
from typing import Union
from docker.models.containers import Container
from docker import DockerClient


class EasierDocker:
_client: DockerClient

def __init__(self, config: dict) -> None:
self.config = config
self.image_name = self.config['image']
self.container_name = self.config['name']
self._config = config
self._image_name = self._config['image']
self._container_name = self._config['name']
self._client: DockerClient() = docker.from_env()
"""
Initialize client, config, image name, container name
"""

def _get_image(self) -> None:
@property
def config(self) -> dict:
"""
Return the config.
"""
return self._config

@property
def image_name(self) -> str:
"""
Return the image name.
"""
return self._image_name

@property
def container_name(self) -> str:
"""
Return the container name.
"""
return self._container_name

@property
def client(self) -> DockerClient:
"""
Return the client.
"""
return self._client

def __get_image(self) -> None:
"""
Search for the image that exists locally. If it does not exist, the image will be pulled.
"""
...

def _get_container(self) -> Union[Container, None]:
def __get_container(self) -> Union[Container, None]:
"""
Find and return the locally existing container.
"""
...

def _run_container(self) -> None:
def __run_container(self) -> None:
"""
Start the found container. If the container does not exist, it will create one according to the image.
"""
Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

setup(
name='easier-docker',
version='2.1.0',
author='weiensong',
version='2.1.1',
author='EnSong Wei',
author_email='touer0018@gmail.com',
description='It can create a container based on the local image. If the image does not exist, the image will be '
'pulled down. If the container exists, it will be started directly. Then execute any service you want '
Expand All @@ -19,7 +19,8 @@
],
entry_points={
'console_scripts': [
'easier-docker=easierdocker.config_run:main',
'easier-docker=easierdocker.__main__:main',

],
},
classifiers=[
Expand Down

0 comments on commit b5165b0

Please sign in to comment.