<commit_before>
# runner that provides some isolation/protection/security.

class DockerRunner

  def run(paas, sandbox, command, max_seconds)

    # TODO: move this out of katas/ subfolder
    cid_filename = paas.path(sandbox) + 'store.cid'

    `rm -f #{cid_filename}`
    language = sandbox.avatar.kata.language
    cmd = "docker run -t -i --rm" +
          " -v #{paas.path(sandbox)}:/sandbox:rw" +
          " -v #{paas.path(language)}:#{paas.path(language)}:ro" +
          " -w /sandbox" +
          " --cidfile=\"#{cid_filename}\"" +
          " #{language.image_name} /bin/bash -c \"#{command}\""

    # timeout must go on 'docker run' command and not on
    # the command passed to docker run. This is to ensure
    # the docker run command does not start doing a docker pull
    # from the docker index which could easily take considerably
    # longer than the max_seconds limit.

    kill = 9
    # timeout not available on OSX ...
    `timeout --signal=#{kill} #{max_seconds}s #{cmd}`

    exitstatus = $?.exitstatus
    timed_out = 124
    cid = `cat #{cid_filename}`

    output = (exitstatus != timed_out) ? `docker logs #{cid}` :
       "Terminated by the cyber-dojo Docker server after #{max_seconds} seconds."

    output += "\n"
    output += exitstatus.to_s
    output
  end

private

end

# To run docker on OSX
# http://zaiste.net/2014/02/lightweight_docker_experience_on_osx/
#
# $curl -o docker http://get.docker.io/builds/Darwin/x86_64/docker-latest
# $chmod +x ./docker<commit_msg>DockerRunner exits with 128+9 if signal==9 (kill)<commit_after>
# runner that provides some isolation/protection/security.

class DockerRunner

  def run(paas, sandbox, command, max_seconds)

    # TODO: move this out of katas/ subfolder
    cid_filename = paas.path(sandbox) + 'store.cid'

    `rm -f #{cid_filename}`
    language = sandbox.avatar.kata.language
    cmd = "docker run -t -i --rm" +
          " -v #{paas.path(sandbox)}:/sandbox:rw" +
          " -v #{paas.path(language)}:#{paas.path(language)}:ro" +
          " -w /sandbox" +
          " --cidfile=\"#{cid_filename}\"" +
          " #{language.image_name} /bin/bash -c \"#{command}\""

    # timeout must go on 'docker run' command and not on
    # the command passed to docker run. This is to ensure
    # the docker run command does not start doing a docker pull
    # from the docker index which could easily take considerably
    # longer than the max_seconds limit.

    kill = 9
    # timeout not available on OSX ...
    `timeout --signal=#{kill} #{max_seconds}s #{cmd}`

    exitstatus = $?.exitstatus
    killed_by_timeout = 128+kill
    cid = `cat #{cid_filename}`

    output = (exitstatus != killed_by_timeout) ? `docker logs #{cid}` :
       "Terminated by the cyber-dojo Docker server after #{max_seconds} seconds."
  end

private

end

# To run docker on OSX
# http://zaiste.net/2014/02/lightweight_docker_experience_on_osx/
#
# $curl -o docker http://get.docker.io/builds/Darwin/x86_64/docker-latest
# $chmod +x ./docker
{'fraction_non_alphanumeric': 0.13271400132714, 'fraction_numerical': 0.010948905109489052, 'mean_word_length': 2.8703465982028242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6475363', 'n_tokens_mistral': 1054, 'n_tokens_neox': 987, 'n_words': 288}