From f966242805e67f91bffdc5f9eb02096bb5e1f856 Mon Sep 17 00:00:00 2001 From: William Yeh Date: Mon, 30 May 2016 11:46:11 +0800 Subject: Add: new `mini` series for building minimal images from playbooks. --- .travis.yml | 9 +++++ README.md | 12 +++++-- circle.yml | 9 +++++ mini-alpine3/Dockerfile | 28 +++++++++++++++ mini-alpine3/ansible-playbook-wrapper | 49 +++++++++++++++++++++++++ mini-alpine3/apk-list | 14 ++++++++ mini-alpine3/install-ansible.sh | 26 ++++++++++++++ mini-alpine3/pip-list | 11 ++++++ mini-alpine3/prepare-pkg-list.sh | 68 +++++++++++++++++++++++++++++++++++ mini-alpine3/uninstall-ansible.sh | 26 ++++++++++++++ mini-test/Dockerfile.alpine3 | 15 ++++++++ mini-test/playbook.yml | 5 +++ 12 files changed, 270 insertions(+), 2 deletions(-) create mode 100644 mini-alpine3/Dockerfile create mode 100755 mini-alpine3/ansible-playbook-wrapper create mode 100644 mini-alpine3/apk-list create mode 100755 mini-alpine3/install-ansible.sh create mode 100644 mini-alpine3/pip-list create mode 100755 mini-alpine3/prepare-pkg-list.sh create mode 100755 mini-alpine3/uninstall-ansible.sh create mode 100644 mini-test/Dockerfile.alpine3 create mode 100644 mini-test/playbook.yml diff --git a/.travis.yml b/.travis.yml index 4e5dfcb..ea6cc78 100644 --- a/.travis.yml +++ b/.travis.yml @@ -58,6 +58,11 @@ before_install: - docker build -t ansible_master_centos7_onbuild master-centos7-onbuild - docker build -t ansible_master_centos6_onbuild master-centos6-onbuild + + - docker build -t ansible_mini_apline3 mini-alpine3 + - docker build -t nginx_apline3 -f mini-test/Dockerfile.alpine3 mini-test + + script: - docker run -i ansible_xenial > result-ubuntu16.04 - docker run -i ansible_trusty > result-ubuntu14.04 @@ -109,6 +114,8 @@ script: - docker run -i ansible_master_centos6_onbuild > result-master-centos6-onbuild + - docker run -i nginx_apline3 2> result-nginx-alpine3 + - echo "==> Validating the test results..." - sh -c "[ -s result-ubuntu16.04 ]" @@ -157,3 +164,5 @@ script: - sh -c "[ -s result-master-debian7-onbuild ]" - sh -c "[ -s result-master-centos7-onbuild ]" - sh -c "[ -s result-master-centos6-onbuild ]" + + - sh -c "[ -s result-nginx-alpine3 ]" diff --git a/README.md b/README.md index f0baebb..8abe0cc 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,12 @@ These are Docker images for [Ansible](https://github.com/ansible/ansible) softwa - OS: Debian (jessie, wheezy), Ubuntu (xenial, trusty, precise), CentOS (7, 6), Alpine (3). -- Ansible: three version series - +- Ansible: four series - 1. the most recent *stable* version; 2. old 1.9 version; - 3. the *experimental* version. + 3. the *experimental* version; + 4. for building *minimal* images from playbooks; i.e., the Ansible body will be removed when mission completed. ## Images and tags @@ -96,6 +97,13 @@ These are Docker images for [Ansible](https://github.com/ansible/ansible) softwa - `williamyeh/ansible:master-centos6-onbuild` +### Minimal series (the Ansible body will be removed when mission completed): + +- Onbuild series: + + - `williamyeh/ansible:mini-alpine3` + + ## For the impatient diff --git a/circle.yml b/circle.yml index 60cbe28..9fc121f 100644 --- a/circle.yml +++ b/circle.yml @@ -59,6 +59,11 @@ dependencies: - docker build -t ansible_master_centos7_onbuild master-centos7-onbuild - docker build -t ansible_master_centos6_onbuild master-centos6-onbuild + + - docker build -t ansible_mini_apline3 mini-alpine3 + - docker build -t nginx_apline3 -f mini-test/Dockerfile.alpine3 mini-test + + test: override: - docker run -i ansible_xenial > result-ubuntu16.04 @@ -111,6 +116,8 @@ test: - docker run -i ansible_master_centos6_onbuild > result-master-centos6-onbuild + - docker run -i nginx_apline3 2> result-nginx-alpine3 + - echo "==> Validating the test results..." - sh -c "[ -s result-ubuntu16.04 ]" @@ -159,3 +166,5 @@ test: - sh -c "[ -s result-master-debian7-onbuild ]" - sh -c "[ -s result-master-centos7-onbuild ]" - sh -c "[ -s result-master-centos6-onbuild ]" + + - sh -c "[ -s result-nginx-alpine3 ]" diff --git a/mini-alpine3/Dockerfile b/mini-alpine3/Dockerfile new file mode 100644 index 0000000..2287deb --- /dev/null +++ b/mini-alpine3/Dockerfile @@ -0,0 +1,28 @@ +# Dockerfile for building Alpine-based image, via Ansible playbooks. +# +# @see https://github.com/gliderlabs/docker-alpine/blob/master/docs/usage.md +# +# Version 1.0 +# + + +# pull base image +FROM alpine:3.3 + +MAINTAINER William Yeh + +#ENV APK_LIST apk-list +#ENV PIP_LIST pip-list + + +COPY . /tmp + +ONBUILD COPY . /tmp +ONBUILD RUN \ + cd /tmp && \ + ./prepare-pkg-list.sh && \ + ./install-ansible.sh && \ + ./ansible-playbook-wrapper && \ + ./uninstall-ansible.sh && \ + cd / && \ + rm -rf /tmp/* diff --git a/mini-alpine3/ansible-playbook-wrapper b/mini-alpine3/ansible-playbook-wrapper new file mode 100755 index 0000000..0ba45e6 --- /dev/null +++ b/mini-alpine3/ansible-playbook-wrapper @@ -0,0 +1,49 @@ +#!/bin/sh +# +# Simple wrapper for executing ansible-galaxy and ansible-playbook +# with local connection. +# +# USAGE: +# ansible-playbook-wrapper [other ansible-playbook arguments] +# +# ENVIRONMENT VARIABLES: +# +# - REQUIREMENTS: requirements filename; default = "requirements.yml" +# - PLAYBOOK: playbook filename; default = "playbook.yml" +# - INVENTORY: inventory filename; default = "/etc/ansible/hosts" +# + + +# +# install Galaxy roles, if any +# + +if [ -z "$REQUIREMENTS" ]; then + REQUIREMENTS=requirements.yml +fi + +if [ -f "$REQUIREMENTS" ]; then + ansible-galaxy install -r $REQUIREMENTS +fi + + +# +# execute playbook +# + +if [ -z "$PLAYBOOK" ]; then + PLAYBOOK=playbook.yml +fi + + +if [ -z "$INVENTORY" ]; then + exec ansible-playbook \ + $PLAYBOOK \ + --connection=local \ + "$@" +else + exec ansible-playbook \ + -i $INVENTORY $PLAYBOOK \ + --connection=local \ + "$@" +fi diff --git a/mini-alpine3/apk-list b/mini-alpine3/apk-list new file mode 100644 index 0000000..eec27a4 --- /dev/null +++ b/mini-alpine3/apk-list @@ -0,0 +1,14 @@ +# +# packages to be installed via APK; +# lines beginning with "! " (an exclamation mark, followed by a space) will *not* be *uninstalled* afterwards. +# + + +# ssl +openssl ca-certificates + +# python-runtime +python py-pip + +# build-dependencies +python-dev libffi-dev openssl-dev build-base diff --git a/mini-alpine3/install-ansible.sh b/mini-alpine3/install-ansible.sh new file mode 100755 index 0000000..98dede0 --- /dev/null +++ b/mini-alpine3/install-ansible.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# +# Simple wrapper for installing ansible +# + + +echo "===> Adding prerequisites..." + +cat ___APK_INSTALL_LIST | \ + while read ITEM; do + apk --update add $ITEM + done + +cat ___PIP_INSTALL_LIST | \ + while read ITEM; do + pip install --upgrade $ITEM + done + + +echo "===> Installing Ansible..." +pip install ansible + + +echo "===> Adding hosts for convenience..." && \ +mkdir -p /etc/ansible && \ +echo 'localhost' > /etc/ansible/hosts diff --git a/mini-alpine3/pip-list b/mini-alpine3/pip-list new file mode 100644 index 0000000..0df21dd --- /dev/null +++ b/mini-alpine3/pip-list @@ -0,0 +1,11 @@ +# +# packages to be installed via PIP; +# lines beginning with "! " (an exclamation mark, followed by a space) will *not* be *uninstalled* afterwards. +# + + +# pip itself +pip + +# for ansible +cffi diff --git a/mini-alpine3/prepare-pkg-list.sh b/mini-alpine3/prepare-pkg-list.sh new file mode 100755 index 0000000..67c720d --- /dev/null +++ b/mini-alpine3/prepare-pkg-list.sh @@ -0,0 +1,68 @@ +#!/bin/sh +# +# Prepare the list of packages to be installed/uninstalled. +# +# ENVIRONMENT VARIABLES: +# +# - APK_LIST: APK package list; default = "apk-list" +# - PIP_LIST: PIP package list; default = "pip-list" +# + +echo "===> Preparing APK package list..." + +if [ -z "$APK_LIST" ]; then + APK_LIST=apk-list +fi + +if [ -f "$APK_LIST" ]; then + + awk '/^#/ {next} \ + { split($0,arrayA); \ + for (i in arrayA) { \ + if (arrayA[i] == "!") { continue; } \ + print arrayA[i] \ + } \ + }' \ + $APK_LIST > ___APK_INSTALL_LIST + + awk '/^(#|!)/ {next} \ + { split($0,arrayA); for (i in arrayA) print arrayA[i] }' \ + $APK_LIST | + awk '{ L[n++] = $0 } \ + END { while(n--) \ + print L[n] }' \ + > ___APK_UNINSTALL_LIST + +fi +#cat ___APK_INSTALL_LIST +#cat ___APK_UNINSTALL_LIST + + +echo "===> Preparing PIP package list..." + +if [ -z "$PIP_LIST" ]; then + PIP_LIST=pip-list +fi + +if [ -f "$PIP_LIST" ]; then + + awk '/^#/ {next} \ + { split($0,arrayA); \ + for (i in arrayA) { \ + if (arrayA[i] == "!") { continue; } \ + print arrayA[i] \ + } \ + }' \ + $PIP_LIST > ___PIP_INSTALL_LIST + + awk '/^(#|!)/ {next} \ + { split($0,arrayA); for (i in arrayA) print arrayA[i] }' \ + $PIP_LIST | + awk '{ L[n++] = $0 } \ + END { while(n--) \ + print L[n] }' \ + > ___PIP_UNINSTALL_LIST + +fi +#cat ___PIP_INSTALL_LIST +#cat ___PIP_UNINSTALL_LIST diff --git a/mini-alpine3/uninstall-ansible.sh b/mini-alpine3/uninstall-ansible.sh new file mode 100755 index 0000000..173c51d --- /dev/null +++ b/mini-alpine3/uninstall-ansible.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# +# Simple wrapper for uninstall ansible and related stuff. +# + + +echo "===> Removing Ansible..." +pip uninstall -y ansible + +echo "===> Removing PIP packages..." +cat ___PIP_UNINSTALL_LIST | \ + while read ITEM; do + pip uninstall -y $ITEM + done + +echo "===> Removing APK packages..." +cat ___APK_UNINSTALL_LIST | \ + while read ITEM; do + apk del $ITEM + done + + +echo "===> Cleaning up package list..." +rm -rf /var/lib/python2.7 /usr/lib/python2.7 +rm -rf /etc/ansible /root/.ansible /root/.cache /root/.ash_history +rm -rf /var/cache/apk diff --git a/mini-test/Dockerfile.alpine3 b/mini-test/Dockerfile.alpine3 new file mode 100644 index 0000000..78e7521 --- /dev/null +++ b/mini-test/Dockerfile.alpine3 @@ -0,0 +1,15 @@ +# Dockerfile for building (near-)minimal nginx +# + + +# pull base image +FROM ansible_mini_apline3 + +MAINTAINER William Yeh + +#ENV APK_LIST apk-list +#ENV PIP_LIST pip-list + +ENTRYPOINT ["/usr/sbin/nginx"] +#CMD ["-g", "daemon off;"] +CMD ["-v"] diff --git a/mini-test/playbook.yml b/mini-test/playbook.yml new file mode 100644 index 0000000..898dfc5 --- /dev/null +++ b/mini-test/playbook.yml @@ -0,0 +1,5 @@ +- hosts: all + become: True + tasks: + - name: install nginx + apk: name=nginx state=present -- cgit v1.2.3