Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 75 lines (57 sloc) 1.82 KB
#!/usr/bin/python
import re
import sys
import signal
from subprocess import call
import subprocess
# Usage : ecc [pattern]
# Examples :
# ecc
# ecc toto
# ecc startstring endstring
# Define pattern
if len(sys.argv) > 1:
pattern = '.*'.join(sys.argv[1:])
pattern = '.*{}.*'.format(pattern)
else:
pattern = '.*'
def signal_handler(signal, frame):
print("\n\nHasta la vista!\n")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
bash_command = ["docker inspect --format '{{ .Config.Image }} {{ .Id }} {{ .Name }}' $(docker ps -q)"]
process = subprocess.Popen(bash_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = process.communicate()
out_clean = out.split('\n')
# Remove last element as it is empty
out_clean.pop()
# Sort list
out_clean = sorted(out_clean)
# Filter list
out_clean_pattern = []
for line in out_clean:
if re.match(pattern, line):
out_clean_pattern.append(line)
# Exit if nothing found
if len(out_clean_pattern) == 0:
print("Nothing found kthxbye!")
sys.exit(0)
# Display filtered list for choice
i = 0
for line in out_clean_pattern:
line_split = line.split(' ')
container_image = line_split[0]
container_id = line_split[1][0:8]
container_name = line_split[2].replace('/','')
print "[{}] {} {} {}".format(i, container_name, container_id, container_image)
i += 1
try:
container_number = int(raw_input("\nWhich container? [0] "))
except ValueError:
container_number = None
if container_number is None:
container_number = 0
container_id = out_clean_pattern[container_number].split(' ')[2].replace('/','')
bash_command = ["docker exec -e COLUMNS=$(tput cols) -e LINES=$(tput lines) -it {} bash".format(container_id)]
print ' '.join(bash_command)
call(bash_command, stdin=None, stdout=None, stderr=None, shell=True)