diff --git a/.gitignore b/.gitignore index 2483976..41c2fdf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ +.gitignore .idea/ __pycache__/ +dist/ +easier_docker.egg-info/ +venv/ diff --git a/MANIFEST.in b/MANIFEST.in index 6868d6f..c2561c7 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,8 @@ exclude *.log exclude __pycache__ exclude .idea +exclude .git +exclude venv +exclude dist +exclude build +exclude *.egg-info \ No newline at end of file diff --git a/easierdocker/config_run.py b/easierdocker/__main__.py similarity index 81% rename from easierdocker/config_run.py rename to easierdocker/__main__.py index 4f6b721..78cc354 100644 --- a/easierdocker/config_run.py +++ b/easierdocker/__main__.py @@ -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 @@ -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() diff --git a/easierdocker/easier_docker.py b/easierdocker/easier_docker.py index 03906f9..e57c81a 100644 --- a/easierdocker/easier_docker.py +++ b/easierdocker/easier_docker.py @@ -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) @@ -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: @@ -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') @@ -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() diff --git a/easierdocker/easier_docker.pyi b/easierdocker/easier_docker.pyi index b3c271e..2657df3 100644 --- a/easierdocker/easier_docker.pyi +++ b/easierdocker/easier_docker.pyi @@ -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. """ diff --git a/setup.py b/setup.py index 23033fd..540a90e 100644 --- a/setup.py +++ b/setup.py @@ -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 ' @@ -19,7 +19,8 @@ ], entry_points={ 'console_scripts': [ - 'easier-docker=easierdocker.config_run:main', + 'easier-docker=easierdocker.__main__:main', + ], }, classifiers=[