<commit_before>#!/bin/sh

if [ "${CYBER_DOJO_SCRIPT_WRAPPER}" = "" ]; then
  echo "Do not call this script directly. Use cyber-dojo (no .sh) instead"
  exit 1
fi

my_dir="$( cd "$( dirname "${0}" )" && pwd )"
docker_compose_cmd="docker-compose --file=${my_dir}/docker-compose.yml"
docker_version=$(docker --version | awk '{print $3}' | sed '$s/.$//')

cyber_dojo_hub=cyberdojo
cyber_dojo_root=/usr/src/cyber-dojo

default_languages_volume=default-languages
default_exercises_volume=default-exercises
default_instructions_volume=default-instructions

# set environment variables required by docker-compose.yml

# important that data-root is not under app so any ruby files it might contain
# are *not* slurped by the rails web server as it starts!
export CYBER_DOJO_DATA_ROOT=${cyber_dojo_root}/data

export CYBER_DOJO_WEB_SERVER=${cyber_dojo_hub}/web:${docker_version}
export CYBER_DOJO_WEB_CONTAINER=cyber-dojo-web

export CYBER_DOJO_KATAS_DATA_CONTAINER=cyber-dojo-katas-DATA-CONTAINER
export CYBER_DOJO_KATAS_ROOT=${cyber_dojo_root}/katas

export CYBER_DOJO_KATAS_CLASS=${CYBER_DOJO_KATAS_CLASS:=HostDiskKatas}
export CYBER_DOJO_SHELL_CLASS=${CYBER_DOJO_SHELL_CLASS:=HostShell}
export CYBER_DOJO_DISK_CLASS=${CYBER_DOJO_DISK_CLASS:=HostDisk}
export CYBER_DOJO_LOG_CLASS=${CYBER_DOJO_LOG_CLASS:=StdoutLog}
export CYBER_DOJO_GIT_CLASS=${CYBER_DOJO_GIT_CLASS:=HostGit}

export CYBER_DOJO_RUNNER_CLASS=${CYBER_DOJO_RUNNER_CLASS:=DockerTarPipeRunner}
export CYBER_DOJO_RUNNER_SUDO='sudo -u docker-runner sudo'
export CYBER_DOJO_RUNNER_TIMEOUT=${CYBER_DOJO_RUNNER_TIMEOUT:=10}

export CYBER_DOJO_RAILS_ENVIRONMENT=production
export CYBER_DOJO_LANGUAGES_VOLUME=default-languages
export CYBER_DOJO_EXERCISES_VOLUME=default-exercises
export CYBER_DOJO_INSTRUCTIONS_VOLUME=default-instructions

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

one_time_creation_of_katas_data_container() {
  # ensure katas-data-container exists.
  # o) if it doesn't and /var/www/cyber-dojo/katas exists on the host
  #    then assume it holds practice sessions from old server and _copy_ them
  #    into the new katas-data-container.
  # o) if it doesn't and /var/www/cyber-dojo/katas does not exist on the host
  #    then create new _empty_ katas-data-container

  local katas_root=/var/www/cyber-dojo/katas

  docker ps --all | grep --silent ${CYBER_DOJO_KATAS_DATA_CONTAINER}
  if [ $? != 0 ]; then
    # determine appropriate Dockerfile (to create katas data container)
    if [ -d "${katas_root}" ]; then
      echo "copying ${katas_root} into new ${CYBER_DOJO_KATAS_DATA_CONTAINER}"
      SUFFIX=copied
      CONTEXT_DIR=${katas_root}
    else
      echo "creating new empty ${CYBER_DOJO_KATAS_DATA_CONTAINER}"
      SUFFIX=empty
      CONTEXT_DIR=.
    fi

    # extract appropriate Dockerfile from web image
    local katas_dockerfile=${CONTEXT_DIR}/Dockerfile
    local cid=$(docker create ${CYBER_DOJO_WEB_SERVER})
    docker cp ${cid}:${cyber_dojo_root}/docker/katas/Dockerfile.${SUFFIX} \
              ${katas_dockerfile}
    docker rm --volumes ${cid} > /dev/null

    # 3. extract appropriate .dockerignore from web image
    local katas_docker_ignore=${CONTEXT_DIR}/.dockerignore
    local cid=$(docker create ${CYBER_DOJO_WEB_SERVER})
    docker cp ${cid}:${cyber_dojo_root}/docker/katas/Dockerignore.${SUFFIX} \
              ${katas_docker_ignore}
    docker rm --volumes ${cid} > /dev/null

    # use Dockerfile to build image
    local tag=${cyber_dojo_hub}/katas
    docker build \
             --build-arg=CYBER_DOJO_KATAS_ROOT=${CYBER_DOJO_KATAS_ROOT} \
             --tag=${tag} \
             --file=${katas_dockerfile} \
             ${CONTEXT_DIR}

    # use image to create data-container
    docker create \
           --name ${CYBER_DOJO_KATAS_DATA_CONTAINER} \
           ${tag} \
           echo 'cdfKatasDC'

    rm ${katas_dockerfile}
    rm ${katas_docker_ignore}
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

cyber_dojo_rb() {
  docker run \
    --rm \
    --user=root \
    --env=CYBER_DOJO_HUB=${cyber_dojo_hub} \
    --volume=/var/run/docker.sock:/var/run/docker.sock \
    ${CYBER_DOJO_WEB_SERVER} \
    ${cyber_dojo_root}/docker/cyber-dojo.rb $1
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# volume_create
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

debug() {
  echo $1
  #:
}

g_tmp_dir=''
g_cidfile=''
g_cid=''
g_vol=''

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

clean_up() {
  local me='clean_up'
  if [ "${g_tmp_dir}" != '' ]; then
    debug "${me}: doing [rm -rf ${g_tmp_dir}]"
    rm -rf ${g_tmp_dir}
  else
    debug "{me}: g_tmp_dir='' -> NOT doing [rm]"
  fi

  if [ "${g_cidfile}" != '' ]; then
    debug "${me}: doing [rm ${g_cidfile}]"
    rm ${g_cidfile}
  else
    debug "${me}: g_cidfile='' -> NOT doing [rm]"
  fi

  if [ "${g_cid}" != '' ]; then
    debug "${me}: doing [docker rm -f ${g_cid}]"
    docker rm -f ${g_cid} > /dev/null 2>&1
  else
    debug "${me}: g_cid='' -> NOT doing [docker rm]"
  fi
  if [ "${g_vol}" != '' ]; then
    debug "${me}: doing [docker volume rm ${g_vol}]"
    # previous command seems to sometimes complete
    # before it is safe to remove its volume?!
    sleep 1
    docker volume rm ${g_vol}
  else
    debug "${me}: g_vol='' -> NOT doing [docker volume rm]"
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

run() {
  local me='run'
  local command=$1
  debug "${me}: command=${command}"
  eval ${command} > /dev/null 2>&1
  local exit_status=$?
  debug "${me}: exit_status=${exit_status}"
  if [ "${exit_status}" = 0 ]; then
    return 0
  else
    return 1
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<commit_msg>trying to ensure whole command seen by run<commit_after>#!/bin/sh

if [ "${CYBER_DOJO_SCRIPT_WRAPPER}" = "" ]; then
  echo "Do not call this script directly. Use cyber-dojo (no .sh) instead"
  exit 1
fi

my_dir="$( cd "$( dirname "${0}" )" && pwd )"
docker_compose_cmd="docker-compose --file=${my_dir}/docker-compose.yml"
docker_version=$(docker --version | awk '{print $3}' | sed '$s/.$//')

cyber_dojo_hub=cyberdojo
cyber_dojo_root=/usr/src/cyber-dojo

default_languages_volume=default-languages
default_exercises_volume=default-exercises
default_instructions_volume=default-instructions

# set environment variables required by docker-compose.yml

# important that data-root is not under app so any ruby files it might contain
# are *not* slurped by the rails web server as it starts!
export CYBER_DOJO_DATA_ROOT=${cyber_dojo_root}/data

export CYBER_DOJO_WEB_SERVER=${cyber_dojo_hub}/web:${docker_version}
export CYBER_DOJO_WEB_CONTAINER=cyber-dojo-web

export CYBER_DOJO_KATAS_DATA_CONTAINER=cyber-dojo-katas-DATA-CONTAINER
export CYBER_DOJO_KATAS_ROOT=${cyber_dojo_root}/katas

export CYBER_DOJO_KATAS_CLASS=${CYBER_DOJO_KATAS_CLASS:=HostDiskKatas}
export CYBER_DOJO_SHELL_CLASS=${CYBER_DOJO_SHELL_CLASS:=HostShell}
export CYBER_DOJO_DISK_CLASS=${CYBER_DOJO_DISK_CLASS:=HostDisk}
export CYBER_DOJO_LOG_CLASS=${CYBER_DOJO_LOG_CLASS:=StdoutLog}
export CYBER_DOJO_GIT_CLASS=${CYBER_DOJO_GIT_CLASS:=HostGit}

export CYBER_DOJO_RUNNER_CLASS=${CYBER_DOJO_RUNNER_CLASS:=DockerTarPipeRunner}
export CYBER_DOJO_RUNNER_SUDO='sudo -u docker-runner sudo'
export CYBER_DOJO_RUNNER_TIMEOUT=${CYBER_DOJO_RUNNER_TIMEOUT:=10}

export CYBER_DOJO_RAILS_ENVIRONMENT=production
export CYBER_DOJO_LANGUAGES_VOLUME=default-languages
export CYBER_DOJO_EXERCISES_VOLUME=default-exercises
export CYBER_DOJO_INSTRUCTIONS_VOLUME=default-instructions

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

one_time_creation_of_katas_data_container() {
  # ensure katas-data-container exists.
  # o) if it doesn't and /var/www/cyber-dojo/katas exists on the host
  #    then assume it holds practice sessions from old server and _copy_ them
  #    into the new katas-data-container.
  # o) if it doesn't and /var/www/cyber-dojo/katas does not exist on the host
  #    then create new _empty_ katas-data-container

  local katas_root=/var/www/cyber-dojo/katas

  docker ps --all | grep --silent ${CYBER_DOJO_KATAS_DATA_CONTAINER}
  if [ $? != 0 ]; then
    # determine appropriate Dockerfile (to create katas data container)
    if [ -d "${katas_root}" ]; then
      echo "copying ${katas_root} into new ${CYBER_DOJO_KATAS_DATA_CONTAINER}"
      SUFFIX=copied
      CONTEXT_DIR=${katas_root}
    else
      echo "creating new empty ${CYBER_DOJO_KATAS_DATA_CONTAINER}"
      SUFFIX=empty
      CONTEXT_DIR=.
    fi

    # extract appropriate Dockerfile from web image
    local katas_dockerfile=${CONTEXT_DIR}/Dockerfile
    local cid=$(docker create ${CYBER_DOJO_WEB_SERVER})
    docker cp ${cid}:${cyber_dojo_root}/docker/katas/Dockerfile.${SUFFIX} \
              ${katas_dockerfile}
    docker rm --volumes ${cid} > /dev/null

    # 3. extract appropriate .dockerignore from web image
    local katas_docker_ignore=${CONTEXT_DIR}/.dockerignore
    local cid=$(docker create ${CYBER_DOJO_WEB_SERVER})
    docker cp ${cid}:${cyber_dojo_root}/docker/katas/Dockerignore.${SUFFIX} \
              ${katas_docker_ignore}
    docker rm --volumes ${cid} > /dev/null

    # use Dockerfile to build image
    local tag=${cyber_dojo_hub}/katas
    docker build \
             --build-arg=CYBER_DOJO_KATAS_ROOT=${CYBER_DOJO_KATAS_ROOT} \
             --tag=${tag} \
             --file=${katas_dockerfile} \
             ${CONTEXT_DIR}

    # use image to create data-container
    docker create \
           --name ${CYBER_DOJO_KATAS_DATA_CONTAINER} \
           ${tag} \
           echo 'cdfKatasDC'

    rm ${katas_dockerfile}
    rm ${katas_docker_ignore}
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

cyber_dojo_rb() {
  docker run \
    --rm \
    --user=root \
    --env=CYBER_DOJO_HUB=${cyber_dojo_hub} \
    --volume=/var/run/docker.sock:/var/run/docker.sock \
    ${CYBER_DOJO_WEB_SERVER} \
    ${cyber_dojo_root}/docker/cyber-dojo.rb $1
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# volume_create
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

debug() {
  echo $1
  #:
}

g_tmp_dir=''
g_cidfile=''
g_cid=''
g_vol=''

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

clean_up() {
  local me='clean_up'
  if [ "${g_tmp_dir}" != '' ]; then
    debug "${me}: doing [rm -rf ${g_tmp_dir}]"
    rm -rf ${g_tmp_dir}
  else
    debug "{me}: g_tmp_dir='' -> NOT doing [rm]"
  fi

  if [ "${g_cidfile}" != '' ]; then
    debug "${me}: doing [rm ${g_cidfile}]"
    rm ${g_cidfile}
  else
    debug "${me}: g_cidfile='' -> NOT doing [rm]"
  fi

  if [ "${g_cid}" != '' ]; then
    debug "${me}: doing [docker rm -f ${g_cid}]"
    docker rm -f ${g_cid} > /dev/null 2>&1
  else
    debug "${me}: g_cid='' -> NOT doing [docker rm]"
  fi
  if [ "${g_vol}" != '' ]; then
    debug "${me}: doing [docker volume rm ${g_vol}]"
    # previous command seems to sometimes complete
    # before it is safe to remove its volume?!
    sleep 1
    docker volume rm ${g_vol}
  else
    debug "${me}: g_vol='' -> NOT doing [docker volume rm]"
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

run() {
  local me='run'
  local command=$*
  debug "${me}: command=${command}"
  eval ${command} > /dev/null 2>&1
  local exit_status=$?
  debug "${me}: exit_status=${exit_status}"
  if [ "${exit_status}" = 0 ]; then
    return 0
  else
    return 1
  fi
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{'fraction_non_alphanumeric': 0.16889449348465743, 'fraction_numerical': 0.0029424127784783522, 'mean_word_length': 2.460151250727167, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 84, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4078614', 'n_tokens_mistral': 4799, 'n_tokens_neox': 4590, 'n_words': 841}