-
Notifications
You must be signed in to change notification settings - Fork 1
docker create and rm logic added #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import docker | ||
| import time | ||
| import sys | ||
| from loguru import logger | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Прикольная вещь |
||
|
|
||
| def get_image(docker_client, image_name): | ||
| try: | ||
| cur_image = docker_client.images.get(image_name) | ||
| return cur_image | ||
| except docker.errors.ImageNotFound: | ||
| logger.info(f'image with tag {image_name} not found') | ||
| return False | ||
|
|
||
|
|
||
| def delete_image(docker_client, image_tag): | ||
| try: | ||
| result = docker_client.images.remove(image_tag, force=True) | ||
| return True | ||
| except Exception as e: | ||
| logger.error('error while remove image', e) | ||
| return False | ||
|
|
||
|
|
||
| def get_container(docker_client, container_name): | ||
| try: | ||
| cur_container = docker_client.containers.get(container_name) | ||
| return cur_container | ||
| except docker.errors.NotFound: | ||
| logger.info(f'container with name {container_name} not found') | ||
| return None | ||
| except Exception as e: | ||
| logger.error(f'non defined exeption {e}') | ||
| return None | ||
|
|
||
|
|
||
| def stop_container(docker_client, container_name): | ||
| try: | ||
| cur_container = docker_client.containers.get(container_name) | ||
| return cur_container.stop() | ||
| except docker.errors.NotFound: | ||
| logger.error(f'container for stop with name {container_name} not found') | ||
| except docker.errors.APIError: | ||
| logger.error(f'error while stop container {container_name}') | ||
|
|
||
|
|
||
| def remove_container(docker_client, container_name): | ||
| try: | ||
| cur_container = docker_client.containers.get(container_name) | ||
| return cur_container.remove(v=True, force=True) | ||
| except docker.errors.NotFound: | ||
| logger.error(f'container for stop with name {container_name} not found') | ||
| except docker.errors.APIError as de: | ||
| logger.error(f'error while remove container {container_name}') | ||
| logger.error(de) | ||
| return None | ||
|
|
||
|
|
||
| def stop_and_rm_container(docker_client, container_name): | ||
| # get container | ||
| cur_container = get_container(docker_client, container_name) | ||
|
|
||
| # get container status | ||
| if cur_container is None: | ||
| logger.info(f'container {container_name} not found') | ||
| return 'deleted' | ||
| else: | ||
| status = cur_container.status | ||
| cont_id = cur_container.id | ||
| logger.info('status', status, type(status)) | ||
|
|
||
| if status == 'running': | ||
| logger.info(f'container {container_name} started already. Stop it!') | ||
| # stop | ||
| stop_result = stop_container(docker_client, cont_id) | ||
| logger.info('stoped', stop_result) | ||
| status = 'exited' | ||
| time.sleep(3) | ||
|
||
|
|
||
| if status == 'exited': | ||
| logger.info(f'container {container_name} exists already. Remove it!') | ||
| # rm | ||
| remove_result = remove_container(docker_client, cont_id) | ||
| logger.info('removed, remove_result is ', remove_result) | ||
| time.sleep(3) | ||
| status = 'deleted' | ||
| return status | ||
|
|
||
|
|
||
| def image_find_and_rm(docker_client, image_tag): | ||
| cur_img = get_image(docker_client, image_tag) | ||
| logger.info(cur_img) | ||
| if cur_img: | ||
| logger.info(f'image {image_tag} exists') | ||
| logger.info(f'cur_img is {cur_img}, ID is {cur_img.id}') | ||
| del_result = delete_image(docker_client, image_tag) | ||
| logger.info(f'del_result of image {del_result}') | ||
| return del_result | ||
| else: | ||
| # не нужно ничего удалять, контейнера нет | ||
| return True | ||
|
|
||
|
|
||
| def create_image_and_container_by_dockerfile(docker_client, path, image_tag, container_name, port): | ||
| # should be deleted | ||
| status = stop_and_rm_container(docker_client, container_name) | ||
|
|
||
| cur_cont = get_container(docker_client, container_name) | ||
| if cur_cont is not None: | ||
| logger.error('container not deleted') | ||
| sys.exit() | ||
|
|
||
| # remove image | ||
| if status == 'deleted': | ||
| # remove image | ||
| del_result = image_find_and_rm(docker_client, image_tag) | ||
| if del_result: | ||
| # create new image | ||
| image, logs = docker_client.images.build( | ||
| path=path, | ||
| tag=image_tag, | ||
| quiet=False, | ||
| rm=True, # creates image only without container | ||
| forcerm=True, | ||
| timeout=20 | ||
| ) | ||
| for log in logs: | ||
| logger.debug(log) | ||
| logger.info('image', image) | ||
|
|
||
| # run container | ||
| try: | ||
| container = docker_client.containers.run(image_tag, name=container_name, detach=True, ports={8080:port}) | ||
| for log in container.logs(): | ||
| logger.debug(log) | ||
| logger.info('container', container) | ||
| time.sleep(10) | ||
| except docker.errors.APIError as api_e: | ||
| logger.error(f'eroror while run container {container_name}') | ||
| logger.error(str(api_e)) | ||
| sys.exit() | ||
| else: | ||
| logger.error(f'error while del container') | ||
| sys.exit() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| docker | ||
| aiohttp | ||
| Jinja2 | ||
| pydantic | ||
| pydantic | ||
| loguru |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А можешь просто динамически находить как-то папку
типо
piper_path = os.getcwd()
applications_path = f"{piper_path}/applications
или вообще их может в /tmp/ сохранять, хотя лучше пока явно где-то рядом с основным
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
я бы вообще это в setting.py вынес, потому что юзеру решать где он хочет свой проект развернуть. А если свойство не указано, тогда на уровень выше и как текущая папка + '_out'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А в чем отличие setting.py от configurations.py будет, более глобальные настройки?