From 9fa380e0e484b4095d08fdb52b5b40d44e8f7452 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Mon, 3 Aug 2020 14:40:26 -0500 Subject: [PATCH] Issue #221: Add tests for current multi-server ad-hoc orchestration examples. --- .travis.yml | 4 +--- orchestration/scripts/ansible.cfg | 3 +++ orchestration/scripts/app.sh | 19 +++++++++++++++++++ orchestration/scripts/db.sh | 14 ++++++++++++++ orchestration/scripts/hosts.ini | 6 ++++++ orchestration/scripts/multi.sh | 14 ++++++++++++++ tests/orchestration.sh | 25 +++++++++++++++++++++++++ 7 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 orchestration/scripts/ansible.cfg create mode 100755 orchestration/scripts/app.sh create mode 100755 orchestration/scripts/db.sh create mode 100644 orchestration/scripts/hosts.ini create mode 100755 orchestration/scripts/multi.sh create mode 100755 tests/orchestration.sh diff --git a/.travis.yml b/.travis.yml index ca11f45f..85dee5ff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -80,9 +80,7 @@ env: - playbook: nodejs-role.yml distro: centos7 - # TODO: Not easy to test in CI at this time. - # - playbook: orchestration.yml - # distro: ubuntu2004 + - shell_script: orchestration.sh - playbook: security.yml distro: centos8 diff --git a/orchestration/scripts/ansible.cfg b/orchestration/scripts/ansible.cfg new file mode 100644 index 00000000..7ff255cd --- /dev/null +++ b/orchestration/scripts/ansible.cfg @@ -0,0 +1,3 @@ +[defaults] +inventory = hosts.ini +nocows = True diff --git a/orchestration/scripts/app.sh b/orchestration/scripts/app.sh new file mode 100755 index 00000000..2ad5267e --- /dev/null +++ b/orchestration/scripts/app.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# +# App server orchestration ad-hoc tasks. +set -e + +# Configure Django on app server. +ansible app -b -m yum -a "name=MySQL-python state=present" +ansible app -b -m yum -a "name=python-setuptools state=present" +ansible app -b -m easy_install -a "name=django<2 state=present" + +# Check Django version. +ansible app -a "python -c 'import django; print django.get_version()'" + +# Other commands from the book. +ansible app -b -a "service ntpd status" +ansible app -b -m group -a "name=admin state=present" +ansible app -b -m user -a "name=johndoe group=admin createhome=yes" +ansible app -b -m user -a "name=johndoe state=absent remove=yes" +ansible app -b -m package -a "name=git state=present" diff --git a/orchestration/scripts/db.sh b/orchestration/scripts/db.sh new file mode 100755 index 00000000..544c5fc9 --- /dev/null +++ b/orchestration/scripts/db.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# +# Database server orchestration ad-hoc tasks. +set -e + +# Configure MySQL (MariaDB) server. +ansible db -b -m yum -a "name=mariadb-server state=present" +ansible db -b -m service -a "name=mariadb state=started enabled=yes" +ansible db -b -a "iptables -F" +ansible db -b -a "iptables -A INPUT -s 192.168.60.0/24 -p tcp -m tcp --dport 3306 -j ACCEPT" + +# Configure DB user for Django. +ansible db -b -m yum -a "name=MySQL-python state=present" +ansible db -b -m mysql_user -a "name=django host=% password=12345 priv=*.*:ALL state=present" diff --git a/orchestration/scripts/hosts.ini b/orchestration/scripts/hosts.ini new file mode 100644 index 00000000..fa3eedfe --- /dev/null +++ b/orchestration/scripts/hosts.ini @@ -0,0 +1,6 @@ +[multi] +app +db + +[multi:vars] +ansible_connection=docker diff --git a/orchestration/scripts/multi.sh b/orchestration/scripts/multi.sh new file mode 100755 index 00000000..e02e4ece --- /dev/null +++ b/orchestration/scripts/multi.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# +# Multi-server tests for the orchestration example. +set -e + +# Other commands from the book. +ansible multi -m stat -a "path=/etc/environment" +ansible multi -m copy -a "src=/etc/hosts dest=/tmp/hosts" +ansible multi -b -m fetch -a "src=/etc/hosts dest=/tmp" +ansible multi -m file -a "dest=/tmp/test mode=644 state=directory" +ansible multi -m file -a "dest=/tmp/test state=absent" +ansible multi -b -B 3600 -P 0 -a "yum -y update" +ansible multi -b -a "tail /var/log/messages" +ansible multi -b -m shell -a "tail /var/log/messages | grep ansible-command | wc -l" diff --git a/tests/orchestration.sh b/tests/orchestration.sh new file mode 100755 index 00000000..2c9d1f18 --- /dev/null +++ b/tests/orchestration.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Orchestration tests. +set -e + +# Make sure pip3 is available. +sudo apt-get update +sudo apt-get install -y python3-pip python3-setuptools +pip3 install --upgrade setuptools pip + +# Install dependencies. +sudo pip3 install ansible + +cd orchestration/scripts + +# Test Django app installation. +docker run -d --name app -p 80:80 --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro geerlingguy/docker-centos7-ansible +./django-app.sh + +# Test Django db installation. +docker run -d --name db -p 3360:3360 --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro geerlingguy/docker-centos7-ansible +./django-db.sh + +# Other tests from the book. +./orchestration.sh