Last active
July 15, 2018 23:47
-
-
Save Nani-o/ce936e8966a020769cf696eaa0f69a76 to your computer and use it in GitHub Desktop.
Bash script to test a role inside lxd containers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Testing ansible role. | |
| # | |
| # Exit on fail. | |
| set -e | |
| _UID=$(id -u) | |
| GID=$(id -g) | |
| # Set colors. | |
| NORMAL=$'\E(B\E[m' | |
| RED=$'\E[31m' | |
| GREEN=$'\E[32m' | |
| function message() | |
| { | |
| COLOR="${1}" | |
| MESSAGE="${2}" | |
| echo -e "${COLOR}${MESSAGE}${NORMAL}\n" | |
| } | |
| # Execute lxc command with sudo -E for taking account of new lxd group. | |
| function lxc_command() | |
| { | |
| COMMAND="lxc $@" | |
| message "${GREEN}" "Executing : ${COMMAND}" | |
| sudo -E su $USER -c "${COMMAND}" | |
| } | |
| # Environment variables. | |
| distribution=${distribution:-"centos"} | |
| distribution_version=${distribution_version:-"7"} | |
| container_name=${container_name:-"c1"} | |
| setup_playbook=${setup_playbook:-"setup.yml"} | |
| test_playbook=${test_playbook:-"test.yml"} | |
| # Defining alias for container pulling. | |
| [[ "${distribution}${distribution_version}" == "centos7" ]] && alias="centos/7/default" | |
| [[ "${distribution}${distribution_version}" == "ubuntu14" ]] && alias="ubuntu:14.04" | |
| [[ "${distribution}${distribution_version}" == "ubuntu16" ]] && alias="ubuntu:16.04" | |
| [[ "${distribution}${distribution_version}" == "ubuntu18" ]] && alias="ubuntu:18.04" | |
| # Installing requirements. | |
| sudo apt -y install -t trusty-backports lxd lxd-client | |
| sudo lxd init --auto | |
| sudo usermod -G $(groups | tr " " ","),lxd $USER | |
| # grep root:$_UID:1 /etc/subuid -qs || sudo usermod --add-subuids ${_UID}-${_UID} --add-subgids ${GID}-${GID} root | |
| # lxc_command "config set ${container_name} raw.idmap 'both 1000 1000'" | |
| # lxc_command "config device add ${container_name} " | |
| # Configuring the lxc network. | |
| # lxc_command "profile device add default eth0 nic nictype=bridged parent=lxdbr0" | |
| # lxc_command "network create lxdbr0" | |
| LXD_BRIDGE='USE_LXD_BRIDGE="true" | |
| LXD_BRIDGE="lxdbr0" | |
| UPDATE_PROFILE="true" | |
| LXD_CONFILE="" | |
| LXD_DOMAIN="lxd" | |
| LXD_IPV4_ADDR="10.217.187.1" | |
| LXD_IPV4_NETMASK="255.255.255.0" | |
| LXD_IPV4_NETWORK="10.217.187.1/24" | |
| LXD_IPV4_DHCP_RANGE="10.217.187.2,10.217.187.254" | |
| LXD_IPV4_DHCP_MAX="252" | |
| LXD_IPV4_NAT="true"' | |
| echo "${LXD_BRIDGE}" | sudo tee /etc/default/lxd-bridge > /dev/null | |
| sudo /etc/init.d/lxd restart | |
| sudo /sbin/ip a | |
| # Creating container | |
| lxc_command "launch ${alias} ${container_name} -c security.nesting=true" | |
| message "${GREEN}" "Sleeping 5 seconds for container to get an ip" | |
| sleep 5 | |
| lxc_command "exec ${container_name} -- /sbin/ip a" | |
| # Installing Ansible on the container. | |
| lxc_command "exec ${container_name} -- apt -y update" | |
| lxc_command "exec ${container_name} -- apt -y install python-pip" | |
| lxc_command "exec ${container_name} -- pip install pip --upgrade" | |
| lxc_command "exec ${container_name} -- pip install ansible jmespath" | |
| # Setting up the ansible project and role to test. | |
| ROLES_PATH="/etc/ansible/roles" | |
| echo -e "[defaults]\ninventory=/etc/ansible/hosts\nroles_path=${ROLES_PATH}\nlibrary=/etc/ansible/modules/" > ansible.cfg | |
| echo -e "localhost ansible_connection=local\n" > hosts | |
| sudo mkdir -p "/var/lib/lxd/containers/${container_name}/rootfs${ROLES_PATH}/" | |
| sudo cp -rf . "/var/lib/lxd/containers/${container_name}/rootfs${ROLES_PATH}/role_to_test" | |
| # lxc_command "file push -r -p . ${container_name}${ROLES_PATH}/role_to_test" | |
| sudo chown -R $(sudo stat -c "%u:%g" /var/lib/lxd/containers/${container_name}/rootfs) /var/lib/lxd/containers/${container_name}/rootfs/etc/ansible | |
| lxc_command "exec ${container_name} -- chown -R root: /etc/ansible" | |
| lxc_command "exec ${container_name} -- mv ${ROLES_PATH}/role_to_test/ansible.cfg /etc/ansible/" | |
| lxc_command "exec ${container_name} -- mv ${ROLES_PATH}/role_to_test/hosts /etc/ansible/" | |
| # Setting up the setup.yml if present. | |
| [[ -f "./tests/${setup_playbook}" ]] && lxc_command "exec ${container_name} -- ansible-playbook ${ROLES_PATH}/role_to_test/tests/setup.yml" | |
| # Testing the syntax of the playbook | |
| message "${GREEN}" "Testing syntax of the role" | |
| lxc_command "exec ${container_name} -- ansible-playbook ${ROLES_PATH}/role_to_test/tests/test.yml --syntax-check" | |
| message "${GREEN}" "Testing execution of the role" | |
| lxc_command "exec ${container_name} -- ansible-playbook ${ROLES_PATH}/role_to_test/tests/test.yml --extra-vars lxd_snap_channel=${lxd_snap_channel:-stable}" | |
| message "${GREEN}" "Testing idempotence of the role" | |
| idempotence=$(mktemp) | |
| lxc_command "exec ${container_name} -- ansible-playbook ${ROLES_PATH}/role_to_test/tests/test.yml --extra-vars lxd_snap_channel=${lxd_snap_channel:-stable}" | tee -a ${idempotence} | |
| tail ${idempotence} | grep -q 'changed=0.*failed=0' \ | |
| && (message "${GREEN}" "Idempotence test: pass") \ | |
| || (message "${RED}" "Idempotence test: fail" && exit 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment