Skip to content

Instantly share code, notes, and snippets.

@Windsooon
Created March 13, 2019 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Windsooon/f25996f2a24f769013f190fe916573ed to your computer and use it in GitHub Desktop.
Save Windsooon/f25996f2a24f769013f190fe916573ed to your computer and use it in GitHub Desktop.
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()
@willpatera
Copy link

willpatera commented Mar 13, 2019

Why do you need kill_process_by_port? Also all of this should just be managed by docker-compose. Maybe I'm misunderstanding the purpose of kill_process_by_port and run_docker_network. Can you clarify?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment