<commit_before>#!/usr/bin/env bash

#
# Docker Aliases
#

# Figure out if we need to use sudo for docker commands
if id -nG "$USER" | grep -qw "docker"; then
    DSUDO=''
else
    DSUDO='sudo'
fi

# Simple Docker aliases
alias di='$DSUDO docker images'

#
#  List the RAM used by a given container.
#  Used by dps().
#
#  docker_mem <container name|id>
#
function docker_mem() {
    if [ -f /sys/fs/cgroup/memory/docker/"$1"/memory.usage_in_bytes ]; then
        echo $(( $(cat /sys/fs/cgroup/memory/docker/"$1"/memory.usage_in_bytes) / 1024 / 1024 )) 'MB'
    else
        echo 'n/a'
    fi
}

#
# Return the ID of the container, given the name.
#
# docker_id <container_name>
#
function docker_id() {
  ID=$( $DSUDO docker inspect --format="{{.Id}}" "$1" 2> /dev/null);
  if (( $? >= 1 )); then
      # Container doesn't exist
      ID=''
  fi
  echo $ID
}

#
# Return the status of the named container.
#
# docker_up <container_name>
#
function docker_up() {
  UP='Y'
  ID=$( $DSUDO docker inspect --format="{{.Id}}" "$1" 2> /dev/null);
  if (( $? >= 1 )); then
    # Container doesn't exist
    UP='N'
  fi
  echo "$UP"
}

#
#  List the IP address for a given container:
#  Used by dps().
#
#  docker_ip <container name|id>
#
function docker_ip() {
    IP=$($DSUDO docker inspect --format="{{.NetworkSettings.IPAddress}}" "$1" 2> /dev/null)
    if (( $? >= 1 )); then
        # Container doesn't exist
        IP='n/a'
    fi
    echo $IP
}

#
# Enhanced version of 'docker ps' which outputs two extra columns:
#
# IP  : The private IP address of the container
# RAM : The amount of RAM the processes inside the container are using
#
# Usage: same as 'docker ps', but 'dps', so 'dps -a', etc...
#
function docker_ps() {
    tmp=$($DSUDO docker ps "$@")
    headings=$(echo "$tmp" | head --lines=1)
    max_len=$(echo "$tmp" | wc --max-line-length)
    dps=$(echo "$tmp" | tail --lines=+2)

    if [[ -n "$dps" ]]; then
      printf "%-${max_len}s %-15s %10s\n" "$headings" IP RAM

      while read -r line; do
          container_short_hash=$( echo "$line" | cut -d' ' -f1 );
          container_long_hash=$( $DSUDO docker inspect --format="{{.Id}}" "$container_short_hash" );
          container_name=$( echo "$line" | rev | cut -d' ' -f1 | rev )
          if [ -n "$container_long_hash" ]; then
              ram=$(docker_mem "$container_long_hash");
              ip=$(docker_ip "$container_name");
              printf "%-${max_len}s %-15s %10s\n" "$line" "$ip" "${ram}";
          fi
      done <<< "$dps"
    fi
}
alias dps='docker_ps'

#
#  List the volumes for a given container:
#
#  docker_vol <container name|id>
#
function docker_vol() {
    vols=$($DSUDO docker inspect --format="{{.HostConfig.Binds}}" "$1")
    vols=${vols:1:-1}
    for vol in $vols
    do
      echo "$vol"
    done
}


#
# Remove any dangling images & exited containers
#
function docker_clean() {
  echo "Removing dangling images:"
  $DSUDO docker rmi "$(docker images -f "dangling=true" -q)"
  echo "Removing exited containers:"
  $DSUDO docker rm -v "$(docker ps -a -q -f status=exited)"
}

#
#  Delete all containers & images,
#  reset dockers container linking DB and restart docker.
#  The nuclear option.
#
#  NB: Does not prompt for confirmation.
#
function docker_wipe() {
  $DSUDO docker rm -f $(docker ps -a -q)
  $DSUDO docker rmi -f $(docker images -q)
  sudo rm "/var/lib/docker/linkgraph.db"
  sudo restart docker
}


#
#  Perform a docker cmd on all docker containers
#
#  docker_all <cmd>
#
function docker_all() {
  if [ "$#" -ne 1 ]; then
    echo "Usage: $0 start|stop|pause|unpause|<any valid docker cmd>"
  fi

  for c in $($DSUDO docker ps -a | awk '{print $1}' | sed "1 d")
  do
    $DSUDO docker $1 $c
  done
}<commit_msg>- Fixed indenting - Added docker_all - Added docker_links - Hack to make docker_wipe work with systemd & upstart - Some more simple aliases<commit_after>#!/usr/bin/env bash

#
# Docker Aliases
#

# Figure out if we need to use sudo for docker commands
if id -nG "$USER" | grep -qw "docker"; then
  DSUDO=''
else
  DSUDO='sudo'
fi

# Simple Docker aliases
alias di='$DSUDO docker images'

#
#  List the RAM used by a given container.
#  Used by dps().
#
#  docker_mem <container name|id>
#
function docker_mem() {
  if [ -f /sys/fs/cgroup/memory/docker/"$1"/memory.usage_in_bytes ]; then
    echo $(( $(cat /sys/fs/cgroup/memory/docker/"$1"/memory.usage_in_bytes) / 1024 / 1024 )) 'MB'
  else
    echo 'n/a'
  fi
}

#
# Return the ID of the container, given the name.
#
# docker_id <container_name>
#
function docker_id() {
  ID=$( $DSUDO docker inspect --format="{{.Id}}" "$1" 2> /dev/null);
  if (( $? >= 1 )); then
    # Container doesn't exist
    ID=''
  fi
  echo $ID
}

#
# Return the status of the named container.
#
# docker_up <container_name>
#
function docker_up() {
  UP='Y'
  ID=$( $DSUDO docker inspect --format="{{.Id}}" "$1" 2> /dev/null);
  if (( $? >= 1 )); then
    # Container doesn't exist
    UP='N'
  fi
  echo "$UP"
}

#
#  List the IP address for a given container:
#  Used by dps().
#
#  docker_ip <container name|id>
#
function docker_ip() {
  IP=$($DSUDO docker inspect --format="{{.NetworkSettings.IPAddress}}" "$1" 2> /dev/null)
  if (( $? >= 1 )); then
    # Container doesn't exist
    IP='n/a'
  fi
  echo $IP
}

#
# Enhanced version of 'docker ps' which outputs two extra columns:
#
# IP  : The private IP address of the container
# RAM : The amount of RAM the processes inside the container are using
#
# Usage: same as 'docker ps', but 'dps', so 'dps -a', etc...
#
function docker_ps() {
  tmp=$($DSUDO docker ps "$@")
  headings=$(echo "$tmp" | head --lines=1)
  max_len=$(echo "$tmp" | wc --max-line-length)
  dps=$(echo "$tmp" | tail --lines=+2)

  if [[ -n "$dps" ]]; then
    printf "%-${max_len}s %-15s %10s\n" "$headings" IP RAM

    while read -r line; do
      container_short_hash=$( echo "$line" | cut -d' ' -f1 );
      container_long_hash=$( $DSUDO docker inspect --format="{{.Id}}" "$container_short_hash" );
      container_name=$( echo "$line" | rev | cut -d' ' -f1 | rev )
      if [ -n "$container_long_hash" ]; then
        ram=$(docker_mem "$container_long_hash");
        ip=$(docker_ip "$container_name");
        printf "%-${max_len}s %-15s %10s\n" "$line" "$ip" "${ram}";
      fi
    done <<< "$dps"
  fi
}
alias dps='docker_ps'

#
#  List the volumes for a given container:
#
#  docker_vol <container name|id>
#
function docker_vol() {
  vols=$($DSUDO docker inspect --format="{{.HostConfig.Binds}}" "$1")
  vols=${vols:1:-1}
  for vol in $vols
  do
    echo "$vol"
  done
}


#
# Remove any dangling images & exited containers
#
function docker_clean() {
  echo "Removing dangling images:"
  $DSUDO docker rmi "$(docker images -f "dangling=true" -q)"
  echo "Removing exited containers:"
  $DSUDO docker rm -v "$(docker ps -a -q -f status=exited)"
}
alias dclean='docker_clean'

#
#  List the links for a given container:
#
#  docker_links <container name|id>
#
docker_links() {
  links=$($DSUDO docker inspect --format="{{.HostConfig.Links}}" "$1")
  # links=${vols:1:-1}
  for link in $links
  do
    echo "$link"
  done
}
alias dlinks='docker_links'

#
#  Delete all containers & images,
#  reset dockers container linking DB and restart docker.
#  The nuclear option.
#
#  NB: Does not prompt for confirmation.
#
docker_wipe() {
  $DSUDO docker rm -f $(docker ps -a -q)
  $DSUDO docker rmi -f $(docker images -q)

  sudo systemctl stop docker.service || true
  sudo stop docker || true

  sudo rm -f /var/lib/docker/linkgraph.db
  sudo rm -rf /var/lib/docker/aufs/diff/*

  sudo systemctl start docker.service || true
  sudo start docker || true
}

#
#  Perform a docker cmd on all docker containers
#
#  docker_all <cmd>
#
function docker_all() {
  if [ "$#" -ne 1 ]; then
    echo "Usage: $0 start|stop|pause|unpause|<any valid docker cmd>"
  fi

  for c in $($DSUDO docker ps -a | awk '{print $1}' | sed "1 d")
  do
    $DSUDO docker $1 $c
  done
}
alias dall='docker_all'
{'fraction_non_alphanumeric': 0.16296853453679327, 'fraction_numerical': 0.010154193305754042, 'mean_word_length': 3.039493670886076, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 30, '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': '1952717', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2844, 'n_words': 908}