Last active
December 14, 2016 09:52
-
-
Save hakobera/a2e1267380eba62b1b66 to your computer and use it in GitHub Desktop.
Helper script to run rails console on deis
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
#!/usr/bin/env python | |
import subprocess | |
import docker | |
import dockerpty | |
from os import path | |
import argparse | |
def get_hosts_list(filename): | |
hosts_list = [] | |
with open(filename) as f: | |
for line in f: | |
line = line.rstrip('\n') | |
hosts_list.append(line) | |
return hosts_list | |
def get_web_containers(docker_host, app_name): | |
web_containers_list = [] | |
for container in docker_host.containers(): | |
if app_name in container['Names'][0] and 'web' in container['Command']: | |
web_containers_list.append(container) | |
return web_containers_list | |
def print_usage(): | |
print 'usage: deis_rails_console -a <app_name>' | |
def main(): | |
parser = argparse.ArgumentParser(description='Run any command on deis') | |
parser.add_argument('commands', metavar='command', nargs='+', help='command list') | |
parser.add_argument('-a', '--app', dest='app', required=True, help='deis app name') | |
args = parser.parse_args() | |
cluster = [] | |
for host_url in get_hosts_list(path.expanduser('deis_hosts')): | |
print 'connect to {}'.format(host_url) | |
cluster.append(docker.Client(base_url='tcp://{}:2375'.format(host_url), version='auto')) | |
target_container = None | |
for host in cluster: | |
web_containers = get_web_containers(host, args.app) | |
if len(web_containers) != 0: | |
target_container = web_containers[0] | |
target_host = host | |
break | |
if target_container is not None: | |
rails_console_container = target_host.create_container( | |
image=target_container['Image'], | |
stdin_open=True, | |
tty=True, | |
command=' '.join(args.commands) | |
) | |
dockerpty.start(target_host, rails_console_container) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment