diff --git a/docker_tools/README.md b/docker_tools/README.md new file mode 100644 index 0000000000000000000000000000000000000000..0ee14b440fbc7f16006446020b3b9e92cfb52bbc --- /dev/null +++ b/docker_tools/README.md @@ -0,0 +1,2 @@ +Useful tools to run docker commands for all running stacks +================= diff --git a/docker_tools/toggl_galaxy_state.py b/docker_tools/toggl_galaxy_state.py new file mode 100644 index 0000000000000000000000000000000000000000..9d5364b499e3c1beb360e33a1b47d2355e600c5e --- /dev/null +++ b/docker_tools/toggl_galaxy_state.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import argparse +import logging +import utilities +import time + +def run_docker_service_scale(stack, service, state): + utilities.run_command(["docker", "service", "scale", stack + "_" + service + "=" + state]) + +def run_docker_stack_services(stack, service): + stdout = utilities.run_command_and_get_stdout(["docker", "stack", "services", + "--format", "'{{.Replicas}}'", + "--filter", "name=" + stack + "_" + service, + stack]) + return stdout + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Start or stop the Galaxy container for all the stacks") + + parser.add_argument("-s", "--state", + type=int, + choices=[0,1], + help="State to set the Galaxy container in") + + parser.add_argument("-v", "--verbose", + help="Increase output verbosity", + action="store_true") + + args = parser.parse_args() + state = args.state + + if args.verbose: + logging.basicConfig(level=logging.DEBUG) + logging.debug("Debug mode") + else: + logging.basicConfig(level=logging.INFO) + + stacks = utilities.run_docker_stack_ls() + + # Stop the Galaxy services + for stack in stacks: + if stack not in ["traefik", "portainer"]: + run_docker_service_scale( + stack=stack, + service="galaxy", + state=state + ) + + time.sleep(60) + + # Check that the Galaxy services are down + for stack in stacks: + if stack not in ["traefik", "portainer"]: + stdout = run_docker_stack_services( + stack=stack, + service="galaxy" + ) + if stdout != "0/1": + logging.error("The Galaxy service is not stopped for %s" % stack) + diff --git a/docker_tools/update_service.py b/docker_tools/update_service.py new file mode 100644 index 0000000000000000000000000000000000000000..9c7b57fc8d2550afb8cae6bfdd5953bc231875b7 --- /dev/null +++ b/docker_tools/update_service.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import argparse +import logging +import utilities + +def run_docker_service_update(stack, service): + utilities.run_command(["docker", "service", "update", "--force", stack + "_" + service]) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Update a service for all the stacks") + + parser.add_argument("-s", "--service", + choices=["tripal", "galaxy", "jbrowse", "tripal-db", "elasticsearch", "proxy"], + help="Service to update") + + parser.add_argument("-v", "--verbose", + help="Increase output verbosity", + action="store_true") + + args = parser.parse_args() + service = args.service + + if args.verbose: + logging.basicConfig(level=logging.DEBUG) + logging.debug("Debug mode") + else: + logging.basicConfig(level=logging.INFO) + + stacks = utilities.run_docker_stack_ls() + + # Stop the Galaxy services + for stack in stacks: + if stack not in ["traefik", "portainer"]: + run_docker_service_update( + stack=stack, + service=service + ) diff --git a/docker_tools/utilities.py b/docker_tools/utilities.py new file mode 100644 index 0000000000000000000000000000000000000000..da8764e975d8a1234d04dc4fa2e2532e60231493 --- /dev/null +++ b/docker_tools/utilities.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import subprocess +import logging + +def run_command(command): + subprocess.run(command) + +def run_command_and_get_stdout(command): + completed_process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) + return completed_process.stdout + +def run_docker_stack_ls(): + stdout = run_command_and_get_stdout(["docker", "stack", "ls", "--format", "{{.Name}}"]) + stacks = stdout.splitlines() + logging.info("{0} stacks running: {1}".format(len(stacks), ", ".join(stacks))) + return stacks