-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import docker | ||
import time | ||
import sys | ||
from loguru import logger | ||
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: | ||
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 False | ||
except Exception as e: | ||
logger.error(f'non defined exeption {e}') | ||
return False | ||
|
||
def get_container_with_status(docker_client, container_name): | ||
try: | ||
cur_container = docker_client.containers.get(container_name) | ||
if cur_container: | ||
status = cur_container.status | ||
cont_id = cur_container.id | ||
return cur_container, status, cont_id | ||
except docker.errors.NotFound: | ||
logger.info(f'container with name {container_name} not found') | ||
return False | ||
except Exception as e: | ||
logger.error(f'non defined exeption {e}') | ||
return False | ||
|
||
def stop_container(docker_client, container_name): | ||
try: | ||
cur_container = docker_client.containers.get(container_name) | ||
cur_container.stop() | ||
return True | ||
except docker.errors.NotFound: | ||
logger.error(f'container for stop with name {container_name} not found') | ||
return False | ||
except docker.errors.APIError: | ||
logger.error(f'error while stop container {container_name}') | ||
return False | ||
|
||
|
||
def remove_container(docker_client, container_name): | ||
try: | ||
cur_container = docker_client.containers.get(container_name) | ||
cur_container.remove(v=True, force=True) | ||
return True | ||
except docker.errors.NotFound: | ||
logger.error(f'container for stop with name {container_name} not found') | ||
return False | ||
except docker.errors.APIError as de: | ||
logger.error(f'error while remove container {container_name}') | ||
logger.error(de) | ||
return False | ||
|
||
|
||
def stop_and_rm_container(docker_client, container_name): | ||
# get container | ||
cur_container = get_container(docker_client, container_name) | ||
|
||
# get container status | ||
if not cur_container: | ||
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' | ||
|
||
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) | ||
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: | ||
logger.error(f'container not deleted, {cur_cont}') | ||
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(f'image {image} created') | ||
|
||
# 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(f'container {container} created') | ||
|
||
i = 0 | ||
while True: | ||
i += 1 | ||
# logger.info(get_container_with_status(docker_client, container_name)) | ||
container.reload() | ||
logger.info(f'container.status {container.status}') | ||
if container.status == 'running': | ||
break | ||
|
||
if i == 20: | ||
logger.error(f'container {container_name} can`t start, status is {container.status}') | ||
sys.exit() | ||
time.sleep(0.5) | ||
|
||
|
||
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 image {image_tag}') | ||
sys.exit() | ||
|
This file contains hidden or 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,4 +1,5 @@ | ||
docker | ||
aiohttp | ||
Jinja2 | ||
pydantic | ||
pydantic | ||
loguru |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 будет, более глобальные настройки?