aboutsummaryrefslogtreecommitdiff
path: root/lib/qsub.nix
blob: 528773effbb1e281ba64ba0c08321f70dbd6f9c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{stdenv, lib, writeScript}:

{ ppn ? 1, mem ? 1, walltime ? "24:00:00", tmpDir ? "/tmp" }: drv: lib.overrideDerivation drv ({ args, builder, ... }: {
  builder = "/bin/bash";
  args = let
    script = writeScript "qsub-script" ''
      #!${stdenv.shell}
      while [ ! -e ${tmpDir}/$PBS_JOBID ] ; do
        sleep 5
      done
      TMPDIR=${tmpDir}/$PBS_JOBID
      TEMP=$TMPDIR
      TMP=$TMPDIR
      NIX_BUILD_TOP=$TMPDIR
      cd $TMPDIR
      set -a
      . nix-set
      set +a
      ${builder} ${lib.escapeShellArgs args} > qsub-stdout 2> qsub-stderr
      echo $? > qsub-exit
    '';

    qsub = writeScript "qsub" ''
      #!/bin/bash
      PATH=/usr/bin:/bin:/usr/sbin:/sbin
      SHELL=/bin/sh
      NIX_BUILD_CORES=${toString ppn}
      id=$(qsub -l nodes=1:ppn=${toString ppn},mem=${toString mem}gb,walltime=${walltime} ${script})

      function cleanup {
        qstat ''${id%%.} 2> /dev/null > /dev/null && qdel $id || true
        sleep 5
        rm -rf ${tmpDir}/$id
      }
      trap cleanup INT TERM EXIT

      cp -r $TMPDIR ${tmpDir}/$id
      set > ${tmpDir}/$id/nix-set
      while qstat ''${id%%.} 2> /dev/null > /dev/null ; do
        sleep 5
      done
      cat ${tmpDir}/$id/qsub-stderr >&2
      cat ${tmpDir}/$id/qsub-stdout
      exitCode=$(cat ${tmpDir}/$id/qsub-exit)
      exit $exitCode
    '';

    in [ "-c" qsub ];
})