Created
March 13, 2019 05:48
-
-
Save Windsooon/f25996f2a24f769013f190fe916573ed to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
import subprocess | |
import logging | |
from signal import SIGTERM | |
# Setup logging | |
logger = logging.getLogger('__name__') | |
logger.setLevel(logging.INFO) | |
# Handler | |
stream = logging.StreamHandler() | |
stream.setLevel(logging.INFO) | |
# Formatter | |
formatter = logging.Formatter( | |
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
stream.setFormatter(formatter) | |
logger.addHandler(stream) | |
def check_module_install(module): | |
""" | |
Check if | |
- docker | |
- docker-compose | |
installed or not | |
""" | |
try: | |
docker_output = subprocess.check_output(module + " -v", shell=True) | |
except subprocess.CalledProcessError: | |
raise ImportError("Please install {0}".format(module)) | |
if "command not found" in docker_output.decode("utf-8"): | |
raise ImportError("Please install {0}".format(module)) | |
def kill_process_by_port(ports): | |
''' | |
Kill the process by port number | |
''' | |
for port in ports: | |
process = subprocess.run( | |
['lsof', '-ti:{0}'.format(str(port))], capture_output=True) | |
# If any output | |
if process.stdout: | |
# May raise ProcessLookupError: [Errno 3] No such process | |
pid = process.stdout.decode('utf-8') | |
logger.info('Killing process {0} with port {1}'.format(pid, port)) | |
os.kill(int(pid), SIGTERM) | |
def run_docker_network(): | |
""" | |
Run docker network | |
""" | |
output = subprocess.run( | |
"docker network create nginx-proxy", shell=True, capture_output=True) | |
if output.returncode: | |
logger.info('nginx-porxy already created') | |
# check_install("docker") | |
# check_install("docker-compose") | |
# kill_process_port([8000]) | |
run_docker_network() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do you need
kill_process_by_port
? Also all of this should just be managed bydocker-compose
. Maybe I'm misunderstanding the purpose ofkill_process_by_port
andrun_docker_network
. Can you clarify?