All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ingularityExecutor.1.5.0.source-code.docker.sh.hbs Maven / Gradle / Ivy

The newest version!
#!/bin/bash
#
# This script was automatically generated.
# Task ID: {{{ runContext.taskId }}}
#

set -e

STOP_TIME={{{ stopTimeout }}} # Max time to wait for container to stop before killing

CONTAINER_NAME="{{{ prefix }}}{{{ runContext.taskId }}}"

CURRENT_DIR=`pwd`

function check_contianer_running {
  status=`docker inspect -f \{{.State.Running}} $1`
  if [ "$status" = "false" ] ; then
    echo "container is no longer running..."
    running=0
  else
    running=1
  fi
}

function setup_signals {
  cid="$1"; shift
  handler="$1"; shift
  for sig; do
    trap "$handler '$cid' '$sig'" "$sig"
  done
}

function handle_signal {
  echo "Received $2"
  echo "Stopping via docker stop -t $STOP_TIME $1"
  docker stop -t $STOP_TIME "$1"
  exit_code=`docker wait "$cid"`
  echo "Attempting to remove container"
  docker rm $1
  exit "$exit_code"
}

# Outputs the path of the deepest cgroup the current process is in.
# The output of:
#   cat /proc/self/cgroup
# will look like:
# --------------------
# 8:net_cls:/
# 7:memory:/test_cgroup/mesos/4e59173b-e2a7-47a6-bcb8-455b637faa5f
# 6:freezer:/test_cgroup/mesos/4e59173b-e2a7-47a6-bcb8-455b637faa5f
# 5:devices:/
# 4:cpuset:/
# 3:cpuacct:/test_cgroup/mesos/4e59173b-e2a7-47a6-bcb8-455b637faa5f
# 2:cpu:/test_cgroup/mesos/4e59173b-e2a7-47a6-bcb8-455b637faa5f
# 1:blkio:/test_cgroup
# --------------------
# This function will return the deepest cgroup path in that output.
# For this example, it would return:
#   /test_cgroup/mesos/4e59173b-e2a7-47a6-bcb8-455b637faa5f
function get_deepest_cgroup {
   local cgroups=$(> docker.env
{{/ifHasNewLinesOrBackticks}}
{{/each}}

# Create log directory for logrotate runs
if [[ ! -d {{{ runContext.logDir }}} ]]; then
  echo "Creating log directory ({{{ runContext.logDir }}})..."
  mkdir -p {{{ runContext.logDir }}}
fi

# load artifact's profile.d
if [[ -d .profile.d ]]; then
  for FILE in $(ls .profile.d/*); do
    echo $FILE >> docker.env
  done
else
  echo "No deploy-specific profile.d"
fi

# set up port mappings
DOCKER_OPTIONS=()
{{#each envContext.dockerInfo.portMappingsList}}DOCKER_OPTIONS+=( -p {{{ hostPort }}}:{{{ containerPort }}} )
{{/each}}

# set up attached volumes
DOCKER_OPTIONS+=( -v "$CURRENT_DIR:/mnt/mesos/sandbox" )
echo "MESOS_SANDBOX=/mnt/mesos/sandbox" >> docker.env
echo "LOG_HOME=/mnt/mesos/sandbox/logs" >> docker.env
echo "MESOS_TASK_ID={{{bashEscaped runContext.taskId}}}" >> docker.env
echo "HOST_SANDBOX_DIR=$CURRENT_DIR" >> docker.env
{{#each envContext.containerVolumes}}
{{#if mode}}raw_mode{{@index}}={{{ mode }}}{{/if}}
DOCKER_OPTIONS+=( -v "{{#if hostPath}}{{{ hostPath }}}:{{{ containerPath }}}{{else}}{{{ containerPath }}}:{{{ containerPath }}}{{/if}}{{#if mode}}:${raw_mode{{@index}},,}{{/if}}" )
{{/each}}

# set up network config
{{#if envContext.dockerInfo.network}}
raw_network={{envContext.dockerInfo.network}}
DOCKER_OPTIONS+=(--net=${raw_network,,})
{{else}}
DOCKER_OPTIONS+=(--net=host)
{{/if}}

PARENT_CGROUP=$(get_deepest_cgroup)

DOCKER_OPTIONS+=(--name=$CONTAINER_NAME)
DOCKER_OPTIONS+=(--cgroup-parent=$PARENT_CGROUP)
DOCKER_OPTIONS+=(--env-file=docker.env)
{{#if runContext.cfsQuota }}
CPU_CGROUP_BASE_DIRECTORY=`findmnt --kernel --first-only --types cgroup --options cpu --noheadings --output TARGET`
CGROUP_CPU_PATH="$CPU_CGROUP_BASE_DIRECTORY$PARENT_CGROUP"
echo {{{ runContext.cfsQuota }}} > "$CGROUP_CPU_PATH/cpu.cfs_quota_us"
echo {{{ runContext.cfsPeriod }}} > "$CGROUP_CPU_PATH/cpu.cfs_period_us"
{{/if}}

{{#unless envContext.dockerWorkdirOverriden }}
DOCKER_OPTIONS+=(-w /mnt/mesos/sandbox/{{{ runContext.taskAppDirectory }}})
{{/unless}}

{{#if privileged}}
DOCKER_OPTIONS+=(--privileged)
{{/if}}

{{#each envContext.dockerParameters}}
DOCKER_OPTIONS+=({{this}})
{{/each}}

mkdir -p {{{ runContext.taskAppDirectory }}}

{{#if runContext.useFileAttributes}}
touch {{{ runContext.logFile }}}
setfattr -n user.logstart -v "$(($(date +%s%N)/1000000))" {{{ runContext.logFile }}}
{{/if}}

{{{ runContext.extraScriptContent }}}

# Print out information about the image we are about to use
docker image inspect $DOCKER_IMAGE > docker_image.json

# Start up the container
env_args=({{#each envContext.env}}{{#ifHasNewLinesOrBackticks value}}-e {{#shellQuote name}}{{/shellQuote}}={{#shellQuote value}}{{/shellQuote}} {{/ifHasNewLinesOrBackticks}}{{/each}})
cmd=(docker create "${DOCKER_OPTIONS[@]}" "${env_args[@]}" "$DOCKER_IMAGE" {{{ runContext.cmd }}})
echo -e "Creating container with: ${cmd[*]}"
cid="$("${cmd[@]}")"
docker start -a $cid >> {{{ runContext.logFile }}} 2>&1 &
running=1

setup_signals "$cid" "handle_signal" SIGINT SIGTERM

while true; do
  if [ "$running" = "1" ]; then
    sleep 10
    check_contianer_running "$cid"
  else
    break
  fi
done

exit_code=`docker wait "$cid"`
exit "$exit_code"




© 2015 - 2025 Weber Informatics LLC | Privacy Policy