<commit_before>#!/bin/sh
# This file should be run as root (via sudo) on the docker host
# after pushing the correct images to the registry
# and copying the config files into /tmp

# It's code must be old school bourne shell (not bash)
# in order to support the busysbox /bin/sh used by boot2docker

# FYI here's the docker options we use on stage:
# DOCKER_OPTS="--ip=0.0.0.0 --host=tcp://0.0.0.0:2375 --dns=8.8.8.8 --icc=false --iptables=true --insecure-registry=docker.peterlyons.com:5000"

set -o errexit    # always exit on error
# set -x          # enable debugging


prepare_registry() {
  if ! grep 'docker\.peterlyons\.com' /etc/hosts > /dev/null; then
    echo "loopback hostname for docker registry not set in /etc/hosts. Setting."
    echo 127.0.0.1 docker.peterlyons.com >> /etc/hosts
  fi
}

docker_clean_rm() {
  local name="$1"
  # This tr -d nonesense is because mustache and docker inspect
  # use the same delimiter of a mustache
  set +e
  running=$(docker inspect -f $(echo "{z{.State.Running}z}" | tr -d z) "${name}")
  local exit_code=$?
  set -e
  if [ "${running}" = "true" ]; then
    printf "✓\nstopping %s" "${name}…"
    docker stop "${name}"
  fi
  if [ "${running}" = "<no value>" ]; then
    return
  fi
  if [ ${exit_code} -eq 0 ]; then
    printf "✓\nremoving %s" "${name}…"
    docker rm "${name}"
  fi
  # non-zero exit code from docker inspect probably means doesn't exist
}

prepare_fs() {
  printf "preparing docker host fs…"
  local www_data_uid=33
  # for the node.js app and nginx
  install --owner="${www_data_uid}" --group=staff --mode=755 --directory \
    /var/local/{{appName}} \
    /var/www/{{hostname}}
  if [ "{{envName}}" = "production" ]; then
    install --owner=root --group=staff --mode=750 \
      /tmp/nginx_{{appName}} /etc/nginx/sites-enabled/{{hostname}}
  fi
  # for the postgresql db. MUST be 0700 or postgresql won't start
  install --owner=root --group=staff --mode=0700 --directory \
    /var/local/{{appName}}_db
  if [ "{{envName}}" = "production" ]; then
    install --owner=root --group=staff --mode=750 \
      /tmp/backup-{{appName}}-db /etc/cron.daily/backup-{{appName}}-db
  fi

  local config_file=/var/local/{{appName}}/config.js
  readonly config_file

  if [ ! -e "${config_file}" ]; then
    install --owner="${www_data_uid}" --group=staff --mode=0460 /dev/null "${config_file}"
    echo "Empty config installed to ${config_file}."
  fi
  chown "${www_data_uid}:staff" "${config_file}"
  chmod 0460 "${config_file}"
}

stop_containers() {
  #app stops first for clean shutdown
  docker_clean_rm "{{appName}}"
  docker_clean_rm "{{appName}}_db"
}

start_containers() {
  printf "✓\nstarting {{appName}}_db…"
  docker run \
    "--name={{appName}}_db" \
    "--publish={{db.port}}:{{db.port}}" \
    "--volume=/var/local/{{appName}}_db:/var/lib/postgresql/data" \
    "--volume=/var/log/{{appName}}_db:/var/log/postgresql" \
    --detach \
    --restart=always \
    postgres:{{postgres.version}}

  printf "✓\nstarting {{appName}}…"
  docker run \
    "--publish={{port}}:{{port}}" \
    "--name={{appName}}" \
    "--link={{appName}}_db:{{appName}}_db" \
    "--volume=/var/local/{{appName}}:/etc/{{appName}}" \
    "--volume=/var/local/{{appName}}:/var/log/{{appName}}" \
    --detach \
    --restart=on-failure \
    {{{registry}}}/{{appName}}:{{envName}}

  # registry should be running with
  # docker run --detach --restart=always --publish=5000:5000 registry
}

main() {
  cd "$(dirname "$0")"
  IFS="$(printf "\n\t")"
  export DOCKER_HOST=tcp://localhost:2375

  if uname -r | grep boot2docker > /dev/null; then
    # we are on stage
    unset DOCKER_HOST
    prepare_registry
  fi
  prepare_fs
  stop_containers
  start_containers
}

main "$@"<commit_msg>Tweak docker setup docs<commit_after>#!/bin/sh
# This file should be run as root (via sudo) on the docker host
# after pushing the correct images to the registry
# and copying the config files into /tmp

# It's code must be old school bourne shell (not bash)
# in order to support the busysbox /bin/sh used by boot2docker

# FYI here's the docker options we use on stage:
# DOCKER_OPTS="--ip=0.0.0.0 --host=tcp://0.0.0.0:2375 --dns=8.8.8.8 --icc=false --iptables=true --insecure-registry=docker.peterlyons.com:5000"

set -o errexit    # always exit on error
# set -x          # enable debugging


prepare_registry() {
  if ! grep 'docker\.peterlyons\.com' /etc/hosts > /dev/null; then
    echo "loopback hostname for docker registry not set in /etc/hosts. Setting."
    echo 127.0.0.1 docker.peterlyons.com >> /etc/hosts
  fi
}

docker_clean_rm() {
  local name="$1"
  # This tr -d nonesense is because mustache and docker inspect
  # both use the same {{}} (mustache) delimiter
  set +e
  running=$(docker inspect -f $(echo "{z{.State.Running}z}" | tr -d z) "${name}")
  local exit_code=$?
  set -e
  if [ "${running}" = "true" ]; then
    printf "✓\nstopping %s" "${name}…"
    docker stop "${name}"
  fi
  if [ "${running}" = "<no value>" ]; then
    return
  fi
  if [ ${exit_code} -eq 0 ]; then
    printf "✓\nremoving %s" "${name}…"
    docker rm "${name}"
  fi
  # non-zero exit code from docker inspect probably means doesn't exist
}

prepare_fs() {
  printf "preparing docker host fs…"
  local www_data_uid=33
  # for the node.js app and nginx
  install --owner="${www_data_uid}" --group=staff --mode=755 --directory \
    /var/local/{{appName}} \
    /var/www/{{hostname}}
  if [ "{{envName}}" = "production" ]; then
    install --owner=root --group=staff --mode=750 \
      /tmp/nginx_{{appName}} /etc/nginx/sites-enabled/{{hostname}}
  fi
  # for the postgresql db. MUST be 0700 or postgresql won't start
  install --owner=root --group=staff --mode=0700 --directory \
    /var/local/{{appName}}_db
  if [ "{{envName}}" = "production" ]; then
    install --owner=root --group=staff --mode=750 \
      /tmp/backup-{{appName}}-db /etc/cron.daily/backup-{{appName}}-db
  fi

  local config_file=/var/local/{{appName}}/config.js
  readonly config_file

  if [ ! -e "${config_file}" ]; then
    install --owner="${www_data_uid}" --group=staff --mode=0460 /dev/null "${config_file}"
    echo "Empty config installed to ${config_file}."
  fi
  chown "${www_data_uid}:staff" "${config_file}"
  chmod 0460 "${config_file}"
}

stop_containers() {
  #app stops first for clean shutdown
  docker_clean_rm "{{appName}}"
  docker_clean_rm "{{appName}}_db"
}

start_containers() {
  printf "✓\nstarting {{appName}}_db…"
  docker run \
    "--name={{appName}}_db" \
    "--publish={{db.port}}:{{db.port}}" \
    "--volume=/var/local/{{appName}}_db:/var/lib/postgresql/data" \
    "--volume=/var/log/{{appName}}_db:/var/log/postgresql" \
    --detach \
    --restart=always \
    postgres:{{postgres.version}}

  printf "✓\nstarting {{appName}}…"
  docker run \
    "--publish={{port}}:{{port}}" \
    "--name={{appName}}" \
    "--link={{appName}}_db:{{appName}}_db" \
    "--volume=/var/local/{{appName}}:/etc/{{appName}}" \
    "--volume=/var/local/{{appName}}:/var/log/{{appName}}" \
    --detach \
    --restart=on-failure \
    {{{registry}}}/{{appName}}:{{envName}}

  # registry should be running with
  # docker run --detach --restart=always --publish=127.0.0.1:5000:5000 registry
}

main() {
  cd "$(dirname "$0")"
  IFS="$(printf "\n\t")"
  export DOCKER_HOST=tcp://localhost:2375

  if uname -r | grep boot2docker > /dev/null; then
    # we are on stage
    unset DOCKER_HOST
    prepare_registry
  fi
  prepare_fs
  stop_containers
  start_containers
}

main "$@"
{'fraction_non_alphanumeric': 0.18181818181818182, 'fraction_numerical': 0.019432982829761747, 'mean_word_length': 3.162880886426593, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 9, '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': '4915157', 'n_tokens_mistral': 2846, 'n_tokens_neox': 2667, 'n_words': 676}