Skip to content

Commit

Permalink
port docker-base role to github
Browse files Browse the repository at this point in the history
  • Loading branch information
vince rosso committed Nov 4, 2015
1 parent 9b1d625 commit 820e680
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 2 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# ansible-role_docker-base
Some base tools/configurations for all boxes that have Docker
docker base
===========

This role installs baseline needs for machines running Docker, beyond the Docker Engine itself,
including:

- Requirements for running Ansible's `docker` module
- Requirements for running Docker containers via `upstart` (optional)
- Utilities for garbage collecting unused Docker resources
- Customized configurations of the Docker Engine

This baseline was originally defined in the `ansible/docker-config` role.


Role Variables
--------------

This role expects the following variables:

- `docker_log_rotate_interval` controls the frequency of Docker log rotation
- `docker_log_rotate_count` controls the number of Docker logs to retain
- `docker_uses_upstart` defines whether `upstart` support will be installed
- `docker_py_version` defines the version of the Docker Python library being used

Note that certain versions of the Docker Engine and Docker Python library are incompatible.


Dependencies
------------

Depends on `ansible/docker` and `ansible/python`
36 changes: 36 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---

# Should upstart be used to manage Docker containers?
#
# If upstart is not used, Docker containers should be run with
# the "always" restart policy.
#
# If upstart is used, some extra dependencies will be required
# (e.g. inotify-tools)

docker_uses_upstart: yes


# Version of docker-py library to use.
#
# The 1.1.0 version is the most recent version that works with Docker 1.5.0;
# it has also been shown to work with newer Docker versions (including 1.8.1).
#
# Versions after 1.1.0 stopped working with Docker 1.5.0, but continue to work
# with newer Docker versions (includings 1.8.1).
#
# Versions after 1.2.3 stopped working with the current Ansible docker module
# (as of Ansible 1.9.2).

docker_py_version: 1.2.3


# Log rotate interval. Log files will be rotated after this interval.

docker_log_rotate_interval: "daily"


# Log files are rotated <log_rotate_count> times before being removed.
# If count is 0, old versions are removed rather then rotated.

docker_log_rotate_count: 3
63 changes: 63 additions & 0 deletions files/clear-zombie-docker-data-dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python
"""
Check all existing Docker containers for their mapped paths, and then purge any
zombie directories in docker's volumes directory which don't corresponding to an
existing container.
See discussion on this Docker issue for more info:
https://github.com/docker/docker/issues/6354
"""
import logging
import os
from os.path import exists
import sys
from shutil import rmtree

import docker


DOCKER_VOLUMES_DIR = "/var/lib/docker/vfs/dir"


def get_immediate_subdirectories(a_dir):
return [os.path.join(a_dir, name) for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]


def main():
logging.basicConfig(level=logging.INFO)

if not exists(DOCKER_VOLUMES_DIR):
# Newer versions of Docker do not use VFS for volumes by default.
# Upgraded versions of Docker may continue to use VFS, so retain
# this cleanup process for those cases and for backwards compatability.
return

client = docker.Client()

valid_dirs = []
for container in client.containers(all=True):
volumes = client.inspect_container(container['Id'])['Volumes']
if not volumes:
continue

for _, real_path in volumes.iteritems():
if real_path.startswith(DOCKER_VOLUMES_DIR):
valid_dirs.append(real_path)

all_dirs = get_immediate_subdirectories(DOCKER_VOLUMES_DIR)
invalid_dirs = set(all_dirs).difference(valid_dirs)

logging.info("Purging %s dangling Docker volumes out of %s total volumes found.",
len(invalid_dirs), len(all_dirs))
for invalid_dir in invalid_dirs:
logging.info("Purging directory: %s", invalid_dir)
rmtree(invalid_dir)

logging.info("All done.")


if __name__ == "__main__":
sys.exit(main())
4 changes: 4 additions & 0 deletions files/docker-rotate
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

# Remove docker image and container garbage with docker-rotate
docker-rotate --clean-images --keep 3 --clean-containers
4 changes: 4 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

- name: restart docker
service: name=docker state=restarted
7 changes: 7 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
galaxy_info:
author: Location Labs
description: Install docker base
licence: Apache v2.0
min_ansible_version: 1.9.2
dependencies: []
24 changes: 24 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---

- name: install inotify-tools
apt: pkg=inotify-tools force=yes
when: docker_uses_upstart

- name: install docker-py
pip: name=docker-py version="{{ docker_py_version }}"

- name: install docker-rotate
pip: name=dockerrotate

- name: install docker-rotate cron script
copy: src=docker-rotate dest=/etc/cron.daily/ mode=755

- name: push logrotate config file for docker container json logs
template: src=logrotate.conf.j2 dest=/etc/logrotate.d/docker

- name: install docker zombie volumes cleanup cron script
copy: src=clear-zombie-docker-data-dirs dest=/etc/cron.daily/ mode=755

- name: push docker configuration
template: src=docker.j2 dest=/etc/default/docker
notify: restart docker
15 changes: 15 additions & 0 deletions templates/docker.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Docker Upstart and SysVinit configuration file

# Customize location of Docker binary (especially for development testing).
#DOCKER="/usr/local/bin/docker"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
{% if docker_opts is defined %}
DOCKER_OPTS="{{ docker_opts }}"
{% endif %}
# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

# This is also a handy place to tweak where Docker's temporary files go.
#export TMPDIR="/mnt/bigdrive/docker-tmp"
8 changes: 8 additions & 0 deletions templates/logrotate.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/var/lib/docker/containers/*/*-json.log {
{{ docker_log_rotate_interval }}
rotate {{ docker_log_rotate_count }}
missingok
compress
notifempty
copytruncate
}

0 comments on commit 820e680

Please sign in to comment.