diff --git a/gga_init.py b/gga_init.py index 42215e6abfc851a431c84a360357bab64be17304..35a46fa8dd00451e07de509e19617966c1d2edae 100755 --- a/gga_init.py +++ b/gga_init.py @@ -236,7 +236,6 @@ def make_traefik_compose_files(config, main_dir): # Return to main directory os.chdir(main_dir) - def create_mounts(working_dir, main_dir): """ Create the folders (volumes) required by a container (to see required volumes, check their compose file) @@ -287,6 +286,12 @@ def create_mounts(working_dir, main_dir): logging.critical("Cannot access %s, exiting" % main_dir) sys.exit(exc) +def run_command(command, working_dir): + subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=working_dir) + +def run_docker_stack_deploy(service, working_dir): + run_command(["docker", "stack", "deploy", "-c", "./docker-compose.yml", service], working_dir) + def deploy_stacks(input_list, main_dir, deploy_traefik): """ This function first deploys/redeploys the traefik stack, then deploys/redeploys the organism stack, then redeploys the traefik stack @@ -296,7 +301,7 @@ def deploy_stacks(input_list, main_dir, deploy_traefik): """ main_dir = os.path.abspath(main_dir) - os.chdir(main_dir) + traefik_dir = os.path.join(main_dir, "traefik") # Get species for which to deploy the stacks # Uses the get_unique_species_list method from utilities to deploy a stack only for the "species" level (i.e genus_species) @@ -305,31 +310,22 @@ def deploy_stacks(input_list, main_dir, deploy_traefik): if deploy_traefik: # Create the swarm cluster if needed logging.info("Initializing docker swarm (adding node)") - subprocess.call(["docker", "swarm", "init"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=main_dir) + run_command(["docker", "swarm", "init"], main_dir) # Deploy traefik stack logging.info("Deploying traefik stack") - os.chdir("./traefik") - subprocess.call(["docker", "stack", "deploy", "-c", "./docker-compose.yml", "traefik"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=".") - os.chdir(main_dir) + run_docker_stack_deploy("traefik", traefik_dir) # Deploy individual species stacks for sp in to_deploy_species_li: - os.chdir(sp) + sp_dir = os.path.join(main_dir, sp) logging.info("Deploying %s stack" % sp) - subprocess.call(["docker", "stack", "deploy", "-c", "./docker-compose.yml", "{0}_{1}".format(sp.split("_")[0], sp.split("_")[1])], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=".") + run_docker_stack_deploy("{0}_{1}".format(sp.split("_")[0], sp.split("_")[1]), sp_dir) logging.info("Deployed %s stack" % sp) - os.chdir(main_dir) # Update traefik stack logging.info("Updating traefik stack") - os.chdir("./traefik") - subprocess.call(["docker", "stack", "deploy", "-c", "./docker-compose.yml", "traefik"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=".") - os.chdir(main_dir) + run_docker_stack_deploy("traefik", traefik_dir) if __name__ == "__main__": @@ -400,7 +396,6 @@ if __name__ == "__main__": # Starting logging.info("gga_init.py called for %s %s", deploy_stack_for_current_organism.genus, deploy_stack_for_current_organism.species) - logging.debug("Jbrowse url to %s %s %s %s", deploy_stack_for_current_organism.genus, deploy_stack_for_current_organism.species, deploy_stack_for_current_organism.sex, deploy_stack_for_current_organism.strain) # Make/update directory tree deploy_stack_for_current_organism.make_directory_tree()