Radek Vykydal 147131
#!/bin/sh
Radek Vykydal 147131
Radek Vykydal 147131
usage () {
Radek Vykydal 147131
    cat <
Radek Vykydal 147131
Radek Vykydal 147131
    $0  [-c] [-a <ARTIFACTS DIR>]
Radek Vykydal 147131
Radek Vykydal 147131
    Run gating tests on test runners provisioned by linchpin and deployed with ansible,
Radek Vykydal 147131
    syncing artifacts to localhost.
Radek Vykydal 147131
Radek Vykydal 147131
   -c  Run configuration check.
Radek Vykydal 147131
   -a  Local host directory for fetching artifacts from test runner.
Radek Vykydal 147131
HELP_USAGE
Radek Vykydal 147131
}
Radek Vykydal 147131
Radek Vykydal 147131
CHECK_ONLY="no"
Radek Vykydal 147131
ARTIFACTS="/tmp/artifacts"
Radek Vykydal 147131
Radek Vykydal 147131
while getopts "ca:" opt; do
Radek Vykydal 147131
    case $opt in
Radek Vykydal 147131
        c)
Radek Vykydal 147131
            # Run only configuration check
Radek Vykydal 147131
            CHECK_ONLY="yes"
Radek Vykydal 147131
            ;;
Radek Vykydal 147131
        a)
Radek Vykydal 147131
            # Set up directory for fetching artifacts
Radek Vykydal 147131
            ARTIFACTS="${OPTARG}"
Radek Vykydal 147131
            ;;
Radek Vykydal 147131
        *)
Radek Vykydal 147131
            echo "Usage:"
Radek Vykydal 147131
            usage
Radek Vykydal 147131
            exit 1
Radek Vykydal 147131
            ;;
Radek Vykydal 147131
    esac
Radek Vykydal 147131
done
Radek Vykydal 147131
Radek Vykydal 147131
DEFAULT_CRED_FILENAME="clouds.yml"
Radek Vykydal 147131
CRED_DIR="${HOME}/.config/linchpin"
Radek Vykydal 147131
CRED_FILE_PATH=${CRED_DIR}/${DEFAULT_CRED_FILENAME}
Radek Vykydal 147131
TOPOLOGY_FILE_PATH="linchpin/topologies/gating-test.yml"
Radek Vykydal 147131
ANSIBLE_CFG_PATH="remote_config/ansible.cfg"
Radek Vykydal 147131
Radek Vykydal 147131
CHECK_RESULT=0
Radek Vykydal 147131
Radek Vykydal 147131
Radek Vykydal 147131
############################## Check the configuration
Radek Vykydal 147131
Radek Vykydal 147131
echo
Radek Vykydal 147131
echo "========= Dependencies are installed"
Radek Vykydal 147131
echo "linchpin and ansible are required to be installed."
Radek Vykydal 147131
echo "For linchpin installation instructions see:"
Radek Vykydal 147131
echo "https://linchpin.readthedocs.io/en/latest/installation.html"
Radek Vykydal 147131
echo
Radek Vykydal 147131
Radek Vykydal 147131
if ! type ansible &> /dev/null; then
Radek Vykydal 147131
    echo "=> FAILED: ansible package is not installed"
Radek Vykydal 147131
    CHECK_RESULT=1
Radek Vykydal 147131
else
Radek Vykydal 147131
    echo "=> OK: ansible is installed"
Radek Vykydal 147131
fi
Radek Vykydal 147131
Radek Vykydal 147131
if ! type linchpin &> /dev/null; then
Radek Vykydal 147131
    echo "=> FAILED: linchpin is not installed"
Radek Vykydal 147131
    CHECK_RESULT=1
Radek Vykydal 147131
else
Radek Vykydal 147131
    echo "=> OK: linchpin is installed"
Radek Vykydal 147131
fi
Radek Vykydal 147131
Radek Vykydal 147131
Radek Vykydal 147131
echo
Radek Vykydal 147131
echo "========= Linchpin cloud credentials configuration"
Radek Vykydal 147131
echo "The credentials file for linchpin provisioner should be in ${CRED_DIR}"
Radek Vykydal 147131
echo "The name of the file and the profile to be used is defined by"
Radek Vykydal 147131
echo "   resource_groups.credentials variables in the topology file"
Radek Vykydal 147131
echo "   (${TOPOLOGY_FILE_PATH})"
Radek Vykydal 147131
echo
Radek Vykydal 147131
Radek Vykydal 147131
config_changed=0
Radek Vykydal 147131
if [[ -f ${TOPOLOGY_FILE_PATH} ]]; then
Radek Vykydal 147131
    grep -q 'filename:.*'${DEFAULT_CRED_FILENAME} ${TOPOLOGY_FILE_PATH}
Radek Vykydal 147131
    config_changed=$?
Radek Vykydal 147131
fi
Radek Vykydal 147131
Radek Vykydal 147131
if [[ ${config_changed} -eq 0 ]]; then
Radek Vykydal 147131
    if [[ -f ${CRED_FILE_PATH} ]]; then
Radek Vykydal 147131
        echo "=> OK: ${CRED_FILE_PATH} exists"
Radek Vykydal 147131
    else
Radek Vykydal 147131
        echo "=> FAILED: ${CRED_FILE_PATH} does not exist"
Radek Vykydal 147131
        CHECK_RESULT=1
Radek Vykydal 147131
    fi
Radek Vykydal 147131
else
Radek Vykydal 147131
    echo "=> NOT CHECKING: seems like this has been configured in a different way"
Radek Vykydal 147131
fi
Radek Vykydal 147131
Radek Vykydal 147131
Radek Vykydal 147131
echo
Radek Vykydal 147131
echo "========== Deployment ssh key configuration"
Radek Vykydal 147131
echo "The ssh key used for deployment with ansible has to be defined by"
Radek Vykydal 147131
echo "private_key_file variable in ${ANSIBLE_CFG_PATH}"
Radek Vykydal 147131
echo "and match the key used for provisioning of the machines with linchpin"
Radek Vykydal 147131
echo "which is defined by resource_groups.resource_definitions.keypair variable"
Radek Vykydal 147131
echo "in topology file (${TOPOLOGY_FILE_PATH})."
Radek Vykydal 147131
echo
Radek Vykydal 147131
Radek Vykydal 147131
Radek Vykydal 147131
deployment_key_defined_line=$(grep 'private_key_file.*=.*[^\S]' ${ANSIBLE_CFG_PATH})
Radek Vykydal 147131
if [[ -n "${deployment_key_defined_line}" ]]; then
Radek Vykydal 147131
    echo "=> OK: ${ANSIBLE_CFG_PATH}: ${deployment_key_defined_line}"
Radek Vykydal 147131
else
Radek Vykydal 147131
    echo "=> FAILED: deployment ssh key not defined in ${ANSIBLE_CFG_PATH}"
Radek Vykydal 147131
    CHECK_RESULT=1
Radek Vykydal 147131
fi
Radek Vykydal 147131
Radek Vykydal 147131
linchpin_keypair=$(grep "keypair:" ${TOPOLOGY_FILE_PATH} | uniq)
Radek Vykydal 147131
echo "=> INFO: should be the same key as ${TOPOLOGY_FILE_PATH}: ${linchpin_keypair}"
Radek Vykydal 147131
Radek Vykydal 147131
Radek Vykydal 147131
if [[ ${CHECK_RESULT} -ne 0 ]]; then
Radek Vykydal 147131
echo
Radek Vykydal 147131
echo "=> Configuration check FAILED, see FAILED messages above."
Radek Vykydal 147131
echo
Radek Vykydal 147131
fi
Radek Vykydal 147131
Radek Vykydal 147131
if [[ ${CHECK_ONLY} == "yes" || ${CHECK_RESULT} -ne 0 ]]; then
Radek Vykydal 147131
    exit ${CHECK_RESULT}
Radek Vykydal 147131
fi
Radek Vykydal 147131
Radek Vykydal 147131
Radek Vykydal 147131
############################## Run the tests
Radek Vykydal 147131
Radek Vykydal 147131
set -x
Radek Vykydal 147131
Radek Vykydal 147131
### Clean the linchpin generated inventory
Radek Vykydal 147131
rm -rf linchpin/inventories/*.inventory
Radek Vykydal 147131
Radek Vykydal 147131
### Provision test runner
Radek Vykydal 147131
linchpin -v --workspace linchpin -p linchpin/PinFile -c linchpin/linchpin.conf up
Radek Vykydal 147131
Radek Vykydal 147131
### Pass inventory generated by linchpin to ansible
Radek Vykydal 147131
cp linchpin/inventories/*.inventory remote_config/inventory/linchpin.inventory
Radek Vykydal 147131
Radek Vykydal 147131
### Use remote hosts in tests playbooks
Radek Vykydal 147131
ansible-playbook set_tests_to_run_on_remote.yml
Radek Vykydal 147131
Radek Vykydal 147131
### Use the ansible configuration for running tests on remote host
Radek Vykydal 147131
export ANSIBLE_CONFIG=${ANSIBLE_CFG_PATH}
Radek Vykydal 147131
Radek Vykydal 147131
### Configure remote user for playbooks
Radek Vykydal 147131
# By default root is used but it can be fedora or cloud-user for cloud images
Radek Vykydal 147131
for USER in root fedora cloud-user; do
Radek Vykydal 147131
  ansible-playbook --extra-vars="remote_user=$USER" check_and_set_remote_user.yml
Radek Vykydal 147131
done
Radek Vykydal 147131
Radek Vykydal 147131
### Prepare test runner
Radek Vykydal 147131
ansible-playbook --extra-vars="artifacts=${ARTIFACTS}" prepare-test-runner.yml
Radek Vykydal 147131
### Run test on test runner (supply artifacts variable which is testing system's job)
Radek Vykydal 147131
ansible-playbook --extra-vars="artifacts=${ARTIFACTS}" tests.yml
Radek Vykydal 147131
Radek Vykydal 147131
### Destroy the test runner
Radek Vykydal 147131
linchpin -v --workspace linchpin -p linchpin/PinFile -c linchpin/linchpin.conf destroy
Radek Vykydal 147131