#!/bin/bash
# Filename: ubuntu-docker.sh
# Perform basic config/tuning of docker and tools on a newly installed Ubuntu server.
# Run this script only once!
#
# To run to run use one of the below commands:
# wget -O - -q https://go.poorna.net/go/ubuntu-docker.sh | bash
# curl -s      https://go.poorna.net/go/ubuntu-docker.sh | bash

# Check that we are running this as root
if [ "$(id -u)" -ne 0 ]; then
  echo 'ERROR: This script must be run as root.'
  exit 1
fi

# Ensure that we are running on an ubuntu host
. /etc/os-release
if [ "$ID" != "ubuntu" ]; then
  echo 'ERROR: This script is intended to run only on an Ubuntu host.'
  exit 1
fi

# Ensure we run on x86_64 architecture only, ctop/docker-compose download URLs are hardcoded
if [ "$(arch)" != "x86_64" ]; then
  echo 'ERROR: This script is intended to run only on x86_64 architecture.'
  exit 1
fi

set -eoux pipefail

echo 'Installing docker jq logrotate'
apt update; apt install -y docker.io jq logrotate; apt clean

echo 'Setting docker daemon.json'
cat > /etc/docker/daemon.json <<EOT
{
    "log-driver": "local",
    "log-opts": {
      "max-size": "15m",
      "max-file": "5"
    }
}
EOT

echo 'Restarting docker with new config'
systemctl restart docker

# Install docker-compose from Github releases
wget -q -O - https://api.github.com/repos/docker/compose/releases/latest | \
  jq -r '.assets[] | select(.name=="docker-compose-linux-x86_64") | .browser_download_url' | \
  wget -i /dev/stdin -O /usr/local/bin/docker-compose

chmod 755 /usr/local/bin/docker-compose
ln -s docker-compose /usr/local/bin/d

# Install ctop from Github releases
wget -q -O - https://api.github.com/repos/bcicen/ctop/releases/latest | \
  jq -r '.assets[] | select(.name | test("linux-amd64$")) | .browser_download_url' | \
  wget -i /dev/stdin -O /usr/local/bin/ctop

chmod 755 /usr/local/bin/ctop

# Creating watchtower docker container
echo 'Creating watchtower docker container'
mkdir -p /opt/watchtower
cat > /opt/watchtower/docker-compose.yml <<EOT
# docker-compose file to start up watchtower, docker container auto updating service
# to start the container run:
#    docker-compose up -d
#
# Note: containrrr/watchtower service will auto update this container

name: watchtower-localhost

services:
  watchtower:
    image: containrrr/watchtower
    container_name: watchtower.localhost
    restart: always

    mem_limit: 128M

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /etc/localtime:/etc/localtime:ro

    labels:
      - com.centurylinklabs.watchtower.enable=true

    # Update containers at 4.15am and delete old images after update
    command: --label-enable --cleanup --schedule "0 15 4 * * *"
EOT

echo 'Starting watchtower docker container'
docker-compose -f /opt/watchtower/docker-compose.yml up -d

echo 'Installing run.sh to run watchtower manually'
cat > /opt/watchtower/run.sh <<EOT
#!/bin/sh
# Filename: run.sh
# Manually run watchtower once to update all docker containers

docker run --rm \
    -v /var/run/docker.sock:/var/run/docker.sock \
    containrrr/watchtower --cleanup --label-enable --run-once
EOT

chmod 755 /opt/watchtower/run.sh

echo 'SUCCESS: docker and other packages installed successfully'
