diff --git a/gantry.py b/gantry.py index 92b72d8..28fd6f1 100755 --- a/gantry.py +++ b/gantry.py @@ -7,7 +7,7 @@ from actions import start_action, update_action, list_action, stop_action, kill_action from config.GantryConfig import Configuration from runtime.manager import RuntimeManager -from util import report, fail +from util import report, fail, connectDockerClient ACTIONS = { @@ -58,6 +58,7 @@ def run(): parser.add_argument('component_name', help='The name of the component to manage') parser.add_argument('-m', dest='monitor', action='store_true', help='If specified and the action is "start" or "update", gantry will remain running to monitor components, auto restarting them as necessary') parser.add_argument('--setconfig', dest='config_overrides', action='append', help='Configuration overrides for the component') + parser.add_argument('-H', '--host', dest='docker_url', default='unix://var/run/docker.sock', help='Set url for docker host, defaults to unix://var/run/docker.sock') args = parser.parse_args() component_name = args.component_name @@ -65,6 +66,10 @@ def run(): should_monitor = args.monitor config_file = args.config_file config_overrides = args.config_overrides + docker_url = args.docker_url + + # Connect to docker + connectDockerClient(docker_url) # Load the config. config = loadConfig(config_file) @@ -78,7 +83,7 @@ def run(): component = manager.getComponent(component_name) if not component: raise Exception('Unknown component: ' + component_name) - + # Apply the config overrides (if any). if config_overrides: component.applyConfigOverrides(config_overrides) @@ -102,4 +107,4 @@ def cleanup_monitor(signum, frame): cleanup_monitor(None, None) if __name__ == "__main__": - run() \ No newline at end of file + run() diff --git a/gantryd.py b/gantryd.py index 41b8052..d2435ce 100755 --- a/gantryd.py +++ b/gantryd.py @@ -70,10 +70,15 @@ def start(): parser.add_argument('-c', help='A component to watch and run', nargs='+', type=str, dest='component') parser.add_argument('-etcd', help='The etcd endpoint to which the client should connect. Defaults to 127.0.0.1', dest='etcd_host', nargs='?', const=ETCD_HOST) parser.add_argument('-etcdport', help='The client port of the etcd endpoint. Defaults to 4001.', dest='etcd_port', nargs='?', const=ETCD_PORT) + parser.add_argument('-H', '--host', dest='docker_url', default='unix://var/run/docker.sock', help='Set url for docker host, defaults to unix://var/run/docker.sock') # Parse the arguments. args = parser.parse_args() port = int(args.etcd_port) if args.etcd_port else ETCD_PORT + docker_url = args.docker_url + + # Connect to docker + connectDockerClient(docker_url) # Initialize the gantryd client. dclient = GantryDClient(args.etcd_host or ETCD_HOST, args.project, port) diff --git a/util.py b/util.py index 566d7fe..a3de193 100644 --- a/util.py +++ b/util.py @@ -11,7 +11,11 @@ def enum(*sequential, **named): ReportLevels = enum(BACKGROUND=-2, EXTRA=-1, NORMAL=0, IMPORTANT=1) -client = docker.Client(version='auto') +client = None + +def connectDockerClient(docker_url): + global client + client = docker.Client(base_url=docker_url, version='auto') def pickUnusedPort(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -47,4 +51,6 @@ def fail(reason, project=None, component=None, exception=None): def getDockerClient(): """ Returns the docker client. """ - return client \ No newline at end of file + if client is None: + connectDockerClient('unix://var/run/docker.sock') + return client