Add meltdown spectre to calibration set
[csit.git] / resources / libraries / bash / function / ansible.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2019 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -exuo pipefail
17
18
19 function ansible_hosts () {
20     # Run ansible playbook on hosts in working topology file. Ansible scope is
21     # determined by tags passed as parameters to this function.
22     #
23     # Variable read:
24     # - ${WORKING_TOPOLOGY} - Reserved working topology.
25     # - ${TOOLS_DIR} - CSIT tools directory, where testbed-setup is located.
26
27     set -exuo pipefail
28
29     if ! installed sshpass; then
30         sudo apt-get update -y || die "apt-get update failed!"
31         sudo apt-get install -y sshpass || die "Install sshpass failed!"
32     fi
33
34     if ! installed ansible-playbook; then
35         # TODO: Consider moving to requirements.txt?
36         pip install ansible==2.7.8 || die "Install ansible via PIP failed!"
37     fi
38
39     hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
40         die "Failed to read hosts from working topology!"
41     }
42     pushd "${TOOLS_DIR}"/testbed-setup/ansible || die "Pushd failed!"
43     ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook \
44         --vault-password-file=vault_pass \
45         --extra-vars '@vault.yml' \
46         --inventory inventories/lf_inventory/hosts site.yaml \
47         --limit "$(echo ${hosts[@]//\"})" \
48         --tags "$(echo $@)" || die "Failed to run ansible on host!"
49     popd || die "Popd failed!"
50 }
51
52 function installed () {
53
54     set -exuo pipefail
55
56     # Check if the given utility is installed. Fail if not installed.
57     #
58     # Arguments:
59     # - ${1} - Utility to check.
60     # Returns:
61     # - 0 - If command is installed.
62     # - 1 - If command is not installed.
63
64     command -v "${1}"
65 }