Switch to unified view

a b/scripts/qiita-private-launcher-slurm
1
#!/usr/bin/env python
2
3
# -----------------------------------------------------------------------------
4
# Copyright (c) 2014--, The Qiita Development Team.
5
#
6
# Distributed under the terms of the BSD 3-clause License.
7
#
8
# The full license is in the file LICENSE, distributed with this software.
9
# -----------------------------------------------------------------------------
10
11
from subprocess import Popen, PIPE
12
from datetime import datetime
13
from tempfile import mkdtemp
14
from os.path import join
15
from os import environ
16
17
import click
18
19
SBATCHFILE = """#!/bin/bash
20
#SBATCH -N=1
21
#SBATCH -n=1
22
#SBATCH --output=%s/slurm-output.txt
23
#SBATCH --error=%s/slurm-error.txt
24
25
# Commands to run
26
echo $SLURM_JOBID
27
%s
28
"""
29
30
31
@click.command()
32
@click.argument('qiita_env', required=True, nargs=1)
33
@click.argument('command', required=True, nargs=1)
34
@click.argument('arguments', required=True, nargs=-1)
35
def start(qiita_env, command, arguments):
36
    """Starts the plugin environment"""
37
    cmd = ['qiita-private', command]
38
    cmd.extend(["'%s'" % arg for arg in arguments])
39
    # When Popen executes, the shell is not in interactive mode,
40
    # so it is not sourcing any of the bash configuration files
41
    # We need to source it so the env_script are available
42
    lines = [' '.join(cmd)]
43
    datestr = datetime.now().strftime("%Y%m%d_%I%M%S.%f")
44
    dirpath = mkdtemp(prefix=datestr,
45
                      dir='/projects/qiita_data/working_dir/private-jobs/')
46
    fp = join(dirpath, 'private')
47
    with open(fp, 'w') as f:
48
        f.write(SBATCHFILE % (dirpath, dirpath, "\n".join(lines)))
49
50
    cmd = "sbatch %s" % fp
51
52
    epilogue = environ.get('QIITA_JOB_SCHEDULER_EPILOGUE', '')
53
    if epilogue:
54
        cmd = f'{cmd} --epilog {epilogue}'
55
56
    proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
57
    stdout, stderr = proc.communicate()
58
    if proc.returncode and proc.returncode != 0:
59
        raise ValueError(
60
            "Error launching internal task:\n\tStdout: %s\n\tStderr: %s"
61
            % (stdout, stderr))
62
63
64
if __name__ == '__main__':
65
    start()